ElijahCrafter

Tracker

Jul 15th, 2026 (edited)
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.97 KB | Source Code | 0 0
  1. -- PocketOS tracker hub.
  2. -- Shows a live table of every PocketOS device reporting its location.
  3. -- The computer whose ID matches 'tracker' in /pocketos/config.lua boots
  4. -- into this automatically. Needs a wireless modem attached.
  5.  
  6. local devices = {}
  7. local hasModem = false
  8. for _, side in ipairs(peripheral.getNames()) do
  9.     if peripheral.getType(side) == "modem" then
  10.         if pcall(rednet.open, side) then hasModem = true end
  11.     end
  12. end
  13. if hasModem then
  14.     rednet.host("pocketos.track", "tracker" .. os.getComputerID())
  15. end
  16.  
  17. -- admin safety: only these IDs may pull the fleet location list
  18. local admins = { 11, 18 }
  19. do
  20.     local ok, cfg = pcall(dofile, "/pocketos/config.lua")
  21.     if ok and type(cfg) == "table" and type(cfg.admins) == "table" then
  22.         admins = cfg.admins
  23.     end
  24. end
  25. local function isAdmin(id)
  26.     for _, a in ipairs(admins) do
  27.         if tonumber(a) == tonumber(id) then return true end
  28.     end
  29.     return false
  30. end
  31.  
  32. local color = term.isColor and term.isColor()
  33. local function tint(c)
  34.     if color then term.setTextColor(c) end
  35. end
  36.  
  37. local function draw()
  38.     term.setBackgroundColor(colors.black)
  39.     term.setTextColor(colors.white)
  40.     term.clear()
  41.     local w, h = term.getSize()
  42.     term.setCursorPos(1, 1)
  43.     tint(colors.yellow)
  44.     write("PocketOS Tracker - computer #" .. os.getComputerID())
  45.     if not hasModem then
  46.         term.setCursorPos(1, 2)
  47.         tint(colors.red)
  48.         write("No modem attached!")
  49.     end
  50.     term.setCursorPos(1, 3)
  51.     tint(colors.lightGray)
  52.     write(("%-4s %-12s %6s %4s %6s %s"):format("ID", "Name", "X", "Y", "Z", "Seen"))
  53.  
  54.     local ids = {}
  55.     for id in pairs(devices) do ids[#ids + 1] = id end
  56.     table.sort(ids)
  57.     tint(colors.white)
  58.     local y = 4
  59.     for _, id in ipairs(ids) do
  60.         if y > h - 1 then break end
  61.         local d = devices[id]
  62.         local age = math.floor(os.clock() - d.seen)
  63.         term.setCursorPos(1, y)
  64.         write(("%-4d %-12s %6d %4d %6d %ds"):format(
  65.             id, (d.label or "?"):sub(1, 12), d.x, d.y, d.z, age))
  66.         y = y + 1
  67.     end
  68.     if #ids == 0 then
  69.         term.setCursorPos(1, 5)
  70.         tint(colors.gray)
  71.         write("Waiting for reports...")
  72.         term.setCursorPos(1, 7)
  73.         write("On each pocket, set tracker = " .. os.getComputerID())
  74.         term.setCursorPos(1, 8)
  75.         write("in /pocketos/config.lua")
  76.     end
  77.     term.setCursorPos(1, h)
  78.     tint(colors.gray)
  79.     write("Hold Ctrl+T to exit")
  80. end
  81.  
  82. local timer = os.startTimer(1)
  83. draw()
  84. while true do
  85.     local e, a, b, c = os.pullEvent()
  86.     if e == "rednet_message" then
  87.         local id, msg, proto = a, b, c
  88.         if proto == "pocketos.track" and type(msg) == "table" then
  89.             if msg.type == "query" then
  90.                 -- a developer's Fleet screen asking for all positions
  91.                 if isAdmin(id) then
  92.                     local list = {}
  93.                     for did, d in pairs(devices) do
  94.                         list[#list + 1] = {
  95.                             id = did, label = d.label, x = d.x, y = d.y, z = d.z,
  96.                             age = math.floor(os.clock() - d.seen),
  97.                         }
  98.                     end
  99.                     rednet.send(id, { ok = true, devices = list }, "pocketos.track")
  100.                 else
  101.                     rednet.send(id, { ok = false, err = "Not an admin" }, "pocketos.track")
  102.                 end
  103.             elseif type(msg.x) == "number" and type(msg.y) == "number"
  104.                 and type(msg.z) == "number" then
  105.                 devices[id] = {
  106.                     label = type(msg.label) == "string" and msg.label or nil,
  107.                     x = math.floor(msg.x),
  108.                     y = math.floor(msg.y),
  109.                     z = math.floor(msg.z),
  110.                     seen = os.clock(),
  111.                 }
  112.                 draw()
  113.             end
  114.         end
  115.     elseif e == "timer" and a == timer then
  116.         draw()
  117.         timer = os.startTimer(1)
  118.     end
  119. end
  120.  
Advertisement
Add Comment
Please, Sign In to add comment