Skip to content

Commit

Permalink
chore: regions for build service
Browse files Browse the repository at this point in the history
  • Loading branch information
momintlh committed Feb 4, 2025
1 parent c5229ec commit 6337835
Showing 1 changed file with 149 additions and 122 deletions.
271 changes: 149 additions & 122 deletions Assets/PlayroomKit/modules/PlayroomBuildService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ public partial class PlayroomKit
public class PlayroomBuildService : IPlayroomBase, IPlayroomBuildExtensions
{
private readonly IInterop _interop;
private static Action<string> _onError;
private static Action _onStatesResetCallback;
private static Action _onPlayersStatesResetCallback;

public PlayroomBuildService()
{
Expand All @@ -24,6 +27,32 @@ public PlayroomBuildService(IInterop interop)
}


#region Init Methods

public void InsertCoin(InitOptions options = null, Action onLaunchCallBack = null,
Action onDisconnectCallback = null)
{
IsPlayRoomInitialized = true;

var onLaunchCallBackKey = CallbackManager.RegisterCallback(onLaunchCallBack, "onLaunchCallBack");
var onDisconnectCallBackKey =
CallbackManager.RegisterCallback(onDisconnectCallback, "onDisconnectCallBack");

string optionsJson = null;
if (options != null) optionsJson = Helpers.SerializeInitOptions(options);

if (options.skipLobby == false)
{
#if UNITY_WEBGL && !UNITY_EDITOR
WebGLInput.captureAllKeyboardInput = false;
#endif
}

_interop.InsertCoinWrapper(
optionsJson, InvokeInsertCoin, IPlayroomBase.__OnQuitInternalHandler, OnDisconnectCallbackHandler,
InvokeOnErrorInsertCoin, onLaunchCallBackKey, onDisconnectCallBackKey);
}

public Action OnPlayerJoin(Action<Player> onPlayerJoinCallback)
{
if (!IPlayroomBase.OnPlayerJoinCallbacks.Contains(onPlayerJoinCallback))
Expand All @@ -43,29 +72,35 @@ void Unsubscribe()
}


public void InsertCoin(InitOptions options = null, Action onLaunchCallBack = null,
Action onDisconnectCallback = null)
public void StartMatchmaking(Action callback = null)
{
IsPlayRoomInitialized = true;

var onLaunchCallBackKey = CallbackManager.RegisterCallback(onLaunchCallBack, "onLaunchCallBack");
var onDisconnectCallBackKey =
CallbackManager.RegisterCallback(onDisconnectCallback, "onDisconnectCallBack");
CallbackManager.RegisterCallback(callback, "matchMakingStarted");
_interop.StartMatchmakingWrapper(InvokeStartMatchmakingCallback);
}

string optionsJson = null;
if (options != null) optionsJson = Helpers.SerializeInitOptions(options);
public void OnDisconnect(Action callback)
{
CallbackManager.RegisterCallback(callback);
_interop.OnDisconnectWrapper(OnDisconnectCallbackHandler);
}

if (options.skipLobby == false)
{
#if UNITY_WEBGL && !UNITY_EDITOR
WebGLInput.captureAllKeyboardInput = false;
#endif
}
#endregion

#region Unsubscribers

_interop.InsertCoinWrapper(
optionsJson, InvokeInsertCoin, IPlayroomBase.__OnQuitInternalHandler, onDisconnectCallbackHandler,
InvokeOnErrorInsertCoin, onLaunchCallBackKey, onDisconnectCallBackKey);
public void UnsubscribeOnQuit()
{
_interop.UnsubscribeOnQuitWrapper();
}

private void UnsubscribeOnPlayerJoin(string callbackID)
{
_interop.UnsubscribeOnPlayerJoinWrapper(callbackID);
}

#endregion

#region Local Player

public Player MyPlayer()
{
Expand All @@ -78,6 +113,10 @@ public Player Me()
return MyPlayer();
}

#endregion

#region Room

public bool IsHost()
{
return _interop.IsHostWrapper();
Expand All @@ -93,12 +132,13 @@ public string GetRoomCode()
return _interop.GetRoomCodeWrapper();
}

public void StartMatchmaking(Action callback = null)
public bool IsStreamScreen()
{
CallbackManager.RegisterCallback(callback, "matchMakingStarted");
_interop.StartMatchmakingWrapper(InvokeStartMatchmakingCallback);
return _interop.IsStreamScreenWrapper();
}

#endregion

#region State

public void SetState<T>(string key, T value, bool reliable = false)
Expand Down Expand Up @@ -175,6 +215,30 @@ public void SetState(string key, object value, bool reliable = false)
_interop.SetStateStringWrapper(key, jsonString, reliable);
}


private string GetStateString(string key)
{
return _interop.GetStateStringWrapper(key);
}

private int GetStateInt(string key)
{
return _interop.GetStateIntWrapper(key);
}

private float GetStateFloat(string key)
{
return _interop.GetStateFloatWrapper(key);
}

private bool GetStateBool(string key)
{
var stateValue = GetStateInt(key);
return stateValue == 1 ? true :
stateValue == 0 ? false :
throw new InvalidOperationException($"GetStateBool: {key} is not a bool");
}

public T GetState<T>(string key)
{
Type type = typeof(T);
Expand All @@ -193,35 +257,6 @@ public T GetState<T>(string key)
}
}

#endregion


