From 8b2829558700272e821cb4b015ec9a5d4bfa01ac Mon Sep 17 00:00:00 2001 From: momintlh <77355191+momintlh@users.noreply.github.com> Date: Thu, 9 May 2024 23:42:13 +0500 Subject: [PATCH 1/7] fix: RPC events sync. - RPC events name sync between players. - Made Players dictionary private #66 --- Assets/PlayroomKit/PlayroomKit.cs | 24 +++++++++ Assets/Scenes/Game.unity | 5 +- Assets/Scripts/GameManager.cs | 90 ++++++++++++++++++++----------- 3 files changed, 85 insertions(+), 34 deletions(-) diff --git a/Assets/PlayroomKit/PlayroomKit.cs b/Assets/PlayroomKit/PlayroomKit.cs index a132e11..f815730 100644 --- a/Assets/PlayroomKit/PlayroomKit.cs +++ b/Assets/PlayroomKit/PlayroomKit.cs @@ -1078,6 +1078,27 @@ private static void InvokeRpcRegisterCallBack(string dataJson, string senderJson Debug.LogError(ex.Message); } + + string name = GetState("rpcCalledEventName"); + Debug.LogWarning("eventName: " + name); + + if (!rpcCalledEvents.Contains(name)) + { + rpcCalledEvents.Add(name); + } + + Debug.Log("rpcRegisteredEvents"); + for (int i = 0; i < rpcRegisteredEvents.Count; i++) + { + Debug.Log(rpcRegisteredEvents[i]); + } + + Debug.Log("rpcCalledEvents"); + for (int i = 0; i < rpcCalledEvents.Count; i++) + { + Debug.Log(rpcCalledEvents[i]); + } + for (int i = 0; i < Math.Min(rpcRegisteredEvents.Count, rpcCalledEvents.Count); i++) { if (rpcRegisteredEvents[i] == rpcCalledEvents[i]) @@ -1096,6 +1117,9 @@ private static void InvokeRpcRegisterCallBack(string dataJson, string senderJson public static void RpcCall(string name, object data, RpcMode mode, Action callbackOnResponse) { + SetState("rpcCalledEventName", name, reliable: true); + + string jsonData = ConvertToJson(data); if (OnResponseCallbacks.ContainsKey(name)) diff --git a/Assets/Scenes/Game.unity b/Assets/Scenes/Game.unity index 10d891f..a2f72f2 100644 --- a/Assets/Scenes/Game.unity +++ b/Assets/Scenes/Game.unity @@ -433,9 +433,8 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: score: 0 - scoreText1: {fileID: 396053685} - scoreText2: {fileID: 197975553} - scoreText: {fileID: 0} + scoreTextPlayer1: {fileID: 396053685} + scoreTextPlayer2: {fileID: 197975553} --- !u!4 &330732770 Transform: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/GameManager.cs b/Assets/Scripts/GameManager.cs index 38ec5f9..33db3c0 100644 --- a/Assets/Scripts/GameManager.cs +++ b/Assets/Scripts/GameManager.cs @@ -12,23 +12,31 @@ public class GameManager : MonoBehaviour { + + [Header("Player holders")] private static readonly List players = new(); private static readonly List playerGameObjects = new(); private static Dictionary PlayerDict = new(); - Dictionary moveData = new(); + + [Header("Score and Score UI")] [SerializeField] private int score; - [SerializeField] private Text scoreText1; - [SerializeField] private Text scoreText2; + [SerializeField] private Text scoreTextPlayer1; + [SerializeField] private Text scoreTextPlayer2; - [SerializeField] private static bool playerJoined; + private Text selectedScoreText; - [SerializeField] private Text scoreText; + private static bool playerJoined; - bool isMoving; - private void Awake() + void Awake() + { + // StartCoroutine(StartMatchmakingCoroutine()); + Initialize(); + } + + private void Initialize() { PlayroomKit.InsertCoin(new PlayroomKit.InitOptions() { @@ -44,11 +52,24 @@ private void Awake() }); } + private IEnumerator StartMatchmakingCoroutine() + { + Initialize(); + + yield return new WaitUntil(() => PlayroomKit.GetPlayers().Count > 0); + PlayroomKit.StartMatchmaking(); + } void Start() { PlayroomKit.RpcRegister("ShootBullet", HandleScoreUpdate, "You shot a bullet!"); + PlayroomKit.RpcRegister("Hello", Hello); + } + + private void Hello(string arg1, string arg2) + { + print("helo"); } void HandleScoreUpdate(string data, string caller) @@ -89,25 +110,15 @@ private void Update() players[index].SetState("pos", playerGameObjects[index].GetComponent().position); - if (Input.GetKeyDown(KeyCode.Space)) - { - Vector3 playerPosition = playerGameObjects[index].transform.position; - playerGameObjects[index].GetComponent().ShootBullet( - playerPosition, - 50f); - score += 50; - PlayroomKit.RpcCall("ShootBullet", score, () => - { - Debug.Log("shooting bullet!"); - }); - } + ShootBullet(index); + // SayHello(); if (Input.GetKeyDown(KeyCode.R) && PlayroomKit.IsHost()) { PlayroomKit.ResetStates(null, () => { var defscore = PlayroomKit.GetState("score"); - scoreText.text = "Score: " + defscore.ToString(); + selectedScoreText.text = "Score: " + defscore.ToString(); }); } @@ -129,11 +140,34 @@ private void Update() } + private void ShootBullet(int pleyerIndex) + { + if (Input.GetKeyDown(KeyCode.Space)) + { + Vector3 playerPosition = playerGameObjects[pleyerIndex].transform.position; + playerGameObjects[pleyerIndex].GetComponent().ShootBullet( + playerPosition, + 50f); + score += 50; + PlayroomKit.RpcCall("ShootBullet", score, () => + { + Debug.Log("Shooting bullet"); + }); + } + } - public void AddPlayer(PlayroomKit.Player player) + private void SayHello() { - Debug.Log("AddPlayer Invoked!"); + if (Input.GetKeyDown(KeyCode.H)) + { + PlayroomKit.RpcCall("Hello", -1, () => { + Debug.Log("saying helo"); + }); + } + } + public void AddPlayer(PlayroomKit.Player player) + { GameObject playerObj = (GameObject)Instantiate(Resources.Load("Player"), new Vector3(Random.Range(-4, 4), Random.Range(1, 5), 0), Quaternion.identity); @@ -143,17 +177,11 @@ public void AddPlayer(PlayroomKit.Player player) players.Add(player); playerGameObjects.Add(playerObj); - scoreText = (players.Count == 1) ? scoreText1 : scoreText2; - playerObj.GetComponent().scoreText = scoreText; + selectedScoreText = (players.Count == 1) ? scoreTextPlayer1 : scoreTextPlayer2; + playerObj.GetComponent().scoreText = selectedScoreText; playerJoined = true; player.OnQuit(RemovePlayer); - - } - - public void SomeThingElseInvoked(PlayroomKit.Player player) - { - Debug.Log("SomeThingElseInvoked Invoked!!!"); } [MonoPInvokeCallback(typeof(Action))] @@ -167,7 +195,7 @@ private static void RemovePlayer(string playerID) } else { - Debug.LogWarning("player not in dict"); + Debug.LogWarning("Player is not in dictionary"); } } From 18f3cb04281fec01e70d5ab839275a6eec22e0f7 Mon Sep 17 00:00:00 2001 From: momintlh <77355191+momintlh@users.noreply.github.com> Date: Thu, 9 May 2024 23:43:36 +0500 Subject: [PATCH 2/7] chore: cleanup --- Assets/PlayroomKit/PlayroomKit.cs | 11 ----------- Assets/Scripts/GameManager.cs | 1 - 2 files changed, 12 deletions(-) diff --git a/Assets/PlayroomKit/PlayroomKit.cs b/Assets/PlayroomKit/PlayroomKit.cs index f815730..7b6010d 100644 --- a/Assets/PlayroomKit/PlayroomKit.cs +++ b/Assets/PlayroomKit/PlayroomKit.cs @@ -1087,17 +1087,6 @@ private static void InvokeRpcRegisterCallBack(string dataJson, string senderJson rpcCalledEvents.Add(name); } - Debug.Log("rpcRegisteredEvents"); - for (int i = 0; i < rpcRegisteredEvents.Count; i++) - { - Debug.Log(rpcRegisteredEvents[i]); - } - - Debug.Log("rpcCalledEvents"); - for (int i = 0; i < rpcCalledEvents.Count; i++) - { - Debug.Log(rpcCalledEvents[i]); - } for (int i = 0; i < Math.Min(rpcRegisteredEvents.Count, rpcCalledEvents.Count); i++) { diff --git a/Assets/Scripts/GameManager.cs b/Assets/Scripts/GameManager.cs index 33db3c0..e3bdd07 100644 --- a/Assets/Scripts/GameManager.cs +++ b/Assets/Scripts/GameManager.cs @@ -111,7 +111,6 @@ private void Update() players[index].SetState("pos", playerGameObjects[index].GetComponent().position); ShootBullet(index); - // SayHello(); if (Input.GetKeyDown(KeyCode.R) && PlayroomKit.IsHost()) { From 40d49dc410f81951f372a8a6cb4598c750c06c3e Mon Sep 17 00:00:00 2001 From: momintlh <77355191+momintlh@users.noreply.github.com> Date: Thu, 9 May 2024 23:45:03 +0500 Subject: [PATCH 3/7] chore: removed comment --- Assets/PlayroomKit/PlayroomKit.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/Assets/PlayroomKit/PlayroomKit.cs b/Assets/PlayroomKit/PlayroomKit.cs index 7b6010d..5227d08 100644 --- a/Assets/PlayroomKit/PlayroomKit.cs +++ b/Assets/PlayroomKit/PlayroomKit.cs @@ -1080,7 +1080,6 @@ private static void InvokeRpcRegisterCallBack(string dataJson, string senderJson string name = GetState("rpcCalledEventName"); - Debug.LogWarning("eventName: " + name); if (!rpcCalledEvents.Contains(name)) { From 10949605852b36e59d7d03f0cf682d7807047465 Mon Sep 17 00:00:00 2001 From: momintlh <77355191+momintlh@users.noreply.github.com> Date: Thu, 9 May 2024 23:48:42 +0500 Subject: [PATCH 4/7] fix: actually made Players dict private --- Assets/PlayroomKit/PlayroomKit.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Assets/PlayroomKit/PlayroomKit.cs b/Assets/PlayroomKit/PlayroomKit.cs index 5227d08..ba9c900 100644 --- a/Assets/PlayroomKit/PlayroomKit.cs +++ b/Assets/PlayroomKit/PlayroomKit.cs @@ -28,7 +28,7 @@ public class PlayroomKit */ private static Dictionary MockDictionary = new(); - public static readonly Dictionary Players = new(); + private static readonly Dictionary Players = new(); @@ -1137,6 +1137,8 @@ private static void InvokeOnResponseCallback() { var namesToRemove = new List(); + var n = GetState("rpcCalledEventName"); + foreach (var name in rpcCalledEvents) { try From 3925d37e6c1e13c2c1eb825791f106c33e91b799 Mon Sep 17 00:00:00 2001 From: momintlh <77355191+momintlh@users.noreply.github.com> Date: Fri, 10 May 2024 00:01:46 +0500 Subject: [PATCH 5/7] chore: added description for future proofing --- Assets/PlayroomKit/PlayroomKit.cs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Assets/PlayroomKit/PlayroomKit.cs b/Assets/PlayroomKit/PlayroomKit.cs index ba9c900..09808c4 100644 --- a/Assets/PlayroomKit/PlayroomKit.cs +++ b/Assets/PlayroomKit/PlayroomKit.cs @@ -1079,6 +1079,7 @@ private static void InvokeRpcRegisterCallBack(string dataJson, string senderJson } + // Getting the name of called Rpc event string name = GetState("rpcCalledEventName"); if (!rpcCalledEvents.Contains(name)) @@ -1104,7 +1105,7 @@ private static void InvokeRpcRegisterCallBack(string dataJson, string senderJson public static void RpcCall(string name, object data, RpcMode mode, Action callbackOnResponse) { - + // setting to sync the event name with other players SetState("rpcCalledEventName", name, reliable: true); @@ -1137,8 +1138,6 @@ private static void InvokeOnResponseCallback() { var namesToRemove = new List(); - var n = GetState("rpcCalledEventName"); - foreach (var name in rpcCalledEvents) { try From 76811f8dc532f0e21449dae40dd930ffafcf33e5 Mon Sep 17 00:00:00 2001 From: momintlh <77355191+momintlh@users.noreply.github.com> Date: Sat, 11 May 2024 00:43:35 +0500 Subject: [PATCH 6/7] Fix: fixed callback invoking in rpcRegister --- Assets/PlayroomKit/PlayroomKit.cs | 45 ++++++++++++++++++++----------- Assets/Scripts/GameManager.cs | 9 ++++--- 2 files changed, 36 insertions(+), 18 deletions(-) diff --git a/Assets/PlayroomKit/PlayroomKit.cs b/Assets/PlayroomKit/PlayroomKit.cs index 09808c4..9b44edb 100644 --- a/Assets/PlayroomKit/PlayroomKit.cs +++ b/Assets/PlayroomKit/PlayroomKit.cs @@ -1044,8 +1044,7 @@ public enum RpcMode HOST } - private static List> rpcRegisterCallbacks = new(); - private static List rpcRegisteredEvents = new(); + private static Dictionary> rpcRegisterCallbacks = new(); private static List rpcCalledEvents = new(); [DllImport("__Internal")] @@ -1053,8 +1052,7 @@ public enum RpcMode public static void RpcRegister(string name, Action rpcRegisterCallback, string onResponseReturn = null) { - rpcRegisterCallbacks.Add(rpcRegisterCallback); - rpcRegisteredEvents.Add(name); + rpcRegisterCallbacks.Add(name, rpcRegisterCallback); RpcRegisterInternal(name, InvokeRpcRegisterCallBack, onResponseReturn); } @@ -1079,20 +1077,28 @@ private static void InvokeRpcRegisterCallBack(string dataJson, string senderJson } - // Getting the name of called Rpc event - string name = GetState("rpcCalledEventName"); + List updatedRpcCalledEvents = new(); + // This state is required to update the called rpc events list + string nameJson = GetState("rpcCalledEventName"); - if (!rpcCalledEvents.Contains(name)) + JSONArray jsonArray = JSON.Parse(nameJson).AsArray; + foreach (JSONNode node in jsonArray) { - rpcCalledEvents.Add(name); + string item = node.Value; + updatedRpcCalledEvents.Add(item); } + Debug.Log("\n\nupdatedRpcCalledEvents: "); + for (int i = 0; i < updatedRpcCalledEvents.Count; i++) + { + Debug.Log(updatedRpcCalledEvents[i]); + } - for (int i = 0; i < Math.Min(rpcRegisteredEvents.Count, rpcCalledEvents.Count); i++) + foreach (string name in updatedRpcCalledEvents) { - if (rpcRegisteredEvents[i] == rpcCalledEvents[i]) + if (rpcRegisterCallbacks.TryGetValue(name, out Action callback)) { - rpcRegisterCallbacks[i].Invoke(dataJson, senderJson); + callback?.Invoke(dataJson, senderJson); } } @@ -1105,9 +1111,6 @@ private static void InvokeRpcRegisterCallBack(string dataJson, string senderJson public static void RpcCall(string name, object data, RpcMode mode, Action callbackOnResponse) { - // setting to sync the event name with other players - SetState("rpcCalledEventName", name, reliable: true); - string jsonData = ConvertToJson(data); @@ -1124,6 +1127,18 @@ public static void RpcCall(string name, object data, RpcMode mode, Action callba } } + + JSONArray jsonArray = new JSONArray(); + foreach (string item in rpcCalledEvents) + { + jsonArray.Add(item); + } + string jsonString = jsonArray.ToString(); + /* + This is requrired to sync the rpc events between all players, without this players won't know which event has been called. + this is a temporary fix, RPC's need to be handled within JS for better control.*/ + SetState("rpcCalledEventName", jsonString, reliable: true); + RpcCallInternal(name, jsonData, mode, InvokeOnResponseCallback); } @@ -1138,7 +1153,7 @@ private static void InvokeOnResponseCallback() { var namesToRemove = new List(); - foreach (var name in rpcCalledEvents) + foreach (string name in rpcCalledEvents) { try { diff --git a/Assets/Scripts/GameManager.cs b/Assets/Scripts/GameManager.cs index e3bdd07..b9f38a4 100644 --- a/Assets/Scripts/GameManager.cs +++ b/Assets/Scripts/GameManager.cs @@ -67,9 +67,10 @@ void Start() PlayroomKit.RpcRegister("Hello", Hello); } - private void Hello(string arg1, string arg2) + private void Hello(string data, string caller) { - print("helo"); + var player = PlayroomKit.GetPlayer(caller); + Debug.Log($"Caller: {caller}, Player Name: {player?.GetProfile().name}, Data: {data}"); } void HandleScoreUpdate(string data, string caller) @@ -111,6 +112,7 @@ private void Update() players[index].SetState("pos", playerGameObjects[index].GetComponent().position); ShootBullet(index); + SayHello(); if (Input.GetKeyDown(KeyCode.R) && PlayroomKit.IsHost()) { @@ -159,7 +161,8 @@ private void SayHello() { if (Input.GetKeyDown(KeyCode.H)) { - PlayroomKit.RpcCall("Hello", -1, () => { + PlayroomKit.RpcCall("Hello", -1, () => + { Debug.Log("saying helo"); }); } From 6fa2d2c7ee99b0edad7784fc6bbb4ced81ce8c5d Mon Sep 17 00:00:00 2001 From: momintlh <77355191+momintlh@users.noreply.github.com> Date: Sat, 11 May 2024 00:44:46 +0500 Subject: [PATCH 7/7] chore: removed debug logs --- Assets/PlayroomKit/PlayroomKit.cs | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/Assets/PlayroomKit/PlayroomKit.cs b/Assets/PlayroomKit/PlayroomKit.cs index 9b44edb..020d4b8 100644 --- a/Assets/PlayroomKit/PlayroomKit.cs +++ b/Assets/PlayroomKit/PlayroomKit.cs @@ -1078,7 +1078,7 @@ private static void InvokeRpcRegisterCallBack(string dataJson, string senderJson List updatedRpcCalledEvents = new(); - // This state is required to update the called rpc events list + // This state is required to update the called rpc events list, (Temp fix see RpcCall for more) string nameJson = GetState("rpcCalledEventName"); JSONArray jsonArray = JSON.Parse(nameJson).AsArray; @@ -1088,12 +1088,6 @@ private static void InvokeRpcRegisterCallBack(string dataJson, string senderJson updatedRpcCalledEvents.Add(item); } - Debug.Log("\n\nupdatedRpcCalledEvents: "); - for (int i = 0; i < updatedRpcCalledEvents.Count; i++) - { - Debug.Log(updatedRpcCalledEvents[i]); - } - foreach (string name in updatedRpcCalledEvents) { if (rpcRegisterCallbacks.TryGetValue(name, out Action callback)) @@ -1136,7 +1130,8 @@ public static void RpcCall(string name, object data, RpcMode mode, Action callba string jsonString = jsonArray.ToString(); /* This is requrired to sync the rpc events between all players, without this players won't know which event has been called. - this is a temporary fix, RPC's need to be handled within JS for better control.*/ + this is a temporary fix, RPC's need to be handled within JS for better control. + */ SetState("rpcCalledEventName", jsonString, reliable: true); RpcCallInternal(name, jsonData, mode, InvokeOnResponseCallback);