diff --git a/Assets/PlayroomKit/modules/PlayroomBuildService.cs b/Assets/PlayroomKit/modules/PlayroomBuildService.cs index 939748f..80aef5a 100644 --- a/Assets/PlayroomKit/modules/PlayroomBuildService.cs +++ b/Assets/PlayroomKit/modules/PlayroomBuildService.cs @@ -12,6 +12,9 @@ public partial class PlayroomKit public class PlayroomBuildService : IPlayroomBase, IPlayroomBuildExtensions { private readonly IInterop _interop; + private static Action _onError; + private static Action _onStatesResetCallback; + private static Action _onPlayersStatesResetCallback; public PlayroomBuildService() { @@ -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 onPlayerJoinCallback) { if (!IPlayroomBase.OnPlayerJoinCallbacks.Contains(onPlayerJoinCallback)) @@ -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() { @@ -78,6 +113,10 @@ public Player Me() return MyPlayer(); } + #endregion + + #region Room + public bool IsHost() { return _interop.IsHostWrapper(); @@ -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(string key, T value, bool reliable = false) @@ -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(string key) { Type type = typeof(T); @@ -193,35 +257,6 @@ public T GetState(string key) } } - #endregion - - - [MonoPInvokeCallback(typeof(Action))] - private static void InvokeStartMatchmakingCallback() - { - CallbackManager.InvokeCallback("matchMakingStarted"); - } - - [MonoPInvokeCallback(typeof(Action))] - 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 onStateSetCallback = null) { @@ -236,36 +271,27 @@ public void WaitForPlayerState(string playerID, string stateKey, Action WaitForPlayerCallback = onStateSetCallback; _interop.WaitForPlayerStateWrapper(playerID, stateKey, OnStateSetCallback); } - - [MonoPInvokeCallback(typeof(Action))] - 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); @@ -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))] - private static void onDisconnectCallbackHandler(string key) - { - CallbackManager.InvokeCallback(key); - } - - private static Action onError; - - [MonoPInvokeCallback(typeof(Action))] - 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 @@ -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))] + private static void InvokeInsertCoin(string key) + { + CallbackManager.InvokeCallback(key); + +#if UNITY_WEBGL && !UNITY_EDITOR + WebGLInput.captureAllKeyboardInput = true; +#endif + } + + [MonoPInvokeCallback(typeof(Action))] + 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))] + private static void OnDisconnectCallbackHandler(string key) + { + CallbackManager.InvokeCallback(key); + } + + [MonoPInvokeCallback(typeof(Action))] + private static void InvokeOnErrorInsertCoin(string error) + { + _onError?.Invoke(error); + Debug.LogException(new Exception(error)); + } + + + #endregion + } } } \ No newline at end of file