Advertisement
pulchroxloom

Turtle Server

Jan 26th, 2025 (edited)
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.12 KB | None | 0 0
  1. LOGS_RECV = 1    -- general purpose logger
  2. CLIENT_RECV = 10 -- all clients receive messages on this channel
  3. SERVER_RECV = 20 -- server receives messages on this channel
  4. EXIT_COMMAND = "exit" -- shared exit command where clients do not respond
  5.  
  6. local modem = nil
  7. local peripherals = peripheral.getNames()
  8. for name = 1, #peripherals, 1 do
  9.     if (peripheral.getType(peripherals[name]) == "modem") then
  10.         modem = peripheral.wrap(peripherals[name])
  11.         break
  12.     end
  13. end
  14.  
  15. if (modem == nil) then
  16.     error("Error, this program requires a Modem!")
  17. end
  18.  
  19. modem.open(SERVER_RECV)
  20.  
  21. repeat
  22.     write("broadcast: ")
  23.     local input = io.read()
  24.     local target = tonumber(input)
  25.     local channel = target or CLIENT_RECV
  26.     if target then
  27.         write("command: ")
  28.     end
  29.     local command = target and io.read() or input
  30.     local log = target and "sending " .. command .. " to " .. channel or "broadcasting " .. command .. " to all clients"
  31.     print(log)
  32.     modem.transmit(LOGS_RECV, SERVER_RECV, log)
  33.     modem.transmit(channel, SERVER_RECV, command)
  34. until command == EXIT_COMMAND
  35.  
  36. modem.close(SERVER_RECV)
  37.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement