PtokaX forum

Archive => Archived 4.0 boards => Help with Lua 4 scripts => Topic started by: Stravides on 27 February, 2004, 23:50:33

Title: Using Multiple timers, want to selectively restart the timers
Post by: Stravides on 27 February, 2004, 23:50:33
ok, I'm using the script timers from this here forum for multiple timere and also tryign to integrate wiseguy variant into my bot.. any suggestions ?

Below is a fragment of the code I'm using

the multiple timers scripts are:
-- Time Definition
Sec = 1000
Min = 60*Sec
Hour = 60*Min
Day = 24*Hour
Week = 7*Day

TmrFreq = 10*Sec
tabTimers = {n=0}

function main()
RegTimer(ForumTimer, 4*Hour)
RegTimer(QuizTimer, 3*Hour)
  RegTimer(RandomTimerMsg, 1*Hour)
  RegTimer(WiseGuyTimerMsg, 2*Hour) -- this is the one I want to restart on demand...
RegTimer(CheckShare, 1*Min)
SetTimer(TmrFreq)
StartTimer()
end

function ForumTimer()
-- func goes here
end

function QuizTimer()
-- func goes here
end

function CheckShare()
-- func goes here
end

function RandomTimerMsg()
-- func goes here
end

function WiseGuyTimerMsg()
-- func goes here
-- need to stop and start only this timer, but norm would use
StopTimer()

StartTimer()
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
tinsert(tabTimers, Timer)
end

