Advertisement
EnterYourName

CCChatBox

Dec 19th, 2019
320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.61 KB | None | 0 0
  1. local chatbox = peripheral.wrap("bottom")
  2. local stats = {}
  3.  
  4. function split (inputstr)
  5.     local t={}
  6.     for str in string.gmatch(inputstr, "([^%s]+)") do
  7.         table.insert(t, str)
  8.     end
  9.     return t
  10. end
  11.  
  12. function broadcast(message)
  13.     chatbox.say("§7"..message,10E308,true)
  14. end
  15.  
  16. function serialize(table)
  17.     return textutils.serialize(table)
  18. end
  19.  
  20. function deserialize(string)
  21.     return textutils.unserialize(string)
  22. end
  23.  
  24. function getStat(player, key)
  25.     if stats[player] == nil then
  26.         return 0
  27.     elseif stats[player][key] == nil then
  28.         return 0
  29.     else
  30.         return stats[player][key]
  31.     end
  32. end
  33.  
  34. function incStat(player, key)
  35.     if stats[player] == nil then
  36.         stats[player] = {}
  37.         stats[player][key] = 1
  38.         return 1
  39.     elseif stats[player][key] == nil then
  40.         stats[player][key] = 1
  41.         return 1
  42.     else
  43.         stats[player][key] = stats[player][key] + 1
  44.         return stats[player][key]
  45.     end
  46. end
  47.  
  48. function setStat(player, key, value)
  49.     if stats[player] == nil then
  50.         stats[player] = {}
  51.         stats[player][key] = value
  52.     else
  53.         stats[player][key] = value
  54.     end
  55. end
  56.  
  57. function save()
  58.     local h = fs.open("data","w")
  59.     h.writeLine(serialize(stats))
  60.     h.close()
  61. end
  62.  
  63. function load()
  64.     if not fs.exists("data") then
  65.         stats = {}
  66.         return
  67.     end
  68.     local h = fs.open("data","r")
  69.     local content = h.readAll()
  70.     if content == "" then
  71.         content = "{}"
  72.     end
  73.     h.close()
  74.     stats = deserialize(content)
  75. end
  76.  
  77. function chatEvent()
  78.     local _,player,message = os.pullEvent("chat")
  79.     onChat(player,message)
  80. end
  81.  
  82. function deathEvent()
  83.     local _,player = os.pullEvent("chat_death")
  84.     onDeath(player)
  85. end
  86.  
  87. function start()
  88.     load()
  89.     while true do
  90.         parallel.waitForAny(deathEvent,chatEvent)
  91.     end
  92. end
  93.  
  94. function onChat(player, message)
  95.  
  96.     local splitted = split(message)
  97.     local length = #splitted
  98.     local isCommand = false
  99.  
  100.     if splitted[1] == "!stats" then
  101.         isCommand = true
  102.         local name = player
  103.         if(length > 1) then
  104.             name = splitted[2]
  105.         end
  106.         local deaths = getStat(name, "deaths")
  107.         local messages = getStat(name, "messages")
  108.         local commands = getStat(name, "commands")
  109.         if name == player then
  110.             name = "You"
  111.         end
  112.         local cmdText = "commands"
  113.         local msgText = "messages"
  114.         local deathText = "times"
  115.         if deaths == 1 then
  116.             deathText = "time"
  117.         end
  118.         if messages == 1 then
  119.             msgText = "message"
  120.         end
  121.         if commands == 1 then
  122.             cmdText = "command"
  123.         end
  124.         broadcast(name.." died "..deaths.." "..deathText.." already, sent "..messages.." "..msgText.." and executed "..commands.." bot "..cmdText..".")
  125.     elseif splitted[1] == "!leaderboard" then
  126.         isCommand = true
  127.         local key = "deaths"
  128.         if length > 1 then
  129.             key = splitted[2]
  130.         end
  131.         local top10 = {}
  132.         for name,v in pairs(stats) do
  133.             local value = 0
  134.             if v[key] ~= nil then
  135.                 value = v[key]
  136.             end
  137.             if value > 0 then
  138.                 insertTop10(top10, name, value)
  139.             end
  140.         end
  141.  
  142.         if top10[1] == nil then
  143.             broadcast("Nobody has any stats registered to that key, try messages, commands or deaths")
  144.         else
  145.             broadcast("§m------------ §7 Top 10 "..key.." §m------------")
  146.             for i=1,10 do
  147.                if top10[i] == nil then
  148.                    break
  149.                end
  150.                broadcast("- #"..i..": "..top10[i]["name"].." - "..top10[i]["value"])
  151.             end
  152.             broadcast("§m--------------------------------")
  153.         end
  154.     end
  155.     if isCommand then
  156.         incStat(player, "commands")
  157.     end
  158.     incStat(player, "messages")
  159.     save()
  160. end
  161.  
  162. function insertTop10(top10, name, value)
  163.     for i=1,10 do
  164.         if top10[i] == nil then
  165.             top10[i] = {["name"]=name,["value"]=value }
  166.             break
  167.         elseif top10[i]["value"] < value then
  168.             for j=10,i+1,-1 do
  169.                 if top10[j-1] ~= nil then
  170.                     top10[j] = top10[j-1]
  171.                 end
  172.             end
  173.             top10[i] = {["name"]=name,["value"]=value }
  174.             break
  175.         end
  176.     end
  177. end
  178.  
  179. function onDeath(player)
  180.  
  181.     local tode = incStat(player,"deaths")
  182.     broadcast(player.." died "..tode.." times.")
  183.     print(player.." died "..tode.." times.")
  184.     save()
  185. end
  186.  
  187. start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement