PtokaX forum

Archive => Archived 5.0 boards => Help with scripts => Topic started by: TTB on 15 April, 2005, 01:57:39

Title: timer function
Post by: TTB on 15 April, 2005, 01:57:39
Hi,

I have a question: How can I put something for about 5 minutes in a table... I use this:

userlist = {}
...
if userlist[sUser.sName] ~= 1 then
>>ACTION<<
if userlist[sUser.sName] == nil then
userlist[sUser.sName] = 1
end

I want to have something like:

userlist = {}
...
if userlist[sUser.sName] ~= 1 then
>>ACTION<<
if userlist[sUser.sName] == nil then
userlist[sUser.sName] = 1
>> ?? put sUser.sName in table for 5 min ?? <<
end


So, the only thing I need is a function that keeps a user in this table for 5 minutes, and then it will be removed from the table...
Title:
Post by: bastya_elvtars on 15 April, 2005, 02:20:38
You assign values the userlist keys and increase the number OnTimer() and if it reaches the value representing 5 mins you remove user or do anything you like. Gonna make example tomorrow.
Title:
Post by: TTB on 15 April, 2005, 02:26:22
thanx bastya!  :D
Title:
Post by: bastya_elvtars on 15 April, 2005, 10:42:45
SetTimer(60000) -- 1 minute
StartTimer()

-- add users this way on MYINFOArrival or NewUserConnected:
userlist[user.sName]=0

function OnTimer()
for a,b in userlist do
if b~=5 then
b=b+1
else
a=nil
end
end
end
Title:
Post by: TTB on 15 April, 2005, 15:45:51
Hi,

I can't get it work... This is maybe because there is already a timer running...:

-- Temp Tables --
warned = {}
warned02 = {}
tTimer = {}
tTimer02 = {}
userlist = {}


-- Main --
function Main()
frmHub:RegBot(bot)
RegTimer(SearchForBadFiles, 20 * 1000, "ActiveBadFileSearch")
SetTimer(1000)
StartTimer()
end


-- Action --
if userlist[sUser.sName] == nil then
>> warn
if userlist[sUser.sName] == nil then
userlist[sUser.sName] = 1
end
end


-- Timers --
RegTimer = function(Function,Interval,str)
   local tmpTrig = Interval / 1000
   table.insert(tTimer,{Function,tmpTrig,1,str})
end

function OnTimer()
for i=1,table.getn(tTimer) do
tTimer[i][3] = tTimer[i][3] + 1
if tTimer[i][3] > tTimer[i][2] then
tTimer[i][3]=1
tTimer[i][1]()
end
end
for a,b in userlist do
if b~=300 then
b=b+1
else
a=nil
end
end
end

This is indeed a piece of the code from NightLitch : Advanced BadFile Active Search // Stripped from Thor 6

I'm kind of optimizing it :) Do you (bastya), or others have suggestions to make it work?
Title:
Post by: Dessamator on 15 April, 2005, 16:09:42
Quoteif userlist[sUser.sName] == nil then

>> warn

if userlist[sUser.sName] == nil then

userlist[sUser.sName] = 1

end

end

 

i dont get that part if something is nil, and if something is nil???, whats the repetition for?, evaluate that, mayb thats the prob !!!

and the table tTimer, is always nil, as far as i can c
Title:
Post by: TTB on 15 April, 2005, 16:15:50
Explaination:

if userlist[sUser.sName] == nil then
>> if in table userlist username = nil then

>> warn (this is just an action) When action is done, in the table the user will be given the number 1:

if userlist[sUser.sName] == nil then
userlist[sUser.sName] = 1

end

So, first table is empty. When the whole function starts, it checks if the user has given the number 0 (when it is empty, table with user is 0). If it is 0, then it will warn (action)...

This is what I think it means, correct me if I'm wrong!
Title:
Post by: Dessamator on 15 April, 2005, 16:45:18
its useless to check if twice, on the same function, just write it like this::

       
if userlist[sUser.sName] == nil then
--warn
--userlist[sUser.sName] = 1

end

Title:
Post by: TTB on 15 April, 2005, 16:54:47
u r right  :D
Title:
Post by: TTB on 17 April, 2005, 17:34:19
It still doesn't work right..

