Skip to content

Commit

Permalink
Merge pull request #81 from asadm/MockMode-V2
Browse files Browse the repository at this point in the history
MockMode V2
  • Loading branch information
momintlh authored Jun 13, 2024
2 parents 38b3130 + 0fd6a3f commit 7ef002b
Show file tree
Hide file tree
Showing 9 changed files with 234 additions and 146 deletions.
90 changes: 90 additions & 0 deletions Assets/PlayroomKit/Headers.cs
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);
}
}
11 changes: 11 additions & 0 deletions Assets/PlayroomKit/Headers.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

90 changes: 90 additions & 0 deletions Assets/PlayroomKit/MockMode.cs
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();
}
}

}

}
11 changes: 11 additions & 0 deletions Assets/PlayroomKit/MockMode.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 7ef002b

Please sign in to comment.