I’m looking for a time API to get the time online in my app for giving rewards to users. A preferably stable (and if possible free) service for a heavy usage app with 100k users. Do you have any experience on this?
Thanks in advance!
I’m looking for a time API to get the time online in my app for giving rewards to users. A preferably stable (and if possible free) service for a heavy usage app with 100k users. Do you have any experience on this?
Thanks in advance!
I’m not sure, but I guess you’re looking at giving rewards to the user each day they play?
Have a look at http://www.currentmillis.com.
They are hosting a service to get the minutes since epoch time directly, which is located here: http://currentmillis.com/time/minutes-since-unix-epoch.php.
Also make sure the OS is not caching the response by providing the Cache-Control header. Look beneath for an example implementation:
local function retrieveCurrentTime(\_onComplete) local url = "http://currentmillis.com/time/minutes-since-unix-epoch.php" network.request(url, "GET", function(event) if event.isError then if \_onComplete then \_onComplete() end elseif event.phase == "ended" then if \_onComplete then \_onComplete(event.response) end end end, {headers = {["Cache-Control"] = "no-cache"}, timeout = 5}) end
And you can use it like this then:
retrieveCurrentTime(function(\_time) local previousDailyBonus = [GETTER\_FOR\_PREVIOUS\_DAILY\_BONUS\_TIME] or 0 if \_time and \_time - previousTimeYouSavedSomewhereElse \>= 60 \* 24 then --Save time to be used next time --Show daily bonus screen end end)
Or did you mean like in Clash of Clans where after X mins you unlock a reward, but you need to track that on a server so that the user doesn’t just put their device clock forwards? I don’t have a good answer if that’s the case, so would be curious to know if anyone else does.
That would be as simple as starting a timer.performWithDelay() every minute. As timers are paused when exiting the app.
No need to use a server for that I think.
timer.performWithDelay(60000, function() -- Increase minute counter and save that value somewhere end)
Are you sure they are paused when the app is killed, rather than suspended? I haven’t tested this specifically but I’d be surprised if that was the case.
If they are not, then you would have to store the remaining time when the app is closed and check the time when the app is reopened, which leaves you open to people manipulating the device clock to skip ahead and finish objectives instantly.
When I last played CoC you had to be online otherwise the game didn’t even start, which I assumed was for this very reason.
I meant the timers are paused when the app is suspended indeed, not killed.
You won’t have to worry about the app being killed and then started again, as you would just start the minuteCounter timer again.
Worse thing that can happen is to have the player lose 1 minute of his play-time, this is when he kills the app at 59 seconds in.
That doesn’t work for those kind of games though, because sometimes a task might say:
“Weapons Factory: Build time 8 hours”
So you tell it to start building and then quit the game. 12 hours later you open the app again, and you expect the building to be finished. No user would expect the game to have to stay open for the entire 8 hours. You need to account for all the time when the app is not open, as well as when it is. This is why I think that the time info is stored online in those types of games. When the app is reopened the game contacts the server and finds out how much time has passed, and calculates what processes should now be completed, or something like that.
Ah alright, I misunderstood what you were trying to achieve then :rolleyes:
Well something like that could be achieved with a variant of the dailyBonus code I posted earlier.
networkTimeHelper.lua
local t = {} function t:retrieveCurrentTime(\_onComplete) local url = "http://currentmillis.com/time/minutes-since-unix-epoch.php" network.request(\_url, "GET", function(event) if event.isError then if \_onComplete then \_onComplete() end elseif event.phase == "ended" then if \_onComplete then \_onComplete(event.response) end end end, {headers = {["Cache-Control"] = "no-cache"}, timeout = 5}) end return t
When the player is starting the "Weapons Factory"
local networkTimeHelper = require("networkTimeHelper") networkTimeHelper:retrieveCurrentTime(function(\_time) if \_time then -- Some code to build the weapons factory eg something like this. -- weaponFactoryBuilder:new(someOtherParams, \_time) else -- Show message that the build has failed because of a time-out end end)
Then each time an application is “started” or “resumed”, verify networkTime and use that time as base for the “percentage build complete bars”. Also to be sure, you can let the player verify when the build is 100%, at that moment you can verify again if it really is completed by checking networkTime again.
There could be some things i’m overseeing right now, but I think you could make it work with something like that, without having to hire a stateful cloud server which keeps all the data.
Is there any function in Corona that returns time counted by a monotonic clock (device uptime, always increasing) ?
This is something that developers use on some other game platforms.
There isn’t an API to get the system uptime. There is one that gets microseconds since the app started, but that’s not the same thing.
Sounds like a great community plugin possibility.
I’m not sure, but I guess you’re looking at giving rewards to the user each day they play?
Have a look at http://www.currentmillis.com.
They are hosting a service to get the minutes since epoch time directly, which is located here: http://currentmillis.com/time/minutes-since-unix-epoch.php.
Also make sure the OS is not caching the response by providing the Cache-Control header. Look beneath for an example implementation:
local function retrieveCurrentTime(\_onComplete) local url = "http://currentmillis.com/time/minutes-since-unix-epoch.php" network.request(url, "GET", function(event) if event.isError then if \_onComplete then \_onComplete() end elseif event.phase == "ended" then if \_onComplete then \_onComplete(event.response) end end end, {headers = {["Cache-Control"] = "no-cache"}, timeout = 5}) end
And you can use it like this then:
retrieveCurrentTime(function(\_time) local previousDailyBonus = [GETTER\_FOR\_PREVIOUS\_DAILY\_BONUS\_TIME] or 0 if \_time and \_time - previousTimeYouSavedSomewhereElse \>= 60 \* 24 then --Save time to be used next time --Show daily bonus screen end end)
Or did you mean like in Clash of Clans where after X mins you unlock a reward, but you need to track that on a server so that the user doesn’t just put their device clock forwards? I don’t have a good answer if that’s the case, so would be curious to know if anyone else does.
That would be as simple as starting a timer.performWithDelay() every minute. As timers are paused when exiting the app.
No need to use a server for that I think.
timer.performWithDelay(60000, function() -- Increase minute counter and save that value somewhere end)
Are you sure they are paused when the app is killed, rather than suspended? I haven’t tested this specifically but I’d be surprised if that was the case.
If they are not, then you would have to store the remaining time when the app is closed and check the time when the app is reopened, which leaves you open to people manipulating the device clock to skip ahead and finish objectives instantly.
When I last played CoC you had to be online otherwise the game didn’t even start, which I assumed was for this very reason.
I meant the timers are paused when the app is suspended indeed, not killed.
You won’t have to worry about the app being killed and then started again, as you would just start the minuteCounter timer again.
Worse thing that can happen is to have the player lose 1 minute of his play-time, this is when he kills the app at 59 seconds in.
That doesn’t work for those kind of games though, because sometimes a task might say:
“Weapons Factory: Build time 8 hours”
So you tell it to start building and then quit the game. 12 hours later you open the app again, and you expect the building to be finished. No user would expect the game to have to stay open for the entire 8 hours. You need to account for all the time when the app is not open, as well as when it is. This is why I think that the time info is stored online in those types of games. When the app is reopened the game contacts the server and finds out how much time has passed, and calculates what processes should now be completed, or something like that.
Ah alright, I misunderstood what you were trying to achieve then :rolleyes:
Well something like that could be achieved with a variant of the dailyBonus code I posted earlier.
networkTimeHelper.lua
local t = {} function t:retrieveCurrentTime(\_onComplete) local url = "http://currentmillis.com/time/minutes-since-unix-epoch.php" network.request(\_url, "GET", function(event) if event.isError then if \_onComplete then \_onComplete() end elseif event.phase == "ended" then if \_onComplete then \_onComplete(event.response) end end end, {headers = {["Cache-Control"] = "no-cache"}, timeout = 5}) end return t
When the player is starting the "Weapons Factory"
local networkTimeHelper = require("networkTimeHelper") networkTimeHelper:retrieveCurrentTime(function(\_time) if \_time then -- Some code to build the weapons factory eg something like this. -- weaponFactoryBuilder:new(someOtherParams, \_time) else -- Show message that the build has failed because of a time-out end end)
Then each time an application is “started” or “resumed”, verify networkTime and use that time as base for the “percentage build complete bars”. Also to be sure, you can let the player verify when the build is 100%, at that moment you can verify again if it really is completed by checking networkTime again.
There could be some things i’m overseeing right now, but I think you could make it work with something like that, without having to hire a stateful cloud server which keeps all the data.
Is there any function in Corona that returns time counted by a monotonic clock (device uptime, always increasing) ?
This is something that developers use on some other game platforms.
There isn’t an API to get the system uptime. There is one that gets microseconds since the app started, but that’s not the same thing.
Sounds like a great community plugin possibility.