Ranking Bot
 

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

Ranking Bot

Started by Dessamator, 07 April, 2005, 21:53:46

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Dessamator

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

jiten

Nice one m8 (again)  ;)

kash?

what's the use of this script

Dessamator

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

SMF spam blocked by CleanTalk