question

Kim Strasser avatar image
Kim Strasser asked

CloudScript: How can I find out if a certain PlayFabId is stored in ReadOnlyData and remove it?

In my game, a player can block other players and the PlayFabIds of the blocked players are stored in his ReadOnlyData. The player can also unblock blocked players if he wants. But I don't know how to remove a certain PlayFabId from ReadOnlyData because it is one big string.

For example, how can I remove the PlayFabId 57440D2996790D06 from this ReadOnlyData key/value pair in CloudScript?

Key:

BlockedPlayers

Value(blocked PlayFabIds):

57440D2996790D06,88440D2996790D06,99440D2996790D06

I get the key/value pair like this in CloudScript:

var friendid = args.FriendId; // it is: 57440D2996790D06
var key = "BlockedPlayers";
var blockedplayers = server.GetUserReadOnlyData({PlayFabId: currentPlayerId, Keys: "BlockedPlayers"});
if (blockedplayers.Error == null)
{
  if ((blockedplayers.Data.hasOwnProperty(key)) && (blockedplayers.Data[key].Value != "") && (blockedplayers.Data[key].Value != null))
  {   
    var jsondata = blockedplayers.Data[key].Value;

CloudScript
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Made Wang avatar image
Made Wang answered

You need to split this string, delete the specified PlayFabId, and then generate a new string with the remaining contents to update ReadOnlyData. Refer to the code below.

handlers.handleBlockedPlayers=function(args,context){
    var unBlockedPlayFabId = args.unBlockedPlayFabId;
    var key="BlockedPlayers";
    var blockedplayers=server.GetUserReadOnlyData(
        {
            PlayFabId: currentPlayerId,
            Keys: key
        });
    if(blockedplayers.Error==null)
    {
        if ((blockedplayers.Data.hasOwnProperty(key)) && (blockedplayers.Data[key].Value != "") && (blockedplayers.Data[key].Value != null))
        {
            var players=blockedplayers.Data[key].Value.split(",");
            if(players.includes(unBlockedPlayFabId))
            {
                var index=players.indexOf(unBlockedPlayFabId);
                players.splice(index,1);
                var newPlayers=players.toString();
                server.UpdateUserReadOnlyData({
                    PlayFabId: currentPlayerId,
                    Data: {
                        [key]: newPlayers
                    }
                });
            }
        }
    }
}
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.