Slotbot For Lua5
 

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

Slotbot For Lua5

Started by Jelf, 28 March, 2005, 21:08:17

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Jelf

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

jiten


bolamix

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
Sharing is of the essence!

Live music >> Aiwadirock! live music hub
PtokaX knowledge >> The PtokaX Wiki

Jelf

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 :)

tezlo

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

Jelf

Didnt no you done a re-write tezlo,

nice job, looks alot tidier :D

SMF spam blocked by CleanTalk