PtokaX forum

Archive => Archived 5.0 boards => Finished Scripts => Topic started by: Dessamator on 22 March, 2005, 13:15:42

Title: Multitimer lua5
Post by: Dessamator on 22 March, 2005, 13:15:42

--lua5 version by Dessamator
-- MultiTimer by OpiumVolage (19/06/2003)
-- Ideas taken from tezlo's retrobot

tabTimers = {n=0}

-- Time Definition
Sec  = 1000
Min  = 60*Sec
Hour = 60*Min
Day  = 24*Hour

TmrFreq = 1*Sec
botname=  frmHub:GetHubBotName() -- bot name
------------------------------MAIN FUNCTION ------------------------
function Main()  
frmHub:RegBot(botname)


--timerS
RegTimer(Trig1, 1*Min)
RegTimer(Trig2, 2*Min)
SetTimer(TmrFreq)
StartTimer()
--
end  
----------------------------------------------------------------------
--Timer Functions--
function Trig1()
SendToAll("txt")

end
function Trig2()
SendToAll("txt2")

end

function OnTimer()
for i in ipairs(tabTimers) do
tabTimers[i].count = tabTimers[i].count + 1
if tabTimers[i].count > tabTimers[i].trig then
tabTimers[i].count=1
tabTimers[i]:func()
end
end
end

function RegTimer(f, Interval)
local tmpTrig = Interval / TmrFreq
assert(Interval >= TmrFreq , "RegTimer(): Please Adjust TmrFreq")
local Timer = {n=0}
Timer.func=f
Timer.trig=tmpTrig
Timer.count=1
table.insert(tabTimers, Timer)
end

-- Timer Functions--------

Title:
Post by: plop on 06 June, 2005, 00:08:28
for i in pairs(tabTimers) do

pairs = loop an array.
ipairs = loop a table.

plop
Title:
Post by: Dessamator on 06 June, 2005, 00:29:18
QuoteOriginally posted by plop
for i in pairs(tabTimers) do

pairs = loop an array.
ipairs = loop a table.

plop

Done  !
Title:
Post by: Dessamator on 06 June, 2005, 12:02:53
-- by OpiumVolage (19/06/2003)
-- Ideas taken from tezlo's retrobot
-- Lua5 version by Dessamator
-- MultiTimer v1.1
-- added trig to view timers
-- added trig to remove timer

tabTimers = {n=0}

-- Time Definition
Sec  = 1000
Min  = 60*Sec
Hour = 60*Min
Day  = 24*Hour

TmrFreq = 1*Sec
botname=  frmHub:GetHubBotName() -- bot name
------------------------------MAIN FUNCTION ------------------------
function Main()  
frmHub:RegBot(botname)


--timerS
RegTimer(Trig1, 1*Min,"trig1")
RegTimer(Trig2, 2*Min,"trig2")
SetTimer(TmrFreq)
StartTimer()
--
end  
----------------------------------------------------------------------
--Timer Functions--
function Trig1()
SendToAll("txt")

end
function Trig2()
SendToAll("txt2")

end

function OnTimer()
for i in ipairs(tabTimers) do
tabTimers[i].count = tabTimers[i].count + 1
if tabTimers[i].count > tabTimers[i].trig then
tabTimers[i].count=1
tabTimers[i]:func()
end
end
end

function RegTimer(f, Interval,name)
local tmpTrig = Interval / TmrFreq
assert(Interval >= TmrFreq , "RegTimer(): Please Adjust TmrFreq")
local Timer = {n=0}
Timer.name=name
Timer.func=f
Timer.trig=tmpTrig
Timer.count=1
table.insert(tabTimers, Timer)
end

-- Timer Functions--------

function ChatArrival(user,data)
data = string.sub(data,1,-2)
local temp="\r\n\t\These are the timers in you script :\r\n"
local _,_,timername = string.find(data,"%b<>%s+%S+%s+(%S+)")
local s,e,cmd = string.find(data,"%b<>%s+(%S+)")
if cmd=="!tremove" then
for i in ipairs(tabTimers) do
if tabTimers[i].name == timername then
table.remove(tabTimers,i)
status="Timer Removed"
elseif not (tabTimers[i].name==timername) then
status="That Timer doesnt exist"
end
end
user:SendData(botname,status)
return 1
elseif cmd=="!tview" then
for i in ipairs(tabTimers) do
temp=temp.."\t\t•"..tabTimers[i].name.."\r\n"
end
user:SendData(botname,temp)
return 1
end
end