PtokaX forum

Development Section => Your Developing Problems => Topic started by: nErBoS on 03 March, 2004, 19:07:11

Title: FreeSlot Checker malfunction...
Post by: nErBoS on 03 March, 2004, 19:07:11
Hi all,

Help me out where...
I get this part of code from guibs channelbot to put on my  bot but it seems not working well, where is the code..

Bot = "Teste"

tabSlots = {n=0}
tmrSlots = 10

function Main()
frmHub:RegBot(Bot)
end

function DataArrival(user, data)

if (strsub(data,1,1)=="<") or (strsub(data,1,5+strlen(Bot))=="$To: "..Bot) then
data=strsub(data,1,strlen(data)-1)
s,e,cmd = strfind(data,"%b<>%s+(%S+)")
if (cmd=="!slots") then
user:SendData(Bot, "SEARCHING...")
tinsert(tabSlots, { sName = user.sName, iClock = clock(), tItems = {} })
DoCheckFreeSlots = "on"
SendToAll("$Search Hub:"..Bot.." T?F?0?1?.|")
end
end

if DoCheckFreeSlots == "on" then
frmHub:EnableSearchData(1)
if (strsub(data, 1, 3) == "$SR") then
slots = 0
local s, e, nickname, descri, size, free, all = strfind(data, "%s+(%S+)%s+(.*)(%d+)%s(%d+)/(%d+)")
local i = 1
while tabSlots[i] do
local table = tabSlots[i]
local usr = GetItemByName(table.sName)
if not table.tItems[user.sName] then
slots = 0
if ((free ~= "0") and (all ~= nil) and (size~= nil) and (free ~= nil)) then
table.tItems[user.sName] = free.."/"..all
usr:SendData(Bot, " user "..user.sName.." "..free.."/"..all)
end
end

if (clock() >= table.iClock + tmrSlots) then
if slots==0 then
usr:SendData(Bot, "NO SLOTS FOUND")
end
usr:SendData(Bot, "DONE")
tremove(tabSlots, i)
DoCheckFreeSlots = "off"
frmHub:EnableSearchData(0)
else
i = i + 1
end
end
end
end
end

The problem is when i do !slots the script sends this..
[17:57] SEARCHING...
[17:57]  user nErBoS 2/2
and i wait more then 10 secs and i doesn't nothing else, but he should send me DONE.
When i type again !slots the script sends this..
[17:57] SEARCHING...
[17:57] NO SLOTS FOUND
[17:57] DONE
[17:57]  user nErBoS 2/2

In my opinion is missing a break on the while, but it wouldn't make any affect because the bug still happens

Can any one help me out here ??

Best regards, nErBoS
Title:
Post by: NotRabidWombat on 03 March, 2004, 19:51:40
I am assuming there are only two users on your test hub.

Here is what is happening when you test:

1) You begin a slot check.
   a) Sends out a search to all users
   b) Creates a table entry of the user that began the slot check. This table has an element that is the clock cycle this was started at
2) The first and only search result is returned. This block is executed:
if not table.tItems[user.sName] then
   slots = 0

   if ((free ~= "0") and (all ~= nil) and (size~= nil) and (free ~= nil)) then
      table.tItems[user.sName] = free.."/"..all

      usr:SendData(Bot, " user "..user.sName.." "..free.."/"..all)
   end
end
Here lies the first problem (not related to what you're doing). What if two people searched at exactly the same time? You will see doubles of everything AND the two users who intialized the search will not see each other's names. "not table.tItems[user.sName]" So this code is not designed for many users using it at the same time.

The next section of code is what annouces the stop:
if (clock() >= table.iClock + tmrSlots) then
   if slots==0 then
      usr:SendData(Bot, "NO SLOTS FOUND")
   end
   usr:SendData(Bot, "DONE")
   tremove(tabSlots, i)
   DoCheckFreeSlots = "off"
   frmHub:EnableSearchData(0)
else
   i = i + 1
end
This section of code is only executed upon receiving a $SR (Search Response).  So if there is only one user responding (and this user responds very quickly), there will be no way to detect if you have passed the timeout (10 milliseconds?). I suggest making a OnTimer function that handles removing the users from the tabSlots table.

It may also be wise to maintain an entire table of slot results. That way you can eliminate doubles and handle to slot requests within 0.5 seconds without sending out another search request.

-NotRabidWombat
Title:
Post by: nErBoS on 03 March, 2004, 20:53:39
Hi,

Thanks RabidWombat for the good explanion. Made a timer and solved the problem.

Best regards, nErBoS
Title:
Post by: Snooze on 20 March, 2004, 03:26:07
Any chance of you posting the finished slot script ? ;)


**Snooze
Title:
Post by: Herodes on 17 June, 2004, 02:51:42
QuoteOriginally posted by Snooze
Any chance of you posting the finished slot script ? ;)


true... :rolleyes:
Title:
Post by: plop on 17 June, 2004, 03:09:13
opiumvollage and 2 other have rewriten this script with the help from wombat in the past  2 make work it a bit better (testdrive+ only).
it's on my site on the page of opium.

plop