[MonoPInvokeCallback(typeof(Action))]
private static void InvokeStartMatchmakingCallback()
{
CallbackManager.InvokeCallback("matchMakingStarted");
}

[MonoPInvokeCallback(typeof(Action<string>))]
private static void InvokeInsertCoin(string key)
{
CallbackManager.InvokeCallback(key);

#if UNITY_WEBGL && !UNITY_EDITOR
WebGLInput.captureAllKeyboardInput = true;
#endif
}

public void OnDisconnect(Action callback)
{
CallbackManager.RegisterCallback(callback);
_interop.OnDisconnectWrapper(onDisconnectCallbackHandler);
}

public bool IsStreamScreen()
{
return _interop.IsStreamScreenWrapper();
}

public void WaitForState(string stateKey, Action<string> onStateSetCallback = null)
{
Expand All @@ -236,36 +271,27 @@ public void WaitForPlayerState(string playerID, string stateKey, Action<string>
WaitForPlayerCallback = onStateSetCallback;
_interop.WaitForPlayerStateWrapper(playerID, stateKey, OnStateSetCallback);
}

[MonoPInvokeCallback(typeof(Action<string>))]
void OnStateSetCallback(string data)
{
WaitForPlayerCallback?.Invoke(data);
}

private static Action onstatesReset;
private static Action onplayersStatesReset;


public void ResetStates(string[] keysToExclude = null, Action onStatesReset = null)
{
onstatesReset = onStatesReset;
_onStatesResetCallback = onStatesReset;
string keysJson = keysToExclude != null ? Helpers.CreateJsonArray(keysToExclude).ToString() : null;
_interop.ResetStatesWrapper(keysJson, InvokeResetCallBack);
}

[MonoPInvokeCallback(typeof(Action))]
private static void InvokeResetCallBack()
{
onstatesReset?.Invoke();
}


public void ResetPlayersStates(string[] keysToExclude = null, Action onStatesReset = null)
{
onstatesReset = onStatesReset;
_onStatesResetCallback = onStatesReset;
string keysJson = keysToExclude != null ? Helpers.CreateJsonArray(keysToExclude).ToString() : null;
_interop.ResetPlayersStatesWrapper(keysJson, InvokePlayersResetCallBack);
}



#endregion

#region Joystick

public void CreateJoyStick(JoystickOptions options)
{
string jsonStr = Helpers.ConvertJoystickOptionsToJson(options);
Expand All @@ -279,59 +305,7 @@ public Dpad DpadJoystick()
return myDpad;
}

public void UnsubscribeOnQuit()
{
_interop.UnsubscribeOnQuitWrapper();
}

[MonoPInvokeCallback(typeof(Action))]
private static void InvokePlayersResetCallBack()
{
onplayersStatesReset?.Invoke();
}

[MonoPInvokeCallback(typeof(Action<string>))]
private static void onDisconnectCallbackHandler(string key)
{
CallbackManager.InvokeCallback(key);
}

private static Action<string> onError;

[MonoPInvokeCallback(typeof(Action<string>))]
private static void InvokeOnErrorInsertCoin(string error)
{
onError?.Invoke(error);
Debug.LogException(new Exception(error));
}

private void UnsubscribeOnPlayerJoin(string callbackID)
{
_interop.UnsubscribeOnPlayerJoinWrapper(callbackID);
}

private string GetStateString(string key)
{
return _interop.GetStateStringWrapper(key);
}

private int GetStateInt(string key)
{
return _interop.GetStateIntWrapper(key);
}

private float GetStateFloat(string key)
{
return _interop.GetStateFloatWrapper(key);
}

private bool GetStateBool(string key)
{
var stateValue = GetStateInt(key);
return stateValue == 1 ? true :
stateValue == 0 ? false :
throw new InvalidOperationException($"GetStateBool: {key} is not a bool");
}
#endregion

#region Persistent API

Expand Down Expand Up @@ -383,6 +357,59 @@ public void ClearTurns(Action callback)
}

#endregion

#region Callbacks

[MonoPInvokeCallback(typeof(Action))]
private static void InvokeStartMatchmakingCallback()
{
CallbackManager.InvokeCallback("matchMakingStarted");
}

[MonoPInvokeCallback(typeof(Action<string>))]
private static void InvokeInsertCoin(string key)
{
CallbackManager.InvokeCallback(key);

#if UNITY_WEBGL && !UNITY_EDITOR
WebGLInput.captureAllKeyboardInput = true;
#endif
}

[MonoPInvokeCallback(typeof(Action<string>))]
void OnStateSetCallback(string data)
{
WaitForPlayerCallback?.Invoke(data);
}

[MonoPInvokeCallback(typeof(Action))]
private static void InvokeResetCallBack()
{
_onStatesResetCallback?.Invoke();
}

[MonoPInvokeCallback(typeof(Action))]
private static void InvokePlayersResetCallBack()
{
_onPlayersStatesResetCallback?.Invoke();
}

[MonoPInvokeCallback(typeof(Action<string>))]
private static void OnDisconnectCallbackHandler(string key)
{
CallbackManager.InvokeCallback(key);
}

[MonoPInvokeCallback(typeof(Action<string>))]
private static void InvokeOnErrorInsertCoin(string error)
{
_onError?.Invoke(error);
Debug.LogException(new Exception(error));
}


#endregion

}
}
}

0 comments on commit 6337835

Please sign in to comment.