0

I have a IOS / cocos2d game with a php/mysql server where a user is given 5 lives at the start. Users can connect on any device to play via their Facebook account.

Each time they play a game, one life is used up. Lives regenerate once every 10 minutes, regardless of whether app is active, resigned or terminated.

I understand that I can use NSTimer or CCDelayTime to "regenerate" lives while the app is active, but how can I get this to persist while the app is minimized or closed? Should I implement this on the server side?

jyek
  • 1,081
  • 9
  • 19

2 Answers2

1

Since you want the life information to be available across all devices, you must therefore store the information on the server. If you just wanted the data to persist on a single device you could use NSUserDefaults for when the app isn't active.

Bogatyr
  • 19,255
  • 7
  • 59
  • 72
1

Should I implement this on the server side?

I think that you should handle this on the server side for two main reasons:

  1. You say that your app can be played from more than one device. The number of remaining lives should be the same whatever the device you are playing from so it needs to be kept in a unique place.

  2. Also keeping the number of lives on the server side will prevent you from users trying to hack your app by changing locally the number of remaining lives.

Davz
  • 1,530
  • 11
  • 24
  • 1
    Also, a user can always change its time setting on the device ==> the user will be able to regenerate lives as he wish ==> server should hold the TRUTH :) – Dan Shelly Apr 05 '13 at 06:56
  • @Davz How would I do this on the server side? I have a php/MySQL server, but am not sure how to run a 'timer' in the background. – jyek Apr 06 '13 at 05:08
  • 1
    Answering my own question: I implemented this by creating a last_update and time_to_next_update DateTime variable in my database. Then, number of lives to add = (elapsed time - time_to_next_update) / 10 minutes + 1. This approach avoids the need to run a cronjob. I also implemented the logic in my app using a timer by modifying this class: http://stackoverflow.com/questions/3519562/how-do-i-write-a-timer-in-objective-c. Code is quite specific to my situation so didn't post any, but happy to answer any questions if people have the same difficulty implementing this. – jyek Apr 08 '13 at 08:03
  • @RaaRaaRaa I don't think that you need to use a timer to do so. Just keep somewhere the last time the user used your application and next time user connects simply compute the elapsed time to determine the amount of lives to "regenerate". – Davz Apr 08 '13 at 08:04
  • @RaaRaaRaa We answered at the same time :-) – Davz Apr 08 '13 at 08:06