question

Matt avatar image
Matt asked

Best way to get server time from CloudScripts

This is sort of a compound question, mostly because I'm very new to JavaScript.

So from my C# game code (using Unity), I communicate with PlayFab through CloudScripting only. I figured having the control to do what I need and still be a single web call was ideal. I don't know if that's bad practice or not, and it definitely makes my job a bit harder (since I'll need to do more JS code). Each of my calls in game code to the CloudScript expect a return of my current currency values, future leaderboard updates, and anything else specific to each call. These are data classes I will be deserializing via JSON.

I'd like to store a timestamp in the game code that I set in each return to make sure I don't update any values to an older response. I just want to make sure everything is up to date. Is that necessary/common? I was looking for a way to get the server time back from my ExecuteCloudScript call. I heard that each Playfab call returns the time (this was maybe added after some requests) but I don't think that's the case with Cloud Script calls?

I hate to be a true novice here, but I also noticed in the docs that the server API included a GetTime call. I added

var now = server.GetTime();

into one of my Cloud Script calls but it errors out ("The best overloaded method match for 'PlayFab.LogicServer.Source.PlayFabAPI.api_request(string, string)' has some invalid arguments"). I just opened the JavaScript file in my Visual Studio Unity C# project...so I get no intellisense and don't really know what functionality is even available to me. In fact, when I get to all the string parsing I'm sure it'll be a headache.

Anyway, any guidance on this would be extremely helpful. Once I get my bearings I can figure it all out from there, but I feel like these few problems have layered into a confusing mess for me.

Thanks for the help!

-Matt

10 |1200

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

brendan avatar image
brendan answered

All the Client API calls are designed to provide secure ways to interact with the data for your players, so I would recommend using them as much as possible, rather than attempting to work that into Cloud Script calls. If nothing else, the time limit on execution of a Cloud Script could mean that you wind up in a situation where you're losing part of that logic because it's trying to do too much - Cloud Script is mainly intended for relatively lightweight operations.

It's not clear why your time check is necessary, though. You said it's to prevent overwriting data with older data, but the only way that would ever be an issue is if you're issuing multiple writes to the same Key at the same time, which should never be the case. Can you describe in more detail the game flow and how it should be using data?

1 comment
10 |1200

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

Matt avatar image Matt commented ·

Hm ok, that makes sense as far as the usage goes. I have a function in my cloud script, for instance, that I call when the player beats a level. It gives the player some points (UserData), some coins/stars (VC), and eventually some statistics and anything else I need to save. I didn't want to send multiple calls, especially because if one fails I pop-up an error and I feel like it'd get messy if it was more than 1 call active at a time.

The scenario that I figured I may need a timestamp for would be if the player purchased, for instance, an upgrade to his ammo in rapid succession. I don't want to force the player to wait a few moments while I wait for the call to the server to return...I have the values locally so I'd like to present the flow as if it's all saved locally and then behind the scenes the values are sent to the server. If the player purchases twice, I don't want to get the second call before the first and refresh the values incorrectly. Is that not a potential risk? Will I always get calls back in order through PlayFab?

Thanks Brendan!

-Matt

0 Likes 0 ·
brendan avatar image
brendan answered

Well first, that's specifically the kind of thing Cloud Script is designed for - player finishes a level, you send up all the info, and then on the server side you evaluate what they sent (to look for cheating), update stats, and give rewards.

But I would highly recommend not sending a request from the client without waiting on the response from a previous request - and this is the key part - if that previous request is going to be writing to the same data. In general, calls to the service take fractions of a second to be returned (you can see the details for your own title in your daily API Usage Report), so you shouldn't see a client having to wait "a few moments" unless the issue is with their ISP - and in that case, the ISP is unreliable enough that you cannot depend upon the order two calls made effectively at the same time are going to arrive in our service. So the net result of sending two calls that update something like the player's VC balance without waiting on the response from the first is that you could wind up with them processing simultaneously. We use locks wherever possible to prevent issues from something like that, but you would have no way to guarantee which would be processed first, since the issue is that they might be delivered to us out-of-order.

So you could add a timestamp, a sequence number, or some other means to try to manage that, but that would be a lot more work than you really need to do. If you want the player to be able to take multiple actions with zero wait time, just store the data locally, and update to the service with aggregated information. That'll mean you'll need to have some sync checks to make sure the player isn't abusing that to try to cheat (especially if they sign into the account on two different devices), but it would be somewhat simpler.

1 comment
10 |1200

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

Matt avatar image Matt commented ·

I recall playing an iOS game recently that always had a small pause (they would, of course, show a little time glass icon to let you know it's communicating with the server) so you couldn't really spam an upgrade fluently like you should. That spurred me to want to bulletproof this, but your suggestion to manage it locally and send a call less often is a perfect remedy! I'll wait to see if the delay is really even that bad once it's all implemented. I don't mind any delay less than a person would need to hit the upgrade button at a steady pace.

Anyway, very helpful information, thanks Brendan!

-Matt

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.