Skip to content

Commit

Permalink
feat: bindings for transferHost
Browse files Browse the repository at this point in the history
  • Loading branch information
momintlh committed Jan 3, 2025
1 parent 01a02d0 commit 088fcf4
Show file tree
Hide file tree
Showing 14 changed files with 101 additions and 100 deletions.
10 changes: 10 additions & 0 deletions Assets/PlayroomKit/PlayroomKit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Player> onPlayerJoinCallback)
{
Expand Down
3 changes: 3 additions & 0 deletions Assets/PlayroomKit/modules/Headers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ void InsertCoinWrapper(string options,
void UnsubscribeOnPlayerJoinWrapper(string id);

bool IsHostWrapper();

void TransferHostWrapper(string playerId);

bool IsStreamScreenWrapper();

Expand Down Expand Up @@ -92,6 +94,7 @@ void RpcCallWrapper(string name, string data, RpcMode mode,

string GetProfileWrapper(string playerID);
//

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ public bool IsHostWrapper()
return IsHostInternal();
}

public void TransferHostWrapper(string playerId)
{
TransferHostInternal(playerId);
}

public bool IsStreamScreenWrapper()
{
return IsStreamScreenInternal();
Expand Down
2 changes: 2 additions & 0 deletions Assets/PlayroomKit/modules/Interfaces/IPlayroomBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ public bool IsHost()
return _ubb.CallJs<bool>("IsHost");
}

public void TransferHost(string playerId)
{
_ubb.CallJs("TransferHost", null, null, true, playerId);
}

public string GetRoomCode()
{
return _ubb.CallJs<string>("GetRoomCode");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
};
Expand Down
5 changes: 5 additions & 0 deletions Assets/PlayroomKit/modules/MockMode/LocalPlayroomService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
5 changes: 5 additions & 0 deletions Assets/PlayroomKit/modules/PlayroomService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ public bool IsHost()
return _interop.IsHostWrapper();
}

public void TransferHost(string playerId)
{
_interop.TransferHostWrapper(playerId);
}


public string GetRoomCode()
{
Expand Down
70 changes: 5 additions & 65 deletions Assets/PlayroomKit/modules/RPC/RPC.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using AOT;
using SimpleJSON;
using UnityEngine;
Expand Down Expand Up @@ -36,19 +37,9 @@ public RPC(PlayroomKit playroomKit, IInterop interop)
public void RpcRegister(string name, Action<string, string> rpcRegisterCallback,
string onResponseReturn = null)
{
CallbackManager.RegisterCallback(rpcRegisterCallback, name);
_interop.RpcRegisterWrapper(name, InvokeRpcRegisterCallBack, onResponseReturn);
}

public static void RpcRegister2(string name, Action<string> 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)
Expand All @@ -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);
}

Expand All @@ -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<string>();
List<string> namesToRemove = new List<string>();

foreach (string name in RpcCalledEvents)
{
Expand Down Expand Up @@ -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<string, string>))]
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<string> updatedRpcCalledEvents = new();
// This state is required to update the called rpc events list, (Temp fix see RpcCall for more)
string nameJson = _playroomKit.GetState<string>("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);
}
}

Expand Down
2 changes: 1 addition & 1 deletion Assets/PlayroomKit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down
35 changes: 25 additions & 10 deletions Assets/PlayroomKit/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand All @@ -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 {
Expand Down
8 changes: 4 additions & 4 deletions Assets/Scenes/TestScene.unity
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
Loading

0 comments on commit 088fcf4

Please sign in to comment.