From 088fcf4e5d83a0cfa02c4006539d4b8c3972249f Mon Sep 17 00:00:00 2001 From: momintlh <77355191+momintlh@users.noreply.github.com> Date: Fri, 3 Jan 2025 13:52:26 +0500 Subject: [PATCH] feat: bindings for transferHost --- Assets/PlayroomKit/PlayroomKit.cs | 10 +++ Assets/PlayroomKit/modules/Headers.cs | 3 + .../InternalFunctions/IInternalFunctions.cs | 3 + .../InternalFunctions/InterlopWrapper.cs | 5 ++ .../modules/Interfaces/IPlayroomBase.cs | 2 + .../BrowserMode/BrowserMockService.cs | 5 ++ .../BrowserMode/PlayroomMockBrowserBridge.js | 8 +++ .../modules/MockMode/LocalPlayroomService.cs | 5 ++ Assets/PlayroomKit/modules/PlayroomService.cs | 5 ++ Assets/PlayroomKit/modules/RPC/RPC.cs | 70 ++----------------- Assets/PlayroomKit/package.json | 2 +- Assets/PlayroomKit/src/index.js | 35 +++++++--- Assets/Scenes/TestScene.unity | 8 +-- Assets/Scripts/GameManager.cs | 40 +++++------ 14 files changed, 101 insertions(+), 100 deletions(-) diff --git a/Assets/PlayroomKit/PlayroomKit.cs b/Assets/PlayroomKit/PlayroomKit.cs index 83324af..93b5045 100644 --- a/Assets/PlayroomKit/PlayroomKit.cs +++ b/Assets/PlayroomKit/PlayroomKit.cs @@ -73,6 +73,16 @@ public bool IsHost() return _playroomService.IsHost(); } + + public void TransferHost(string playerId) + { + if (!IsPlayRoomInitialized) + { + Debug.LogError("[Mock Mode] Playroom not initialized yet! Please call InsertCoin."); + } + + _playroomService.TransferHost(playerId); + } public void OnPlayerJoin(Action onPlayerJoinCallback) { diff --git a/Assets/PlayroomKit/modules/Headers.cs b/Assets/PlayroomKit/modules/Headers.cs index cc6e355..d04df2d 100644 --- a/Assets/PlayroomKit/modules/Headers.cs +++ b/Assets/PlayroomKit/modules/Headers.cs @@ -23,6 +23,9 @@ private static extern void InsertCoinInternal(string options, [DllImport("__Internal")] private static extern bool IsHostInternal(); + + [DllImport("__Internal")] + private static extern bool TransferHostInternal(string playerId); [DllImport("__Internal")] private static extern bool IsStreamScreenInternal(); diff --git a/Assets/PlayroomKit/modules/Helpers/InternalFunctions/IInternalFunctions.cs b/Assets/PlayroomKit/modules/Helpers/InternalFunctions/IInternalFunctions.cs index 50fe7dd..9efd8fe 100644 --- a/Assets/PlayroomKit/modules/Helpers/InternalFunctions/IInternalFunctions.cs +++ b/Assets/PlayroomKit/modules/Helpers/InternalFunctions/IInternalFunctions.cs @@ -19,6 +19,8 @@ void InsertCoinWrapper(string options, void UnsubscribeOnPlayerJoinWrapper(string id); bool IsHostWrapper(); + + void TransferHostWrapper(string playerId); bool IsStreamScreenWrapper(); @@ -92,6 +94,7 @@ void RpcCallWrapper(string name, string data, RpcMode mode, string GetProfileWrapper(string playerID); // + } } } \ No newline at end of file diff --git a/Assets/PlayroomKit/modules/Helpers/InternalFunctions/InterlopWrapper.cs b/Assets/PlayroomKit/modules/Helpers/InternalFunctions/InterlopWrapper.cs index 120bf6c..3725170 100644 --- a/Assets/PlayroomKit/modules/Helpers/InternalFunctions/InterlopWrapper.cs +++ b/Assets/PlayroomKit/modules/Helpers/InternalFunctions/InterlopWrapper.cs @@ -37,6 +37,11 @@ public bool IsHostWrapper() return IsHostInternal(); } + public void TransferHostWrapper(string playerId) + { + TransferHostInternal(playerId); + } + public bool IsStreamScreenWrapper() { return IsStreamScreenInternal(); diff --git a/Assets/PlayroomKit/modules/Interfaces/IPlayroomBase.cs b/Assets/PlayroomKit/modules/Interfaces/IPlayroomBase.cs index a3a7dfd..53b623f 100644 --- a/Assets/PlayroomKit/modules/Interfaces/IPlayroomBase.cs +++ b/Assets/PlayroomKit/modules/Interfaces/IPlayroomBase.cs @@ -22,6 +22,8 @@ public void InsertCoin(InitOptions options = null, Action onLaunchCallBack = nul public Player Me(); public bool IsHost(); + + public void TransferHost(string playerId); public string GetRoomCode(); diff --git a/Assets/PlayroomKit/modules/MockMode/BrowserMode/BrowserMockService.cs b/Assets/PlayroomKit/modules/MockMode/BrowserMode/BrowserMockService.cs index 2e5746d..2b4d00c 100644 --- a/Assets/PlayroomKit/modules/MockMode/BrowserMode/BrowserMockService.cs +++ b/Assets/PlayroomKit/modules/MockMode/BrowserMode/BrowserMockService.cs @@ -97,6 +97,11 @@ public bool IsHost() return _ubb.CallJs("IsHost"); } + public void TransferHost(string playerId) + { + _ubb.CallJs("TransferHost", null, null, true, playerId); + } + public string GetRoomCode() { return _ubb.CallJs("GetRoomCode"); diff --git a/Assets/PlayroomKit/modules/MockMode/BrowserMode/PlayroomMockBrowserBridge.js b/Assets/PlayroomKit/modules/MockMode/BrowserMode/PlayroomMockBrowserBridge.js index 928ee45..52dd653 100644 --- a/Assets/PlayroomKit/modules/MockMode/BrowserMode/PlayroomMockBrowserBridge.js +++ b/Assets/PlayroomKit/modules/MockMode/BrowserMode/PlayroomMockBrowserBridge.js @@ -94,6 +94,14 @@ IsHost = function () { return Playroom.isHost(); }; +TransferHost = async function (playerId) { + try { + await transferHost(playerId); + } catch (error) { + console.error("Error transferring host:", error); + } +}; + IsStreamScreen = function () { return Playroom.isStreamScreen(); }; diff --git a/Assets/PlayroomKit/modules/MockMode/LocalPlayroomService.cs b/Assets/PlayroomKit/modules/MockMode/LocalPlayroomService.cs index 1404152..582207f 100644 --- a/Assets/PlayroomKit/modules/MockMode/LocalPlayroomService.cs +++ b/Assets/PlayroomKit/modules/MockMode/LocalPlayroomService.cs @@ -52,6 +52,11 @@ public bool IsHost() return true; } + public void TransferHost(string playerId) + { + Debug.Log("Host transfer doesn't work in local mock mode"); + } + public string GetRoomCode() { return "mock123"; diff --git a/Assets/PlayroomKit/modules/PlayroomService.cs b/Assets/PlayroomKit/modules/PlayroomService.cs index ea4416c..1f5af97 100644 --- a/Assets/PlayroomKit/modules/PlayroomService.cs +++ b/Assets/PlayroomKit/modules/PlayroomService.cs @@ -84,6 +84,11 @@ public bool IsHost() return _interop.IsHostWrapper(); } + public void TransferHost(string playerId) + { + _interop.TransferHostWrapper(playerId); + } + public string GetRoomCode() { diff --git a/Assets/PlayroomKit/modules/RPC/RPC.cs b/Assets/PlayroomKit/modules/RPC/RPC.cs index cf7c852..ff2bde3 100644 --- a/Assets/PlayroomKit/modules/RPC/RPC.cs +++ b/Assets/PlayroomKit/modules/RPC/RPC.cs @@ -1,6 +1,7 @@ using System; using System.Collections; using System.Collections.Generic; +using System.Reflection; using AOT; using SimpleJSON; using UnityEngine; @@ -36,19 +37,9 @@ public RPC(PlayroomKit playroomKit, IInterop interop) public void RpcRegister(string name, Action rpcRegisterCallback, string onResponseReturn = null) { - CallbackManager.RegisterCallback(rpcRegisterCallback, name); - _interop.RpcRegisterWrapper(name, InvokeRpcRegisterCallBack, onResponseReturn); - } - - public static void RpcRegister2(string name, Action rpcRegisterCallback, - string onResponseReturn = null) - { - // CallbackManager.RegisterRpcCallback(rpcRegisterCallback, name); - + Debug.Log(rpcRegisterCallback.GetMethodInfo().Name); CallbackManager.RegisterCallback(rpcRegisterCallback, name); PlayroomKitInterop.RpcRegisterInternal(name, RpcRegisterCallBackHandler, onResponseReturn); - - // _interop.RpcRegisterWrapper(name, InvokeRpcRegisterCallBack, onResponseReturn); } public void RpcCall(string name, object data, RpcMode mode, Action callbackOnResponse = null) @@ -68,20 +59,6 @@ public void RpcCall(string name, object data, RpcMode mode, Action callbackOnRes } } - JSONArray jsonArray = new JSONArray(); - foreach (string item in RpcCalledEvents) - { - jsonArray.Add(item); - } - - string jsonString = jsonArray.ToString(); - -/* - This is required to sync the rpc events between all players, without this players won't know which event has been called. - Update: This fix works fine for now, but there might be a better way. - this is a temporary fix, RPCs need to be handled within JSLIB for better control. -*/ - _playroomKit.SetState("rpcCalledEventName", jsonString, reliable: true); _interop.RpcCallWrapper(name, jsonData, mode, InvokeOnResponseCallback); } @@ -95,7 +72,7 @@ public void RpcCall(string name, object data, Action callbackOnResponse = null) [MonoPInvokeCallback(typeof(Action))] protected static void InvokeOnResponseCallback() { - var namesToRemove = new List(); + List namesToRemove = new List(); foreach (string name in RpcCalledEvents) { @@ -129,54 +106,17 @@ protected static void RpcRegisterCallBackHandler(string combinedData) { try { - Debug.Log($"Data: {combinedData}"); - JSONNode jsonNode = JSON.Parse(combinedData); string eventName = jsonNode["eventName"]; string stringData = jsonNode["data"]; string senderID = jsonNode["senderId"]; - - Debug.LogWarning($"RPC Register Callback: {eventName} - {stringData} - {senderID}"); - CallbackManager.InvokeCallback(eventName, stringData); - } - catch (Exception ex) - { - Debug.LogError("Error: " + ex.Message); - } - } - [MonoPInvokeCallback(typeof(Action))] - protected static void InvokeRpcRegisterCallBack(string dataJson, string senderJson) - { - try - { - if (!Players.ContainsKey(senderJson)) - { - var player = new Player(senderJson, new Player.PlayerService(senderJson)); - Players.Add(senderJson, player); - } + CallbackManager.InvokeCallback(eventName, stringData, senderID); } catch (Exception ex) { - Debug.LogError(ex.Message); - } - - List updatedRpcCalledEvents = new(); - // This state is required to update the called rpc events list, (Temp fix see RpcCall for more) - string nameJson = _playroomKit.GetState("rpcCalledEventName"); - - JSONArray jsonArray = JSON.Parse(nameJson).AsArray; - foreach (JSONNode node in jsonArray) - { - string item = node.Value; - updatedRpcCalledEvents.Add(item); - } - - for (var i = 0; i < updatedRpcCalledEvents.Count; i++) - { - string name = updatedRpcCalledEvents[i]; - CallbackManager.InvokeRpcRegisterCallBack(name, dataJson, senderJson); + Debug.LogError("Error: " + ex.Message); } } diff --git a/Assets/PlayroomKit/package.json b/Assets/PlayroomKit/package.json index e50b613..6223d1f 100644 --- a/Assets/PlayroomKit/package.json +++ b/Assets/PlayroomKit/package.json @@ -16,7 +16,7 @@ "author": "", "license": "ISC", "dependencies": { - "playroomkit": "0.0.83", + "playroomkit": "0.0.85", "react": "^18.2.0", "react-dom": "^18.2.0" } diff --git a/Assets/PlayroomKit/src/index.js b/Assets/PlayroomKit/src/index.js index 53bb4db..069b7d9 100644 --- a/Assets/PlayroomKit/src/index.js +++ b/Assets/PlayroomKit/src/index.js @@ -72,6 +72,30 @@ mergeInto(LibraryManager.library, { return Playroom.isHost(); }, + + /** + * @description Transfers the host to another player if they are in the room + * @param {string} playerId + */ + TransferHostInternal: function (playerId) { + if (!window.Playroom) { + console.error("Playroom library is not loaded. Please make sure to call InsertCoin first."); + return; + } + + try { + Playroom.transferHost(playerId) + .then(() => { + console.log("Host privileges successfully transferred."); + }) + .catch((error) => { + console.error("Failed to transfer host privileges: ", error); + }); + } catch (error) { + console.error("Error transferring host: ", error); + } + }, + /** * @description Checks whether the local game is running in stream mode. * @returns {boolean} True if the local game is running in stream mode, otherwise false. @@ -843,24 +867,18 @@ mergeInto(LibraryManager.library, { return; } - var n = UTF8ToString(name) - console.log(n) - onResponseReturn = UTF8ToString(onResponseReturn); - this.rpcEvents.push(name) function registerCallback(data, sender) { var combinedData = { data: data, senderId: sender.id, - // eventName : n + eventName : n }; var dataJson = JSON.stringify(combinedData); - console.log(`[JS] dataJson: ${dataJson}`); - {{{ makeDynCall('vi', 'callback') }}}(stringToNewUTF8(dataJson)); return onResponseReturn; @@ -883,9 +901,6 @@ mergeInto(LibraryManager.library, { try { data = JSON.parse(UTF8ToString(dataJson)); } catch (parseError) { - console.warn( - "Failed to parse dataJson as JSON. Treating it as a regular string." - ); data = UTF8ToString(dataJson); } } else { diff --git a/Assets/Scenes/TestScene.unity b/Assets/Scenes/TestScene.unity index 4d0df78..2c8a406 100644 --- a/Assets/Scenes/TestScene.unity +++ b/Assets/Scenes/TestScene.unity @@ -152,10 +152,6 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: dc3a2b9cdc24aab40906ce4bcdee9943, type: 3} m_Name: m_EditorClassIdentifier: - playerPrefab: {fileID: 5691958192272525149, guid: 1983f481b86c250409a264643733c292, type: 3} - playerIDText: {fileID: 3918495209311863502} - score: {fileID: 2030615215} - playerID: --- !u!4 &1054460207 Transform: m_ObjectHideFlags: 0 @@ -287,6 +283,10 @@ PrefabInstance: propertyPath: webDriverDirectory value: Assets\ objectReference: {fileID: 0} + - target: {fileID: 8987662522597341863, guid: 1b30fa3a265114a45a7e23165a20aecc, type: 3} + propertyPath: mockMode + value: 0 + objectReference: {fileID: 0} - target: {fileID: 8987662522597341863, guid: 1b30fa3a265114a45a7e23165a20aecc, type: 3} propertyPath: insertCoinCaller value: diff --git a/Assets/Scripts/GameManager.cs b/Assets/Scripts/GameManager.cs index cad5e44..c77d201 100644 --- a/Assets/Scripts/GameManager.cs +++ b/Assets/Scripts/GameManager.cs @@ -1,48 +1,48 @@ -using System; -using System.Collections.Generic; -using AOT; using Playroom; -using TMPro; using UnityEngine; -using Random = UnityEngine.Random; public class GameManager : MonoBehaviour { - private PlayroomKit kit; + private PlayroomKit _kit; - void Start() + private void Start() { - kit = new(); - kit.InsertCoin(new InitOptions() + _kit = new(); + + _kit.InsertCoin(new InitOptions() { gameId = "[my game id]", maxPlayersPerRoom = 8, discord = true }, () => { - PlayroomKit.RPC.RpcRegister2("A", A); - PlayroomKit.RPC.RpcRegister2("B", B); + _kit.RpcRegister("A", A); + _kit.RpcRegister("B", B); }); } private void Update() { if (Input.GetMouseButtonDown(0)) - kit.RpcCall("A", 1, PlayroomKit.RpcMode.ALL); - - if (Input.GetMouseButtonDown(1)) - kit.RpcCall("B", 2, PlayroomKit.RpcMode.ALL); - + _kit.RpcCall("A", 1, PlayroomKit.RpcMode.HOST); + if (Input.GetMouseButtonDown(1)) + Debug.LogWarning(_kit.IsHost()); } - private void A(string data) + private void A(string data, string sender) { - Debug.Log($"[Unity] A data: {data}"); + Debug.Log($"[Unity] A called only on HOST {data} and {sender}"); + + _kit.TransferHost(sender); + + _kit.RpcCall("B", 2, PlayroomKit.RpcMode.OTHERS); } - private void B(string data) + private void B(string data, string sender) { - Debug.Log($"[Unity] B data: {data}"); + Debug.Log($"[Unity] B called on ALL data: {data} and {sender}"); + + } } \ No newline at end of file