Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local chatbox = peripheral.wrap("bottom")
- local stats = {}
- function split (inputstr)
- local t={}
- for str in string.gmatch(inputstr, "([^%s]+)") do
- table.insert(t, str)
- end
- return t
- end
- function broadcast(message)
- chatbox.say("§7"..message,10E308,true)
- end
- function serialize(table)
- return textutils.serialize(table)
- end
- function deserialize(string)
- return textutils.unserialize(string)
- end
- function getStat(player, key)
- if stats[player] == nil then
- return 0
- elseif stats[player][key] == nil then
- return 0
- else
- return stats[player][key]
- end
- end
- function incStat(player, key)
- if stats[player] == nil then
- stats[player] = {}
- stats[player][key] = 1
- return 1
- elseif stats[player][key] == nil then
- stats[player][key] = 1
- return 1
- else
- stats[player][key] = stats[player][key] + 1
- return stats[player][key]
- end
- end
- function setStat(player, key, value)
- if stats[player] == nil then
- stats[player] = {}
- stats[player][key] = value
- else
- stats[player][key] = value
- end
- end
- function save()
- local h = fs.open("data","w")
- h.writeLine(serialize(stats))
- h.close()
- end
- function load()
- if not fs.exists("data") then
- stats = {}
- return
- end
- local h = fs.open("data","r")
- local content = h.readAll()
- if content == "" then
- content = "{}"
- end
- h.close()
- stats = deserialize(content)
- end
- function chatEvent()
- local _,player,message = os.pullEvent("chat")
- onChat(player,message)
- end
- function deathEvent()
- local _,player = os.pullEvent("chat_death")
- onDeath(player)
- end
- function start()
- load()
- while true do
- parallel.waitForAny(deathEvent,chatEvent)
- end
- end
- function onChat(player, message)
- local splitted = split(message)
- local length = #splitted
- local isCommand = false
- if splitted[1] == "!stats" then
- isCommand = true
- local name = player
- if(length > 1) then
- name = splitted[2]
- end
- local deaths = getStat(name, "deaths")
- local messages = getStat(name, "messages")
- local commands = getStat(name, "commands")
- if name == player then
- name = "You"
- end
- local cmdText = "commands"
- local msgText = "messages"
- local deathText = "times"
- if deaths == 1 then
- deathText = "time"
- end
- if messages == 1 then
- msgText = "message"
- end
- if commands == 1 then
- cmdText = "command"
- end
- broadcast(name.." died "..deaths.." "..deathText.." already, sent "..messages.." "..msgText.." and executed "..commands.." bot "..cmdText..".")
- elseif splitted[1] == "!leaderboard" then
- isCommand = true
- local key = "deaths"
- if length > 1 then
- key = splitted[2]
- end
- local top10 = {}
- for name,v in pairs(stats) do
- local value = 0
- if v[key] ~= nil then
- value = v[key]
- end
- if value > 0 then
- insertTop10(top10, name, value)
- end
- end
- if top10[1] == nil then
- broadcast("Nobody has any stats registered to that key, try messages, commands or deaths")
- else
- broadcast("§m------------ §7 Top 10 "..key.." §m------------")
- for i=1,10 do
- if top10[i] == nil then
- break
- end
- broadcast("- #"..i..": "..top10[i]["name"].." - "..top10[i]["value"])
- end
- broadcast("§m--------------------------------")
- end
- end
- if isCommand then
- incStat(player, "commands")
- end
- incStat(player, "messages")
- save()
- end
- function insertTop10(top10, name, value)
- for i=1,10 do
- if top10[i] == nil then
- top10[i] = {["name"]=name,["value"]=value }
- break
- elseif top10[i]["value"] < value then
- for j=10,i+1,-1 do
- if top10[j-1] ~= nil then
- top10[j] = top10[j-1]
- end
- end
- top10[i] = {["name"]=name,["value"]=value }
- break
- end
- end
- end
- function onDeath(player)
- local tode = incStat(player,"deaths")
- broadcast(player.." died "..tode.." times.")
- print(player.." died "..tode.." times.")
- save()
- end
- start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement