Skip to content

Commit

Permalink
feat: Player methods
Browse files Browse the repository at this point in the history
  • Loading branch information
momintlh committed Nov 14, 2024
1 parent 5d7c9b5 commit 5bbcfc1
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 83 deletions.
Original file line number Diff line number Diff line change
@@ -1,61 +1,84 @@

using System;
using System;
using UBB;
using UnityEngine;

namespace Playroom
{
public class BrowserMockPlayerService : PlayroomKit.Player.IPlayerBase
{

#region State
private readonly UnityBrowserBridge _ubb;
private string _id;

public BrowserMockPlayerService(UnityBrowserBridge ubb, string id)
{
_ubb = ubb;
_id = id;
}

#region State

public void SetState(string key, int value, bool reliable = false)
{
throw new NotImplementedException();
_ubb.CallJs("SetPlayerStateByPlayerId", null, null, false, _id, key, value.ToString(),
reliable.ToString().ToLower());
}

public void SetState(string key, float value, bool reliable = false)
{
throw new NotImplementedException();
_ubb.CallJs("SetPlayerStateByPlayerId", null, null, false, _id, key, value.ToString(),
reliable.ToString().ToLower());
}

public void SetState(string key, bool value, bool reliable = false)
{
throw new NotImplementedException();
_ubb.CallJs("SetPlayerStateByPlayerId", null, null, false, _id, key, value.ToString(),
reliable.ToString().ToLower());
}

public void SetState(string key, string value, bool reliable = false)
{
throw new NotImplementedException();
_ubb.CallJs("SetPlayerStateByPlayerId", null, null, false, _id, key, value.ToString(),
reliable.ToString().ToLower());
}

public void SetState(string key, object value, bool reliable = false)
{
throw new NotImplementedException();
_ubb.CallJs("SetPlayerStateByPlayerId", null, null, false, _id, key, value.ToString(),
reliable.ToString().ToLower());
}

public T GetState<T>(string key)
{
throw new NotImplementedException();
Debug.Log(key);
return _ubb.CallJs<T>(key);
}

#endregion

public PlayroomKit.Player.Profile GetProfile()
{
throw new NotImplementedException();
string json = _ubb.CallJs<string>("GetProfile", null, null, false, _id);

Debug.Log(json);

var profileData = Helpers.ParseProfile(json);
return profileData;
}

public Action OnQuit(Action<string> callback)
{
throw new NotImplementedException();
Debug.LogWarning("OnQuit not supported yet in Browser Mode");
return default;
}

public void Kick(Action onKickCallBack = null)
{
throw new NotImplementedException();
_ubb.CallJs("Kick", null, null, true, _id);
}

public void WaitForState(string stateKey, Action<string> onStateSetCallback = null)
{
throw new NotImplementedException();
_ubb.CallJs("WaitForPlayerState", null, null, true, _id, stateKey);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,13 @@ private static void MockOnPlayerJoinBrowser(Action<Player> onPlayerJoinCallback)
// #endif
// }
//
// private static void MockOnPlayerQuitLocal(Action<string> onPlayerQuitCallback)
// {
// Debug.Log("On Player Quit");
// var testPlayer = GetPlayer(PlayerId);
// testPlayer.OnQuitCallbacks.Add(onPlayerQuitCallback);
// __OnQuitInternalHandler(PlayerId);
// }
// private static void MockOnPlayerQuitLocal(Action<string> onPlayerQuitCallback)
// {
// Debug.Log("On Player Quit");
// var testPlayer = GetPlayer(PlayerId);
// testPlayer.OnQuitCallbacks.Add(onPlayerQuitCallback);
// __OnQuitInternalHandler(PlayerId);
// }

/// <summary>
/// This function is used by GetPlayerID in PlayroomKitDevManager, GetPlayer is only invoked
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void InsertCoin(InitOptions options = null, Action onLaunchCallBack = nul
if (options != null) optionsJson = Helpers.SerializeInitOptions(options);

var gameObjectName = _ubb.GetGameObject("InsertCoin").name;
CallJs("InsertCoin", onLaunchCallBack.GetMethodInfo().Name, gameObjectName, true, optionsJson);
_ubb.CallJs("InsertCoin", onLaunchCallBack.GetMethodInfo().Name, gameObjectName, true, optionsJson);
PlayroomKit.IsPlayRoomInitialized = true;
}

Expand All @@ -34,7 +34,7 @@ public Action OnPlayerJoin(Action<PlayroomKit.Player> onPlayerJoinCallback)
if (!PlayroomKit.IPlayroomBase.OnPlayerJoinCallbacks.Contains(onPlayerJoinCallback))
PlayroomKit.IPlayroomBase.OnPlayerJoinCallbacks.Add(onPlayerJoinCallback);

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

void Unsub()
{
Expand All @@ -46,7 +46,7 @@ void Unsub()

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

Expand All @@ -57,7 +57,7 @@ public void OnDisconnect(Action callback)
GameObject callbackObject = new GameObject(callbackKey);
MockCallbackInvoker invoker = callbackObject.AddComponent<MockCallbackInvoker>();
invoker.SetCallback(callback, callbackKey);
CallJs("OnDisconnect", callbackKey);
_ubb.CallJs("OnDisconnect", callbackKey);
}

#endregion
Expand All @@ -66,7 +66,7 @@ public void OnDisconnect(Action callback)

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

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

public bool IsHost()
{
return CallJs<bool>("IsHost");
return _ubb.CallJs<bool>("IsHost");
}

public string GetRoomCode()
{
return CallJs<string>("GetRoomCode");
return _ubb.CallJs<string>("GetRoomCode");
}

public bool IsStreamScreen()
{
return CallJs<bool>("IsStreamScreen");
return _ubb.CallJs<bool>("IsStreamScreen");
}

#endregion
Expand All @@ -100,13 +100,13 @@ public bool IsStreamScreen()

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

public T GetState<T>(string key)
{
return CallJs<T>("GetState", null, null, false, key);
return _ubb.CallJs<T>("GetState", null, null, false, key);
}

public void WaitForState(string stateKey, Action<string> onStateSetCallback = null)
Expand All @@ -119,7 +119,7 @@ public void WaitForState(string stateKey, Action<string> onStateSetCallback = nu

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

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

public void WaitForPlayerState(string playerID, string stateKey, Action<string> onStateSetCallback = null)
Expand All @@ -132,18 +132,18 @@ public void WaitForPlayerState(string playerID, string stateKey, Action<string>

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

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

public void ResetStates(string[] keysToExclude = null, Action onStatesReset = null)
{
CallJs("ResetStates", null, null, true, keysToExclude);
_ubb.CallJs("ResetStates", null, null, true, keysToExclude);
onStatesReset?.Invoke();
}

public void ResetPlayersStates(string[] keysToExclude = null, Action onStatesReset = null)
{
CallJs("ResetPlayersStates", null, null, true, keysToExclude);
_ubb.CallJs("ResetPlayersStates", null, null, true, keysToExclude);
onStatesReset?.Invoke();
}

Expand Down Expand Up @@ -177,53 +177,6 @@ public Dpad DpadJoystick()
#endregion

#region Utils

private 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);
_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();
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 _ubb.ExecuteJS<T>(jsCall);
}

public static void MockOnPlayerJoinWrapper(string playerId)
{
PlayroomKit.IPlayroomBase.OnPlayerJoinWrapperCallback(playerId);
Expand Down
1 change: 0 additions & 1 deletion Assets/PlayroomKit/modules/Player/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ public partial class PlayroomKit
{
public partial class Player
{

//DI
public string id;
IPlayerBase _playerService;
Expand Down

0 comments on commit 5bbcfc1

Please sign in to comment.