-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #81 from asadm/MockMode-V2
MockMode V2
- Loading branch information
Showing
9 changed files
with
234 additions
and
146 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
using System; | ||
using System.Runtime.InteropServices; | ||
|
||
|
||
namespace Playroom | ||
{ | ||
public partial class PlayroomKit | ||
{ | ||
[DllImport("__Internal")] | ||
private static extern void InsertCoinInternal(string options, | ||
Action<string> onLaunchCallback, | ||
Action<string> onQuitInternalCallback, | ||
Action<string> onDisconnectCallback, | ||
Action<string> onError, | ||
string onLaunchCallBackKey, | ||
string onDisconnectCallBackKey); | ||
|
||
[DllImport("__Internal")] | ||
private static extern string OnPlayerJoinInternal(Action<string> callback); | ||
|
||
[DllImport("__Internal")] | ||
private static extern void UnsubscribeOnPlayerJoinInternal(string id); | ||
|
||
[DllImport("__Internal")] | ||
private static extern bool IsHostInternal(); | ||
|
||
[DllImport("__Internal")] | ||
private static extern bool IsStreamModeInternal(); | ||
|
||
[DllImport("__Internal")] | ||
private static extern string MyPlayerInternal(); | ||
|
||
[DllImport("__Internal")] | ||
public static extern string GetRoomCode(); | ||
|
||
[DllImport("__Internal")] | ||
private static extern void OnDisconnectInternal(Action<string> callback); | ||
|
||
[DllImport("__Internal")] | ||
private static extern void SetStateString(string key, string value, bool reliable = false); | ||
|
||
[DllImport("__Internal")] | ||
private static extern void SetStateInternal(string key, int value, bool reliable = false); | ||
|
||
[DllImport("__Internal")] | ||
private static extern void SetStateInternal(string key, bool value, bool reliable = false); | ||
|
||
[DllImport("__Internal")] | ||
private static extern void SetStateFloatInternal(string key, string floatAsString, bool reliable = false); | ||
|
||
[DllImport("__Internal")] | ||
private static extern void SetStateDictionary(string key, string jsonValues, bool reliable = false); | ||
|
||
[DllImport("__Internal")] | ||
private static extern string GetStateStringInternal(string key); | ||
|
||
[DllImport("__Internal")] | ||
private static extern int GetStateIntInternal(string key); | ||
|
||
[DllImport("__Internal")] | ||
private static extern float GetStateFloatInternal(string key); | ||
|
||
[DllImport("__Internal")] | ||
private static extern string GetStateDictionaryInternal(string key); | ||
|
||
[DllImport("__Internal")] | ||
private static extern void WaitForStateInternal(string stateKey, Action<string, string> onStateSetCallback); | ||
|
||
[DllImport("__Internal")] | ||
private static extern void WaitForPlayerStateInternal(string playerID, string StateKey, Action onStateSetCallback); | ||
|
||
[DllImport("__Internal")] | ||
private static extern void ResetStatesInternal(string keysToExclude = null, Action OnStatesReset = null); | ||
|
||
[DllImport("__Internal")] | ||
private static extern void ResetPlayersStatesInternal(string keysToExclude, Action OnPlayersStatesReset = null); | ||
|
||
[DllImport("__Internal")] | ||
private static extern void UnsubscribeOnQuitInternal(); | ||
|
||
[DllImport("__Internal")] | ||
private static extern void CreateJoystickInternal(string joyStickOptionsJson); | ||
|
||
[DllImport("__Internal")] | ||
private static extern string DpadJoystickInternal(); | ||
|
||
[DllImport("__Internal")] | ||
private static extern void StartMatchmakingInternal(Action callback); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
using System; | ||
using System.Buffers.Text; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using Unity.VisualScripting; | ||
using UnityEngine; | ||
|
||
namespace Playroom | ||
{ | ||
public partial class PlayroomKit | ||
{ | ||
private const string PlayerId = "mockplayerID123"; | ||
private static bool mockIsStreamMode; | ||
|
||
///<summary> | ||
/// This is private, instead of public, to prevent tampering in Mock Mode. | ||
/// Reason: In Mock Mode, only a single player can be tested. | ||
/// Ref: https://docs.joinplayroom.com/usage/unity#mock-mode | ||
///</summary> | ||
private static Dictionary<string, object> MockDictionary = new(); | ||
|
||
|
||
|
||
|
||
private static void MockInsertCoin(InitOptions options, Action onLaunchCallBack) | ||
{ | ||
isPlayRoomInitialized = true; | ||
|
||
Debug.Log("Coin Inserted"); | ||
|
||
string optionsJson = null; | ||
if (options != null) optionsJson = SerializeInitOptions(options); | ||
onLaunchCallBack?.Invoke(); | ||
} | ||
|
||
private static void MockSetState(string key, object value) | ||
{ | ||
if (MockDictionary.ContainsKey(key)) | ||
MockDictionary[key] = value; | ||
else | ||
MockDictionary.Add(key, value); | ||
} | ||
|
||
private static T MockGetState<T>(string key) | ||
{ | ||
if (MockDictionary.TryGetValue(key, out var value) && value is T typedValue) | ||
{ | ||
return typedValue; | ||
} | ||
else | ||
{ | ||
Debug.LogWarning($"No {key} in States or value is not of type {typeof(T)}"); | ||
return default; | ||
} | ||
} | ||
|
||
private static Dictionary<string, (Action<string, string> callback, string response)> mockRegisterCallbacks = new(); | ||
private static Dictionary<string, Action> mockResponseCallbacks = new(); | ||
|
||
private static void MockRpcRegister(string name, Action<string, string> rpcRegisterCallback, string onResponseReturn = null) | ||
{ | ||
mockRegisterCallbacks.TryAdd(name, (rpcRegisterCallback, onResponseReturn)); | ||
} | ||
|
||
private static void MockRpcCall(string name, object data, RpcMode mode, Action callbackOnResponse) | ||
{ | ||
mockResponseCallbacks.TryAdd(name, callbackOnResponse); | ||
|
||
string stringData = Convert.ToString(data); | ||
var player = MyPlayer(); | ||
|
||
if (mockRegisterCallbacks.TryGetValue(name, out var responseHandler)) | ||
{ | ||
responseHandler.callback?.Invoke(stringData, player.id); | ||
|
||
if (!string.IsNullOrEmpty(responseHandler.response)) | ||
{ | ||
Debug.Log($"Response received: {responseHandler.response}"); | ||
} | ||
} | ||
|
||
if (mockResponseCallbacks.TryGetValue(name, out var callback)) | ||
{ | ||
callback?.Invoke(); | ||
} | ||
} | ||
|
||
} | ||
|
||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.