Skip to content

Commit

Permalink
Merge pull request #24 from asadm/New-Update
Browse files Browse the repository at this point in the history
API Update
  • Loading branch information
asadm authored Jan 13, 2024
2 parents 9b36800 + 1771204 commit a96b4a9
Show file tree
Hide file tree
Showing 7 changed files with 415 additions and 214 deletions.
154 changes: 125 additions & 29 deletions Assets/PlayroomKit/PlayroomKit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,15 @@
using AOT;
using System;
using SimpleJSON;


using System.Xml.Xsl;

namespace Playroom
{
public class PlayroomKit
{

private static bool isPlayRoomInitialized;


/// <summary>
/// Required Mock Mode:
/// </summary>
Expand All @@ -32,11 +30,16 @@ public class InitOptions
public bool streamMode = false;
public bool allowGamepads = false;
public string baseUrl = "";
public string[] avatars = null;
public string roomCode = "";
public bool skipLobby = false;
public int reconnectGracePeriod = 0;
public int? maxPlayersPerRoom;
}

private static Action InsertCoinCallback = null;

[DllImport("__Internal")]
[DllImport("__Internal")]
private static extern void InsertCoinInternal(Action callback, string options, Action<string> onPlayerJoinInternalCallback, Action<string> onQuitInternalCallback);

[MonoPInvokeCallback(typeof(Action))]
Expand All @@ -53,7 +56,8 @@ public static void InsertCoin(Action callback, InitOptions options = null)
isPlayRoomInitialized = true;
InsertCoinCallback = callback;
string optionsJson = null;
if (options != null) optionsJson = SerializeInitOptions(options);
if (options != null) { optionsJson = SerializeInitOptions(options); }
Debug.Log("C# " + optionsJson);
InsertCoinInternal(InvokeInsertCoin, optionsJson, __OnPlayerJoinCallbackHandler, __OnQuitInternalHandler);
}
else
Expand All @@ -62,7 +66,11 @@ public static void InsertCoin(Action callback, InitOptions options = null)

Debug.Log("Coin Inserted");

if (options != null && options.streamMode) mockIsStreamMode = options.streamMode;
// if (options != null && options.streamMode) mockIsStreamMode = options.streamMode;
string optionsJson = null;
if (options != null) optionsJson = SerializeInitOptions(options);

// Debug.Log(optionsJson);

callback?.Invoke();
}
Expand All @@ -72,7 +80,33 @@ private static string SerializeInitOptions(InitOptions options)
{
if (options == null) return null;

return JsonUtility.ToJson(options);
JSONNode node = JSON.Parse("{}");

node["streamMode"] = options.streamMode;
node["allowGamepads"] = options.allowGamepads;
node["baseUrl"] = options.baseUrl;

if (options.avatars != null)
{
JSONArray avatarsArray = new JSONArray();
foreach (string avatar in options.avatars)
{
avatarsArray.Add(avatar);
}
node["avatars"] = avatarsArray;
}

node["roomCode"] = options.roomCode;
node["skipLobby"] = options.skipLobby;
node["reconnectGracePeriod"] = options.reconnectGracePeriod;

// Check if maxPlayersPerRoom is provided, otherwise omit the property
if (options.maxPlayersPerRoom.HasValue)
{
node["maxPlayersPerRoom"] = options.maxPlayersPerRoom.Value;
}

return node.ToString();
}

// [DllImport("__Internal")]
Expand All @@ -88,7 +122,7 @@ private static void __OnPlayerJoinCallbackHandler(string id)
OnPlayerJoinWrapperCallback(id);
}


private static void OnPlayerJoinWrapperCallback(string id)
{
var player = GetPlayer(id);
Expand Down Expand Up @@ -235,6 +269,15 @@ public static Player Me()
return MyPlayer();
}

[DllImport("__Internal")]
public static extern string GetRoomCode();

[MonoPInvokeCallback(typeof(Action))]
public static void OnDisconnect(Action callback)
{
callback.Invoke();
}


[DllImport("__Internal")]
private static extern void SetStateString(string key, string value, bool reliable = false);
Expand Down Expand Up @@ -548,6 +591,28 @@ public static T GetState<T>(string key)
}
}

