Networking: Retrieve Server Time and Date Example

MantoManto Member Posts: 796

Here's a simple example of a game that retrieves time from a server. It can be used to prevent user from cheating by changing their devices time.

The time and date is loaded to the table called TimeTable. You can use tableCellValue function to get time values, for example tableCellValue( game.TimeTable, 1, 5) will get the current minutes. See the image below for the column numbers for each value.

Requirements:
- A server capable of running PHP.

How to use the script:
1. Upload the script file to your server.
2. Change the gs project's serverURL game attribute to the server url for the file (http://yourdomain.com/servertime.php)
3. Pressing the update time button should load the time from the server

Download Project and PHP files here.

If you have problems using the script let me know.

Comments

  • SummationSummation Member, PRO Posts: 476
    edited April 2015

    @Manto said:
    Here's a simple example of a game that retrieves time from a server. It can be used to prevent user from cheating by changing their devices time.

    [image]

    The time and date is loaded to the table called TimeTable. You can use tableCellValue function to get time values, for example tableCellValue( game.TimeTable, 1, 5) will get the current minutes. See the image below for the column numbers for each value.
    [image]

    Requirements:
    - A server capable of running PHP.

    How to use the script:
    1. Upload the script file to your server.
    2. Change the gs project's serverURL game attribute to the server url for the file (http://yourdomain.com/servertime.php)
    3. Pressing the update time button should load the time from the server

    Download Project and PHP files here.

    If you have problems using the script let me know.

    So many possibilities just opened up.
    Though I wonder if it's possible to query an existing time server the same way as many operating systems do, for example time.nist.gov

  • MantoManto Member Posts: 796

    Nice to hear you find it useful.

    @Summation said:
    Though I wonder if it's possible to query an existing time server the same way as many operating systems do, for example time.nist.gov

    Should be possible, but not directly. You'll have to have your own server get the time from existing time server, because GS needs a specific json format to be able to create a table from it.

  • LovejoyLovejoy Member Posts: 2,078

    @Manto What happens with users that play offline?

    Fortuna Infortuna Forti Una

  • MantoManto Member Posts: 796

    @Lovejoy said:
    Manto What happens with users that play offline?

    In the example the time and date will just not be received. What I suggest you to do is to only try to get the server time at app startup. Then if there will be no answer from the server (callback attribute < 1) show an error message, meaning there is a problem with connection between the user and server.

    If you need to get the server time more often, you could check the callback attribute every time.

  • clee2005clee2005 http://Donkeysoft.caMember, PRO Posts: 194
    edited May 2015

    Hey @manto thanks for the sample! I just thought I'd drop a tip that I used Google App Engine which is Free for small usage (which I believe would be fine for more indie games - and if you cross the usage threshold and have to pay it means that your game is doing well!). A super simple way to use for those that are non programmers, and those that just want something simple, would be to create the .json file as you've specified, and with the data desired, and then just point directly at that file from your GS game. This would require you as the dev to modify this file by hand anytime you want an update, but there'd be no coding involved, just a file editor required (and some effort to set up Google App Engine - You could of course use any web hosting site that you have... doesn't have to be Google App Engine).

    So for example your URL might be http://newappenginesite.appspot.com/testfile.json

    In that file you'd have the following :

    {"Properties":[],"Name":"","Children":[{"Properties":[{"Name":"rowCount","Value":1},{"Name":"columnCount","Value":6},{"Name":"0-1-name","Value":"Year"},{"Name":"0-1-type","Value":2},{"Name":"0-2-name","Value":"Month"},{"Name":"0-2-type","Value":2},{"Name":"0-3-name","Value":"Day"},{"Name":"0-3-type","Value":2},{"Name":"0-4-name","Value":"Hour"},{"Name":"0-4-type","Value":2},{"Name":"0-5-name","Value":"Minute"},{"Name":"0-5-type","Value":2},{"Name":"0-6-name","Value":"Second"},{"Name":"0-6-type","Value":2}],"Name":"id858386_headers","Children":[]},{"Properties":[{"Name":"1","Value":"|12|12|22|6|5|4|"}],"Name":"id858386","Children":[]}]}

    Then whenever you want to send out a change (not that you'd do this for a time server, but something else, like a notification or some kind of update), you'd just modify the appropriate values in the .json file and all your users would receive the update on their next bootup.

    I'm sure there's some useful application for this somewhere. :-)

    Cheers,
    Chris.

Sign In or Register to comment.