timer function
 

News:

29 December 2022 - PtokaX 0.5.3.0 (20th anniversary edition) released...
11 April 2017 - PtokaX 0.5.2.2 released...
8 April 2015 Anti child and anti pedo pr0n scripts are not allowed anymore on this board!
28 September 2015 - PtokaX 0.5.2.1 for Windows 10 IoT released...
3 September 2015 - PtokaX 0.5.2.1 released...
16 August 2015 - PtokaX 0.5.2.0 released...
1 August 2015 - Crowdfunding for ADC protocol support in PtokaX ended. Clearly nobody want ADC support...
30 June 2015 - PtokaX 0.5.1.0 released...
30 April 2015 Crowdfunding for ADC protocol support in PtokaX
26 April 2015 New support hub!
20 February 2015 - PtokaX 0.5.0.3 released...
13 April 2014 - PtokaX 0.5.0.2 released...
23 March 2014 - PtokaX testing version 0.5.0.1 build 454 is available.
04 March 2014 - PtokaX.org sites were temporary down because of DDOS attacks and issues with hosting service provider.

Main Menu

timer function

Started by TTB, 15 April, 2005, 01:57:39

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

TTB

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...
TTB

(? ?.??.-> Admin @ Surfnet hubs <-.??.???)

bastya_elvtars

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.
Everything could have been anything else and it would have just as much meaning.

TTB

TTB

(? ?.??.-> Admin @ Surfnet hubs <-.??.???)

bastya_elvtars

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
Everything could have been anything else and it would have just as much meaning.

TTB

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?
TTB

(? ?.??.-> Admin @ Surfnet hubs <-.??.???)

Dessamator

#5
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
Ignorance is Bliss.

TTB

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!
TTB

(? ?.??.-> Admin @ Surfnet hubs <-.??.???)

Dessamator

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
Ignorance is Bliss.

TTB

TTB

(? ?.??.-> Admin @ Surfnet hubs <-.??.???)

TTB

It still doesn't work right..

someone who can help me out with this?  ?(

thanx
TTB

(? ?.??.-> Admin @ Surfnet hubs <-.??.???)

Sedulus

you don't need a timer
just look up the time of the elements everytime you access them, and remove the old (>5mins) crap

Dessamator

hmm, sedulus , i think he needs an example to learn from !!!
Ignorance is Bliss.

plop

#12
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
http://www.plop.nl lua scripts/howto\'s.
http://www.thegoldenangel.net
http://www.vikingshub.com
http://www.lua.org

>>----> he who fights hatred with hatred, drives the spreading of hatred <----<<

TTB

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...
TTB

(? ?.??.-> Admin @ Surfnet hubs <-.??.???)

bastya_elvtars

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
Everything could have been anything else and it would have just as much meaning.

TTB

Nice info  :]

It goes step by step to learn this wonderful language...
TTB

(? ?.??.-> Admin @ Surfnet hubs <-.??.???)

plop

also check my texter 4.5 for a more advanced multitimer system.

plop
http://www.plop.nl lua scripts/howto\'s.
http://www.thegoldenangel.net
http://www.vikingshub.com
http://www.lua.org

>>----> he who fights hatred with hatred, drives the spreading of hatred <----<<

SMF spam blocked by CleanTalk