Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

IOS: Calculate time interval without using system time

Discussion in 'Scripting' started by litebox, Sep 8, 2015.

  1. litebox

    litebox

    Joined:
    Aug 29, 2011
    Posts:
    158
    Hi, I'm developing game in which I give free gifts to players every 3 hours.
    For this I'm using System.DateTime and System.TimeSpan classes. All is working good except one thing: player can change data time in his iPhone/iPad and get gifts every minute, because algorithm thinks 3 hours interval was passed.
    I check this situation in few Unity games (for example, Crossy Road) and some how they managed this problem: system time doesn't affect calculation of free gift interval.

    Maybe someone know how? The only idea is they are using Push Notifications, perhaps this is the key to calculate time?

    Thanks.
     
  2. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    I'd imagine Crossy Road is getting the time from the internet. Getting a WWW response from time.is, or something.
     
    Kiwasi likes this.
  3. litebox

    litebox

    Joined:
    Aug 29, 2011
    Posts:
    158
    Thanks
     
  4. Spoke44

    Spoke44

    Joined:
    Feb 16, 2015
    Posts:
    11
    You must use a distant server to do this. Your server must store the player last gift time (let's say last_time_gift).

    Then :
    - Send a request to the server
    - The server check if it's time to give a gift : server_time() - last_time_gift > 3 hours
    - Update the last_time_gift on the server and send back the response (success or error)
     
  5. litebox

    litebox

    Joined:
    Aug 29, 2011
    Posts:
    158
    Thanks. The idea with server is clear.
    But, I check how Crossy Road and Sling Kong behave without internet connection (in Airplain Mode) and discover next: they still able to calculate time intervals correctly.

    When I read an article about Android Push Notifications, it was mentioned, that push notifications is like a separate thread and can work all time even when App is closed. Maybe iOS Push Notifications works same way? Maybe it's possible to run a daemon process wich could calculate time?
     
  6. litebox

    litebox

    Joined:
    Aug 29, 2011
    Posts:
    158
    I found this article about iOS Push Notifications very interesting: http://forum.unity3d.com/threads/ios-push-notification-tutorial.210142/

    Also is't good to save last date when player collects gift, and if current date is less than saved - most probably this is a cheat: player sets date in future, come to game, collect gifts, closes game, returns to real date. Of course this is no a 100% guarantee, but it forces player to do more iterations to achieve "cheat gifts".
     
  7. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Simply fake it of the player can't reach the internet. Count down for a single gift, then don't reset until you can contact your www.
     
  8. Spoke44

    Spoke44

    Joined:
    Feb 16, 2015
    Posts:
    11
    As BoredMormon said, you have to fake if the user has not internet.

    - Every 3 hours, locally on the device (timer based on current phone time), trigger a function
    - In this function :
    - First, try to reach your server
    - If internet is enabled, you can calculate the gift like I said in my previous post
    - Second, if internet IS NOT reachable OR (internet is reachable AND gift has been gave)
    - Emit a local notification

    If the player change his local time, a notification will be send, but when internet will be available the server will say 'hey man, it's not true !'. So you're good ;)
     
  9. litebox

    litebox

    Joined:
    Aug 29, 2011
    Posts:
    158
    Thanks for answers :)