How can I detect the passage of time even when the game is closed؟؟

Hi everyone — I’m adding a daily rewards system to my game.
If a player claims Day 1, they will only be able to claim Day 2 after 24 hours have passed. The game must work offline, so I also need to prevent players from cheating by changing their device clock. I’ll implement anti-tamper checks so players can’t gain rewards simply by moving their phone time forward.

Hi @ALI3116
You can get the device time with the c# class DateTime. By saving the last seen value frequently you can detect going-back-in-time tampering but it is harder to detect jumping forward while fully offline. You can never be sure that what you get represent the correct date and time.

The only fully tamper-proof way of getting the real time is to get your time source from a server (your own back-end, from the http header of a big domain or from a ntp server).

So what do offline games like Candy Crush or Sweet Candy Match do that even when you change the device’s time, you still can’t get extra lives, and the game somehow knows the real time?

According to Google, the daily rewards in Candy Crush don’t work offline.
It’s likely that the rewards are also determined server-side, not only the time.

AFAIK you can cheat the extra lives in candy crush by changing the device time. It’s a compromise they had to make for the game to work offline.

One approch is have a time management class that tries to fetch the time from a network source :

  • if it can’t fetch it consider the time as unreliabale

  • if it can compute an offset for the network time and local time and infer the real time for the next calls by adding that delta to the device time. You can consider the returned time as reliable until the app goes in background. Persist that delta somehow so you can re-evaluate ‘real’ time in future potentially offline sessions.

  • if you are back from pause or in a new session but you couldn’t fetch the real time and refresh your delta you can still reuse the previous delta to get a probably reliable real time estimation.

    Base on that time estimation and reliability heuristic you can decide what features and rewards can be triggered. Live ops events, daily rewards would require a reliable time to be started but could be continues in a probably reliable time. Life recharging could tolearate unreliable time with minimal tamper detection.

No, I mean a system where the player loses a life each time they play, and each life refills every N minutes, encouraging the player to watch ads or videos to get lives faster.

Sorry, I didn’t quite understand. Do you have any code for that ?