question

Kim Strasser avatar image
Kim Strasser asked

CloudScript: How can I compare the time between two calls?

I want to make sure that there are at least 50 seconds between the two calls. If not, then the player is cheating because it's not possible to finish a level 50 seconds after another level was finished. I always call UpdatePlayerStatistics after the player finished a level.

How can I compare the time between the player's last UpdatePlayerStatistics call and his current UpdatePlayerStatistics call?

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

·
Seth Du avatar image
Seth Du answered

To help you implement anti-cheating function, you may need to add verification procedure on the Cloud Script.

In terms of the verification, you may need another Statistic/Player internal Data/Player Read-Only Data for recording a timestamp(statistic updates fast and is able to be used for timestamp). When the Cloud Script function is executed, you may create a new timestamp via server time, then compare the current timestamp with stored timestamp. Followings are the rest of things you implement previously in the Cloud Script.

6 comments
10 |1200

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

Kim Strasser avatar image Kim Strasser commented ·

Do you mean this timestamp?

"Timestamp": "2019-12-09T13:56:49.2536994Z"

Is it possible to save "Timestamp" in Player internal Data when I update the leaderboard?

Or is it better to save now.getTime() instead of "Timestamp" in Player internal Data when the player calls UpdatePlayerStatistics?

var now = new Date();
var time = now.getTime();
function UpdatePlayerScore(leaderboardname, score)
{
  var result = server.UpdatePlayerStatistics({
           PlayFabId: currentPlayerId,
           Statistics : [{ StatisticName : leaderboardname, Value : score }]
        });
        
  if (result.Error == null)
      return { leaderboardupdated: true };
  else
      return { leaderboardupdated: false };
}
{
    "EventName": "player_executed_cloudscript",
    "Source": "CloudScript",
    "FunctionName": "UpdateLeaderboard",
    "CloudScriptExecutionResult": {
        "FunctionName": "UpdateLeaderboard",
        ...
    },
    "EventNamespace": "com.playfab",
    "EntityType": "player",
    "TitleId": "BFD0A",
    "EntityId": "B735784F8BE943A9",
    "EventId": "230f3f3fc4a94840990cd79203eb4f14",
    "SourceType": "BackEnd",
    "Timestamp": "2019-12-09T13:56:49.2536994Z",
    ...
    }
},
0 Likes 0 ·
Seth Du avatar image Seth Du ♦ Kim Strasser commented ·

Both methods should work fine since the difference should be the time of Cloud Script execution.(The event is generated after the function execution is done.)

0 Likes 0 ·
Kim Strasser avatar image Kim Strasser Seth Du ♦ commented ·

How can I get this timestamp in my CloudScript code?

"Timestamp": "2019-12-09T13:56:49.2536994Z"
if (result.Error == null)
{
// var newtimestamp = ...?
    return { leaderboardupdated: true };
}
else
    return { leaderboardupdated: false };
0 Likes 0 ·
Show more comments
Show more comments

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.