Skip to content

Commit

Permalink
Merge pull request #70 from asadm/startmatchmaking-fix
Browse files Browse the repository at this point in the history
StartMatchMaking() and RPC in MockMode
  • Loading branch information
momintlh authored May 15, 2024
2 parents b105cce + f6233e4 commit e84d1c3
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 96 deletions.
84 changes: 58 additions & 26 deletions Assets/PlayroomKit/PlayroomKit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -914,7 +914,7 @@ private static T MockGetState<T>(string key)
}
else
{
Debug.LogError($"No {key} in States or value is not of type {typeof(T)}");
Debug.LogWarning($"No {key} in States or value is not of type {typeof(T)}");
return default;
}
}
Expand Down Expand Up @@ -1052,8 +1052,15 @@ public enum RpcMode

public static void RpcRegister(string name, Action<string, string> rpcRegisterCallback, string onResponseReturn = null)
{
rpcRegisterCallbacks.Add(name, rpcRegisterCallback);
RpcRegisterInternal(name, InvokeRpcRegisterCallBack, onResponseReturn);
if (IsRunningInBrowser())
{
rpcRegisterCallbacks.Add(name, rpcRegisterCallback);
RpcRegisterInternal(name, InvokeRpcRegisterCallBack, onResponseReturn);
}
else
{
Debug.LogError("[Mock Mode] RPC is currently not supported in Mock Mode!\n Please build the project to test RPC.");
}
}

[MonoPInvokeCallback(typeof(Action<string, string>))]
Expand Down Expand Up @@ -1106,35 +1113,40 @@ private static void InvokeRpcRegisterCallBack(string dataJson, string senderJson
public static void RpcCall(string name, object data, RpcMode mode, Action callbackOnResponse)
{

string jsonData = ConvertToJson(data);

if (OnResponseCallbacks.ContainsKey(name))
{
OnResponseCallbacks[name].Add(callbackOnResponse);
}
else
if (IsRunningInBrowser())
{
OnResponseCallbacks.Add(name, new List<Action> { callbackOnResponse });
if (!rpcCalledEvents.Contains(name))
string jsonData = ConvertToJson(data);
if (OnResponseCallbacks.ContainsKey(name))
{
rpcCalledEvents.Add(name);
OnResponseCallbacks[name].Add(callbackOnResponse);
}
}

else
{
OnResponseCallbacks.Add(name, new List<Action> { callbackOnResponse });
if (!rpcCalledEvents.Contains(name))
{
rpcCalledEvents.Add(name);
}
}
JSONArray jsonArray = new JSONArray();
foreach (string item in rpcCalledEvents)
{
jsonArray.Add(item);
}
string jsonString = jsonArray.ToString();
/*
This is requrired to sync the rpc events between all players, without this players won't know which event has been called.
this is a temporary fix, RPC's need to be handled within JS for better control.
*/
SetState("rpcCalledEventName", jsonString, reliable: true);

JSONArray jsonArray = new JSONArray();
foreach (string item in rpcCalledEvents)
RpcCallInternal(name, jsonData, mode, InvokeOnResponseCallback);
}
else
{
jsonArray.Add(item);
Debug.LogError("[Mock Mode] RPC Calls are not supported in Mock Mode! yet.\nPlease make a build to test RPC.");
}
string jsonString = jsonArray.ToString();
/*
This is requrired to sync the rpc events between all players, without this players won't know which event has been called.
this is a temporary fix, RPC's need to be handled within JS for better control.
*/
SetState("rpcCalledEventName", jsonString, reliable: true);

RpcCallInternal(name, jsonData, mode, InvokeOnResponseCallback);
}

// Default Mode
Expand Down Expand Up @@ -1236,7 +1248,27 @@ private static string ConvertComplexToJson(object data)
}

[DllImport("__Internal")]
public static extern void StartMatchmaking();
public static extern void StartMatchmakingInternal(Action callback);

static Action startMatchmakingCallback = null;
public static void StartMatchmaking(Action callback = null)
{
if (IsRunningInBrowser())
{
startMatchmakingCallback = callback;
StartMatchmakingInternal(InvokeStartMatchmakingCallback);
}
else
{
Debug.LogError("[Mock Mode] Matchmaking is not supported in Mock Mode! yet.\nPlease build the project to test Matchmaking.");
}
}

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

// Player class
public class Player
Expand Down
Loading

0 comments on commit e84d1c3

Please sign in to comment.