Skip to content

Commit

Permalink
fix: persistent mode to true if turnbased is true
Browse files Browse the repository at this point in the history
  • Loading branch information
momintlh committed Feb 4, 2025
1 parent 25f49bb commit 53fd859
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 30 deletions.
6 changes: 2 additions & 4 deletions Assets/PlayroomKit/PlayroomKit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,12 @@ public void InsertCoin(InitOptions options = null, Action onLaunchCallBack = nul
_playroomService.InsertCoin(options, onLaunchCallBack, onDisconnectCallback);
}


public void OnPlayerJoin(Action<Player> onPlayerJoinCallback)
{
CheckPlayRoomInitialized();
_playroomService.OnPlayerJoin(onPlayerJoinCallback);
}


public bool IsHost()
{
CheckPlayRoomInitialized();
Expand Down Expand Up @@ -322,10 +320,10 @@ public void SaveMyTurnData(object data)
_playroomService.SaveMyTurnData(data);
}

public string GetMyTurnData()
public void GetMyTurnData(Action<string> callback)
{
CheckPlayRoomInitialized();
return _playroomService.GetMyTurnData();
_playroomService.GetMyTurnData(callback);
}

public void GetAllTurns(Action<string> callback)
Expand Down
1 change: 0 additions & 1 deletion Assets/PlayroomKit/modules/Helpers/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ public static string SerializeInitOptions(InitOptions options)

node["avatars"] = avatarsArray;
}


