Advertisement
osmarks

anavrins' SC tracker

Nov 10th, 2018
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local a=http.get"https://raw.githubusercontent.com/rxi/json.lua/bee7ee3431133009a97257bde73da8a34e53c15c/json.lua"local b=fs.open("/libs/json.lua","w")b.write(a.readAll())a.close()b.close()
  2.  
  3. local json = require("/libs/json")
  4. local worldurl = "https://dynmap.switchcraft.pw/up/world/world/"
  5. local tX, tY = term.getSize()
  6. local reqdelay = 1
  7. local errdelay = 10
  8. local world = {}
  9.  
  10. local function writeAt(text, x, y, tc, bg)
  11.   term.setCursorPos(x, y)
  12.   term.setTextColor(tc or colors.white)
  13.   term.setBackgroundColor(bg or colors.black)
  14.   term.write(text)
  15. end
  16.  
  17. local function getMinecraftTime(servertime)
  18.     return {
  19.       hours = ((servertime / 1000) + 6) % 24,
  20.       minutes = ((servertime / 1000) % 1) * 60,
  21.       isDay = (servertime >= 0 and servertime < 13700),
  22.     }
  23. end
  24.  
  25. local function getWorld(world)
  26.   if world == "world" then return "Overworld"
  27.   elseif world == "-some-other-bogus-world-" then return "Nether"
  28.   elseif world == "DIM1" then return "The End"
  29.   else return "? "..world
  30.   end
  31. end
  32.  
  33. local function draw(world)
  34.   term.setBackgroundColor(colors.black)
  35.   term.clear()
  36.  
  37.   local time = getMinecraftTime(world.servertime)
  38.   local weather = "Sunny"
  39.   if world.hasStorm then weather = "Raining"
  40.     if world.isThundering then weather = "Thunderstorm"
  41.     end
  42.   end
  43.  
  44.   writeAt(("%d players online | %#2d:%02d (%s) | %s"):format(world.currentcount, time.hours, time.minutes, time.isDay and "Day" or "Night", weather), 1, 1)
  45.  
  46.   for id, player in pairs(world.players) do
  47.     local bg = (id % 2 == 0) and colors.black or colors.gray
  48.     local cy = 2+id
  49.     term.setCursorPos(1, cy)
  50.     term.setBackgroundColor(bg)
  51.     term.clearLine()
  52.     writeAt("\16 "..player.name, 1, cy, nil, bg)
  53.     writeAt(("| \3%#2d \4%#2d |"):format(player.health, player.armor), 20, cy, nil, bg)
  54.     writeAt(getWorld(player.world), 32, cy, nil, bg)
  55.     writeAt(("| %#5d, %#3d, %#5d"):format(math.floor(player.x), math.floor(player.y), math.floor(player.z)), 42, cy, nil, bg)
  56.   end
  57. end
  58.  
  59. local timeout = os.startTimer(0)
  60. while true do
  61.   local event, url, data, data2 = os.pullEvent()
  62.   if event == "http_success" then
  63.     local recv = data.readAll()
  64.     data.close()
  65.     world = json.decode(recv)
  66.     if world then
  67.       draw(world)
  68.       timeout = os.startTimer(reqdelay)
  69.     else
  70.       printError("Could not decode json: "..recv)
  71.       timeout = os.startTimer(errdelay)
  72.     end
  73.   elseif event == "http_failure" then
  74.     printError("Could not connect to "..url)
  75.     timeout = os.startTimer(errdelay)
  76.   elseif event == "timer" and url == timeout then http.request(worldurl)
  77.   elseif event == "term_resize" then
  78.     tX, tY = term.getSize()
  79.     draw(world)
  80.   elseif event == "key" then
  81.     if url == keys.q then
  82.       sleep(0)
  83.       break
  84.     end
  85.   end
  86. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement