ZNZNCOOP

sorter.lua

Apr 1st, 2016
93
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local c = require('component')
  2. local fs = require('filesystem')
  3. local term = require('term')
  4. local atm = c.atm
  5.  
  6. function status(x, y, txt, len)
  7.     local private, public = {}, {}
  8.  
  9.     do
  10.         private.x = x
  11.         private.y = y
  12.         private.txt = txt
  13.         private.len = len
  14.         private.lastPercent = 0
  15.     end
  16.  
  17.     public.draw = function(percent)
  18.         percent = math.floor(percent)
  19.        if private.lastPercent < percent then
  20.          term.setCursor(private.x, private.y)
  21.          term.clearLine()
  22.          io.write(private.txt..'[')
  23.          local cp = 100 / len
  24.          local chars = math.floor(percent / cp)
  25.          for i = 1, chars do
  26.             io.write('#')
  27.          end
  28.          term.setCursor(private.x + len + #(private.txt..'['), y)
  29.          io.write(']'..math.floor(percent)..'%')
  30.          print()
  31.          private.lastPercent = percent
  32.         end
  33.     end
  34.     return public
  35. end
  36.  
  37. local players = atm.getAllPlayers()
  38. local playersBalance = {}
  39.  
  40. local load = status(1, 1, "Loading ", 20)
  41. local sort = status(1, 2, "Sorting ", 20)
  42. local save = status(1, 3, "Saving ", 20)
  43.  
  44. local len = #players
  45. for i = 1, len do
  46.     playersBalance[#playersBalance + 1] = {players[i], atm.getBalance(players[i])}
  47.     load.draw(i / (len / 100))
  48. end
  49.  
  50. local len = #playersBalance
  51. local count = 1
  52. for i = 1, len do
  53.     for q = 1, len do
  54.         if playersBalance[q][2] < playersBalance[i][2] then
  55.             local tmp = playersBalance[q]
  56.             playersBalance[q] = playersBalance[i]
  57.             playersBalance[i] = tmp
  58.         end
  59.         sort.draw(count / (len * len / 100))
  60.         count = count + 1
  61.     end
  62. end
  63.  
  64. local f = fs.open('rangs', 'w')
  65.  
  66. for i = 1, len do
  67.     f:write(playersBalance[i][1] .. " : " .. playersBalance[i][2].."\n")
  68.     save.draw(i / (len / 100))
  69. end
  70.  
  71. f:close()
RAW Paste Data