antonsavov

WIP tracker

Nov 29th, 2017
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.89 KB | None | 0 0
  1. -------------------------------
  2. -- /lib/tracker
  3. -------------------------------
  4.  
  5. --PLAYER TRACKING HELP SCRIPTS
  6.  
  7. function getScore(playername,scorename)
  8.     local result,message = commands.scoreboard("players","add",playername,scorename,0)
  9.     --if result == false then print(result,message[1]) end
  10.     if result then
  11.         local wordpattern = "[^, ]+"
  12.         local words = {}
  13.         for word in string.gmatch(message[1], wordpattern) do table.insert(words,word) end
  14.         local scoreobj = {name=scorename,count=tonumber(words[#words])}
  15.         --print(message[1])
  16.         --print("Score: "..scoreobj.name.." : "..scoreobj.count)
  17.         return scoreobj
  18.     end
  19.    
  20.     return {name=score,count=0}
  21. end
  22.  
  23. --for player name, create a lua object of all scoreboard scores
  24. local function tallyScores(name,boards)
  25.     local tally = {}
  26.     for i,board in ipairs(boards) do
  27.         table.insert(tally,getScore(name,board))
  28.     end
  29.     return tally
  30. end
  31.  
  32. --provide a list of reoccuring strings and return a list with unique strings and a count
  33. function tallyTable(list)
  34.     local tally = {}
  35.     for _,item in ipairs(list) do
  36.         if tally[item] then
  37.             tally[item] = tally[item] +1
  38.         else
  39.             tally[item] = 1
  40.         end
  41.     end
  42.     return tally
  43. end
  44.  
  45. local function printRecord(record)
  46.     print(record.name)
  47.     for i, score in ipairs(record.scores) do
  48.         print("-->  "..score.name..":  "..score.count)
  49.     end
  50. end
  51.  
  52. --creates a list of all scores sorted by player
  53. local function tallyPlayers(playerlist,boards)
  54.     local results = {}
  55.    
  56.     for i, player in ipairs(playerlist) do
  57.         table.insert(results,{name=player.name,scores=tallyScores(player.name,boards)})
  58.     end
  59.     return results
  60. end
  61.  
  62. local function createTotals(tallies)
  63.     local results = {}
  64.     for i, record in ipairs(tallies) do
  65.         local count = 0
  66.         for j, score in ipairs(record.scores) do
  67.             if score.count > 0 then
  68.                 count = count +1
  69.             end
  70.         end
  71.         table.insert(results,{name=record.name,total=count})
  72.     end
  73.     return results
  74. end
Add Comment
Please, Sign In to add comment