PtokaX forum

Archive => Archived 5.0 boards => Finished Scripts => Topic started by: Jelf on 28 March, 2005, 21:08:17

Title: Slotbot For Lua5
Post by: Jelf on 28 March, 2005, 21:08:17
Here ya go....
-- SlotsBot for PtokaX DC Hub 0.3.3.0
-- On command +slots, sends a search and checks the results for free slots & total number of slots
-- Sends the results in PM from Hub bot
-- Adaptation by OpiumVolage (01 July 2003) based on
-- retrobot? v0.99?
-- slots module
-- by tezlo
-- 2003
-- Twiddled a bit to have results sent in PM - by bolamix 1 July 2003
-- unused sTarget left for future syntax parsing.
-- Converted to Lua5 by Jelf 28/03/05

botName = "-Slots-"

-- library
------------------
function getid(Table, name)
return table.foreachi(Table, function(id, item) if item.sName == name then return id end end)
end
------------------
function putitem(Table, item)
if not item then
return
end
local id = getid(Table, item.sName)
if id then
Table[id] = item
else
table.insert(Table, item)
id = Table.n
end
return id
end
-- events
------------------
function OnTimer()
local i = 1 while tabSlots[i] do
local Table = tabSlots[i]
if os.clock() >= Table.iClock + tmrSlots then
if Table.tItems.n == 0 then
SendPmToNick(Table.sName, botName, "No users with free slots found|")
end
SendPmToNick(Table.sName, botName, "Done|")
table.remove(tabSlots, i)
if tabSlots.n == 0 then
end
else i = i + 1
end
end
end
------------------
function doSlots(user, nr, nick)
user:SendPM(botName, "Checking..|")
SendToAll("$Search Hub:"..Command.." T?F?0?1?.|")
putitem(tabSlots, { sName = user.sName, iClock = os.clock(), iNR = nr, tItems = {n=0} })
end
 
tmrSlots = 5 -- Number of seconds waiting for replys

tabSlots = {n=0}

Command = "+slots"
-----------------
function ChatArrival(user, data)
if string.find(data, "%b<>%s+("..Command..").*|") then
s, e, nr = string.find(data, "%b<>%s+"..Command.."%s+([0-9]+)")
if s then
nr = tonumber(nr)
else
nr = 1
end
doSlots(user, nr)
return 1
end
end
----------------
function SRArrival(user, data)
local s, e, free, all = string.find(data, " (%d+)/(%d+)"..string.char(5))
table.foreachi(tabSlots, function(id, item)
if not getid(item.tItems, user.sName) then
putitem(item.tItems, { sName = user.sName, sValue = free.."/"..all })
if (tonumber(free) >= item.iNR) then
SendPmToNick(item.sName, botName, " user "..user.sName.." "..free.."/"..all.."|")
end
end end)
end
------------------
function Main()
frmHub:RegBot(botName)
SetTimer(tmrSlots*1000)
StartTimer()
end
Title:
Post by: jiten on 28 March, 2005, 21:13:59
Nice one, Jelf ;)
Title:
Post by: bolamix on 29 March, 2005, 10:53:03
Thanks a lot! Gonna test it as soon as I manage to make VNC work from my linux box and to access my hub pc :D
Title:
Post by: Jelf on 29 March, 2005, 11:06:52
ty guys, I didnt realise so many peeps wanted this, I tried converting it ages ago, but kept gettting errors, so I left it for a while.
Surprising what you pick up over time just doing conversions :)
Title:
Post by: tezlo on 29 March, 2005, 12:20:50
hey.. i didn't realize anyone still used this
this way you do have fresh info, but the hub gets spammed with search results on every command

that getid putitem stuff was just a bad idea and the timer is not really necessary either
i posted a rewrite here (http://board.univ-angers.fr/thread.php?boardid=13&threadid=512&page=1#3) using the same approach, it would make more sense to convert that

but here's another rewrite..
given that your hub allows passive users to search, this won't generate any extra bandwidth
for small hubs it may be necessary to refresh on a command or on a timer like it's done in Main
slots = {}
nslots = 0
timeout = 360 -- cache life in seconds
message = "%s has %d slots free [as of %d minutes ago]"
botname = frmHub:GetHubBotName()

function Main()
SendToAll("$Search Hub:"..botname.." T?F?0?1?.|")
end

function SRArrival(user, data)
local s, e, free, all = string.find(data, " (%d+)/(%d+)\005")
if free ~= "0" then
if not slots[user.sName] then
nslots = nslots + 1
end
slots[user.sName] = { tonumber(free), os.clock() }
end
end

function ChatArrival(user, data)
local s, e, cmd = string.find(data,"^%b<> [!+](%a+)")
if cmd == "slots" then
local now = os.clock()
for k, v in pairs(slots) do
if now > v[2] + timeout then
nslots = nslots - 1
slots[k] = nil
else
user:SendPM(botname, string.format(message, k, v[1], math.floor((now-v[2])/60)))
end
end
user:SendPM(botname, nslots.." items")
return 1
end
end
Title:
Post by: Jelf on 29 March, 2005, 14:41:02
Didnt no you done a re-write tezlo,

nice job, looks alot tidier :D