someone who can help me out with this?  ?(

thanx
Title:
Post by: Sedulus on 17 April, 2005, 21:38:37
you don't need a timer
just look up the time of the elements everytime you access them, and remove the old (>5mins) crap
Title:
Post by: Dessamator on 17 April, 2005, 21:51:49
hmm, sedulus , i think he needs an example to learn from !!!
Title:
Post by: plop on 17 April, 2005, 23:07:08
after a pm with TTB from dc dev i made the next script.
sBot = "warner_bross"

tDelay = {}

function Main()
   SetTimer(60* 1000)
   StartTimer()
end

function ChatArrival(user, data)
   data = string.sub(data, 1, (string.len(data) - 1))
   local s,e,sCmd,sVictem,sReason = string.find(Data, "%b<>%s+%p(%S+)%s*(%S*)%s*(%S*)")
   if sCmd and sCmd == "warn" then
if sVictem ~= "" then
  local oVictem = GetItemByName(sVictem)
  if oVictem then
if not tDelay[oVictem.sName] then
  if sReason == "" then
sReason = "no reason given"
  end
  tDelay[oVictem.sName] = 0
  oVictem:SendData(sBot, "--WARNING-- "..sReason.." --WARNING-- ")
end
  else
user:SendData(sBot, sVictem.." is a unknown user!")
  end
else
  user:SendData(sBot, "no username given!")
end
   end
end

function OnTimer()
   for user,timer in pairs(tDelay) do
if timer ~= 5 then
  tDelay[user] = tDelay[user] + 1
else
  tDelay[user] = nil
end
   end
end

and here's 1 to the idea of sedules (no timer used)
sBot = "warner_bross"
iDelay = 5 * 60 -- (mins)

tDelay = {}

function ChatArrival(user, data)
   data = string.sub(data, 1, (string.len(data) - 1))
   local s,e,sCmd,sVictem,sReason = string.find(Data, "%b<>%s+%p(%S+)%s*(%S*)%s*(%S*)")
   if sCmd and sCmd == "warn" then
if sVictem ~= "" then
  local oVictem = GetItemByName(sVictem)
  if oVictem then
if not tDelay[oVictem.sName] or ( (os.clock() / 60) - iDelay ) > tDelay[oVictem.sName] then
  Warn(oVictem, sReason)
end
  else
user:SendData(sBot, sVictem.." is a unknown user!")
  end
else
  user:SendData(sBot, "no username given!")
end
   end
end

function Warn(oVictem, sReason)
   if sReason == "" then
sReason = "no reason given"
   end
   tDelay[oVictem.sName] = os.clock()
   oVictem:SendData(sBot, "--WARNING-- "..sReason.." --WARNING-- ")
end

both scripts aren't tested, it's up 2 TTB 2 debug it.

plop
Title:
Post by: TTB on 17 April, 2005, 23:34:22
Hi, first of all... PLOP thanx for your great help!

Secondly... tiny mini mistake made by you:

function ChatArrival(user, data)
will give:
local s,e,sCmd,sVictem,sReason = string.find(data, "%b<>%s+%p(%S+)%s*(%S*)%s*(%S*)")
so no uppercase in Data.

Well.. The script by bastya seems to be almost right. I had to use this:

for user,timer in pairs(userlist) do
if timer ~= time01 then
userlist[user] = userlist[user] + 1
else
userlist[user] = nil
end
end

time01 is defined by myself.

Thanx all ppl for helping me out! Now I can move on with this script...
Title:
Post by: bastya_elvtars on 17 April, 2005, 23:43:37
Credits to be given: I got the idea from MiniMultiTimer by Phatty, just made it a bit more solid. Here is my timer function from LawMaker:

function OnTimer()
if (tmr < 60) then tmr = tmr + 1;
else
local h = tonumber((os.date("%H")/2)) -- is it time to remember every1 what the bot is called?
if ( (math.floor(h) == h) and (tonumber(os.date("%M")) == 0) ) then botversion();end;
tmr=0;
end
-- take care of our disabled people
for a,b in MUTE do b=b+1; if (b==mutetime*60) then MUTE[a] = nil;end;end;

if timer<3600 then
timer=timer+1
else
Statistics_Refresh() -- parse the stats every hour
timer=0
Clear()
end
if ahm<900 then
ahm=ahm+1
else
ahm=0
antihammer=nil
Clear()
antihammer={}
end
end
Title:
Post by: TTB on 17 April, 2005, 23:47:54
Nice info  :]

It goes step by step to learn this wonderful language...
Title:
Post by: plop on 18 April, 2005, 00:08:16
also check my texter 4.5 for a more advanced multitimer system.

plop