System.DateTime.now and how to avoid time cheats

Hi,

I want to reward my player if he comes back to my game every day.

The obvious solution for this would be something like:

	DateTime currentTime = DateTime.Now;
	TimeSpan ts = currentTime - Convert.ToDateTime (GameParameters.lastPlayDate);
	if (ts.TotalHours > 24)
	{
		Debug.Log ("Welcome back, here's a prize for you!");
	}

But, this approach can be exploited by changing the time and date on my device. I’d like to avoid that, I’ve look for an answer here, in Unity forums and in game dev in general and I couldn’t find any answer…

Is there any way to avoid this cheat at all? Either locally or, say, getting time and date from a server via Unity?

Thanks in advance!

For Local use in Android use this method to get system elapsed Time , independent of System Date And Time

C#

public static long ElapsedTime(){ if (Application.platform != RuntimePlatform.Android) return 0; AndroidJavaClass systemClock = new AndroidJavaClass("android.os.SystemClock"); return systemClock.CallStatic<long>("elapsedRealtime"); }

I’ve published an asset to solve this task locally on mobile platforms. With the help of native system code it tracks down device date and time changes and calculates unbiased time.

Works only on real Android and iOS devices. It is useful even if you use server timestamps, since user can turn off connection and cheat.

http://forum.unity3d.com/threads/system-datetime-now-and-how-to-avoid-time-cheats.124282/

Have your game ping a server to get the time. So if you are serving content (like news and such)to the game, use the server for that service to get the time. If they can’t connect to the server (no connection, or site is down) they don’t receive the reward(possibly build in a recheck on next launch?)

Hello,

You can work with a database containing a last_logged for your players and create a php server, here is an example :
http://wiki.unity3d.com/index.php?title=Server_Side_Highscores#Step_3:_The_Unity_HSController_Script

With php you can get the server time so like this even if the player change is time locally this will not have any impact.

Of course you need to check if he can get his reward in the php file :

if [time] > 0 and [time] <= [lastlogged] {
    // send reward
} else {
    // no reward for you
}

Like this, if the player connects after 00:00 server time you can send him a notification of reward

Sorry for this “code” because that is no code but working progress. Actually I plan to do a system reward in my project, it is not already implemented but I know how it will process.

I hope for you that you already found a way, cause it is a 2 years old post, but I was passing by ^^

edit : WWW can get the content of the url specified, so printing the response with ‘echo’ is sufficient

I use the internet time to keep my time and date the same across all devices.
http://leatonm.net/unity3d-internet-time-date-setup-guide-stop-time-cheat/