diff --git a/Assets/PlayroomKit/modules/MockMode/BrowserMode/BrowserMockPlayerService.cs b/Assets/PlayroomKit/modules/MockMode/BrowserMode/BrowserMockPlayerService.cs index 358fc69..270e8b4 100644 --- a/Assets/PlayroomKit/modules/MockMode/BrowserMode/BrowserMockPlayerService.cs +++ b/Assets/PlayroomKit/modules/MockMode/BrowserMode/BrowserMockPlayerService.cs @@ -1,61 +1,84 @@ - -using System; +using System; +using UBB; +using UnityEngine; namespace Playroom { public class BrowserMockPlayerService : PlayroomKit.Player.IPlayerBase { - - #region State + private readonly UnityBrowserBridge _ubb; + private string _id; + + public BrowserMockPlayerService(UnityBrowserBridge ubb, string id) + { + _ubb = ubb; + _id = id; + } + + #region State + public void SetState(string key, int value, bool reliable = false) { - throw new NotImplementedException(); + _ubb.CallJs("SetPlayerStateByPlayerId", null, null, false, _id, key, value.ToString(), + reliable.ToString().ToLower()); } public void SetState(string key, float value, bool reliable = false) { - throw new NotImplementedException(); + _ubb.CallJs("SetPlayerStateByPlayerId", null, null, false, _id, key, value.ToString(), + reliable.ToString().ToLower()); } public void SetState(string key, bool value, bool reliable = false) { - throw new NotImplementedException(); + _ubb.CallJs("SetPlayerStateByPlayerId", null, null, false, _id, key, value.ToString(), + reliable.ToString().ToLower()); } public void SetState(string key, string value, bool reliable = false) { - throw new NotImplementedException(); + _ubb.CallJs("SetPlayerStateByPlayerId", null, null, false, _id, key, value.ToString(), + reliable.ToString().ToLower()); } public void SetState(string key, object value, bool reliable = false) { - throw new NotImplementedException(); + _ubb.CallJs("SetPlayerStateByPlayerId", null, null, false, _id, key, value.ToString(), + reliable.ToString().ToLower()); } public T GetState(string key) { - throw new NotImplementedException(); + Debug.Log(key); + return _ubb.CallJs(key); } + #endregion public PlayroomKit.Player.Profile GetProfile() { - throw new NotImplementedException(); + string json = _ubb.CallJs("GetProfile", null, null, false, _id); + + Debug.Log(json); + + var profileData = Helpers.ParseProfile(json); + return profileData; } public Action OnQuit(Action callback) { - throw new NotImplementedException(); + Debug.LogWarning("OnQuit not supported yet in Browser Mode"); + return default; } public void Kick(Action onKickCallBack = null) { - throw new NotImplementedException(); + _ubb.CallJs("Kick", null, null, true, _id); } public void WaitForState(string stateKey, Action onStateSetCallback = null) { - throw new NotImplementedException(); + _ubb.CallJs("WaitForPlayerState", null, null, true, _id, stateKey); } } } \ No newline at end of file diff --git a/Assets/PlayroomKit/modules/MockMode/BrowserMode/MockModeBrowser_old.cs b/Assets/PlayroomKit/modules/MockMode/BrowserMode/MockModeBrowser_old.cs index 11efb6b..67fc023 100644 --- a/Assets/PlayroomKit/modules/MockMode/BrowserMode/MockModeBrowser_old.cs +++ b/Assets/PlayroomKit/modules/MockMode/BrowserMode/MockModeBrowser_old.cs @@ -72,13 +72,13 @@ private static void MockOnPlayerJoinBrowser(Action onPlayerJoinCallback) // #endif // } // -// private static void MockOnPlayerQuitLocal(Action onPlayerQuitCallback) -// { -// Debug.Log("On Player Quit"); -// var testPlayer = GetPlayer(PlayerId); -// testPlayer.OnQuitCallbacks.Add(onPlayerQuitCallback); -// __OnQuitInternalHandler(PlayerId); -// } + // private static void MockOnPlayerQuitLocal(Action onPlayerQuitCallback) + // { + // Debug.Log("On Player Quit"); + // var testPlayer = GetPlayer(PlayerId); + // testPlayer.OnQuitCallbacks.Add(onPlayerQuitCallback); + // __OnQuitInternalHandler(PlayerId); + // } /// /// This function is used by GetPlayerID in PlayroomKitDevManager, GetPlayer is only invoked diff --git a/Assets/PlayroomKit/modules/MockMode/BrowserMode/PlayroomBrowserMockService.cs b/Assets/PlayroomKit/modules/MockMode/BrowserMode/PlayroomBrowserMockService.cs index ebef2a4..44abf45 100644 --- a/Assets/PlayroomKit/modules/MockMode/BrowserMode/PlayroomBrowserMockService.cs +++ b/Assets/PlayroomKit/modules/MockMode/BrowserMode/PlayroomBrowserMockService.cs @@ -25,7 +25,7 @@ public void InsertCoin(InitOptions options = null, Action onLaunchCallBack = nul if (options != null) optionsJson = Helpers.SerializeInitOptions(options); var gameObjectName = _ubb.GetGameObject("InsertCoin").name; - CallJs("InsertCoin", onLaunchCallBack.GetMethodInfo().Name, gameObjectName, true, optionsJson); + _ubb.CallJs("InsertCoin", onLaunchCallBack.GetMethodInfo().Name, gameObjectName, true, optionsJson); PlayroomKit.IsPlayRoomInitialized = true; } @@ -34,7 +34,7 @@ public Action OnPlayerJoin(Action onPlayerJoinCallback) if (!PlayroomKit.IPlayroomBase.OnPlayerJoinCallbacks.Contains(onPlayerJoinCallback)) PlayroomKit.IPlayroomBase.OnPlayerJoinCallbacks.Add(onPlayerJoinCallback); - CallJs("OnPlayerJoin", null, _ubb.GetGameObject("devManager").name); + _ubb.CallJs("OnPlayerJoin", null, _ubb.GetGameObject("devManager").name); void Unsub() { @@ -46,7 +46,7 @@ void Unsub() public void StartMatchmaking(Action callback = null) { - CallJs("StartMatchmaking", null, null, true); + _ubb.CallJs("StartMatchmaking", null, null, true); callback?.Invoke(); } @@ -57,7 +57,7 @@ public void OnDisconnect(Action callback) GameObject callbackObject = new GameObject(callbackKey); MockCallbackInvoker invoker = callbackObject.AddComponent(); invoker.SetCallback(callback, callbackKey); - CallJs("OnDisconnect", callbackKey); + _ubb.CallJs("OnDisconnect", callbackKey); } #endregion @@ -66,7 +66,7 @@ public void OnDisconnect(Action callback) public PlayroomKit.Player MyPlayer() { - string id = CallJs("MyPlayer"); + string id = _ubb.CallJs("MyPlayer"); return PlayroomKit.GetPlayerById(id); } @@ -81,17 +81,17 @@ public PlayroomKit.Player Me() public bool IsHost() { - return CallJs("IsHost"); + return _ubb.CallJs("IsHost"); } public string GetRoomCode() { - return CallJs("GetRoomCode"); + return _ubb.CallJs("GetRoomCode"); } public bool IsStreamScreen() { - return CallJs("IsStreamScreen"); + return _ubb.CallJs("IsStreamScreen"); } #endregion @@ -100,13 +100,13 @@ public bool IsStreamScreen() public void SetState(string key, T value, bool reliable = false) { - CallJs("SetState", null, null, true, + _ubb.CallJs("SetState", null, null, true, key, value.ToString(), reliable.ToString().ToLower()); } public T GetState(string key) { - return CallJs("GetState", null, null, false, key); + return _ubb.CallJs("GetState", null, null, false, key); } public void WaitForState(string stateKey, Action onStateSetCallback = null) @@ -119,7 +119,7 @@ public void WaitForState(string stateKey, Action onStateSetCallback = nu CallBacksHandlerMock.Instance.RegisterCallbackObject(callbackKey, callbackObject, "ExecuteCallback"); - CallJs("WaitForState", null, null, true, stateKey, callbackKey); + _ubb.CallJs("WaitForState", null, null, true, stateKey, callbackKey); } public void WaitForPlayerState(string playerID, string stateKey, Action onStateSetCallback = null) @@ -132,18 +132,18 @@ public void WaitForPlayerState(string playerID, string stateKey, Action CallBacksHandlerMock.Instance.RegisterCallbackObject(callbackKey, callbackObject, "ExecuteCallback"); - CallJs("WaitForPlayerState", null, null, true, playerID, stateKey, callbackKey); + _ubb.CallJs("WaitForPlayerState", null, null, true, playerID, stateKey, callbackKey); } public void ResetStates(string[] keysToExclude = null, Action onStatesReset = null) { - CallJs("ResetStates", null, null, true, keysToExclude); + _ubb.CallJs("ResetStates", null, null, true, keysToExclude); onStatesReset?.Invoke(); } public void ResetPlayersStates(string[] keysToExclude = null, Action onStatesReset = null) { - CallJs("ResetPlayersStates", null, null, true, keysToExclude); + _ubb.CallJs("ResetPlayersStates", null, null, true, keysToExclude); onStatesReset?.Invoke(); } @@ -177,53 +177,6 @@ public Dpad DpadJoystick() #endregion #region Utils - - private void CallJs(string jsFunctionName, string callbackName = null, string gameObjectName = null, - bool isAsync = false, params string[] args) - { - List allParams = new(); - - foreach (var param in args) - { - if (param.StartsWith("{") && param.EndsWith("}")) - allParams.Add(param); - else - allParams.Add($"'{param}'"); - } - - if (!string.IsNullOrEmpty(callbackName)) allParams.Add($"'{callbackName}'"); - if (!string.IsNullOrEmpty(gameObjectName)) allParams.Add($"'{gameObjectName}'"); - - string jsCall = $"{jsFunctionName}({string.Join(", ", allParams)})"; - if (isAsync) jsCall = $"await {jsCall}"; - - - Debug.Log(jsCall); - _ubb.ExecuteJS(jsCall); - } - - - private T CallJs(string jsFunctionName, string callbackName = null, string gameObjectName = null, - bool isAsync = false, params string[] args) - { - List allParams = new(); - foreach (string param in args) - { - if (param.StartsWith("{") && param.EndsWith("}")) - allParams.Add(param); - else - allParams.Add($"'{param}'"); - } - - if (!string.IsNullOrEmpty(callbackName)) allParams.Add($"'{callbackName}'"); - if (!string.IsNullOrEmpty(gameObjectName)) allParams.Add($"'{gameObjectName}'"); - - string jsCall = $"{jsFunctionName}({string.Join(", ", allParams)})"; - if (isAsync) jsCall = $"await {jsCall}"; - - return _ubb.ExecuteJS(jsCall); - } - public static void MockOnPlayerJoinWrapper(string playerId) { PlayroomKit.IPlayroomBase.OnPlayerJoinWrapperCallback(playerId); diff --git a/Assets/PlayroomKit/modules/Player/Player.cs b/Assets/PlayroomKit/modules/Player/Player.cs index d065876..ec1864d 100644 --- a/Assets/PlayroomKit/modules/Player/Player.cs +++ b/Assets/PlayroomKit/modules/Player/Player.cs @@ -15,7 +15,6 @@ public partial class PlayroomKit { public partial class Player { - //DI public string id; IPlayerBase _playerService;