Skip to content

Commit

Permalink
Merge pull request #126 from playroomkit/double-rpc-fix
Browse files Browse the repository at this point in the history
Transfer host and RPCs
  • Loading branch information
SaadBazaz authored Jan 3, 2025
2 parents 7f4c90a + 76cd7b9 commit dbd10ca
Show file tree
Hide file tree
Showing 26 changed files with 255 additions and 237 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ private static void RemovePlayer(string playerID)
if (PlayerDict.TryGetValue(playerID, out GameObject player))
{
PlayerDict.Remove(playerID);
players.Remove(players.Find(p => p.id == playerID));
playerGameObjects.Remove(player);
Destroy(player);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ RenderSettings:
m_ReflectionIntensity: 1
m_CustomReflection: {fileID: 0}
m_Sun: {fileID: 1429187865}
m_IndirectSpecularColor: {r: 0.44657815, g: 0.49641192, b: 0.5748165, a: 1}
m_IndirectSpecularColor: {r: 0.44657844, g: 0.49641222, b: 0.57481694, a: 1}
m_UseRadianceAmbientProbe: 0
--- !u!157 &3
LightmapSettings:
Expand Down Expand Up @@ -224,7 +224,7 @@ PrefabInstance:
objectReference: {fileID: 0}
- target: {fileID: 8987662522597341863, guid: 1b30fa3a265114a45a7e23165a20aecc, type: 3}
propertyPath: mockMode
value: 1
value: 0
objectReference: {fileID: 0}
- target: {fileID: 8987662522597341863, guid: 1b30fa3a265114a45a7e23165a20aecc, type: 3}
propertyPath: enableLogs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,17 @@ public class GameManagerIsometric : MonoBehaviour
[SerializeField]
private GameObject playerPrefab;

private readonly PlayroomKit _playroomKit = new();
private PlayroomKit _playroomKit;

private void Awake()
{
_playroomKit = new PlayroomKit();

}

private void Start()
{

_playroomKit.InsertCoin(new InitOptions
{
maxPlayersPerRoom = 3,
Expand Down Expand Up @@ -111,7 +118,7 @@ private static void RemovePlayer(string playerID)
Destroy(player);

foreach (var (key, value) in PlayerDict) Debug.Log($"player {key} is still in the room");

Debug.Log(playerID + " has left the room!");
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ public class GameManagerDemo : MonoBehaviour

private PlayroomKit _playroomKit = new();


[Serializable]
private class TempData
{
public Vector3 pos;
public string message;
}

// Update is called once per frame
private void Update()
{
Expand Down Expand Up @@ -149,6 +157,18 @@ private void SetStateColor(Color color)
if (playerJoined)
{
var myPlayer = _playroomKit.MyPlayer();

var data = new TempData()
{
message = "Sending Pos",
pos = new Vector3(6f, 1f, 4f)
};

Debug.LogWarning($"Setting data : {data}");

string d = JsonUtility.ToJson(data);
myPlayer.SetState("data", d);

myPlayer.SetState("color", color);
logsText.text = $"setting color to {color}";
}
Expand All @@ -170,6 +190,8 @@ private void GetStateColor()
for (var i = 0; i < players.Count; i++)
if (players[i] != null)
{
Debug.LogWarning($"Getting data ${players[i].GetState<string>("data")}");

var color = players[i].GetState<Color>("color");

if (playerGameObjects[i] != null)
Expand Down Expand Up @@ -238,7 +260,8 @@ public void HandleValueChange(int index)

public void RegisterRpcShoot()
{
_playroomKit.RpcRegister("ShootLaser", HandleScoreUpdate);
_playroomKit.RpcRegister("hostCall", hostCall);
_playroomKit.RpcRegister("clientCall", clientCall);

Debug.Log("Shoot function registered");
logsText.text = "ShootLaser RPC registered";
Expand All @@ -265,6 +288,19 @@ private void HandleScoreUpdate(string data, string caller)
{
Debug.LogError($"No GameObject found for caller: {caller}");
}

_playroomKit.RpcCall("ChainCall", "", PlayroomKit.RpcMode.ALL);
}

private void hostCall(string data, string senderID)
{
Debug.LogWarning("HOST CALL");
_playroomKit.RpcCall("clientCall", "", PlayroomKit.RpcMode.ALL);
}

private void clientCall(string data, string senderID)
{
Debug.LogWarning("CLIENT CALL");
}


Expand All @@ -273,12 +309,15 @@ public void ShootLaser()
var myPlayer = _playroomKit.MyPlayer();
var index = players.IndexOf(myPlayer);
score = playerGameObjects[index].GetComponent<Laser>().ShootLaser(score);

_playroomKit.RpcCall("ShootLaser", score, PlayroomKit.RpcMode.ALL,
() =>
{
Debug.Log("ShootLaser RPC Called");
logsText.text = "ShootLaser RPC Called"; });

// _playroomKit.RpcCall("ShootLaser", score, PlayroomKit.RpcMode.ALL,
// () =>
// {
// Debug.Log("ShootLaser RPC Called");
// logsText.text = "ShootLaser RPC Called";
// });

_playroomKit.RpcCall("hostCall", "", PlayroomKit.RpcMode.HOST);
}

public void GetRoomCode()
Expand Down
6 changes: 5 additions & 1 deletion Assets/PlayroomKit/Examples/package-showcase/showcase.unity
Original file line number Diff line number Diff line change
Expand Up @@ -6823,6 +6823,10 @@ PrefabInstance:
propertyPath: m_LocalEulerAnglesHint.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 8899696337967424923, guid: 1b30fa3a265114a45a7e23165a20aecc, type: 3}
propertyPath: browser
value: 1
objectReference: {fileID: 0}
- target: {fileID: 8899696337967424923, guid: 1b30fa3a265114a45a7e23165a20aecc, type: 3}
propertyPath: webDriverDirectory
value: Assets\
Expand All @@ -6833,7 +6837,7 @@ PrefabInstance:
objectReference: {fileID: 0}
- target: {fileID: 8987662522597341863, guid: 1b30fa3a265114a45a7e23165a20aecc, type: 3}
propertyPath: enableLogs
value: 0
value: 1
objectReference: {fileID: 0}
- target: {fileID: 8987662522597341863, guid: 1b30fa3a265114a45a7e23165a20aecc, type: 3}
propertyPath: insertCoinCaller
Expand Down
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
2 changes: 1 addition & 1 deletion Assets/PlayroomKit/Tests/Editor/PlayroomKitTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ void HandleShoot(string data, string caller)
}

_playroomKit.RpcRegister("Shoot", HandleShoot, "You shot!");
_interop.Received(1).RpcRegisterWrapper("Shoot", Arg.Any<Action<string, string>>(), "You shot!");
_interop.Received(1).RpcRegisterWrapper("Shoot", Arg.Any<Action<string>>(), "You shot!");
}

[Test]
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
18 changes: 13 additions & 5 deletions Assets/PlayroomKit/modules/Helpers/CallbackManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ namespace Playroom
public static class CallbackManager
{
private static Dictionary<string, Delegate> callbacks = new();
private static readonly HashSet<string> RegisteredEvents = new();

private static Dictionary<string, List<Action<string, string>>> RpcCallBacks = new();

public static string RegisterRpcCallback(Action<string, string> callback, string key = null)
public static void RegisterRpcCallback(Action<string, string> callback, string key = null)
{
if (string.IsNullOrEmpty(key))
key = GenerateKey();
Expand All @@ -24,8 +25,6 @@ public static string RegisterRpcCallback(Action<string, string> callback, string
{
RpcCallBacks.Add(key, new List<Action<string, string>> { callback });
}

return key;
}

public static void InvokeRpcRegisterCallBack(string name, string data, string sender)
Expand All @@ -34,7 +33,7 @@ public static void InvokeRpcRegisterCallBack(string name, string data, string se
{
for (var i = 0; i < callbacks.Count; i++)
{
var callback = callbacks[i];
Action<string, string> callback = callbacks[i];
callback?.Invoke(data, sender);
}
}
Expand All @@ -45,6 +44,15 @@ public static void InvokeRpcRegisterCallBack(string name, string data, string se
}
}

public static bool IsEventRegistered(string eventName)
{
return RegisteredEvents.Contains(eventName);
}

public static void MarkEventAsRegistered(string eventName)
{
RegisteredEvents.Add(eventName);
}

public static string RegisterCallback(Delegate callback, string key = null)
{
Expand Down Expand Up @@ -77,7 +85,7 @@ public static void InvokeCallback(string key, params string[] args)
}
else
{
Debug.LogWarning(
DebugLogger.Log(
$"Callback with key {key} not found!, maybe register the callback or call the correct playroom function?");
}
}
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 @@ -62,7 +64,7 @@ void InsertCoinWrapper(string options,

void StartMatchmakingWrapper(Action callback);

void RpcRegisterWrapper(string name, Action<string, string> rpcRegisterCallback,
void RpcRegisterWrapper(string name, Action<string> rpcRegisterCallback,
string onResponseReturn = null);

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

string GetProfileWrapper(string playerID);
//

}
}
}
Loading

0 comments on commit dbd10ca

Please sign in to comment.