--Ranking Bot
--By Dessamator
---tables & files
ranking = {}
fRanking ="ranks.dat"
---
Bot = frmHub:GetHubBotName() -- bot name
-- Activated on startup/then script is started
function Main()
LoadFromFile(fRanking)
frmHub:RegBot(Bot)
end
-- Activated then script is stoped/hub closes
function OnExit()
SaveToFile(fRanking , ranking , "ranking")
end
------>Tables Processing functions by Nerbos<------------------
function GetPosition(table)
local pos = 0
while 1 do
if (table[pos] == nil) then
return pos
else
pos = pos + 1
end
end
end
-- Incoming chat message from user. If script return 1 hub don't process data.
function ChatArrival(curUser,sdata)
nickname=curUser
sdata = string.sub(sdata,1,string.len(sdata)-1)
s,e,cmd = string.find(sdata, "%b<>%s+(%S+)")
if cmd=="!rank" then
local s,e,nick,status = string.find(sdata,"%b<>%s+%S+%s+(%S+)%s+(.*)")
if nick==nil or status== "" then
curUser:SendData("Syntax Error. Correct syntax is : !rank eg.: !rank frosty w ")
elseif not(nick==nil) and status=="n" then
processranking("n",nick)
elseif status=="w" then
processranking("Victories",nick)
elseif status=="l" then
processranking("Defeats",nick)
elseif status=="d" then
processranking("Draws",nick)
end
return 1
elseif cmd=="!showranks" then
showrank(curUser,sdata)
return 1
elseif cmd=="!resetranking" then
ranking=nil
ranking={}
curUser:SendData(Bot,"All Rankings have been deleted !!!")
return 1
elseif cmd=="!delrank" then
local s,e,victim = string.find(sdata, "%b<>%s+%S+%s+(.+)")
Delrank(victim)
return 1
end
end
--- tables processing function -----------
function processranking(value,nick)
if value == "n" and not(nick==nil) then
local pos = GetPosition(ranking)
ranking[pos] = {}
ranking[pos]["Name"] = nick
ranking[pos]["Victories"] = "0"
ranking[pos]["Defeats"] = "0"
ranking[pos]["Draws"] = "0"
nickname:SendPM(Bot,"New name added!!")
else
local pos,table
for pos,table in ranking do
if not(ranking[pos][value]==nil) and ranking[pos]["Name"]==nick then
score=ranking[pos][value]
score = score + 1
ranking[pos][value] = score
temp="The number of "..value.." for "..nick.." has been increased by 1 !!!"
elseif not(ranking[pos]["Name"]==nick) then
temp="That user is not in the ranking table, add the user first. eg.: !rank Toasty n"
end
end
nickname:SendPM(Bot,temp)
end
end
-------------><------------------------
function Delrank(nick)
SendToAll(nick)
local pos,table
for pos,table in ranking do
if ranking[pos]["Name"] == nick then
ranking[pos]=nil
tmp=nick.." has been deleted from the ranking table"
end
end
nickname:SendPM(Bot,tmp)
end
ToArrival = ChatArrival
--Show the ranking ----
function showrank(user,sdata)
if isEmpty(ranking) then
user:SendData(Bot,"Nothing to show, The Ranking Table is empty !!!")
else
local temp,pos, table = "\t".."Rankings:".."\r\n\r\n".."\t".."Name".."\t\t".."Victories".."\t\t".."Defeats".."\t\t".."Draws".."\r\n"
for pos,table in ranking do
temp =temp.."\t"..ranking[pos]["Name"]
temp =temp.."\t\t"..ranking[pos]["Victories"]
temp =temp.."\t\t"..ranking[pos]["Defeats"]
temp =temp.."\t\t"..ranking[pos]["Draws"].."\r\n"
end
user:SendPM(Bot,temp)
end
end
--><----
function Serialize(tTable, sTableName, sTab)
assert(tTable, "tTable equals nil");
assert(sTableName, "sTableName equals nil");
assert(type(tTable) == "table", "tTable must be a table!");
assert(type(sTableName) == "string", "sTableName must be a string!");
sTab = sTab or "";
sTmp = ""
sTmp = sTmp..sTab..sTableName.." = {\n"
for key, value in tTable do
local sKey = (type(key) == "string") and string.format("[%q]",key) or string.format("[%d]",key);
if(type(value) == "table") then
sTmp = sTmp..Serialize(value, sKey, sTab.."\t");
else
local sValue = (type(value) == "string") and string.format("%q",value) or tostring(value);
sTmp = sTmp..sTab.."\t"..sKey.." = "..sValue
end
sTmp = sTmp..",\n"
end
sTmp = sTmp..sTab.."}"
return sTmp
end
function SaveToFile(file , table , tablename)
local handle = io.open(file,"w+")
handle:write(Serialize(table, tablename))
handle:flush()
handle:close()
end
function LoadFromFile(file)
local handle = io.open(file,"r")
if (handle ~= nil) then
dofile(file)
handle:flush()
handle:close()
end
end
-------------table checker by herodes
--- for an associative table, like ["smth"] = "smth else",
function isEmpty(t)
for i,v in t do
return false;
end
return true;
end;
----------------------------------------------------------
:D
Nice one m8 (again) ;)
what's the use of this script
QuoteOriginally posted by kash?
what's the use of this script
silly me i thought the name of the script was enough, anyways ,
youmay use it in any way you see fit, but its meant to record the rankings of people in a certain game or something !!