Advertisement
Feddis08

Client

Sep 28th, 2022 (edited)
632
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.00 KB | None | 0 0
  1. print("client")
  2. local component = require("component")
  3. local computer = require("computer")
  4. local event = require("event")
  5. local m = component.modem
  6. local spaceing = "}|}!"
  7. local spaceing2 = "!?!?db?!?}{w"
  8. local self_address
  9. local connectedToRouter = false
  10. local router_address = ""
  11. local test_passed = false
  12. local prefix = "remote_robots"
  13. local robots_string
  14. local isConnected = false
  15. m.open(5)
  16. computer.beep()
  17.  
  18. function split(s, delimiter)
  19.     result = {};
  20.     for match in (s..delimiter):gmatch("(.-)"..delimiter) do
  21.         table.insert(result, match);
  22.     end
  23.     return result;
  24. end
  25. function sendMessage(to, message)
  26.     m.send(router_address, 5, "send" .. spaceing .. to .. spaceing .. message)
  27.     end
  28. function sendBroadcast(message)
  29.     m.send(router_address, 5, "broadcast" .. spaceing .. message)
  30.     end
  31. function listenToRouter()
  32.     local _, _, from, port, _, message = event.pull("modem_message")
  33.     if port == 5 then
  34.         command = split(message, spaceing)
  35.         if command[1] == "transmit" then
  36.             if command[2] == "send" then
  37.                 if command[4] == "connectionTest" then
  38.                     test_passed = true
  39.                     end    
  40.                 end
  41.             if command[2] == "broadcast" then
  42.                 end
  43.             end
  44.         if command[1] == "connect" then
  45.             if command[2] == "2" then
  46.                 connectToRouter()
  47.                 end
  48.             end
  49.         end
  50.     end
  51.  
  52. function connectToRouter()
  53.     m.broadcast(5, "connect" .. spaceing)
  54.     local _, _, from, port, _, message = event.pull("modem_message")
  55.     if port == 5 then
  56.         command = split(message, spaceing)
  57.         if command[1] == "connect" then
  58.             if command[2] == "1" then
  59.                 router_address = from
  60.                 self_address = command[3]
  61.                 connectedToRouter = true
  62.                 end
  63.             end
  64.         end
  65.     end
  66. function start()
  67.     print("searching for router...")
  68.     connectToRouter()
  69.     connectToRouter()
  70.     print("Done...")  
  71.     print("test connection...")
  72.     sendMessage(self_address, "connectionTest")
  73.     listenToRouter()
  74.     if test_passed == true then
  75.         print("Done... Connection works...")
  76.         print("searching for robots...")
  77.         sendBroadcast(prefix .. spaceing2 .. "searching_for_robot")
  78.         local run = true
  79.         while true do
  80.             local _, _, from, port, _, message = event.pull(10, "modem_message")
  81.             if from == nil then
  82.                 run = false
  83.                 break
  84.                 else
  85.                     if port == 5 then
  86.                         command = split(message, spaceing)
  87.                         if command[3] == self_address then
  88.                         else
  89.                             print(message)
  90.                         end
  91.                     end
  92.             end
  93.         end
  94.         print("")
  95.         else
  96.             print("test failed, try again...")
  97.             start()
  98.             end
  99.     end
  100. start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement