Skip to content

Commit

Permalink
feat: all player methods implemented.
Browse files Browse the repository at this point in the history
  • Loading branch information
momintlh committed Nov 14, 2024
1 parent 5bbcfc1 commit c55b18b
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 9 deletions.
1 change: 1 addition & 0 deletions Assets/PlayroomKit/PlayroomKit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ public void ResetStates(string[] keysToExclude = null, Action OnStatesReset = nu
public void ResetPlayersStates(string[] keysToExclude = null, Action OnStatesReset = null)
{
_playroomService.ResetPlayersStates(keysToExclude, OnStatesReset);

}

// Joystick
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,8 @@ public void CallJs(string jsFunctionName, string callbackName = null, string gam
{
List<string> allParams = new();

Debug.LogWarning(args);

foreach (var param in args)
{
if (param.StartsWith("{") && param.EndsWith("}"))
Expand Down Expand Up @@ -401,6 +403,7 @@ public T CallJs<T>(string jsFunctionName, string callbackName = null, string gam
string jsCall = $"{jsFunctionName}({string.Join(", ", allParams)})";
if (isAsync) jsCall = $"await {jsCall}";

Debug.Log(jsCall);
return ExecuteJS<T>(jsCall);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,45 @@ public void SetState(string key, string value, bool reliable = false)

public void SetState(string key, object value, bool reliable = false)
{
_ubb.CallJs("SetPlayerStateByPlayerId", null, null, false, _id, key, value.ToString(),
string json;

// Check if the value is a primitive type and wrap it if necessary
json = JsonUtility.ToJson(value is int or float or bool or string
? new PrimitiveWrapper<object>(value)
: value);

_ubb.CallJs("SetPlayerStateByPlayerId", null, null, false, _id, key, json,
reliable.ToString().ToLower());
}

public T GetState<T>(string key)
{
Debug.Log(key);
return _ubb.CallJs<T>(key);
string stringValue = _ubb.CallJs<string>("GetPlayerStateByPlayerId", null, null, false, _id, key);

if (string.IsNullOrEmpty(stringValue))
{
Debug.LogWarning($"State for key '{key}' is null or empty.");
return default;
}

try
{
if (typeof(T) == typeof(int) || typeof(T) == typeof(float) || typeof(T) == typeof(bool) ||
typeof(T) == typeof(string))
{
var wrapper = JsonUtility.FromJson<PrimitiveWrapper<T>>(stringValue);
return wrapper.value;
}
else
{
return JsonUtility.FromJson<T>(stringValue);
}
}
catch (Exception e)
{
Debug.LogError($"Failed to parse JSON for key '{key}': {e.Message}");
return default; // Return default if parsing fails.
}
}

#endregion
Expand All @@ -60,7 +91,7 @@ public PlayroomKit.Player.Profile GetProfile()
string json = _ubb.CallJs<string>("GetProfile", null, null, false, _id);

Debug.Log(json);

var profileData = Helpers.ParseProfile(json);
return profileData;
}
Expand All @@ -80,5 +111,17 @@ public void WaitForState(string stateKey, Action<string> onStateSetCallback = nu
{
_ubb.CallJs("WaitForPlayerState", null, null, true, _id, stateKey);
}


#region UTILS

[System.Serializable]
private class PrimitiveWrapper<T>
{
public T value;
public PrimitiveWrapper(T value) => this.value = value;
}

#endregion
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
unityInstance.SendMessage(gameObjectName, onLaunchCallBackName);
}

console.log(
"InsertCoin called",
options,
onLaunchCallBackName,
gameObjectName
);

console.log("InsertCoin called", options, onLaunchCallBackName, gameObjectName);

await Playroom.insertCoin(options, onLaunchCallBack);
};

Expand Down Expand Up @@ -177,7 +181,16 @@ WaitForPlayerState = async function (playerId, stateKey, onStateSetCallback) {
return null;
}

await Playroom.waitForPlayerState(playerState, stateKey, onStateSetCallback);
await Playroom.waitForPlayerState().then((stateVal) => {
// playerState, stateKey, onStateSetCallback
const data = {
key: onStateSetCallback,
parameter: stateVal,
};

const jsonData = JSON.stringify(data);
unityInstance.SendMessage("CallbackManager", "InvokeCallback", jsonData);
});
};

Kick = async function (playerID) {
Expand Down
2 changes: 0 additions & 2 deletions Assets/PlayroomKit/modules/MockMode/CallBacksManagerMock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,6 @@ public void HandleRPC(string jsonData)

public void InvokeCallback(string jsonData)
{


var jsonNode = JSON.Parse(jsonData);

string key = jsonNode["key"];
Expand Down

0 comments on commit c55b18b

Please sign in to comment.