MRtecno98

OpenComputers TNTLauncher Server

Feb 26th, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.26 KB | None | 0 0
  1. local component = require("component")
  2. local event = require("event")
  3. local serialization = require("serialization")
  4. local sides = require("sides")
  5.  
  6. local m = component.modem
  7.  
  8. clients = {}
  9. processed = {}
  10.  
  11. local function has_value (tab, val)
  12.     for index, value in ipairs(tab) do
  13.       if value == val then
  14.         return true
  15.       end
  16.     end
  17.                                    
  18.     return false
  19. end
  20.  
  21. PORT = 666
  22.  
  23. m.open(PORT)
  24. print("Working port opened")
  25.  
  26. -- Main While
  27.  
  28. while true do
  29.   local _, localNetworkCard, remoteAddress, port, distance, payload = event.pull("modem_message")
  30.  
  31.   if payload ~= nil then
  32.     local res = serialization.unserialize(payload)
  33.    
  34.     if not has_value(processed, res["id"]) then
  35.       print(tostring(res["id"]) .. ": Message Received")
  36.       local request = res["type"]
  37.       print("  Payload deserialized")
  38.      
  39.       if clients[remoteAddress] == nil then
  40.      
  41.         if request == "starter" then
  42.           clients[remoteAddress] = res["name"]
  43.      
  44.           local resp = {}
  45.           resp["serveraddr"] = m.address
  46.           resp["status"] = "ok"
  47.        
  48.           print("  New starter request found")
  49.           print("  Sender: " .. res["name"] .. " @ " .. remoteAddress)
  50.          
  51.           os.sleep(3)
  52.           m.send(remoteAddress, PORT, serialization.serialize(resp))
  53.           print("  Machine added and response sended")
  54.         end
  55.       end
  56.      
  57.       if request == "disconnect" then
  58.         print("  Found disconnection request")
  59.         clients[remoteAddress] = nil
  60.         print("  Client " .. remoteAddress .. " succesfully disconnected")
  61.       end
  62.      
  63.       if request == "launch" then
  64.         print("  Launch request received, starting bomb sequence...")
  65.         component.redstone.setOutput(sides.right, 15)
  66.         print("  Frontal door opened")
  67.         component.redstone.setOutput(sides.back, 15)
  68.         print("  TNT Ignited")
  69.         os.sleep(1)
  70.         component.redstone.setOutput(sides.back, 0)
  71.         os.sleep(5)
  72.         print("  Launch done, closing door")
  73.         component.redstone.setOutput(sides.right, 0)
  74.         print("  Door closed, sequence terminated")
  75.       end
  76.       processed[#processed+1] = res["id"]
  77.       print("\n")
  78.     end
  79.   end
  80. end
Add Comment
Please, Sign In to add comment