[DllImport("__Internal")]
private static extern void WaitForStateInternal(string stateKey, Action onStateSetCallback = null);


private static Action OnStateChangeCallBack = null;

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

public static void WaitForState(string stateKey, Action onStateSetCallback = null)
{
if (IsRunningInBrowser())
{
OnStateChangeCallBack = onStateSetCallback;
WaitForStateInternal(stateKey, InvokeCallback);
}
}



[DllImport("__Internal")]
private static extern string GetStateDictionaryInternal(string key);
Expand Down Expand Up @@ -651,7 +716,8 @@ private static T MockGetState<T>(string key)
}

[MonoPInvokeCallback(typeof(Action<string>))]
private static void __OnQuitInternalHandler(string playerId) {
private static void __OnQuitInternalHandler(string playerId)
{
if (Players.TryGetValue(playerId, out Player player))
{
player.OnQuitWrapperCallback();
Expand All @@ -661,8 +727,8 @@ private static void __OnQuitInternalHandler(string playerId) {
Debug.LogError("[__OnQuitInternalHandler] Couldn't find player with id " + playerId);
}
}


// Joystick
[DllImport("__Internal")]
private static extern void CreateJoystickInternal(string joyStickOptionsJson);
Expand Down Expand Up @@ -723,8 +789,8 @@ private static JSONNode ConvertButtonOptionsToJson(ButtonOptions button)
buttonJson["icon"] = button.icon;
return buttonJson;
}


public class JoystickOptions
{
public string type = "angular"; // default = angular, can be dpad
Expand All @@ -739,28 +805,27 @@ public class ButtonOptions
public string id = null;
public string label = "";
public string icon = null;
}
}

public class ZoneOptions
{
public ButtonOptions up = null;
public ButtonOptions down = null;
public ButtonOptions left = null;
public ButtonOptions right = null;
}
}


[System.Serializable]
public class Dpad
{
public string x;
public string y;
}

// Player class
public class Player
{


[Serializable]
public class Profile
Expand All @@ -771,7 +836,7 @@ public class Profile
public JsonColor jsonColor;
public string name;
public string photo;

[Serializable]
public class JsonColor
{
Expand All @@ -781,9 +846,9 @@ public class JsonColor
public string hexString;
public int hex;
}



}


Expand Down Expand Up @@ -811,7 +876,7 @@ public Player(string id)

private List<Action<string>> OnQuitCallbacks = new();


private void OnQuitDefaultCallback()
{
if (!isPlayRoomInitialized)
Expand Down Expand Up @@ -1160,6 +1225,22 @@ public Dictionary<string, float> GetStateFloat(string id, string key)
return ParseJsonToDictionary<float>(jsonString);
}

public void WaitForState(string StateKey, Action onStateSetCallback = null)
{
if (IsRunningInBrowser())
{
WaitForPlayerStateInternal(id, StateKey, onStateSetCallback);
}
}

public void Kick(Action OnKickCallBack = null)
{
if (IsRunningInBrowser())
{
OnKickCallBack = onKickCallBack;
KickInternal(id, InvokeKickCallBack);
}
}

[DllImport("__Internal")]
private static extern void SetPlayerStateByPlayerId(string playerID, string key, int value,
Expand Down Expand Up @@ -1200,7 +1281,7 @@ private static Profile ParseProfile(string json)

return profileData;
}

public Profile GetProfile()
{
if (IsRunningInBrowser())
Expand Down Expand Up @@ -1232,14 +1313,29 @@ public Profile GetProfile()
name = "CoolPlayTest",
jsonColor = mockJsonColor,
photo = "testPhoto"

};
return testProfile;
}
}
}


private static Action onKickCallBack = null;

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

[DllImport("__Internal")]
private static extern void KickInternal(string playerID, Action onKickCallBack = null);

[DllImport("__Internal")]
private static extern void WaitForPlayerStateInternal(string playerID, string stateKey, Action onStateSetCallback = null);


[DllImport("__Internal")]
private static extern int GetPlayerStateIntById(string playerID, string key);

Expand Down Expand Up @@ -1278,5 +1374,5 @@ private void SetStateHelper<T>(string id, string key, Dictionary<string, T> valu
}
}


}
Loading

0 comments on commit a96b4a9

Please sign in to comment.