question

Kim Strasser avatar image
Kim Strasser asked

Is it possible to wait a few milliseconds or seconds in a cloud script function?

I want to avoid that a player can call server.UpdateUserInternalData and server.GetUserReadOnlyData very frequently. Therefore I want to save the timestamp of the last server.UpdateUserInternalData call in UserInternalData and the timestamp of the last server.GetUserReadOnlyData call in UserReadOnlyData.

For example, the player calls the cloud script function Function1 that uses server.UpdateUserInternalData API. I just want to update "PlayerData1": newdata if the last server.UpdateUserInternalData call was at least 4 seconds before the current time(currenttimeinseconds). But I don't know what I should do if the last update call was not at least 4 seconds before the current time(currenttimeinseconds).

Is it possible to wait in Function1 until ((Number(playertimestamp) + 4) < Number(currenttimeinseconds)) is true and then continue normally with updating "PlayerData1": newdata ? How can I do that? How can I wait in Function1?

Or is it better to quit Function1 without updating anything and then the client needs to call ExecuteCloudScript(Function1) again so that "PlayerData1": newdata can maybe be updated?

function Function1(string newdata)
{
    var playertimestamp = GetTimestampInternalData();
    var currenttimeinseconds = getServerTimestamp();

    if ((Number(playertimestamp) + 4) < Number(currenttimeinseconds))
    {
        server.UpdateUserInternalData({
           PlayFabId: currentPlayerId,
           Data: {
               "PlayerData1": newdata
           },
           Permission: UserDataPermission.Private
        });

        var timestampinseconds = getServerTimestamp();      
        CreateTimestampInternalData(timestampinseconds);
    }
    else
    {
        // Is it possible to wait here until ((Number(playertimestamp) + 4) < Number(currenttimeinseconds)) is true and then continue normally with updating "PlayerData1": newdata ?

    }
}

function GetTimestampInternalData()
{
     var resultdata = server.GetUserInternalData({PlayFabId: currentPlayerId, Keys: "InternalDataTimestamp"});
     var currenttimestamp = "";
     if ((resultdata.Data.hasOwnProperty("InternalDataTimestamp")) && (resultdata.Data.InternalDataTimestamp.Value != null) && (resultdata.Data.InternalDataTimestamp.Value != ""))
        currenttimestamp = resultdata.Data.InternalDataTimestamp.Value;
     else
       currenttimestamp = 0;
   
   return currenttimestamp;
}

function CreateTimestampInternalData(currenttime)
{
    server.UpdateUserInternalData({
           PlayFabId: currentPlayerId,
           Data: {
               "InternalDataTimestamp": currenttime.toString()
           },
           Permission: UserDataPermission.Private
        });   
}

function getServerTimestamp()
{
    var now = new Date();
    var time = now.getTime();
    time = Math.floor(time / 1000);
    return time;
}
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

·
David Coombes avatar image
David Coombes answered

Surely you can solve this by including a wait statement of some sort? In C#, you can use an IEnumerable instead of a function and wait for four seconds. Googlage suggestsyou could use a ScheduledExecutorService to launch some code after 4 seconds.


Basically, I think the solution lies in the language you are using rather than PlayFab. Heck, from the sounds of it you could just record on the client how long it's been since the call was made and not allow the client to call that function until 4 seconds has elapsed (grey out the button or however it is the player is making the call).

3 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 ·

I use this code to wait 1 second in my cloud script function:

handlers.UpdateLeaderboard = function (args, context)
{
    var waitingtime = 1000; // 1000 milliseconds
    sleep(waitingtime);
    server.UpdateUserReadOnlyData
    ...
}

function sleep(milliseconds)
{
    const date = Date.now();
    let currentDate = null;
    do {
    currentDate = Date.now();
    } while (currentDate - date < milliseconds);
}
0 Likes 0 ·
Sarah Zhang avatar image Sarah Zhang Kim Strasser commented ·

>> Is it possible to wait in Function1 until ((Number(playertimestamp) + 4) < Number(currenttimeinseconds)) is true and then continue normally with updating "PlayerData1": newdata ? How can I do that? How can I wait in Function1? Or is it better to quit Function1 without updating anything and then the client needs to call ExecuteCloudScript(Function1) again so that "PlayerData1": newdata can maybe be updated?

As your code shows, it’s possible to “wait in Function1 until ((Number(playertimestamp) + 4) < Number(currenttimeinseconds)) is true”, but PlayFab has the limits of Cloud Script execution time. Generally, for a free tier, this limit is 4.5 seconds, you can navigate to https://developer.playfab.com/en-US/[YourTitleId]/limits to check the limit of your title. So it’s unrealistic to let the CloudScript function “sleep” 4 seconds in the process of execution, such operations may exceed the PlayFab limits. It’s better to “quit Function1 without updating anything”, just do the judgment if the difference of time is less than 4 seconds, then quit the function. You can add the corresponding automatic retry mechanism and tips in clients’ code to control the frequency.

0 Likes 0 ·
Kim Strasser avatar image Kim Strasser Sarah Zhang commented ·

I will try to create an automatic retry mechanism in the client code, because I don't want that the player always needs to tap a button in my game if the cloud script code couldn't get executed.

0 Likes 0 ·

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.