Advertisement
Guest User

scoreboard.lua

a guest
Jan 14th, 2017
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.08 KB | None | 0 0
  1. local component = require( "component" )
  2. local gpu = component.gpu
  3. local event = require( "event" )
  4. local filesystem = require("filesystem")
  5. local internet = require("internet")
  6. local oldW, oldH = gpu.getResolution()
  7. local url = "http://writeescape.com/points.php"
  8. gpu.setResolution( 160, 50 )
  9.  
  10. JSON = (loadfile "JSON.lua")()
  11.  
  12. function compareCaseInsensitive(a, b)
  13.   return a:lower() < b:lower()
  14. end
  15.  
  16. function getScoreboard()
  17.   local payload = ""
  18.   for chunk in internet.request(url) do
  19.     payload = payload .. chunk
  20.   end
  21.   local scores = JSON:decode(payload)
  22.   local temp = {}
  23.   for n in pairs(scores) do
  24.     table.insert(temp, n)
  25.   end
  26.   table.sort(temp, compareCaseInsensitive)
  27.   local i = 0
  28.   local iter = function()
  29.     i = i + 1
  30.     if temp[i] == nil then
  31.       return nil
  32.     else
  33.       return temp[i], scores[temp[i]]
  34.     end
  35.   end
  36.   return iter
  37. end
  38.  
  39. function clearScreen()
  40.   local oldColor = gpu.getBackground( false )
  41.   local w,h = gpu.getResolution()
  42.   gpu.setBackground( 0x000000, false )
  43.   gpu.fill( 1, 1, w, h, " " )
  44.   gpu.setBackground( oldColor, false )
  45. end
  46.  
  47. function progressBar( label, y, value, maxVal, color, show, unit )
  48.   local oldColor = gpu.getBackground( false )
  49.   gpu.setBackground(0x000000, false)
  50.   gpu.fill( 3, y, 155, 2, " " )
  51.   w = math.floor( value * (75 / maxVal) )
  52.   p = math.floor( (w / 155) * 100 )
  53.   gpu.set( 3, y, label .. ": " .. tostring( value ) .. " Points" )
  54.   gpu.setBackground( 0x222222, false )
  55.   gpu.fill( 3, y+2, 155, 2, " " )
  56.   gpu.setBackground( color, false )
  57.   gpu.fill( 3, y+2, w, 2, " " )
  58.   gpu.setBackground( oldColor, false )
  59.   if show then
  60.     local valStr = formatBig( value ) .. unit
  61.     local n = string.len( valStr )
  62.     gpu.set( 78 - n, y, valStr )
  63.   end
  64. end
  65.  
  66. clearScreen()
  67. local max = 100
  68. local cur = 0
  69.  
  70. local yLevel = 10
  71.  
  72.  
  73. while true do
  74.   local _,_,x,y = event.pull( 1, "touch" )
  75.   if x and y then goto quit end
  76.  
  77.   for name, value in getScoreboard() do
  78.     progressBar(name, yLevel, value["pts"], value["total"], value["color"], false, "Points")
  79.     yLevel = yLevel + 5
  80.   end
  81.   yLevel = 10
  82.  
  83.   os.sleep(5)
  84. end
  85.  
  86.  
  87. ::quit::
  88. gpu.setResolution( oldW, oldH )
  89. clearScreen()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement