1

During my game the user gets rewards every 10 minutes.

Even if they leave the app/game, when they come back, if 10 minutes has passed, they still get their reward.

So lets say for example: the user is playing and leaves the game with 5 minutes left until the next reward.

The user then leaves the game and returns an hour later.

How would I go about seeing if the 5 minutes have passed since the last open?

Does anyone know how to do this? I am using unity.

Backs
  • 24,430
  • 5
  • 58
  • 85
coder4life22
  • 199
  • 1
  • 2
  • 9
  • In the server side you should check the last time for each user by comparison you'll be able to achieve your goal – Bader Jan 19 '16 at 05:29

2 Answers2

1

You may want to use DateTime struct, especially its DateTime.Now.

  1. When your player leaves the game save the date time:

    DateTime leaveDateTime = DateTime.Now;
    //Store leaveDateTime value
    
  2. When your player resumes,

    //load leaveDateTime value
    TimeSpan diff = DateTime.Now - leaveDateTime;
    

Then use your TimeSpan values (check TimeSpan.TotalMinutes) to give reward to your player.

if (TimeSpan.TotalMinutes >= 5){
    //Give rewards for your players
}
Ian
  • 30,182
  • 19
  • 69
  • 107
  • Awesome! Gonna try this out and report back! – coder4life22 Jan 19 '16 at 04:09
  • @coder4life22 great! ;) take a look more on the `DateTime` and `TimeSpan` – Ian Jan 19 '16 at 04:10
  • How do I go about saving the time in playerprefs in unity? The DateTime into playerprefs – coder4life22 Jan 19 '16 at 12:45
  • I have never used playerprefs before... But I found this article: http://answers.unity3d.com/questions/27973/idiots-guide-to-player-prefs.html the title is quite tempting to open. :) see if it may help. – Ian Jan 19 '16 at 14:39
1

Ian answer is OK but it has one problem, if user change the system (PC, mobile, ...) date and time, lets say add one year to the date, when he come back to game, he will get zillion reward unfairly! I think current dateTime must be recived from a ntp server instead of DateTime.Now.

Take a look at this answer : How to Query an NTP Server using C#?

Community
  • 1
  • 1
Hossein Rashno
  • 3,073
  • 1
  • 25
  • 49
  • I could just have some type of check to avoid this. – coder4life22 Jan 19 '16 at 12:10
  • If reward is at a certain number of they received a certain amount they can't receive anymore until a certain amount has been used or . I am aware they can cheat this but if they want to cheat this bad, awesome! – coder4life22 Jan 19 '16 at 12:11