I want to avoid that a player can call server.UpdateUserInternalData very frequently. Therefore I want to save the timestamp of the last server.UpdateUserInternalData call in UserInternalData.
I just want to update "PlayerData1": newdata if the last server.UpdateUserInternalData call was at least 4000 milliseconds before the current time(currenttimeinmilliseconds). But I don't know what I should do if the last update call was not at least 4000 milliseconds before the current time(currenttimeinmilliseconds).
Is it possible to wait in Function1 until ((Number(playertimestamp) + 4000) < Number(currenttimeinmilliseconds)) is true and then continue normally with updating "PlayerData1": newdata ? I don't want that the line return issuccessful; is executed before "PlayerData1": newdata is executed.
How can I do that? How can I wait in Function1?
function Function1(string newdata)
{
var issuccessful = "";
var playertimestamp = GetTimestampInternalData();
var currenttimeinmilliseconds = getServerTimestamp();
if ((Number(playertimestamp) + 4000) < Number(currenttimeinmilliseconds))
{
server.UpdateUserInternalData({
PlayFabId: currentPlayerId,
Data: {
"PlayerData1": newdata
},
Permission: UserDataPermission.Private
});
issuccessful = true;
var timestampinmilliseconds = getServerTimestamp();
CreateTimestampInternalData(timestampinmilliseconds);
}
return issuccessful;
}
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();
return time;
}
EDIT: The function sleep(milliseconds) in this article(Bringing Sleep to Native JavaScript) works for me if I just want to wait a few seconds before the next line of code is executed: https://www.sitepoint.com/delay-sleep-pause-wait/