Page 1 of 2

Countdown timer

Posted: Sun Oct 24, 2010 5:39 am
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.

Re: Countdown timer

Posted: Sun Oct 24, 2010 6:08 am
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.

Re: Countdown timer

Posted: Sun Oct 24, 2010 1:08 pm
by Vadi
Lua doesn't have timers, and Mudlets timers do go by decimals.

Re: Countdown timer

Posted: Sun Oct 24, 2010 5:34 pm
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.

Re: Countdown timer

Posted: Sun Oct 24, 2010 5:36 pm
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.

Re: Countdown timer

Posted: Sun Oct 24, 2010 5:49 pm
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

Re: Countdown timer

Posted: Sun Oct 24, 2010 5:54 pm
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.

Re: Countdown timer

Posted: Sun Oct 24, 2010 6:01 pm
by tsuujin
without any extra level of precision, how's it different than just creating a permTimer interface?

Re: Countdown timer

Posted: Sun Oct 24, 2010 6:27 pm
by Vadi
Oh, sorry. It'll have the additional feature of doing actions as the time is closer to expiring.

Re: Countdown timer

Posted: Sun Oct 24, 2010 7:00 pm
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.