Check time online for in-app rewards

Hello!

I parsed through Corona forums and documentation for a way to reward players with power ups whenever a certain duration had passed in order to increase player retention. For instance, I would offer a player 1 free power up every 3 hours, up to a total of 3 power ups. As I didn’t find any solutions, I thought to offer mine if anyone is interested.

The standard os.time() command, as explained in Rob Miracle’s tutorial from January 2013, is almost perfect for the task, however, is susceptible to players messing with their phones’ time settings. Any player could simply forward their device’s time by 3 to 9 hours, receive the free power ups and repeat. The only way to prevent this is simply checking time online.

Using the aforementioned scenario, at its simplest, my solution is that whenever the player has 2 or fewer power ups, we run:
 

function epochTimer(inEvent) if (inEvent.isError) then print("You need to be connected to the Internet to earn free power ups.") else print(inEvent.response) end end network.request("http://just-the-time.appspot.com/?f=%t", "GET", epochTimer )

The function above tries to fetch epoch time (same as os.time()) from the URL. If there is no connection, or there is a connection error, the simulator will print an error message. Of course, if you implement this to your game, you should make this visible to the player as well. If there is no connection, you could show the player a red button that display the error message when pressed and encourages them to connect to the Internet. If your game displays ads or uses other online services, this will also make the user see or use them more.

If the connection is successful and time is fetched, then we can start counting down to 3 hours where we will reward the player with his or her free power up. Counting down time is explained in Rob Miracle’s tutorial.

Since the player can suspend the game, or close it entirely at any point, we can protect the timer by:

1) always storing the epoch time we fetched when the timer started, and by
2) running the epochTimer function again when the game is restarted, and then
3) determining how much time has passed between the previous epochTime and the new epochTime, and finally,
4) we store the new epochTime as well as the remaining time.

This way, we will know down to 1/100 of a second how much time has passed since every suspend or shutdown and whether we should continue counting down or whether the player has already waited long enough for his or her rewards.

As a bonus: You could also use this system to create “daily missions or quests” for your players. You can determine when a new day starts by using and storing math.round(inEvent.response/86400) (60s * 60min * 24h = 86400s). By rounding up to integers, we know that whenever we fetch new epochTime, if the number above has increased, then at least one day has passed and we can present the player with a new daily quest.

I hope you guys find this to be of use. If you know of any better methods, please let me know!

1 Like