Advertisement
higbead

redstone_server.lua

Oct 23rd, 2020 (edited)
1,594
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.93 KB | None | 0 0
  1. local ARGS = {...}
  2. local SERVER_NAME = ARGS[3] or 'tydium_siren'
  3. local CHANNEL = ARGS[1] or 'siren'
  4. local OUTPUT_SIDE = ARGS[4] or 'left'
  5. local PORT = ARGS[2] or 17234
  6. local AUTHENTICATION = nil -- if specified, clients must include a password under message.auth
  7.  
  8. local repr = require('cc.pretty').pretty
  9.  
  10. local modem = peripheral.find('modem')
  11. modem.open(PORT)
  12.  
  13. function handleEvent(message, senderPort, replyPort)
  14.    
  15.     if type(message) ~= 'table' or type(message.level) ~= 'number' then
  16.         modem.transmit(replyPort,senderPort, {
  17.             success = false,
  18.             response = 'Invalid message "'..repr(message)..'"',
  19.             channel = CHANNEL..'_response',
  20.             author_name = SERVER_NAME,
  21.  
  22.             description = {
  23.                 received_message = message
  24.             }
  25.         })
  26.         return
  27.     end
  28.  
  29.     if not (message.channel == CHANNEL or message.channel == 'all') then
  30.         return
  31.     end
  32.  
  33.     if AUTHENTICATION and message.auth ~= AUTHENTICATION then
  34.         print('Client "'..tostring(message.author_name)..'" sent unauthorized request (key: '..tostring(message.auth)..')')
  35.         modem.transmit(replyPort, senderPort, {
  36.             success = false,
  37.             response = 'Unauthorized',
  38.             channel = CHANNEL..'_response',
  39.             author_name = SERVER_NAME,
  40.  
  41.             description = {
  42.                 received_auth = message.auth,
  43.                 device_name = message.author_name
  44.             }
  45.         })
  46.    
  47.     end
  48.  
  49.     redstone.setAnalogOutput(OUTPUT_SIDE, message.level)
  50.     print('Request recieved from client "'..tostring(message.author_name)..'" (port '..senderPort..') to change redstone level to '..message.level)
  51.     modem.transmit(replyPort, senderPort, {
  52.         success = true,
  53.         response = 'Setting redstone output level to '..message.level,
  54.         channel = CHANNEL..'_response',
  55.         author_name = SERVER_NAME,
  56.  
  57.         description = {
  58.             output_level = message.level,
  59.             output_side = OUTPUT_SIDE,
  60.         }
  61.     })
  62. end
  63.  
  64. while true do
  65.  
  66.     local event, side, senderPort, replyPort, message, distance = os.pullEvent('modem_message')
  67.     handleEvent(message, senderPort, replyPort)
  68.    
  69. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement