Advertisement
Guest User

Code

a guest
Dec 15th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.47 KB | None | 0 0
  1. --[[
  2. @projectDescription Basic scoreboard implementation
  3. @author Mike D Sutton
  4. @version 1.0.0
  5. @remarks IMPORTANT:
  6. Requires an attached ChatBox from MiscPeripherals mod pack
  7. ]]
  8.  
  9. local Scoreboard
  10.  
  11. (function()
  12. -- Return the key names in a table
  13. local function getKeys(t)
  14. local ret = {}
  15.  
  16. for k,_ in pairs(t) do
  17. table.insert(ret, k)
  18. end
  19.  
  20. return ret
  21. end
  22.  
  23. Scoreboard = {
  24. -- Format of score table:
  25. -- killer: { victim: count }
  26. scoreTable = {},
  27. config = {
  28. countSuicides = true
  29. },
  30. reset = function(self)
  31. self.scoreTable = {}
  32. end
  33. }
  34.  
  35. --[[
  36. Register a kill
  37. @param {table} self Scoreboard (implicit if called using ':')
  38. @param {string} player Player name who died
  39. @param {string} killer Player/Mob name who killed player
  40. @param {string} cause Reason for death (currently unused)
  41. ]]
  42. Scoreboard.addKill = function(self, player, killer, cause)
  43. if (killer == nil) then
  44. killer = player -- Suicide
  45. end
  46.  
  47. local kills = self.scoreTable[killer]
  48. if (kills == nil) then
  49. kills = {}
  50. self.scoreTable[killer] = kills
  51. end
  52.  
  53. kills[player] = (kills[player] or 0) + 1
  54. end
  55.  
  56. --[[
  57. Lookup all known player names (killers and victims)
  58. @param {table} self Scoreboard (implicit if called using ':')
  59. @returns {table} List of player names
  60. ]]
  61. Scoreboard.getKnownPlayerNames = function(self)
  62. local known = {}
  63.  
  64. for k,v in pairs(self.scoreTable) do
  65. known[k] = true
  66.  
  67. for l,_ in pairs(v) do
  68. known[l] = true
  69. end
  70. end
  71.  
  72. return getKeys(known)
  73. end
  74.  
  75. --[[
  76. Lookup the number of kills this player made
  77. @param {table} self Scoreboard (implicit if called using ':')
  78. @param {string} playerName Player name
  79. @param {boolean} countSuicides Should suicides be included in count
  80. @returns {number} Kill count
  81. ]]
  82. Scoreboard.getPlayerKills = function(self, playerName, countSuicides)
  83. local playerKills, kills = self.scoreTable[playerName], 0
  84.  
  85. if (playerKills ~= nil) then
  86. for k,v in pairs(playerKills) do
  87. if ((k ~= playerName) or
  88. (countSuicides or self.config.countSuicides)) then
  89. kills = kills + v
  90. end
  91. end
  92. end
  93.  
  94. return kills
  95. end
  96.  
  97. --[[
  98. Lookup the number of times this player died
  99. @param {table} self Scoreboard (implicit if called using ':')
  100. @param {string} playerName Player name
  101. @param {boolean} countSuicides Should suicides be included in count
  102. @returns {number} Death count
  103. ]]
  104. Scoreboard.getPlayerDeaths = function(self, playerName, countSuicides)
  105. local deaths = 0
  106.  
  107. for k,v in pairs(self.scoreTable) do
  108. if ((k ~= playerName) or
  109. (countSuicides or self.config.countSuicides)) then
  110. local killCount = v[playerName]
  111.  
  112. if (killCount ~= nil) then
  113. deaths = deaths + killCount
  114. end
  115. end
  116. end
  117.  
  118. return deaths
  119. end
  120.  
  121. --[[
  122. Lookup a player's score
  123. @param {table} self Scoreboard (implicit if called using ':')
  124. @param {string} playerName Player name
  125. @returns {number, number, number} Kills, deaths, and KDR
  126. ]]
  127. Scoreboard.getPlayerScore = function(self, playerName, countSuicides)
  128. local kills, deaths, KDR =
  129. self:getPlayerKills(playerName, countSuicides),
  130. self:getPlayerDeaths(playerName, countSuicides)
  131.  
  132. if (deaths > 0) then
  133. KDR = kills / deaths
  134. else
  135. KDR = kills
  136. end
  137.  
  138. return kills, deaths, KDR
  139. end
  140.  
  141. --[[
  142. Basic scoreboard display implementation - Printed to terminal
  143. @param {table} self Scoreboard (implicit if called using ':')
  144. ]]
  145. Scoreboard.printScores = function(self)
  146. local players, maxNameLen = self:getKnownPlayerNames(), 0
  147. local scoreData = {}
  148.  
  149. -- Work out longest player name length
  150. for _,v in ipairs(players) do
  151. if (#v > maxNameLen) then
  152. maxNameLen = #v
  153. end
  154.  
  155. local playerData = { self:getPlayerScore(v) }
  156. table.insert(playerData, 1, v)
  157. table.insert(scoreData, playerData)
  158. end
  159.  
  160. -- Sort scores by kills
  161. table.sort(scoreData,
  162. function(a, b)
  163. return a[2] > b[2]
  164. end)
  165.  
  166. pad = function(v, len, left, char)
  167. local ret = tostring(v or "")
  168. local pad = string.rep(
  169. (char or " "), math.max(len - #ret, 0))
  170.  
  171. return (left and pad or "") .. ret .. (left and "" or pad)
  172. end
  173.  
  174. print("\n", pad("Name ", maxNameLen + 1), "Kills Deaths KDR")
  175. print(string.rep("-", math.max(maxNameLen, 4) + 23))
  176.  
  177. for _,v in ipairs(scoreData) do
  178. print(pad(v[1], maxNameLen + 1),
  179. pad(v[2], 5, true), " ",
  180. pad(v[3], 6, true), " ",
  181. pad(pad(string.format("%f",
  182. math.floor((v[4] * 100) + 0.5) / 100), 4, false, "0"),
  183. 6, true))
  184. end
  185.  
  186. print(string.rep("-", math.max(maxNameLen, 4) + 23), "\n")
  187. end
  188. end)()
  189.  
  190. -- Register event handlers
  191. local working = true
  192. local eventHandlers = {
  193. chat_death = function(player, killer, cause)
  194. Scoreboard:addKill(player, killer, cause)
  195. end,
  196. key = function(keycode)
  197. if (keycode == keys.q) then -- Quit
  198. working = false
  199. elseif (keycode == keys.r) then -- Reset
  200. print("Resetting scroes")
  201. Scoreboard:reset()
  202. elseif (keycode == keys.p) then -- Print scores
  203. Scoreboard:printScores()
  204. end
  205. end
  206. }
  207.  
  208. print("Listening.\n\t'q' to quit\n\t'r' to reset\n\t'p' to print scores")
  209.  
  210. -- Main app loop
  211. while working do
  212. local evt = { os.pullEvent() }
  213. local handler = eventHandlers[evt[1]]
  214.  
  215. if (handler) then
  216. -- Pop event name, and invoke handler with remaining params
  217. table.remove(evt, 1)
  218. handler(unpack(evt))
  219. end
  220. end
  221.  
  222. term.clear()
  223. term.setCursorPos(1, 1)
  224. term.write("Goodbye!")
  225. term.setCursorPos(1, 2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement