osmarks

SCPanopticon

Oct 28th, 2018 (edited)
3,168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.00 KB | None | 0 0
  1. if not fs.exists "json" then local a=http.get"https://raw.githubusercontent.com/rxi/json.lua/bee7ee3431133009a97257bde73da8a34e53c15c/json.lua"local b=fs.open("json","w")b.write(a.readAll())a.close()b.close() end
  2. local json = require "/json"
  3. local dynmap = settings.get "tracker.map" or "https://dynmap.switchcraft.pw/"
  4. local API = dynmap .. "up/world/world/"
  5. local mon = peripheral.find "monitor"
  6. if mon then mon.setTextScale(0.5) term.redirect(mon) end
  7.  
  8. local function fetch(url)
  9.     local h = http.get(url)
  10.     local o = h.readAll()
  11.     h.close()
  12.     return o
  13. end
  14.  
  15. local worlds = {
  16.     world = "overworld",
  17.     DIM1_the_end = "end",
  18.     DIM1 = "end",
  19.     ["DIM-1"] = "nether",
  20.     ["DIM-11325"] = "deepdark"
  21. }
  22.  
  23. local function render_coords(player)
  24.     return ("%0d %0d %0d"):format(player.x, player.y, player.z)
  25. end
  26.  
  27. local function render_pos(player)
  28.     local coords = render_coords(player)
  29.     if player.x == 0 and player.y == 64 and player.z == 0 then coords = "[unknown]" end
  30.     return coords .. " " .. (worlds[player.world] or player.world)
  31. end
  32.  
  33. local w, h = term.getSize()
  34.  
  35. local function longest_by(t, k)
  36.     local sofar = 0
  37.     for _, v in pairs(t) do
  38.         if k then v = v[k] end
  39.         if #v > sofar then
  40.             sofar = #v
  41.         end
  42.     end
  43.     return sofar
  44. end
  45.  
  46. while true do
  47.     local raw = fetch(API .. os.epoch "utc")
  48.     local data = json.decode(raw)
  49.     local players = data.players
  50.     term.setBackgroundColor(colors.black)
  51.     term.clear()
  52.     term.setTextColor(colors.black)
  53.     local max_name_length = longest_by(players, "account")
  54.     for rowix, player in ipairs(players) do
  55.         function moveto(x) term.setCursorPos(x, rowix) end
  56.         function at(x, txt) moveto(x) term.write(txt) end
  57.         if rowix % 2 == 0 then term.setBackgroundColor(colors.white)
  58.         else term.setBackgroundColor(colors.lightGray) end
  59.  
  60.         moveto(1)
  61.         term.clearLine()
  62.  
  63.         at(1, player.account)
  64.         at(max_name_length + 2, render_pos(player))
  65.         at(w - 5, ("%0d"):format(player.health))
  66.         at(w - 2, ("%0d"):format(player.armor))
  67.     end
  68.     if #raw < 2048 then
  69.         sleep(1)
  70.     else
  71.         sleep(10)
  72.     end
  73. end
Add Comment
Please, Sign In to add comment