Skip to content

Commit

Permalink
DefaultPlayerStates
Browse files Browse the repository at this point in the history
  • Loading branch information
momintlh committed Jan 18, 2024
1 parent 5dfaec4 commit 5f315f8
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 12 deletions.
30 changes: 29 additions & 1 deletion Assets/PlayroomKit/PlayroomKit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class InitOptions
public int? maxPlayersPerRoom;

public Dictionary<string, object> defaultStates = null;
public Dictionary<string, object> defaultPlayerState = null;

}

Expand Down Expand Up @@ -117,6 +118,16 @@ private static string SerializeInitOptions(InitOptions options)
node["defaultStates"] = defaultStatesObject;
}

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


return node.ToString();
}
Expand Down Expand Up @@ -742,6 +753,7 @@ private static Dictionary<string, T> ParseJsonToDictionary<T>(string jsonString)
private static extern void ResetStatesInternal(string keysToExclude = null, Action OnStatesReset = null);

private static Action onstatesReset;
private static Action onplayersStatesReset;

public static void ResetStates(string[] keysToExclude = null, Action OnStatesReset = null)
{
Expand All @@ -759,9 +771,25 @@ private static void InvokeResetCallBack()
onstatesReset?.Invoke();
}

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


[DllImport("__Internal")]
public static extern void ResetPlayersStates(string[] keysToExclude, Action OnPlayersStatesReset = null);
private static extern void ResetPlayersStatesInternal(string keysToExclude, Action OnPlayersStatesReset = null);

public static void ResetPlayersStates(string[] keysToExclude = null, Action OnStatesReset = null)
{
if (IsRunningInBrowser())
{
onstatesReset = OnStatesReset;
string keysJson = keysToExclude != null ? CreateJsonArray(keysToExclude).ToString() : null;
ResetPlayersStatesInternal(keysJson, InvokePlayersResetCallBack);
}
}

private static JSONArray CreateJsonArray(string[] array)
{
Expand Down
23 changes: 12 additions & 11 deletions Assets/Plugins/PlayroomPlugin.jslib
Original file line number Diff line number Diff line change
Expand Up @@ -744,8 +744,6 @@ mergeInto(LibraryManager.library, {
}

var keys = keysToExclude ? JSON.parse(UTF8ToString(keysToExclude)) : [];
console.log(keys);

Playroom.resetStates(keys)
.then(() => {
dynCall('v', onStatesReset, []);
Expand All @@ -756,20 +754,23 @@ mergeInto(LibraryManager.library, {
});
},



ResetPlayersStates: function (keysToExclude, onPlayersStatesReset) {
ResetPlayersStatesInternal: 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.resetPlayersStates(keysToExclude).then(() => {
dynCall('v', onPlayersStatesReset, [])
}).catch((error) => {
console.error("Error reseting players states:", error);
});
var keys = keysToExclude ? JSON.parse(UTF8ToString(keysToExclude)) : [];
Playroom.resetPlayersStates(keys)
.then(() => {
dynCall('v', onStatesReset, []);
})
.catch((error) => {
console.error("Error resetting players states:", error);
throw error;
});
},



});

0 comments on commit 5f315f8

Please sign in to comment.