DrFair

Deathcounter

Jun 18th, 2013
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  1. m = peripheral.wrap("top")
  2. chat = peripheral.wrap("right")
  3. w,h = m.getSize()
  4. w = w+1
  5. h = h+1
  6. col = { ["white"]=1, ["orange"]=2, ["magenta"]=4, ["lightblue"]=8, ["yellow"]=16, ["lime"]=32, ["pink"]=64, ["gray"]=128, ["lightgray"]=256, ["cyan"]=512, ["purple"]=1024, ["blue"]=2048, ["brown"]=4096, ["green"]=8192, ["red"]=16384, ["black"]=32768 }
  7. popular = ""
  8.  
  9. function printData()
  10. local path = shell.resolve("data")
  11. data = { [1] = textutils.serialize(names), [2] = textutils.serialize(deaths) }
  12. if fs.exists("data") then
  13. fs.delete("data")
  14. end
  15. local file = fs.open(path,"w")
  16. file.write(textutils.serialize(data))
  17. file.close()
  18. end
  19.  
  20. function getData()
  21. local path = shell.resolve("data")
  22. if fs.exists("data") then
  23. local file = fs.open(path,"r")
  24. data = textutils.unserialize(file.readAll())
  25. names = textutils.unserialize(data[1])
  26. deaths = textutils.unserialize(data[2])
  27. return true
  28. else
  29. names = { [1] = "Fair" }
  30. deaths = { ["Fair"] = 0 }
  31. return false
  32. end
  33. end
  34.  
  35. function swrite(str,x,y,color)
  36. m.setCursorPos(x,y)
  37. m.setTextColor(col[color])
  38. m.write(str)
  39. end
  40.  
  41. function drawbox(str,x1,x2,y1,y2,strcol,color)
  42. m.setCursorPos(x1,y1)
  43. m.setBackgroundColor(col[color])
  44. m.setTextColor(col[strcol])
  45. local bw = x2-x1
  46. local bh = y2-y1
  47. local bstr = " "
  48. while #bstr < bw do
  49. bstr = bstr.." "
  50. end
  51. for i=1,bh do
  52. m.setCursorPos(x1,y1+i-1)
  53. m.write(bstr)
  54. end
  55. m.setCursorPos(x1+bw/2-#str/2,y1+bh/2)
  56. m.write(str)
  57. m.setBackgroundColor(col["black"])
  58. return { [1]=x1, [2]=x2, [3]=y1, [4]=y2 }
  59. end
  60.  
  61. function drawbackground(str,strcol,color)
  62. drawbox("",1,w,1,h,strcol,color)
  63. drawbox("",2,w-1,2,h-1,"black","black")
  64. drawbox(str,1,w,1,2,strcol,color)
  65. end
  66.  
  67. function printDeaths()
  68. m.clear()
  69. drawbackground("Death counter","black","white")
  70. for i=1,#names do
  71. swrite(names[i],3,2+i,"white")
  72. swrite(tostring(deaths[names[i]]),w-2-#tostring(deaths[i]),2+i,"white")
  73. end
  74. end
  75.  
  76. getData()
  77. printDeaths()
  78.  
  79. while true do
  80. event,pr1,pr2,pr3 = os.pullEvent()
  81. getData()
  82. print(tostring(event))
  83. print(tostring(pr1))
  84. if event == "chat_death" then
  85. print("Detected death.")
  86. for i=1,#names do
  87. if names[i] == pr1 then
  88. deaths[names[i]] = deaths[names[i]] + 1
  89. print("Found old name.")
  90. else
  91. table.insert(names,pr1)
  92. deaths[names[i]] = 1
  93. print("Added new name.")
  94. end
  95. end
  96. end
  97. printData()
  98. printDeaths()
  99. end
Advertisement
Add Comment
Please, Sign In to add comment