function OnTimer()
for i=1, getn(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

Title:
Post by: nErBoS on 28 February, 2004, 00:23:31
You want to stop the timer with another timer or with a command ???
Title:
Post by: Stravides on 28 February, 2004, 00:39:35
I wanna be able to say anywhere in the code

stop this timer
or restart this timer,  but only one ..

ie
if (cmd=="!quiet") then
quiet = "true"
talk = "notallow"
StopTimer()
user:SendPM(botname,"Ok,...I'll be quiet now... :s")
end
if (cmd=="!talk") then
talk = "allow"
quiet = "false"
WaitingForAnswerAfterQuiet = "false"
WaitingForAnswerMorning = "false"
WaitingForAnswerEvening = "false"
StartTimer()
user:SendPM(botname, "Ahh... i'm back :)")
end
Title:
Post by: nErBoS on 28 February, 2004, 00:53:14
Is this what you want ???

time = 0

---in Data Arrival
if (cmd=="!stop") then
time = 0
end
if (cmd=="!start") then
time = 1
end


---- In the function WiseGuy

if (time == 0 or time == nil) then
StopTimer()
else
StartTimer()
end
Title:
Post by: Stravides on 28 February, 2004, 00:57:03
the way the bot works if ya look at the original post in func main it loads 5 timers

   RegTimer(ForumTimer, 4*Hour)
   RegTimer(QuizTimer, 3*Hour)
    RegTimer(RandomTimerMsg, 1*Hour)
    RegTimer(WiseGuyTimerMsg, 2*Hour)    RegTimer(CheckShare, 1*Min)

I just want the wiseguy timer to be stopped and started, but by using StopTimer() or StartTimer() will that not reset all timers ?
I think the key is in here... but dont completely understand it...
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
tinsert(tabTimers, Timer)
end

function OnTimer()
for i=1, getn(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
Title:
Post by: Stravides on 28 February, 2004, 01:01:28
Do I take it from this that the timer table is composed of 3 tables func, trig and count ?

So for :
  RegTimer(ForumTimer, 4*Hour)
  RegTimer(QuizTimer, 3*Hour)
  RegTimer(RandomTimerMsg, 1*Hour)
  RegTimer(WiseGuyTimerMsg, 2*Hour)
  RegTimer(CheckShare, 1*Min)

TabTimers should have in it

   Timer.func=ForumTimer   
   Timer.trig=4*Hour/10*Sec
   Timer.count=1

   Timer.func=QuizTimer   
   Timer.trig=3*Hour/10*Sec
   Timer.count=1

   Timer.func=RandomTimerMsg   
   Timer.trig=1*Hour/10*Sec
   Timer.count=1

   Timer.func=WiseGuyTimerMsg   
   Timer.trig=4*Hour/10*Sec
   Timer.count=1

   Timer.func=CheckShare   
   Timer.trig=1*Min/10*Sec
   Timer.count=1

is this as clear as mus as it is to me :S
Title:
Post by: nErBoS on 28 February, 2004, 01:06:16
I think this will do...

if (time == 0 or time == nil) then
           StopTimer()
RegTimer(ForumTimer, 4*Hour)
RegTimer(QuizTimer, 3*Hour)
  RegTimer(RandomTimerMsg, 1*Hour)
RegTimer(CheckShare, 1*Min)
SetTimer(TmrFreq)
StartTimer()
else
RegTimer(ForumTimer, 4*Hour)
RegTimer(QuizTimer, 3*Hour)
  RegTimer(RandomTimerMsg, 1*Hour)
  RegTimer(WiseGuyTimerMsg, 2*Hour)
RegTimer(CheckShare, 1*Min)
SetTimer(TmrFreq)
StartTimer()

end
Title:
Post by: kepp on 28 February, 2004, 01:10:11
yea, don't use StopTimer() that will stop all timers

i have no idea how to stop any of those...
let's wait and see what the author has to say about this :)
Title:
Post by: Stravides on 28 February, 2004, 01:14:27
QuoteOriginally posted by nErBoS
I think this will do...

if (time == 0 or time == nil) then
           StopTimer()
RegTimer(ForumTimer, 4*Hour)
RegTimer(QuizTimer, 3*Hour)
  RegTimer(RandomTimerMsg, 1*Hour)
RegTimer(CheckShare, 1*Min)
SetTimer(TmrFreq)
StartTimer()
else
RegTimer(ForumTimer, 4*Hour)
RegTimer(QuizTimer, 3*Hour)
  RegTimer(RandomTimerMsg, 1*Hour)
  RegTimer(WiseGuyTimerMsg, 2*Hour)
RegTimer(CheckShare, 1*Min)
SetTimer(TmrFreq)
StartTimer()

end

this again will restart all the timers... basically the wiseguy timer looks for a keypress in main chat if it receves a comment it will reset the timer..
but I want the general advert to go out as timed , ie never reset. :)  Never easy is it :D
Title:
Post by: NightLitch on 01 March, 2004, 12:22:56
Well why not just putting triggers that will start them, or not start them but doing the function inside the timer.

ex:


WiseGuyTrig=0

function ForumTimer()
-- func goes here
end

function QuizTimer()
-- func goes here
end

function CheckShare()
-- func goes here
end

function RandomTimerMsg()
-- func goes here
end

function WiseGuyTimerMsg()
   if WiseGuyTrig==1 then
-- func goes here
   end
end

and in datarrival:

---in Data Arrival
if (cmd=="!start") then  -- and adding an arg. in !start then you can use it like this: !start wiseguy, !start cshare etc.
WiseGuyTrig=1
end
if (cmd=="!stop") then  -- same goes here...
WiseGuyTrig=0
end


---- In the function WiseGuy

if (time == 0 or time == nil) then
Title:
Post by: OpiumVolage on 01 March, 2004, 12:39:31
QuoteOriginally posted by Stravides
Do I take it from this that the timer table is composed of 3 tables func, trig and count ?

So for :
  RegTimer(ForumTimer, 4*Hour)

   Timer.func=ForumTimer   
   Timer.trig=4*Hour/10*Sec
   Timer.count=1


You can use tremove to stop the timer (removing a timer from the table: timer[n]

And timer.count=1 to reset a timer.
Title:
Post by: Stravides on 01 March, 2004, 12:43:16
thanks a lot, will try that and see how I get on..