Skip to content

Commit

Permalink
feat: browser player service
Browse files Browse the repository at this point in the history
  • Loading branch information
momintlh committed Nov 14, 2024
1 parent 1842ca0 commit 5d7c9b5
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public void InsertCoin()
{
_playroomKit.InsertCoin(new InitOptions
{
skipLobby = true,
maxPlayersPerRoom = 3,
matchmaking = false,
roomCode = roomCode
Expand Down
46 changes: 31 additions & 15 deletions Assets/PlayroomKit/PlayroomKit.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using UnityEngine;
using System.Collections.Generic;
using System;
using UBB;


namespace Playroom
Expand All @@ -25,23 +26,22 @@ public enum MockModeSelector
// Constructor
public PlayroomKit()
{
#if !UNITY_EDITOR

#if !UNITY_EDITOR
_playroomService = new PlayroomBuildService(new PlayroomKitInterop());
_rpc = new RPC(this);

#elif UNITY_EDITOR

if (CurrentMockMode == MockModeSelector.Local)
{
_playroomService = new LocalMockPlayroomService();
_rpc = new RPCLocal();
}
else if (CurrentMockMode == MockModeSelector.Browser)
{
_playroomService = new PlayroomBrowserMockService();
// _rpc = new RPCLocal();
}
if (CurrentMockMode == MockModeSelector.Local)
{
_playroomService = new LocalMockPlayroomService();
_rpc = new RPCLocal();
}
else if (CurrentMockMode == MockModeSelector.Browser)
{
_playroomService = new PlayroomBrowserMockService();
// _rpc = new RPCLocal();
}
#endif
}

Expand Down Expand Up @@ -89,7 +89,15 @@ public Player GetPlayer(string playerId)
{
if (!IsRunningInBrowser())
{
player = new Player(playerId, new Player.LocalPlayerService(playerId));
if (CurrentMockMode == MockModeSelector.Local)
{
player = new Player(playerId, new Player.LocalPlayerService(playerId));
}
else if (CurrentMockMode == MockModeSelector.Browser)
{
player = new Player(playerId,
new BrowserMockPlayerService(UnityBrowserBridge.Instance, playerId));
}
}
else
{
Expand All @@ -111,7 +119,15 @@ public static Player GetPlayerById(string playerId)
{
if (!IsRunningInBrowser())
{
player = new Player(playerId, new Player.LocalPlayerService(playerId));
if (CurrentMockMode == MockModeSelector.Local)
{
player = new Player(playerId, new Player.LocalPlayerService(playerId));
}
else if (CurrentMockMode == MockModeSelector.Browser)
{
player = new Player(playerId,
new BrowserMockPlayerService(UnityBrowserBridge.Instance, playerId));
}
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,52 @@ public GameObject GetGameObject(string key)
return gameObject;
}

public void CallJs(string jsFunctionName, string callbackName = null, string gameObjectName = null,
bool isAsync = false, params string[] args)
{
List<string> allParams = new();

foreach (var param in args)
{
if (param.StartsWith("{") && param.EndsWith("}"))
allParams.Add(param);
else
allParams.Add($"'{param}'");
}

if (!string.IsNullOrEmpty(callbackName)) allParams.Add($"'{callbackName}'");
if (!string.IsNullOrEmpty(gameObjectName)) allParams.Add($"'{gameObjectName}'");

string jsCall = $"{jsFunctionName}({string.Join(", ", allParams)})";
if (isAsync) jsCall = $"await {jsCall}";


Debug.Log(jsCall);
ExecuteJS(jsCall);
}


public T CallJs<T>(string jsFunctionName, string callbackName = null, string gameObjectName = null,
bool isAsync = false, params string[] args)
{
List<string> allParams = new();
foreach (string param in args)
{
if (param.StartsWith("{") && param.EndsWith("}"))
allParams.Add(param);
else
allParams.Add($"'{param}'");
}

if (!string.IsNullOrEmpty(callbackName)) allParams.Add($"'{callbackName}'");
if (!string.IsNullOrEmpty(gameObjectName)) allParams.Add($"'{gameObjectName}'");

string jsCall = $"{jsFunctionName}({string.Join(", ", allParams)})";
if (isAsync) jsCall = $"await {jsCall}";

return ExecuteJS<T>(jsCall);
}

# endif
}
}

0 comments on commit 5d7c9b5

Please sign in to comment.