From f139a5d17c0d12465853993282a7be5b4c458190 Mon Sep 17 00:00:00 2001 From: momintlh <77355191+momintlh@users.noreply.github.com> Date: Tue, 11 Jun 2024 21:26:22 +0500 Subject: [PATCH 1/4] feat: separated native functions and mockmode --- Assets/PlayroomKit/Headers.cs | 90 +++++++++++++++++++++ Assets/PlayroomKit/Headers.cs.meta | 11 +++ Assets/PlayroomKit/MockMode.cs | 42 ++++++++++ Assets/PlayroomKit/MockMode.cs.meta | 11 +++ Assets/PlayroomKit/PlayroomKit.cs | 120 +++------------------------- Assets/Scripts/GameManager.cs | 3 + 6 files changed, 169 insertions(+), 108 deletions(-) create mode 100644 Assets/PlayroomKit/Headers.cs create mode 100644 Assets/PlayroomKit/Headers.cs.meta create mode 100644 Assets/PlayroomKit/MockMode.cs create mode 100644 Assets/PlayroomKit/MockMode.cs.meta diff --git a/Assets/PlayroomKit/Headers.cs b/Assets/PlayroomKit/Headers.cs new file mode 100644 index 0000000..cdf723b --- /dev/null +++ b/Assets/PlayroomKit/Headers.cs @@ -0,0 +1,90 @@ +using System; +using System.Runtime.InteropServices; + + +namespace Playroom +{ + public partial class PlayroomKit + { + [DllImport("__Internal")] + private static extern void InsertCoinInternal(string options, + Action onLaunchCallback, + Action onQuitInternalCallback, + Action onDisconnectCallback, + Action onError, + string onLaunchCallBackKey, + string onDisconnectCallBackKey); + + [DllImport("__Internal")] + private static extern string OnPlayerJoinInternal(Action callback); + + [DllImport("__Internal")] + private static extern void UnsubscribeOnPlayerJoinInternal(string id); + + [DllImport("__Internal")] + private static extern bool IsHostInternal(); + + [DllImport("__Internal")] + private static extern bool IsStreamModeInternal(); + + [DllImport("__Internal")] + private static extern string MyPlayerInternal(); + + [DllImport("__Internal")] + public static extern string GetRoomCode(); + + [DllImport("__Internal")] + private static extern void OnDisconnectInternal(Action callback); + + [DllImport("__Internal")] + private static extern void SetStateString(string key, string value, bool reliable = false); + + [DllImport("__Internal")] + private static extern void SetStateInternal(string key, int value, bool reliable = false); + + [DllImport("__Internal")] + private static extern void SetStateInternal(string key, bool value, bool reliable = false); + + [DllImport("__Internal")] + private static extern void SetStateFloatInternal(string key, string floatAsString, bool reliable = false); + + [DllImport("__Internal")] + private static extern void SetStateDictionary(string key, string jsonValues, bool reliable = false); + + [DllImport("__Internal")] + private static extern string GetStateStringInternal(string key); + + [DllImport("__Internal")] + private static extern int GetStateIntInternal(string key); + + [DllImport("__Internal")] + private static extern float GetStateFloatInternal(string key); + + [DllImport("__Internal")] + private static extern string GetStateDictionaryInternal(string key); + + [DllImport("__Internal")] + private static extern void WaitForStateInternal(string stateKey, Action onStateSetCallback); + + [DllImport("__Internal")] + private static extern void WaitForPlayerStateInternal(string playerID, string StateKey, Action onStateSetCallback); + + [DllImport("__Internal")] + private static extern void ResetStatesInternal(string keysToExclude = null, Action OnStatesReset = null); + + [DllImport("__Internal")] + private static extern void ResetPlayersStatesInternal(string keysToExclude, Action OnPlayersStatesReset = null); + + [DllImport("__Internal")] + private static extern void UnsubscribeOnQuitInternal(); + + [DllImport("__Internal")] + private static extern void CreateJoystickInternal(string joyStickOptionsJson); + + [DllImport("__Internal")] + private static extern string DpadJoystickInternal(); + + [DllImport("__Internal")] + private static extern void StartMatchmakingInternal(Action callback); + } +} \ No newline at end of file diff --git a/Assets/PlayroomKit/Headers.cs.meta b/Assets/PlayroomKit/Headers.cs.meta new file mode 100644 index 0000000..38ae076 --- /dev/null +++ b/Assets/PlayroomKit/Headers.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 9a9b84c597b4c2c47b6c9c762060b126 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/PlayroomKit/MockMode.cs b/Assets/PlayroomKit/MockMode.cs new file mode 100644 index 0000000..a0c5721 --- /dev/null +++ b/Assets/PlayroomKit/MockMode.cs @@ -0,0 +1,42 @@ +using System.Buffers.Text; +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +namespace Playroom +{ + public partial class PlayroomKit + { + private const string PlayerId = "mockPlayer"; + private static bool mockIsStreamMode; + + /* + This is private, instead of public, to prevent tampering in Mock Mode. + Reason: In Mock Mode, only a single player can be tested. + Ref: https://docs.joinplayroom.com/usage/unity#mock-mode + */ + private static Dictionary MockDictionary = new(); + + public static void MockSetState(string key, object value) + { + if (MockDictionary.ContainsKey(key)) + MockDictionary[key] = value; + else + MockDictionary.Add(key, value); + } + + public static T MockGetState(string key) + { + if (MockDictionary.TryGetValue(key, out var value) && value is T typedValue) + { + return typedValue; + } + else + { + Debug.LogWarning($"No {key} in States or value is not of type {typeof(T)}"); + return default; + } + } + } + +} \ No newline at end of file diff --git a/Assets/PlayroomKit/MockMode.cs.meta b/Assets/PlayroomKit/MockMode.cs.meta new file mode 100644 index 0000000..38e8799 --- /dev/null +++ b/Assets/PlayroomKit/MockMode.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 2caf136702f132c4fa146a9a7ae15f81 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/PlayroomKit/PlayroomKit.cs b/Assets/PlayroomKit/PlayroomKit.cs index 891c182..3c21126 100644 --- a/Assets/PlayroomKit/PlayroomKit.cs +++ b/Assets/PlayroomKit/PlayroomKit.cs @@ -14,19 +14,6 @@ public partial class PlayroomKit { private static bool isPlayRoomInitialized; - /// - /// Required Mock Mode: - /// - private const string PlayerId = "mockPlayer"; - private static bool mockIsStreamMode; - - /* - This is private, instead of public, to prevent tampering in Mock Mode. - Reason: In Mock Mode, only a single player can be tested. - Ref: https://docs.joinplayroom.com/usage/unity#mock-mode - */ - private static Dictionary MockDictionary = new(); - private static readonly Dictionary Players = new(); [Serializable] @@ -55,17 +42,6 @@ public class MatchMakingOptions public int waitBeforeCreatingNewRoom = 5000; } - [DllImport("__Internal")] - private static extern void InsertCoinInternal( - string options, - Action onLaunchCallback, - Action onQuitInternalCallback, - Action onDisconnectCallback, - Action onError, - string onLaunchCallBackKey, - string onDisconnectCallBackKey - ); - [MonoPInvokeCallback(typeof(Action))] private static void InvokeInsertCoin(string key) { @@ -216,11 +192,7 @@ private static JSONNode ConvertValueToJSON(object value) } } - [DllImport("__Internal")] - private static extern string OnPlayerJoinInternal(Action callback); - [DllImport("__Internal")] - private static extern void UnsubscribeOnPlayerJoinInternal(string id); private static List> OnPlayerJoinCallbacks = new(); @@ -327,8 +299,7 @@ public static Player GetPlayer(string playerId) } - [DllImport("__Internal")] - private static extern bool IsHostInternal(); + public static bool IsHost() { @@ -350,8 +321,7 @@ public static bool IsHost() } } - [DllImport("__Internal")] - private static extern bool IsStreamModeInternal(); + public static bool IsStreamMode() { @@ -373,8 +343,7 @@ public static bool IsStreamMode() } } - [DllImport("__Internal")] - private static extern string MyPlayerInternal(); + public static Player MyPlayer() { @@ -402,13 +371,8 @@ public static Player Me() return MyPlayer(); } - [DllImport("__Internal")] - public static extern string GetRoomCode(); - [DllImport("__Internal")] - private static extern void OnDisconnectInternal(Action callback); - public static void OnDisconnect(Action callback) { @@ -417,19 +381,6 @@ public static void OnDisconnect(Action callback) } - [DllImport("__Internal")] - private static extern void SetStateString(string key, string value, bool reliable = false); - - [DllImport("__Internal")] - private static extern void SetStateInternal(string key, int value, bool reliable = false); - - - [DllImport("__Internal")] - private static extern void SetStateInternal(string key, bool value, bool reliable = false); - - [DllImport("__Internal")] - private static extern void SetStateFloatInternal(string key, string floatAsString, bool reliable = false); - public static void SetState(string key, string value, bool reliable = false) { if (IsRunningInBrowser()) @@ -511,8 +462,7 @@ public static void SetState(string key, bool value, bool reliable = false) } } - [DllImport("__Internal")] - private static extern void SetStateDictionary(string key, string jsonValues, bool reliable = false); + public static void SetState(string key, Dictionary values, bool reliable = false) @@ -597,8 +547,7 @@ public static void SetState(string key, Dictionary values, bool // GETTERS - [DllImport("__Internal")] - private static extern string GetStateStringInternal(string key); + private static string GetStateString(string key) { @@ -620,8 +569,7 @@ private static string GetStateString(string key) } } - [DllImport("__Internal")] - private static extern int GetStateIntInternal(string key); + private static int GetStateInt(string key) { @@ -643,8 +591,7 @@ private static int GetStateInt(string key) } } - [DllImport("__Internal")] - private static extern float GetStateFloatInternal(string key); + private static float GetStateFloat(string key) { @@ -752,8 +699,7 @@ public static Dictionary GetState(string key, bool isReturnDiction } - [DllImport("__Internal")] - private static extern void WaitForStateInternal(string stateKey, Action onStateSetCallback); + [MonoPInvokeCallback(typeof(Action))] @@ -774,8 +720,7 @@ public static void WaitForState(string stateKey, Action onStateSetCallba - [DllImport("__Internal")] - private static extern void WaitForPlayerStateInternal(string playerID, string StateKey, Action onStateSetCallback); + Action Callback = null; public void WaitForPlayerState(string playerID, string StateKey, Action onStateSetCallback = null) @@ -794,11 +739,6 @@ void OnStateSetCallback() } - [DllImport("__Internal")] - private static extern string GetStateDictionaryInternal(string key); - - - // Utils: private static void SetStateHelper(string key, Dictionary values, bool reliable = false) { @@ -844,8 +784,6 @@ private static Dictionary ParseJsonToDictionary(string jsonString) return dictionary; } - [DllImport("__Internal")] - private static extern void ResetStatesInternal(string keysToExclude = null, Action OnStatesReset = null); private static Action onstatesReset; private static Action onplayersStatesReset; @@ -873,8 +811,7 @@ private static void InvokePlayersResetCallBack() } - [DllImport("__Internal")] - private static extern void ResetPlayersStatesInternal(string keysToExclude, Action OnPlayersStatesReset = null); + public static void ResetPlayersStates(string[] keysToExclude = null, Action OnStatesReset = null) { @@ -908,29 +845,7 @@ public static bool IsRunningInBrowser() #endif } - private static void MockSetState(string key, object value) - { - if (MockDictionary.ContainsKey(key)) - MockDictionary[key] = value; - else - MockDictionary.Add(key, value); - } - - private static T MockGetState(string key) - { - if (MockDictionary.TryGetValue(key, out var value) && value is T typedValue) - { - return typedValue; - } - else - { - Debug.LogWarning($"No {key} in States or value is not of type {typeof(T)}"); - return default; - } - } - [DllImport("__Internal")] - private static extern void UnsubscribeOnQuitInternal(); private static void UnsubscribeOnQuit() { @@ -952,18 +867,12 @@ private static void __OnQuitInternalHandler(string playerId) // Joystick - [DllImport("__Internal")] - private static extern void CreateJoystickInternal(string joyStickOptionsJson); - public static void CreateJoyStick(JoystickOptions options) { var jsonStr = ConvertJoystickOptionsToJson(options); CreateJoystickInternal(jsonStr); } - [DllImport("__Internal")] - private static extern string DpadJoystickInternal(); - public static Dpad DpadJoystick() { var jsonString = DpadJoystickInternal(); @@ -1021,7 +930,7 @@ public class JoystickOptions public ZoneOptions zones = null; } - [System.Serializable] + [Serializable] public class ButtonOptions { public string id = null; @@ -1038,18 +947,13 @@ public class ZoneOptions } - [System.Serializable] + [Serializable] public class Dpad { public string x; public string y; } - - - [DllImport("__Internal")] - private static extern void StartMatchmakingInternal(Action callback); - static Action startMatchmakingCallback = null; public static void StartMatchmaking(Action callback = null) { diff --git a/Assets/Scripts/GameManager.cs b/Assets/Scripts/GameManager.cs index 86dec15..426d6b5 100644 --- a/Assets/Scripts/GameManager.cs +++ b/Assets/Scripts/GameManager.cs @@ -57,6 +57,9 @@ void Start() PlayroomKit.RpcRegister("ShootBullet", HandleScoreUpdate, "You shot a bullet!"); PlayroomKit.WaitForState("posX", PosX); PlayroomKit.WaitForState("posY", PosY); + + + } private static void PosX(string pos) From b05e55d7aeb468a450c7c3e13422b48f747a2972 Mon Sep 17 00:00:00 2001 From: momintlh <77355191+momintlh@users.noreply.github.com> Date: Tue, 11 Jun 2024 21:57:51 +0500 Subject: [PATCH 2/4] chore: changed to private --- Assets/PlayroomKit/MockMode.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Assets/PlayroomKit/MockMode.cs b/Assets/PlayroomKit/MockMode.cs index a0c5721..4f1c9c1 100644 --- a/Assets/PlayroomKit/MockMode.cs +++ b/Assets/PlayroomKit/MockMode.cs @@ -17,7 +17,7 @@ public partial class PlayroomKit */ private static Dictionary MockDictionary = new(); - public static void MockSetState(string key, object value) + private static void MockSetState(string key, object value) { if (MockDictionary.ContainsKey(key)) MockDictionary[key] = value; @@ -25,7 +25,7 @@ public static void MockSetState(string key, object value) MockDictionary.Add(key, value); } - public static T MockGetState(string key) + private static T MockGetState(string key) { if (MockDictionary.TryGetValue(key, out var value) && value is T typedValue) { From 674f69d07355f8f3614b6c763671feb6d3425aa3 Mon Sep 17 00:00:00 2001 From: momintlh <77355191+momintlh@users.noreply.github.com> Date: Wed, 12 Jun 2024 20:50:09 +0500 Subject: [PATCH 3/4] feat: added RPC mockmode, cleaned logs --- Assets/PlayroomKit/MockMode.cs | 51 ++++++++++++++++--- Assets/PlayroomKit/PlayroomKit.cs | 18 ++----- Assets/PlayroomKit/modules/CallbackManager.cs | 2 +- Assets/PlayroomKit/modules/Player.cs | 7 +-- Assets/PlayroomKit/modules/RPC.cs | 5 +- Assets/Scripts/GameManager.cs | 28 +++++----- 6 files changed, 65 insertions(+), 46 deletions(-) diff --git a/Assets/PlayroomKit/MockMode.cs b/Assets/PlayroomKit/MockMode.cs index 4f1c9c1..0e0999b 100644 --- a/Assets/PlayroomKit/MockMode.cs +++ b/Assets/PlayroomKit/MockMode.cs @@ -1,22 +1,38 @@ +using System; using System.Buffers.Text; using System.Collections; using System.Collections.Generic; +using Unity.VisualScripting; using UnityEngine; namespace Playroom { public partial class PlayroomKit { - private const string PlayerId = "mockPlayer"; + private const string PlayerId = "mockplayerID123"; private static bool mockIsStreamMode; - /* - This is private, instead of public, to prevent tampering in Mock Mode. - Reason: In Mock Mode, only a single player can be tested. - Ref: https://docs.joinplayroom.com/usage/unity#mock-mode - */ + /// + /// This is private, instead of public, to prevent tampering in Mock Mode. + /// Reason: In Mock Mode, only a single player can be tested. + /// Ref: https://docs.joinplayroom.com/usage/unity#mock-mode + /// private static Dictionary MockDictionary = new(); + + + + private static void MockInsertCoin(InitOptions options, Action onLaunchCallBack) + { + isPlayRoomInitialized = true; + + Debug.Log("Coin Inserted"); + + string optionsJson = null; + if (options != null) optionsJson = SerializeInitOptions(options); + onLaunchCallBack?.Invoke(); + } + private static void MockSetState(string key, object value) { if (MockDictionary.ContainsKey(key)) @@ -37,6 +53,29 @@ private static T MockGetState(string key) return default; } } + + private static Dictionary mockCallbackOnResponse = new(); + + private static void MockRpcRegister(string name, Action rpcRegisterCallback, string onResponseReturn = null) + { + CallbackManager.RegisterCallback(rpcRegisterCallback, name); + if (!string.IsNullOrEmpty(onResponseReturn)) Debug.Log(onResponseReturn); + } + + private static void MockRpcCall(string name, object data, RpcMode mode, Action callbackOnResponse) + { + if (!mockCallbackOnResponse.ContainsKey(name)) + mockCallbackOnResponse.Add(name, callbackOnResponse); + + string stringData = Convert.ToString(data); + var player = MyPlayer(); + CallbackManager.InvokeCallback(name, stringData, player.id); + + if (mockCallbackOnResponse.TryGetValue(name, out var callback)) + { + callback?.Invoke(); + } + } } } \ No newline at end of file diff --git a/Assets/PlayroomKit/PlayroomKit.cs b/Assets/PlayroomKit/PlayroomKit.cs index 3c21126..88e874a 100644 --- a/Assets/PlayroomKit/PlayroomKit.cs +++ b/Assets/PlayroomKit/PlayroomKit.cs @@ -95,16 +95,12 @@ public static void InsertCoin(InitOptions options = null, Action onLaunchCallBac } else { - isPlayRoomInitialized = true; - - Debug.Log("Coin Inserted"); - - string optionsJson = null; - if (options != null) optionsJson = SerializeInitOptions(options); - onLaunchCallBack?.Invoke(); + MockInsertCoin(options, onLaunchCallBack); } } + + private static string SerializeInitOptions(InitOptions options) { if (options == null) return null; @@ -395,7 +391,6 @@ public static void SetState(string key, string value, bool reliable = false) } else { - Debug.Log($"State Set! Key: {key}, Value: {value}"); MockSetState(key, value); } } @@ -415,7 +410,6 @@ public static void SetState(string key, int value, bool reliable = false) } else { - Debug.Log($"State Set! Key: {key}, Value: {value}"); MockSetState(key, value); } } @@ -436,7 +430,6 @@ public static void SetState(string key, float value, bool reliable = false) } else { - Debug.Log($"State Set! Key: {key}, Value: {value}"); MockSetState(key, value); } } @@ -456,7 +449,6 @@ public static void SetState(string key, bool value, bool reliable = false) } else { - Debug.Log($"State Set! Key: {key}, Value: {value}"); MockSetState(key, value); } } @@ -479,7 +471,6 @@ public static void SetState(string key, Dictionary values, bool rel } else { - Debug.Log($"State Set! Key: {key}, Value: {values}"); MockSetState(key, values); } } @@ -499,7 +490,6 @@ public static void SetState(string key, Dictionary values, bool r } else { - Debug.Log($"State Set! Key: {key}, Value: {values}"); MockSetState(key, values); } } @@ -519,7 +509,6 @@ public static void SetState(string key, Dictionary values, bool re } else { - Debug.Log($"State Set! Key: {key}, Value: {values}"); MockSetState(key, values); } } @@ -539,7 +528,6 @@ public static void SetState(string key, Dictionary values, bool } else { - Debug.Log($"State Set! Key: {key}, Value: {values}"); MockSetState(key, values); } } diff --git a/Assets/PlayroomKit/modules/CallbackManager.cs b/Assets/PlayroomKit/modules/CallbackManager.cs index 206009b..849f29d 100644 --- a/Assets/PlayroomKit/modules/CallbackManager.cs +++ b/Assets/PlayroomKit/modules/CallbackManager.cs @@ -34,7 +34,7 @@ public static void InvokeCallback(string key, params string[] args) if (callback is Action action && args.Length == 0) action?.Invoke(); else if (callback is Action stringAction && args.Length == 1) stringAction?.Invoke(args[0]); else if (callback is Action doubleStringAction && args.Length == 2) doubleStringAction?.Invoke(args[0], args[1]); - else Debug.LogError($"Callback with key {key} is of unsupported type or incorrect number of arguments!"); + else Debug.LogError($"Callback with key {key} is of unsupported type or incorrect number of arguments: {args[0]}!"); } else { diff --git a/Assets/PlayroomKit/modules/Player.cs b/Assets/PlayroomKit/modules/Player.cs index a0a815f..48d24ed 100644 --- a/Assets/PlayroomKit/modules/Player.cs +++ b/Assets/PlayroomKit/modules/Player.cs @@ -212,7 +212,6 @@ public void SetState(string key, object value, bool reliable = false) } else { - Debug.Log($"State Set! Key: {key}, Value: {value}"); MockSetState(key, value); } } @@ -383,7 +382,6 @@ public void SetState(string key, Dictionary values, bool reliable = } else { - Debug.Log($"PlayerState Set! Key: {key}, Value: {values}"); MockSetState(key, values); } } @@ -403,7 +401,6 @@ public void SetState(string key, Dictionary values, bool reliable } else { - Debug.Log($"PlayerState Set! Key: {key}, Value: {values}"); MockSetState(key, values); } } @@ -423,7 +420,6 @@ public void SetState(string key, Dictionary values, bool reliable } else { - Debug.Log($"PlayerState Set! Key: {key}, Value: {values}"); MockSetState(key, values); } } @@ -443,7 +439,6 @@ public void SetState(string key, Dictionary values, bool reliabl } else { - Debug.Log($"PlayerState Set! Key: {key}, Value: {values}"); MockSetState(key, values); } } @@ -531,7 +526,7 @@ public Profile GetProfile() var testProfile = new Profile() { color = color1, - name = "CoolPlayTest", + name = "MockPlayer", jsonColor = mockJsonColor, photo = "testPhoto" diff --git a/Assets/PlayroomKit/modules/RPC.cs b/Assets/PlayroomKit/modules/RPC.cs index 9555f8d..c32e7f8 100644 --- a/Assets/PlayroomKit/modules/RPC.cs +++ b/Assets/PlayroomKit/modules/RPC.cs @@ -32,7 +32,7 @@ public static void RpcRegister(string name, Action rpcRegisterCa } else { - Debug.LogError("[Mock Mode] RPC is currently not supported in Mock Mode!\n Please build the project to test RPC."); + MockRpcRegister(name, rpcRegisterCallback, onResponseReturn); } } @@ -79,7 +79,6 @@ private static void InvokeRpcRegisterCallBack(string dataJson, string senderJson public static void RpcCall(string name, object data, RpcMode mode, Action callbackOnResponse) { - if (IsRunningInBrowser()) { string jsonData = ConvertToJson(data); @@ -111,7 +110,7 @@ public static void RpcCall(string name, object data, RpcMode mode, Action callba } else { - Debug.LogError("[Mock Mode] RPC Calls are not supported in Mock Mode! yet.\nPlease make a build to test RPC."); + MockRpcCall(name, data, RpcMode.ALL, callbackOnResponse); } } diff --git a/Assets/Scripts/GameManager.cs b/Assets/Scripts/GameManager.cs index 426d6b5..74be0b0 100644 --- a/Assets/Scripts/GameManager.cs +++ b/Assets/Scripts/GameManager.cs @@ -55,21 +55,13 @@ private void Initialize() void Start() { PlayroomKit.RpcRegister("ShootBullet", HandleScoreUpdate, "You shot a bullet!"); - PlayroomKit.WaitForState("posX", PosX); - PlayroomKit.WaitForState("posY", PosY); - - - + PlayroomKit.RpcRegister("Siu", Siu, "siuu"); } - private static void PosX(string pos) + private void Siu(string arg1, string arg2) { - Debug.Log($"pos X state is ready! {pos}"); - } - - private static void PosY(string pos) - { - Debug.Log($"pos Y state2 is ready! {pos}"); + var player = PlayroomKit.GetPlayer(arg2); + Debug.Log($"Caller: {arg2}, Player Name: {player?.GetProfile().name}, Data: {arg1}"); } void HandleScoreUpdate(string data, string caller) @@ -77,14 +69,11 @@ void HandleScoreUpdate(string data, string caller) var player = PlayroomKit.GetPlayer(caller); Debug.Log($"Caller: {caller}, Player Name: {player?.GetProfile().name}, Data: {data}"); - if (PlayerDict.TryGetValue(caller, out GameObject playerObj)) { - var playerController = playerObj.GetComponent(); if (playerController != null) { - playerController.scoreText.text = $"Score: {data}"; } else @@ -112,6 +101,15 @@ private void Update() ShootBullet(index); + + if (Input.GetKeyDown(KeyCode.W)) + { + PlayroomKit.RpcCall("Siu", 69, () => + { + Debug.LogWarning("hheeh"); + }); + } + if (Input.GetKeyDown(KeyCode.R) && PlayroomKit.IsHost()) { PlayroomKit.ResetStates(null, () => From 0fd6a3f7b5e2b4e1d5f8b7ad87f0007bb6b91340 Mon Sep 17 00:00:00 2001 From: momintlh <77355191+momintlh@users.noreply.github.com> Date: Wed, 12 Jun 2024 21:14:59 +0500 Subject: [PATCH 4/4] fix: fixed rpc response in mockmode --- Assets/PlayroomKit/MockMode.cs | 23 +++++++++++++------ Assets/PlayroomKit/modules/CallbackManager.cs | 1 - Assets/PlayroomKit/modules/RPC.cs | 2 +- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/Assets/PlayroomKit/MockMode.cs b/Assets/PlayroomKit/MockMode.cs index 0e0999b..5a62ee9 100644 --- a/Assets/PlayroomKit/MockMode.cs +++ b/Assets/PlayroomKit/MockMode.cs @@ -54,28 +54,37 @@ private static T MockGetState(string key) } } - private static Dictionary mockCallbackOnResponse = new(); + private static Dictionary callback, string response)> mockRegisterCallbacks = new(); + private static Dictionary mockResponseCallbacks = new(); private static void MockRpcRegister(string name, Action rpcRegisterCallback, string onResponseReturn = null) { - CallbackManager.RegisterCallback(rpcRegisterCallback, name); - if (!string.IsNullOrEmpty(onResponseReturn)) Debug.Log(onResponseReturn); + mockRegisterCallbacks.TryAdd(name, (rpcRegisterCallback, onResponseReturn)); } private static void MockRpcCall(string name, object data, RpcMode mode, Action callbackOnResponse) { - if (!mockCallbackOnResponse.ContainsKey(name)) - mockCallbackOnResponse.Add(name, callbackOnResponse); + mockResponseCallbacks.TryAdd(name, callbackOnResponse); string stringData = Convert.ToString(data); var player = MyPlayer(); - CallbackManager.InvokeCallback(name, stringData, player.id); - if (mockCallbackOnResponse.TryGetValue(name, out var callback)) + if (mockRegisterCallbacks.TryGetValue(name, out var responseHandler)) + { + responseHandler.callback?.Invoke(stringData, player.id); + + if (!string.IsNullOrEmpty(responseHandler.response)) + { + Debug.Log($"Response received: {responseHandler.response}"); + } + } + + if (mockResponseCallbacks.TryGetValue(name, out var callback)) { callback?.Invoke(); } } + } } \ No newline at end of file diff --git a/Assets/PlayroomKit/modules/CallbackManager.cs b/Assets/PlayroomKit/modules/CallbackManager.cs index 849f29d..fc8e9cb 100644 --- a/Assets/PlayroomKit/modules/CallbackManager.cs +++ b/Assets/PlayroomKit/modules/CallbackManager.cs @@ -14,7 +14,6 @@ public static string RegisterCallback(Delegate callback, string key = null) if (string.IsNullOrEmpty(key)) key = GenerateKey(); - if (!callbacks.ContainsKey(key)) { callbacks.Add(key, callback); diff --git a/Assets/PlayroomKit/modules/RPC.cs b/Assets/PlayroomKit/modules/RPC.cs index c32e7f8..0d9b6b3 100644 --- a/Assets/PlayroomKit/modules/RPC.cs +++ b/Assets/PlayroomKit/modules/RPC.cs @@ -110,7 +110,7 @@ public static void RpcCall(string name, object data, RpcMode mode, Action callba } else { - MockRpcCall(name, data, RpcMode.ALL, callbackOnResponse); + MockRpcCall(name, data, mode, callbackOnResponse); } }