Countdown timer

User avatar
Vadi
Posts: 5035
Joined: Sat Mar 14, 2009 3:13 pm

Countdown timer

Post by Vadi »

Thinking about creating a countdown timer for the LuaGlobal API. Has anyone played around with the idea? The problem here is making it convenient and simple to use.

From what I can see there are two things to do here - intervals, and what to do at specific intervals. Intervals should also be either custom times, or just time deltas to use (ie every 5s). What to do should be either strings to echo or functions to execute.

That's pretty vague, but it's what I got in my head atm. Will add more refinement / comments welcome.

User avatar
tsuujin
Posts: 695
Joined: Fri Feb 26, 2010 12:59 am
Location: California
Contact:

Re: Countdown timer

Post by tsuujin »

I thought about doing this myself. The problem I had was with precision. Lua's timers, for what I can see, aren't precise (they just go by seconds, rather than microseconds) which kind of ruined my purpose for creating such a thing. I'd want as many decimals as possible in the counter.

User avatar
Vadi
Posts: 5035
Joined: Sat Mar 14, 2009 3:13 pm

Re: Countdown timer

Post by Vadi »

Lua doesn't have timers, and Mudlets timers do go by decimals.

User avatar
tsuujin
Posts: 695
Joined: Fri Feb 26, 2010 12:59 am
Location: California
Contact:

Re: Countdown timer

Post by tsuujin »

Vadi wrote:Lua doesn't have timers, and Mudlets timers do go by decimals.
lua has os.clock(), which can be used to tell you how long, in minutes and seconds, the program has been running. It's just plain seconds, though, with no microseconds.

As far as I know, you can't arbitrarily set a mudlet timer and just call to see how long it's been running, which is what you'd really need.

User avatar
tsuujin
Posts: 695
Joined: Fri Feb 26, 2010 12:59 am
Location: California
Contact:

Re: Countdown timer

Post by tsuujin »

if we could get a timer with, say, four decimals to start counting upwards when mudlet is instantiated then an awsome timer class would be a few keystrokes away. Really I think it has to be done on the C++ side.

User avatar
tsuujin
Posts: 695
Joined: Fri Feb 26, 2010 12:59 am
Location: California
Contact:

Re: Countdown timer

Post by tsuujin »

oh hey, cool. I still have this old function from my MUSH system. Here's how I calculated time differences there:
Code: [show] | [select all] lua
function bushido:getTime()
	local tm = os.date("*t")
	local td = string.format("%s:%s:%s",tm.hour,tm.min,tm.sec)
	self.lastmil = self.mil
	self.mil = GetInfo(232)
	local diff = self.mil - self.lastmil
	diff = string.format("%.3f",diff)
	return {["time"] = td, ["diff"] = diff}
end

User avatar
Vadi
Posts: 5035
Joined: Sat Mar 14, 2009 3:13 pm

Re: Countdown timer

Post by Vadi »

Mudlet works around Lua's os.date not reporting miliseconds as well: http://mudlet.org/asciidoc/manual.html#getTime

But this isn't the question at hand... an easy to use API for creating a countdown timer to fill all kinds of needs while being easy to use for newbies too is what's problematic.

User avatar
tsuujin
Posts: 695
Joined: Fri Feb 26, 2010 12:59 am
Location: California
Contact:

Re: Countdown timer

Post by tsuujin »

without any extra level of precision, how's it different than just creating a permTimer interface?

User avatar
Vadi
Posts: 5035
Joined: Sat Mar 14, 2009 3:13 pm

Re: Countdown timer

Post by Vadi »

Oh, sorry. It'll have the additional feature of doing actions as the time is closer to expiring.

User avatar
Vadi
Posts: 5035
Joined: Sat Mar 14, 2009 3:13 pm

Re: Countdown timer

Post by Vadi »

Some ideas...

-- countdown timer for 60s that every 10s will print a msg
countdownTimer {endin = 60, {interval = 10, echo = "$timeleft seconds left! $timepassed seconds passed"}}

-- or can use a custom function...
countdownTimer {endin = 60, {interval = 4, echo = function(timeleft, timepassed) ... end}}

... also need a way to stick things to do when the time expires / give customized intervals. Just rough ideas.

Post Reply