Celestial_Dodo

Remote-Functions-Receiver

Jun 2nd, 2024
894
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.64 KB | None | 0 0
  1. rednet.open("top")
  2.  
  3. local WaitingForInput = true
  4. local commands = {"Time"}
  5.  
  6. --local time = 10 --tonumber(io.read())
  7. local speaker = peripheral.wrap("back")
  8. local monitor = peripheral.find("monitor")
  9. monitor.setCursorPos(1, 1)
  10. monitor.clear()
  11.  
  12. monitor.write(os.getComputerID())
  13. print(os.getComputerID())
  14.  
  15. monitor.setCursorPos(1,2)
  16. print("Waiting For Command...")
  17. monitor.write("Waiting For Command...")
  18.  
  19. function mvalid(message)
  20.     for _, validMessage in ipairs(commands) do
  21.         if string.find(message, validMessage) then
  22.             return true
  23.         end
  24.     end
  25.     return false
  26. end
  27.  
  28. function convertTime(time)
  29.     local hours = math.floor(time / 3600)
  30.     local minutes = math.floor((time % 3600) / 60)
  31.     local seconds = time % 60
  32.    
  33.     monitor.clear()
  34.     monitor.setCursorPos(1,1)
  35.     monitor.write("Hours:   "..hours)
  36.     monitor.setCursorPos(1,2)
  37.     monitor.write("Minutes: "..minutes)
  38.     monitor.setCursorPos(1,3)
  39.     monitor.write("Seconds: "..seconds)
  40. end
  41.  
  42. function StartTimer(time)
  43.     repeat
  44.         convertTime(time)
  45.         time = time - 1
  46.         os.sleep(1)
  47.     until time == -1
  48.  
  49.     monitor.clear()
  50.  
  51.     if speaker then
  52.         -- Play a sound (replace "sound_name" with the name of the sound file)
  53.         speaker.playSound("UwU")
  54.     else
  55.         print("Speaker not avalable right now")
  56.     end
  57.     WaitingForInput = true
  58. end
  59.  
  60. function executeCommand(command)
  61.     local action, value = string.match(command, "(%a+)%s+(%d+)")
  62.     if action == "Time" then
  63.         local number = tonumber(value)
  64.         if number then
  65.             StartTimer(number)
  66.             return "Running Command: Time"
  67.         else
  68.             return "Invalid format."
  69.         end
  70.     else
  71.         return "Unknown command."
  72.     end
  73. end
  74.  
  75. while WaitingForInput do
  76.     local senderId, message, protocol = rednet.receive()
  77.     monitor.setCursorPos(1,3)
  78.     local Text = "Received message from ID" .. senderId .. ": " .. message
  79.     print(Text)
  80.     monitor.write(Text)
  81.     os.sleep(1.5)
  82.     Text = "Message Type: Command"
  83.     monitor.setCursorPos(1,4)
  84.     print(Text)
  85.     monitor.write(Text)
  86.     os.sleep(2)
  87.     monitor.setCursorPos(1,5)
  88.     Text = executeCommand(message)
  89.     print(Text)
  90.     monitor.write(Text)
  91.     os.sleep(0.5)
  92.     if message == "Invalid format." or "Unknown command." then
  93.         monitor.setCursorPos(1, 1)
  94.         monitor.clear()
  95.  
  96.         monitor.write(os.getComputerID())
  97.         print(os.getComputerID())
  98.  
  99.         monitor.setCursorPos(1,2)
  100.         print("Waiting For Command...")
  101.         monitor.write("Waiting For Command...")
  102.     else
  103.         WaitingForInput = false
  104.     end
  105. end
Advertisement
Add Comment
Please, Sign In to add comment