Advertisement
higbead

redstone_client.lua

Oct 23rd, 2020 (edited)
1,837
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.18 KB | None | 0 0
  1. local CLIENT_NAME = 'Tydium'
  2. local DEFAULT_AUTH = nil -- optional
  3. local SERVER_PORT,CLIENT_PORT,AUTH = ...
  4.  
  5. SERVER_PORT = SERVER_PORT or 17234
  6. CLIENT_PORT = CLIENT_PORT or 17235
  7. AUTH = AUTH or DEFAULT_AUTH
  8.  
  9. local modem = peripheral.find('modem')
  10.  
  11. print('Starting redstone client on port '..CLIENT_PORT)
  12.  
  13. if not modem then
  14.     print('No modem connected')
  15.     return
  16. end
  17.  
  18. modem.open(CLIENT_PORT)
  19.  
  20. print('Successfully connected')
  21. print('Type your commands below using the format "channel=level".')
  22. print('Example: siren=15\n')
  23. print('Other commands:')
  24. print('     enable <channel>')
  25. print('     disable <channel>')
  26. print('     impulse <channel> [number duration]')
  27. print('     exit')
  28.  
  29. function showResponses()
  30.     while true do
  31.         local event, side, senderPort, replyPort, message, distance = os.pullEvent('modem_message')
  32.         if type(message) == 'string' then
  33.             print('Recieved string response: "'..message..'"')
  34.         elseif type(message) == 'table' and message.response then
  35.             print(tostring(message.author_name)..': '..tostring(message.response))
  36.         end
  37.     end
  38. end
  39.  
  40. function sendSignal(channel, level)
  41.     modem.transmit(SERVER_PORT, CLIENT_PORT, {
  42.         level = level,
  43.         channel = channel,
  44.         author_name = CLIENT_NAME,
  45.     })
  46. end
  47.  
  48. function processCommands()
  49.     while true do
  50.         command = read()
  51.         if command == 'exit' then
  52.             return
  53.         elseif command:sub(1,8) == 'disable ' then
  54.             sendSignal(command:sub(9, #command), 0)
  55.         elseif command:sub(1,7) == 'enable ' then
  56.             sendSignal(command:sub(8, #command), 15)
  57.         elseif command:find('=') then
  58.             local equalIndex = command:find('=')
  59.             local level = tonumber(command:sub(equalIndex + 1))
  60.             if level then
  61.                 sendSignal(command:sub(1, equalIndex - 1), level)
  62.             else
  63.                 print('level is not a valid number')
  64.             end
  65.         elseif command:sub(1,8) == 'impulse ' then
  66.             sendSignal(command:sub(9, #command), 15)
  67.  
  68.             local spaceIndex = command:sub(9,#command):find(' ') or #command
  69.             local length = tonumber(command:sub(spaceIndex + 1, #command)) or 1
  70.            
  71.             os.sleep(length)
  72.            
  73.             sendSignal(command:sub(9, #command), 0)
  74.         else
  75.             print('Unknown command.')
  76.         end
  77.     end
  78. end
  79.  
  80. parallel.waitForAny(processCommands, showResponses)
  81.  
  82. modem.close(CLIENT_PORT)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement