question

brendan avatar image
brendan asked

Any calls to get the ServerTime?

flekoun
started a topic on Sat, 27 September 2014 at 1:12 AM

Hello,

so far I am amazed by your service it looks exactly what I have been waiting for years! However it seems like I cannot find support for one of the most important thing that the most mobile F2P games needs. That is the ability to get the time from server instead of the client. As everyone know in the F2P without server backend the user can simply turn his time on the device forward to cheat in the game. As time-based rewards/waits etc. are very common in F2P game I would expect some support for this in PlayFab.

Reallife examples i need to solve:

User build a building and it takes 10 hours to complete

User gets a new bag of seed every hours the maximum capacity is 5.

Do you have and idea how to implement this with API calls you have now in place?

Keep the good work going!

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

·
brendan avatar image
brendan answered

Best Answer
Brendan Vanous said on Thu, 29 January 2015 at 8:33 PM

At the moment, this hasn't been called out as a priority by our community, though we do have it listed in the Feature Requests forum. In addition to reading the time from the Web API response, you can also query the now.GetTime() in Cloud Script (an example is in the Rewards sample in our Cloud Script GitHub: https://github.com/PlayFab/CloudScriptSamples/blob/master/Rewards/CloudScript_Rewards.js).


8 Comments
Brendan Vanous said on Sat, 27 September 2014 at 3:44 AM

While a query for the time isn't in the current API set, we do have a backlog item to create one. I've added this concept to the Feature Request forum, and we'll be adding this to an upcoming sprint. We'll update the forum with info on this when it has been added. Thanks!


gwertzman said on Sat, 27 September 2014 at 4:04 AM

If you need this sooner than we provide it officially, there is also a temporary workaround that exists. Because the PlayFab API is a web based API, all of the PlayFab SDK's are just wrappers to the web base API. And whenever you call a web API, the response includes a timestamp from the server in the header. So actually, every call to the server returns a server time stamp -- but right now this information is discarded.

An enterprising developer could modify the SDK (it's open sourced on GitHub for exactly this reason) to return the timestamp, along with the rest of the data. If you're using Unity, for example, the Unity WWW object has a "ResponseHeaders" property which returns all the headers. You could use this to get the "Date" property.

The file in question would be:

https://github.com/PlayFab/UnitySDK/blob/master/PlayFabClientSDK/Playfab/PlayFabSDK/Internal/PlayFabHTTP.cs

And the code might look something like this:

var serverTime = www.responseHeaders["Date"];

However passing this back up the chain would be a big piece of work, since you'd need to modify the entire callback chain to include 3 parameters (response, error, timestamp) instead of the current 2.

So probably best to wait for this to be officially supported :)


flekoun said on Sat, 27 September 2014 at 4:24 AM

Thanks a lot for your support! I am glad to hear you are already working on this as I guess more developers are interested in this feature as well. We are not in hurry for now so I guess I will wait for the official support.


mattgogii said on Thu, 29 January 2015 at 11:49 AM

Any updates on this? I'm not seeing any time calls in the web api documentation.


Brendan Vanous said on Thu, 29 January 2015 at 8:33 PM

At the moment, this hasn't been called out as a priority by our community, though we do have it listed in the Feature Requests forum. In addition to reading the time from the Web API response, you can also query the now.GetTime() in Cloud Script (an example is in the Rewards sample in our Cloud Script GitHub: https://github.com/PlayFab/CloudScriptSamples/blob/master/Rewards/CloudScript_Rewards.js).


mattgogii said on Mon, 06 April 2015 at 1:01 PM

I'm just starting to implement a time function. On ios using the unity sdk the http responses now go through the PlayFabURLRequest.mm function pf_make_http_request. Is there an way to pull the time from the response in that function? I'm guessing once I find the time I'll need to append it to the replyBuffer as well to get it back to unity. In the editor I'm able to use the www.responseHeaders["DATE"] to get the time string without issue.


Brendan Vanous said on Fri, 10 April 2015 at 10:41 AM

Indeed, the date is in the headers - we're looking at providing this as part of the response, to make things easier.


mattgogii said on Fri, 10 April 2015 at 10:53 AM

I modified the sendAsychronousRequest in PlayFabURLRequest.mm and added these lines

NSString *date = @"null";
if ([response isKindOfClass:[NSHTTPURLResponse class]])
{
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*) response;
NSDictionary *headers = httpResponse.allHeaderFields;
date = headers[@"Date"];

}

const char* cdate = [date UTF8String];

int maxLen = data.length + 32 + date.length;
char* replyBuffer = (char*)malloc(maxLen+1);
int written = snprintf(replyBuffer, maxLen, "%i^%s^", requestId, cdate);

then in PlayFabPluginEventHandler.cs in OnHttpResponse

string[] args = response.Split("^".ToCharArray(), 3);
int reqId = int.Parse(args[0]);

// lets try and convert arg[1] to a date time
System.DateTime serverTime = DateTime.Parse(args[1]);

Had to change the ":" separator to a "^" as the date in the header has the time as 12:34:10 so the ":" breaks the string split.

Missing some error checking in there but it seems to be working.

Thinking of triggering an event at that point and then the game can just subscribe that event and update as necessary. Would save passing it back up the chain.

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.