Skip to content

Commit

Permalink
fixed: more getplayer and local mode fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
momintlh committed Nov 11, 2024
1 parent c877d65 commit 16faa05
Show file tree
Hide file tree
Showing 11 changed files with 28 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ void Start()
/// </summary>
void HandleScoreUpdate(string data, string caller)
{
var player = PlayroomKit.GetPlayer(caller);
var player = _playroomKit.GetPlayer(caller);
Debug.Log($"Caller: {caller}, Player Name: {player?.GetProfile().name}, Data: {data}");

if (PlayerDict.TryGetValue(caller, out GameObject playerObj))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ private void HandleScoreUpdate(string data, string caller)
}



public void ShootLaser()
{
var myPlayer = _playroomKit.MyPlayer();
Expand Down
2 changes: 1 addition & 1 deletion Assets/PlayroomKit/Tests/Editor/PlayroomKitLocalTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public void ResetPlayersStates_InvokeCallback()
[Test]
public void MyPlayer_ReturnLocalPlayer()
{
var expectedPlayer = PlayroomKit.GetPlayer("mockplayerID123");
var expectedPlayer = _playroomKit.GetPlayer("mockplayerID123");
var player = _playroomKit.MyPlayer();
Assert.AreEqual(expectedPlayer, player);
}
Expand Down
6 changes: 3 additions & 3 deletions Assets/PlayroomKit/Tests/Editor/PlayroomKitTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public void GetPlayer_ShouldReturnSamePlayer_ForSameId()
string playerId = "Player1";

// Act
var player1 = PlayroomKit.GetPlayer(playerId);
var player2 = PlayroomKit.GetPlayer(playerId);
var player1 = _playroomKit.GetPlayer(playerId);
var player2 = _playroomKit.GetPlayer(playerId);

// Assert
Assert.AreEqual(player1, player2, "GetPlayer should return the same instance for the same playerId.");
Expand Down Expand Up @@ -119,7 +119,7 @@ public void GetPlayer_ShouldCreateNewPlayer_IfNotExists()
string newPlayerId = "Player2";

// Act
var player = PlayroomKit.GetPlayer(newPlayerId);
var player = _playroomKit.GetPlayer(newPlayerId);

// Assert
Assert.IsNotNull(player, "GetPlayer should create a new Player if one does not exist.");
Expand Down
2 changes: 1 addition & 1 deletion Assets/PlayroomKit/modules/Interfaces/IPlayroomBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void InsertCoin(InitOptions options = null, Action onLaunchCallBack = nul

public void ResetStates(string[] keysToExclude = null, Action OnStatesReset = null);

public void ResetPlayersStates(string[] keysToExclude = null, Action OnStatesReset = null);
public void ResetPlayersStates(string[] keysToExclude = null, Action onStatesReset = null);

public void CreateJoyStick(JoystickOptions options);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ private static Player MockMyPlayerBrowser()
{
#if UNITY_EDITOR
var id = UnityBrowserBridge.Instance.ExecuteJS<string>($"MyPlayer()");
return GetPlayer(id);
return GetPlayerById(id);
#else
return default;
#endif
Expand Down
17 changes: 6 additions & 11 deletions Assets/PlayroomKit/modules/MockMode/LocalPlayroomService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,9 @@ public partial class PlayroomKit
public class LocalMockPlayroomService : IPlayroomBase
{
private Dictionary<string, object> mockGlobalStates = new();

// private Dictionary<string, Action<string>> stateCallbacks = new();

private const string PlayerId = "mockplayerID123";

private static bool mockIsStreamMode;
private Dictionary<string, string> mockGlobalStatesDictionary = new();
private Dictionary<string, string> mockPlayerStatesDictionary = new();

public Action OnPlayerJoin(Action<Player> onPlayerJoinCallback)
{
Expand Down Expand Up @@ -128,27 +123,27 @@ public void WaitForPlayerState(string playerID, string stateKey, Action<string>
public void ResetStates(string[] keysToExclude = null, Action OnStatesReset = null)
{
List<string> keysToRemove =
mockGlobalStatesDictionary.Keys.Where(key => !keysToExclude.Contains(key)).ToList();
foreach (string key in keysToRemove) mockGlobalStatesDictionary.Remove(key);
mockGlobalStates.Keys.Where(key => !keysToExclude.Contains(key)).ToList();
foreach (string key in keysToRemove) mockGlobalStates.Remove(key);
OnStatesReset?.Invoke();
}

public void ResetPlayersStates(string[] keysToExclude = null, Action OnStatesReset = null)
public void ResetPlayersStates(string[] keysToExclude = null, Action onStatesReset = null)
{
if (keysToExclude == null || keysToExclude.Length == 0)
{
keysToExclude = Array.Empty<string>();
}

List<string> keysToRemove =
mockPlayerStatesDictionary.Keys.Where(key => !keysToExclude.Contains(key)).ToList();
Player.LocalPlayerService.GetMockPlayerStates().Keys.Where(key => !keysToExclude.Contains(key)).ToList();

foreach (string key in keysToRemove)
{
mockPlayerStatesDictionary.Remove(key);
Player.LocalPlayerService.GetMockPlayerStates().Remove(key);
}

OnStatesReset?.Invoke();
onStatesReset?.Invoke();
}

public void CreateJoyStick(JoystickOptions options)
Expand Down
8 changes: 6 additions & 2 deletions Assets/PlayroomKit/modules/Player/LocalPlayerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@ public LocalPlayerService(string id)
}

private Dictionary<(string playerID, string stateKey), Action> stateCallbacks = new();
private static Dictionary<string, object> mockPlayerStatesDictionary = new();


private static Dictionary<string, object> mockPlayerStatesDictionary = new();
public static Dictionary<string, object> GetMockPlayerStates()
{
return mockPlayerStatesDictionary;
}

public void SetStateHelper<T>(string key, T value, bool reliable = false)
{
Expand Down Expand Up @@ -100,7 +104,7 @@ public Action OnQuit(Action<string> callback)

public void Kick(Action onKickCallBack = null)
{
var player = GetPlayer(_id);
var player = GetPlayerById(_id);
Players.Remove(player.id);
IPlayerBase.onKickCallBack?.Invoke();
}
Expand Down
18 changes: 6 additions & 12 deletions Assets/PlayroomKit/modules/PlayroomService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void InsertCoin(InitOptions options = null, Action onLaunchCallBack = nul
public Player MyPlayer()
{
var id = _interop.MyPlayerWrapper();
return GetPlayer(id);
return GetPlayerById(id);
}

public Player Me()
Expand Down Expand Up @@ -100,8 +100,6 @@ public void StartMatchmaking(Action callback = null)

public void SetState<T>(string key, T value, bool reliable = false)
{
//#Debug.Log("SetState "+ key + ", value is " + value + "of type " + value.GetType());
// Use type checking to call the correct overload of SetStateWrapper
if (value is string)
{
_interop.SetStateStringWrapper(key, (string)(object)value, reliable);
Expand All @@ -118,9 +116,9 @@ public void SetState<T>(string key, T value, bool reliable = false)
{
float floatValue = (float)(object)value;
var floatAsString = floatValue.ToString(CultureInfo.InvariantCulture);
_interop.SetStateFloatWrapper(key, floatAsString, reliable); // Assumes float is serialized as string
_interop.SetStateFloatWrapper(key, floatAsString, reliable);
}
else if (value is object) // For JSON-like object, you'd serialize it to JSON (example using Newtonsoft.Json)
else if (value is object)
{
Debug.Log("SetState "+ key + ", value is " + value + "of type " + value.GetType());
string jsonString = JsonUtility.ToJson(value);
Expand All @@ -136,18 +134,14 @@ public void SetState<T>(string key, Dictionary<string, T> values, bool reliable
{
var jsonObject = new JSONObject();

// Add key-value pairs to the JSON object
foreach (var kvp in values)
{
// Convert the value to double before adding to JSONNode

var value = Convert.ToDouble(kvp.Value);
jsonObject.Add(kvp.Key, value);
}

// Serialize the JSON object to a string
var jsonString = jsonObject.ToString();

// Output the JSON string
_interop.SetStateDictionaryWrapper(key, jsonString, reliable);
}

Expand Down Expand Up @@ -276,9 +270,9 @@ private static void InvokeResetCallBack()
onstatesReset?.Invoke();
}

public void ResetPlayersStates(string[] keysToExclude = null, Action OnStatesReset = null)
public void ResetPlayersStates(string[] keysToExclude = null, Action onStatesReset = null)
{
onstatesReset = OnStatesReset;
onstatesReset = onStatesReset;
string keysJson = keysToExclude != null ? CreateJsonArray(keysToExclude).ToString() : null;
_interop.ResetPlayersStatesWrapper(keysJson, InvokePlayersResetCallBack);
}
Expand Down
2 changes: 1 addition & 1 deletion Assets/PlayroomKit/modules/RPC/RPCLocal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void RpcCall(string name, object data, RpcMode mode, Action callbackOnRes
mockResponseCallbacks.TryAdd(name, callbackOnResponse);

string stringData = Convert.ToString(data);
var player = GetPlayer("mockplayerID123");
var player = GetPlayerById("mockplayerID123");

if (mockRegisterCallbacks.TryGetValue(name, out var responseHandler))
{
Expand Down
2 changes: 1 addition & 1 deletion Assets/Scripts/GameManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ void Start()
}, () =>
{
_playroomKit.OnPlayerJoin(AddPlayer);
_playroomKit.RpcRegister("score", (data, caller) => print($"{data} by {PlayroomKit.GetPlayer(caller).GetProfile().name}"));
_playroomKit.RpcRegister("score", (data, caller) => print($"{data} by {_playroomKit.GetPlayer(caller).GetProfile().name}"));
}, () => { Debug.Log("OnDisconnect callback"); });
}

Expand Down

0 comments on commit 16faa05

Please sign in to comment.