Skip to content

Commit

Permalink
DefaultStates and ResetStates
Browse files Browse the repository at this point in the history
  • Loading branch information
momintlh committed Jan 18, 2024
1 parent 2a57fb7 commit c085ca6
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 11 deletions.
65 changes: 61 additions & 4 deletions Assets/PlayroomKit/PlayroomKit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ public class InitOptions
public bool skipLobby = false;
public int reconnectGracePeriod = 0;
public int? maxPlayersPerRoom;

public Dictionary<string, object> defaultStates = null;

}

private static Action InsertCoinCallback = null;
Expand All @@ -57,7 +60,6 @@ public static void InsertCoin(Action callback, InitOptions options = null)
InsertCoinCallback = callback;
string optionsJson = null;
if (options != null) { optionsJson = SerializeInitOptions(options); }
Debug.Log("C# " + optionsJson);
InsertCoinInternal(InvokeInsertCoin, optionsJson, __OnPlayerJoinCallbackHandler, __OnQuitInternalHandler);
}
else
Expand All @@ -70,7 +72,7 @@ public static void InsertCoin(Action callback, InitOptions options = null)
string optionsJson = null;
if (options != null) optionsJson = SerializeInitOptions(options);

// Debug.Log(optionsJson);
Debug.Log(optionsJson);

callback?.Invoke();
}
Expand Down Expand Up @@ -100,15 +102,50 @@ private static string SerializeInitOptions(InitOptions options)
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;
}

if (options.defaultStates != null)
{
JSONObject defaultStatesObject = new JSONObject();
foreach (var kvp in options.defaultStates)
{
defaultStatesObject[kvp.Key] = ConvertValueToJSON(kvp.Value);
}
node["defaultStates"] = defaultStatesObject;
}


return node.ToString();
}

private static JSONNode ConvertValueToJSON(object value)
{
if (value is string stringValue)
{
return stringValue;
}
else if (value is int intValue)
{
return intValue;
}
else if (value is float floatValue)
{
return floatValue;
}
else if (value is bool boolValue)
{
return boolValue;
}
else
{
// Handle other types if needed
return JSON.Parse("{}");
}
}

// [DllImport("__Internal")]
// private static extern void OnPlayerJoinInternal(Action<string> callback);

Expand Down Expand Up @@ -702,7 +739,27 @@ private static Dictionary<string, T> ParseJsonToDictionary<T>(string jsonString)
}

[DllImport("__Internal")]
public static extern void ResetStates(string[] keysToExclude = null, Action OnStatesReset = null);
private static extern void ResetStatesInternal(string keysToExclude = null, Action OnStatesReset = null);

private static Action onstatesReset;

public static void ResetStates(string[] keysToExclude = null, Action OnStatesReset = null)
{
if (IsRunningInBrowser())
{
onstatesReset = OnStatesReset;
string keysJson = keysToExclude != null ? JSONArray.Create(keysToExclude).ToString() : null;

ResetStatesInternal(keysJson, InvokeResetCallBack);
}
}

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


[DllImport("__Internal")]
public static extern void ResetPlayersStates(string[] keysToExclude, Action OnPlayersStatesReset = null);
Expand Down
21 changes: 14 additions & 7 deletions Assets/Plugins/PlayroomPlugin.jslib
Original file line number Diff line number Diff line change
Expand Up @@ -737,20 +737,27 @@ mergeInto(LibraryManager.library, {

},

ResetStates: function (keysToExclude, onStatesReset) {
ResetStatesInternal: function (keysToExclude, onStatesReset) {
if (!window.Playroom) {
console.error("Playroom library is not loaded. Please make sure to call InsertCoin first.");
reject("Playroom library not loaded");
return;
}

Playroom.resetStates(keysToExclude).then(() => {
dynCall('v', onStatesReset, [])
}).catch((error) => {
console.error("Error reseting states:", error);
});
keysToExclude = Array.isArray(keysToExclude) ? keysToExclude : [];
console.log(keysToExclude);

Playroom.resetStates(keysToExclude)
.then(() => {
dynCall('v', onStatesReset, []);
})
.catch((error) => {
console.error("Error resetting states:", error);
throw error;
});
},



ResetPlayersStates: function (keysToExclude, onPlayersStatesReset) {
if (!window.Playroom) {
console.error("Playroom library is not loaded. Please make sure to call InsertCoin first.");
Expand Down

0 comments on commit c085ca6

Please sign in to comment.