MtnMCG

tracking_dashboard.lua

Dec 17th, 2025 (edited)
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. -- Tracking Dashboard (tracking_dashboard.lua)
  2. -- Runs in a separate tab on the server to monitor student locations
  3.  
  4. local disk = peripheral.find("drive")
  5. if not disk then
  6. print("Error: No disk drive found.")
  7. -- Continue anyway as we might not need disk if API works
  8. end
  9.  
  10. -- Load API using dofile for compatibility
  11. local ndisk = dofile("ndisk_api.lua")
  12.  
  13. -- Initialize NDisk
  14. ndisk.init()
  15.  
  16. local function getStudents()
  17. local students = ndisk.list("students")
  18. local data = {}
  19. for _, id in ipairs(students) do
  20. -- Check if it's a valid student (not strictly needed as list returns items in students/)
  21. -- Assuming id is the folder name
  22. local historyPath = "students/" .. id .. "/location_history.txt"
  23. if ndisk.exists(historyPath) then
  24. local content = ndisk.read(historyPath)
  25. if content and #content > 0 then
  26. -- Get last line
  27. local lastLine = nil
  28. for line in content:gmatch("[^\r\n]+") do
  29. lastLine = line
  30. end
  31.  
  32. if lastLine then
  33. local timestamp, coords = lastLine:match("([^|]+)|(.+)")
  34. if timestamp and coords then
  35. table.insert(data, {id = id, time = timestamp, coords = coords})
  36. end
  37. end
  38. end
  39. end
  40. end
  41. return data
  42. end
  43.  
  44. local function drawDashboard()
  45. term.clear()
  46. term.setCursorPos(1, 1)
  47. print("MtnLabs Student Tracking")
  48. print("========================")
  49.  
  50. local students = getStudents()
  51. if #students == 0 then
  52. print("No active tracking data found.")
  53. else
  54. print(string.format("%-10s | %-15s | %s", "ID", "Time", "Location"))
  55. print(string.rep("-", 40))
  56. for _, s in ipairs(students) do
  57. -- Format time (simple epoch to string if needed, or raw)
  58. -- os.epoch("local") returns milliseconds.
  59. -- Maybe convert to readable? For now raw.
  60. local timeStr = s.time
  61. if tonumber(s.time) then
  62. timeStr = os.date("%H:%M:%S", tonumber(s.time)/1000)
  63. end
  64. print(string.format("%-10s | %-15s | %s", s.id, timeStr, s.coords))
  65. end
  66. end
  67. print("========================")
  68. print("Press 'q' to exit.")
  69. end
  70.  
  71. while true do
  72. drawDashboard()
  73. local timer = os.startTimer(60) -- Refresh every 60s
  74. local event, p1 = os.pullEvent()
  75. if event == "char" and p1 == "q" then
  76. break
  77. elseif event == "timer" and p1 == timer then
  78. -- Refresh
  79. end
  80. end
  81.  
Advertisement
Add Comment
Please, Sign In to add comment