MtnMCG

MtnLabs student startup.lua

Sep 2nd, 2024 (edited)
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. -- Student device startup.lua
  2.  
  3. local modem = peripheral.find("modem")
  4. if not modem then
  5. error("No modem found. Please attach a wireless modem.")
  6. end
  7.  
  8. local function downloadStudentLua()
  9. modem.open(1)
  10. print("Requesting latest student.lua...")
  11.  
  12. -- Send a request to the server for the latest student.lua
  13. modem.transmit(1, 1, {type = "request_update"})
  14.  
  15. local timer = os.startTimer(5) -- 5-second timeout
  16. while true do
  17. local event, param1, param2, param3, message = os.pullEvent()
  18. if event == "modem_message" then
  19. if type(message) == "table" and message.type == "update" then
  20. local file = fs.open("student.lua", "w")
  21. file.write(message.content)
  22. file.close()
  23. print("student.lua updated successfully.")
  24. return true
  25. end
  26. elseif event == "timer" and param1 == timer then
  27. print("Timeout: Failed to receive student.lua")
  28. return false
  29. end
  30. end
  31. end
  32.  
  33. if downloadStudentLua() then
  34. shell.run("student.lua")
  35. else
  36. print("Failed to update. Running existing student.lua if available.")
  37. if fs.exists("student.lua") then
  38. shell.run("student.lua")
  39. else
  40. error("No student.lua available. Please contact an administrator.")
  41. end
  42. end
Advertisement
Add Comment
Please, Sign In to add comment