Skip to content

Commit

Permalink
feat: more implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
momintlh committed Nov 12, 2024
1 parent 0a4e9d0 commit c66a100
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 20 deletions.
3 changes: 2 additions & 1 deletion Assets/PlayroomKit/PlayroomKit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ public Player GetPlayer(string playerId)
}
}

private static Player GetPlayerById(string playerId)

public static Player GetPlayerById(string playerId)
{
if (Players.TryGetValue(playerId, out var player))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,14 +305,10 @@ private static void MockOnDisconnectBrowser(Action callback)
{
#if UNITY_EDITOR
string key = Guid.NewGuid().ToString();

string callbackKey = $"OnDisconnect_{key}";
GameObject callbackObject = new GameObject(callbackKey);

MockCallbackInvoker invoker = callbackObject.AddComponent<MockCallbackInvoker>();
invoker.SetCallback(callback, callbackKey);


UnityBrowserBridge.Instance.ExecuteJS(
$"OnDisconnect('{callbackKey}')");
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,20 @@ private void CallJs(string jsFunctionName, string callbackName = null, string ga
_ubb.ExecuteJS(jsCall);
}


private T CallJs<T>(string jsFunctionName, string callbackName = null, string gameObjectName = null,
bool isAsync = false, params string[] args)
{
List<string> allParams = new List<string>(args);
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 _ubb.ExecuteJS<T>(jsCall);
}

public static void MockOnPlayerJoinWrapper(string playerId)
{
PlayroomKit.IPlayroomBase.OnPlayerJoinWrapperCallback(playerId);
Expand All @@ -48,17 +62,33 @@ public void InsertCoin(InitOptions options = null, Action onLaunchCallBack = nul

public Action OnPlayerJoin(Action<PlayroomKit.Player> onPlayerJoinCallback)
{
throw new NotImplementedException();
if (!PlayroomKit.IPlayroomBase.OnPlayerJoinCallbacks.Contains(onPlayerJoinCallback))
PlayroomKit.IPlayroomBase.OnPlayerJoinCallbacks.Add(onPlayerJoinCallback);

CallJs("OnPlayerJoin", null, _ubb.GetGameObject("devManager").name);

void Unsub()
{
Debug.Log("Unsubscribing from OnPlayerJoin");
}

return Unsub;
}

public void StartMatchmaking(Action callback = null)
{
throw new NotImplementedException();
CallJs("StartMatchmaking", null, null, true);
callback?.Invoke();
}

public void OnDisconnect(Action callback)
{
throw new NotImplementedException();
string key = Guid.NewGuid().ToString();
string callbackKey = $"OnDisconnect_{key}";
GameObject callbackObject = new GameObject(callbackKey);
MockCallbackInvoker invoker = callbackObject.AddComponent<MockCallbackInvoker>();
invoker.SetCallback(callback, callbackKey);
CallJs("OnDisconnect", callbackKey);
}

#endregion
Expand All @@ -67,12 +97,13 @@ public void OnDisconnect(Action callback)

public PlayroomKit.Player MyPlayer()
{
throw new NotImplementedException();
string id = CallJs<string>("MyPlayer");
return PlayroomKit.GetPlayerById(id);
}

public PlayroomKit.Player Me()
{
throw new NotImplementedException();
return MyPlayer();
}

#endregion
Expand All @@ -81,17 +112,17 @@ public PlayroomKit.Player Me()

public bool IsHost()
{
throw new NotImplementedException();
return CallJs<bool>("IsHost");
}

public string GetRoomCode()
{
throw new NotImplementedException();
return CallJs<string>("GetRoomCode");
}

public bool IsStreamScreen()
{
throw new NotImplementedException();
return CallJs<bool>("IsStreamScreen");
}

#endregion
Expand All @@ -100,22 +131,38 @@ public bool IsStreamScreen()

public void SetState<T>(string key, T value, bool reliable = false)
{
throw new NotImplementedException();
CallJs("SetState", null, null, true, key, value.ToString(), reliable.ToString().ToLower());
}

public T GetState<T>(string key)
{
throw new NotImplementedException();
return CallJs<T>("GetState", null, null, true, key);
}

public void WaitForState(string stateKey, Action<string> onStateSetCallback = null)
{
throw new NotImplementedException();
string callbackKey = $"WaitForState_{stateKey}";
GameObject callbackObject = new GameObject(callbackKey);

MockCallbackInvoker invoker = callbackObject.AddComponent<MockCallbackInvoker>();
invoker.SetCallback(onStateSetCallback, callbackKey);

CallBacksHandlerMock.Instance.RegisterCallbackObject(callbackKey, callbackObject, "ExecuteCallback");

CallJs("WaitForState", null, null, true, stateKey, callbackKey);
}

public void WaitForPlayerState(string playerID, string stateKey, Action<string> onStateSetCallback = null)
{
throw new NotImplementedException();
string callbackKey = $"WaitForPlayerState_{stateKey}";
GameObject callbackObject = new GameObject(callbackKey);

MockCallbackInvoker invoker = callbackObject.AddComponent<MockCallbackInvoker>();
invoker.SetCallback(onStateSetCallback, callbackKey);

CallBacksHandlerMock.Instance.RegisterCallbackObject(callbackKey, callbackObject, "ExecuteCallback");

CallJs("WaitForPlayerState", null, null, true, playerID, stateKey, callbackKey);
}

public void ResetStates(string[] keysToExclude = null, Action onStatesReset = null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ OnPlayerJoin = function (gameObjectName) {
// States
SetState = function (key, value, reliable) {
reliable = !!reliable;

Playroom.setState(key, value, reliable);
};

Expand Down Expand Up @@ -149,7 +148,6 @@ WaitForState = function (stateKey, callbackKey) {
};

const jsonData = JSON.stringify(data);

unityInstance.SendMessage("CallbackManager", "InvokeCallback", jsonData);
})
.catch((error) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ public void SetState<T>(string key, T value, bool reliable = false)
mockGlobalStates[key] = value;
else
mockGlobalStates.Add(key, value);

CallbackManager.InvokeCallback(key, value as string);
}

Expand Down

0 comments on commit c66a100

Please sign in to comment.