if (options.matchmaking is bool booleanMatchmaking)
{
Expand Down
12 changes: 7 additions & 5 deletions Assets/PlayroomKit/modules/Interfaces/IPlayroomBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,17 @@ public void InsertCoin(InitOptions options = null, Action onLaunchCallBack = nul
public void UnsubscribeOnQuit();

#region TurnBased

public string GetChallengeId();

public void SaveMyTurnData(object data);
public string GetMyTurnData();

public void GetMyTurnData(Action<string> callback);

public void GetAllTurns(Action<string> callback);

public void ClearTurns(Action callback);

#endregion


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,12 +207,24 @@ public string GetChallengeId()

public void SaveMyTurnData(object data)
{
_ubb.CallJs("SaveMyTurnData", null, null, true, JsonUtility.ToJson(data));
string jsonData;

if (data is int || data is string || data is float)
{
jsonData = JSONNode.Parse(data.ToString()).ToString();
}
else
{
jsonData = JsonUtility.ToJson(data);
}

_ubb.CallJs("SaveMyTurnData", null, null, true, jsonData);
}

public string GetMyTurnData()
public void GetMyTurnData(Action<string> callback)
{
return _ubb.CallJs<string>("GetMyTurnData", null, null, true);
string data = _ubb.CallJs<string>("GetMyTurnData", null, null, true);
callback.Invoke(data);
}

public void GetAllTurns(Action<string> callback)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,16 +285,25 @@ GetChallengeId = function () {
};

SaveMyTurnData = async function (data) {

console.warn(`saving data ${data}`)

await Playroom.saveMyTurnData(data);
};

GetAllTurns = async function () {
const data = await Playroom.getAllTurns();

console.warn(`Getting All Turns ${data}`)

return JSON.stringify(data);
};

GetMyTurnData = async function () {
const data = await Playroom.getMyTurnData();

console.warn(`Getting My Turn Data ${data}`)

return JSON.stringify(data);
};

Expand Down
3 changes: 1 addition & 2 deletions Assets/PlayroomKit/modules/MockMode/LocalPlayroomService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,9 @@ public void SaveMyTurnData(object data)
DebugLogger.LogWarning("[MockMode] Turn based API is currently not supported in local mode!");
}

public string GetMyTurnData()
public void GetMyTurnData(Action<string> callback)
{
DebugLogger.LogWarning("[MockMode] Turn based API is currently not supported in local mode!");
return default;
}

public void GetAllTurns(Action<string> callback)
Expand Down
29 changes: 20 additions & 9 deletions Assets/PlayroomKit/modules/PlayroomBuildService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using SimpleJSON;
using System;
using System.Collections.Generic;
using OpenQA.Selenium.DevTools.V96.Browser;

namespace Playroom
{
Expand All @@ -20,6 +21,7 @@ public PlayroomBuildService()
{
_interop = new PlayroomKitInterop();
}

public PlayroomBuildService(IInterop interop)
{
_interop = interop;
Expand All @@ -46,6 +48,19 @@ public void InsertCoin(InitOptions options = null, Action onLaunchCallBack = nul
#endif
}

// UNTESTED:
if (options.turnBased is true)
{
options.persistentMode = true;
}
else if (options.turnBased is TurnBasedOptions turnBasedOptions)
{
if (string.IsNullOrEmpty(turnBasedOptions.challengeId))
{
options.persistentMode = true;
}
}

_interop.InsertCoinWrapper(
optionsJson, InvokeInsertCoin, IPlayroomBase.__OnQuitInternalHandler, OnDisconnectCallbackHandler,
InvokeOnErrorInsertCoin, onLaunchCallBackKey, onDisconnectCallBackKey);
Expand Down Expand Up @@ -83,14 +98,14 @@ public void OnDisconnect(Action callback)
}

#endregion

#region Unsubscribers

public void UnsubscribeOnQuit()
{
_interop.UnsubscribeOnQuitWrapper();
}

private void UnsubscribeOnPlayerJoin(string callbackID)
{
_interop.UnsubscribeOnPlayerJoinWrapper(callbackID);
Expand Down Expand Up @@ -269,23 +284,21 @@ public void WaitForPlayerState(string playerID, string stateKey, Action<string>
WaitForPlayerCallback = onStateSetCallback;
_interop.WaitForPlayerStateWrapper(playerID, stateKey, OnStateSetCallback);
}

public void ResetStates(string[] keysToExclude = null, Action onStatesReset = null)
{
_onStatesResetCallback = onStatesReset;
string keysJson = keysToExclude != null ? Helpers.CreateJsonArray(keysToExclude).ToString() : null;
_interop.ResetStatesWrapper(keysJson, InvokeResetCallBack);
}

public void ResetPlayersStates(string[] keysToExclude = null, Action onStatesReset = null)
{
_onStatesResetCallback = onStatesReset;
string keysJson = keysToExclude != null ? Helpers.CreateJsonArray(keysToExclude).ToString() : null;
_interop.ResetPlayersStatesWrapper(keysJson, InvokePlayersResetCallBack);
}



#endregion

#region Joystick
Expand Down Expand Up @@ -339,7 +352,7 @@ public void SaveMyTurnData(object data)
throw new NotImplementedException();
}

public string GetMyTurnData()
public void GetMyTurnData(Action<string> callback)
{
throw new NotImplementedException();
}
Expand Down Expand Up @@ -404,10 +417,8 @@ private static void InvokeOnErrorInsertCoin(string error)
_onError?.Invoke(error);
Debug.LogException(new Exception(error));
}


#endregion

}
}
}
12 changes: 6 additions & 6 deletions Assets/Scripts/GameManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@ private void Start()
{
_kit.InsertCoin(new InitOptions()
{
turnBased = new TurnBasedOptions()
{
challengeId = "123"
},
turnBased = true,
persistentMode = true,
maxPlayersPerRoom = 2,
}, OnLaunchCallBack);
}
Expand Down Expand Up @@ -47,8 +45,10 @@ private void Update()

if (Input.GetKeyDown(KeyCode.M))
{
string data = _kit.GetMyTurnData();
Debug.Log($"Getting my turn data: {data}");
_kit.GetMyTurnData((data) =>
{
Debug.Log($"Getting my turn data: {data}");
});
}

if (Input.GetKeyDown(KeyCode.A))
Expand Down

0 comments on commit 53fd859

Please sign in to comment.