mirror of
https://github.com/Facepunch/Facepunch.Steamworks.git
synced 2024-12-24 13:45:37 +03:00
Implements ability for a user to set/get individual user metadata for the joined lobby
This commit is contained in:
parent
bcd8495d6e
commit
a1f24c5f24
@ -288,5 +288,43 @@ public void SendChatMessage()
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void SetGetUserMetadata()
|
||||
{
|
||||
using (var client = new Facepunch.Steamworks.Client(252490))
|
||||
{
|
||||
Assert.IsTrue(client.IsValid);
|
||||
|
||||
client.Lobby.OnLobbyCreated = (success) =>
|
||||
{
|
||||
Assert.IsTrue(success);
|
||||
Assert.IsTrue(client.Lobby.IsValid);
|
||||
Console.WriteLine("lobby created: " + client.Lobby.CurrentLobby);
|
||||
client.Lobby.SetMemberData("testkey", "testvalue");
|
||||
};
|
||||
|
||||
client.Lobby.OnLobbyMemberDataUpdated = (steamID) =>
|
||||
{
|
||||
string name = client.Friends.GetName(steamID);
|
||||
Console.WriteLine(name + " updated data");
|
||||
Assert.IsTrue(client.Lobby.GetMemberData(steamID, "testkey") == "testvalue");
|
||||
Console.WriteLine("testkey is now: " + client.Lobby.GetMemberData(steamID, "testkey"));
|
||||
};
|
||||
|
||||
client.Lobby.Create(Steamworks.Lobby.Type.Public, 10);
|
||||
|
||||
var sw = Stopwatch.StartNew();
|
||||
|
||||
while (sw.Elapsed.TotalSeconds < 5)
|
||||
{
|
||||
client.Update();
|
||||
System.Threading.Thread.Sleep(10);
|
||||
}
|
||||
|
||||
client.Lobby.Leave();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -123,6 +123,9 @@ internal void OnLobbyCreatedAPI(LobbyCreated_t callback, bool error)
|
||||
/// </summary>
|
||||
public Action<bool> OnLobbyCreated;
|
||||
|
||||
/// <summary>
|
||||
/// Class to hold global lobby data. This is stuff like maps/modes/etc. Data set here can be filtered by LobbyList.
|
||||
/// </summary>
|
||||
public class LobbyData
|
||||
{
|
||||
internal Client client;
|
||||
@ -195,15 +198,39 @@ public bool RemoveData(string k)
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets user data for the Lobby. Things like Character, Skin, Ready, etc. Can only set your own member data
|
||||
/// </summary>
|
||||
public void SetMemberData(string key, string value)
|
||||
{
|
||||
if(CurrentLobby == 0) { return; }
|
||||
client.native.matchmaking.SetLobbyMemberData(CurrentLobby, key, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the per-user metadata from this lobby. Can get data from any user
|
||||
/// </summary>
|
||||
/// <param name="steamID">ulong SteamID of the user you want to get data from</param>
|
||||
/// <param name="key">String key of the type of data you want to get</param>
|
||||
/// <returns></returns>
|
||||
public string GetMemberData(ulong steamID, string key)
|
||||
{
|
||||
if (CurrentLobby == 0) { return "ERROR: NOT IN ANY LOBBY"; }
|
||||
return client.native.matchmaking.GetLobbyMemberData(CurrentLobby, steamID, key);
|
||||
}
|
||||
|
||||
internal void OnLobbyDataUpdatedAPI(LobbyDataUpdate_t callback, bool error)
|
||||
{
|
||||
if(error) { return; }
|
||||
if(error || (callback.SteamIDLobby != CurrentLobby)) { return; }
|
||||
if(callback.SteamIDLobby == CurrentLobby) //actual lobby data was updated by owner
|
||||
{
|
||||
UpdateLobbyData();
|
||||
}
|
||||
|
||||
//TODO: need to check and see if the updated member is in this lobby
|
||||
if(UserIsInCurrentLobby(callback.SteamIDMember)) //some member of this lobby updated their information
|
||||
{
|
||||
if (OnLobbyMemberDataUpdated != null) { OnLobbyMemberDataUpdated(callback.SteamIDMember); }
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -224,20 +251,15 @@ internal void UpdateLobbyData()
|
||||
if(OnLobbyDataUpdated != null) { OnLobbyDataUpdated(); }
|
||||
}
|
||||
|
||||
internal void UpdateLobbyMemberData()
|
||||
{
|
||||
if (OnLobbyMemberDataUpdated != null) { OnLobbyMemberDataUpdated(); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called when the lobby data itself has been updated. Called when someone has joined/Owner has updated data
|
||||
/// Called when the lobby data itself has been updated. Called when someone has joined/left, Owner has updated data, etc.
|
||||
/// </summary>
|
||||
public Action OnLobbyDataUpdated;
|
||||
|
||||
/// <summary>
|
||||
/// Called when a member of the lobby has updated either their personal Lobby metadata or someone's global steam state has changed (like a display name)
|
||||
/// Called when a member of the lobby has updated either their personal Lobby metadata or someone's global steam state has changed (like a display name). Parameter is the user who changed.
|
||||
/// </summary>
|
||||
public Action OnLobbyMemberDataUpdated;
|
||||
public Action<ulong> OnLobbyMemberDataUpdated;
|
||||
|
||||
|
||||
public Type LobbyType
|
||||
@ -456,6 +478,7 @@ public void Dispose()
|
||||
/// <returns></returns>
|
||||
public ulong[] GetMemberIDs()
|
||||
{
|
||||
|
||||
ulong[] memIDs = new ulong[NumMembers];
|
||||
for (int i = 0; i < NumMembers; i++)
|
||||
{
|
||||
@ -464,6 +487,21 @@ public ulong[] GetMemberIDs()
|
||||
return memIDs;
|
||||
}
|
||||
|
||||
public bool UserIsInCurrentLobby(ulong steamID)
|
||||
{
|
||||
if(CurrentLobby == 0) { return false; }
|
||||
ulong[] mems = GetMemberIDs();
|
||||
for (int i = 0; i < mems.Length; i++)
|
||||
{
|
||||
if(mems[i] == steamID)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Invites the specified user to the CurrentLobby the user is in.
|
||||
/// </summary>
|
||||
@ -499,8 +537,8 @@ internal void OnLobbyJoinRequestedAPI(GameLobbyJoinRequested_t callback, bool er
|
||||
/// </summary>
|
||||
internal void OnLobbyMemberPersonaChangeAPI(PersonaStateChange_t callback, bool error)
|
||||
{
|
||||
if (error || !client.native.friends.IsUserInSource(callback.SteamID, CurrentLobby)) { return; }
|
||||
UpdateLobbyMemberData();
|
||||
if (error || !UserIsInCurrentLobby(callback.SteamID)) { return; }
|
||||
if (OnLobbyMemberDataUpdated != null) { OnLobbyMemberDataUpdated(callback.SteamID); }
|
||||
}
|
||||
|
||||
/*not implemented
|
||||
@ -509,10 +547,6 @@ internal void OnLobbyMemberPersonaChangeAPI(PersonaStateChange_t callback, bool
|
||||
client.native.matchmaking.GetLobbyGameServer;
|
||||
client.native.matchmaking.SetLobbyGameServer;
|
||||
|
||||
//data for people in the actual lobby - scores/elo/characters/etc.
|
||||
client.native.matchmaking.SetLobbyMemberData; //local user
|
||||
client.native.matchmaking.GetLobbyMemberData; //any user in this lobby
|
||||
|
||||
//used with game server stuff
|
||||
SteamNative.LobbyGameCreated_t
|
||||
*/
|
||||
|
Loading…
Reference in New Issue
Block a user