Advertisement
Feddis08

Router2

Sep 26th, 2022 (edited)
715
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.49 KB | Gaming | 0 0
  1. print("router")
  2. local computer = require("computer")
  3. local component = require("component")
  4. local event = require("event")
  5. local m = component.modem
  6. local spaceing = "}|}!"
  7. local clients = {}
  8. m.open(5)
  9. computer.beep()
  10. function split(s, delimiter)
  11.     result = {};
  12.     for match in (s..delimiter):gmatch("(.-)"..delimiter) do
  13.         table.insert(result, match);
  14.     end
  15.     return result;
  16. end
  17. function getClientById(id)
  18.     local index = 0
  19.     local result = nil
  20.     while index < #clients do
  21.         index = index + 1
  22.         if split(clients[index], spaceing)[2] == id then
  23.             result = clients[index]
  24.             end
  25.         end
  26.     return result
  27.     end
  28. function getClientByAddress(id)
  29.     local index = 0
  30.     local result = nil
  31.     while index < #clients do
  32.         index = index + 1
  33.         if split(clients[index], spaceing)[1] == id then
  34.             result = clients[index]
  35.             end
  36.         end
  37.     return result
  38.     end
  39. while true do
  40.     local _, _, from, port, _, message = event.pull("modem_message")
  41.     print(from .. " " .. message)
  42.     command = split(message, spaceing)
  43.     if command[1] == "broadcast" then
  44.         local client_string = getClientByAddress(from)
  45.         if client_string ~= nil then
  46.             local index = 0
  47.             local result = nil
  48.             while index < #clients do
  49.                 index = index + 1
  50.                 m.send(split(clients[index], spaceing)[1], 5, "transmit" .. spaceing .. "broadcast" .. spaceing .. split(client_string, spaceing)[2] .. spaceing .. command[2])
  51.                 end
  52.             print("broadcasted to " .. #clients .. " clients!")
  53.         else
  54.             m.send(from, 5, "connect" .. spaceing .. "2")
  55.             end
  56.         end
  57.     if command[1] == "connect" then
  58.         print("connecting: " .. from .. " ...")
  59.         local client_string = getClientByAddress(from)
  60.         if client_string ~= nil then
  61.             if split(client_string, spaceing)[1] == from then
  62.                 client_string = getClientByAddress(from)
  63.                 print("client_string: " .. client_string)
  64.                 m.send(split(client_string, spaceing)[1], 5, "connect" .. spaceing .. "1" .. spaceing .. split(client_string, spaceing)[2])
  65.                 print("client connected " .. from .. " as " .. split(client_string, spaceing)[2] .. " !")
  66.                 end
  67.             end
  68.         if client_string == nil then
  69.             client_string = from .. spaceing .. #clients
  70.             table.insert(clients, client_string)
  71.             print("client_string: " .. client_string)
  72.             m.send(from, 5, "connect" .. spaceing .. "1" .. spaceing .. #clients)
  73.             print("client connected " .. from .. " as " .. #clients .. " !")
  74.             end
  75.         end
  76.     if command[1] == "send" then
  77.         local client_string = getClientByAddress(from)
  78.         if client_string ~= nil then
  79.             local client2_string = getClientById(command[2])
  80.             if client2_string ~= nil then  
  81.                 print("sending for: " .. split(client_string, spaceing)[2])
  82.                 m.send(split(getClientById(command[2]), spaceing)[1], 5, "transmit" .. spaceing .. "send" .. spaceing .. split(client_string, spaceing)[2] .. spaceing .. command[3])
  83.                 print("sent from " .. split(client_string, spaceing)[2] .. " to " .. split(getClientById(command[2]), spaceing)[1] .. " " .. command[3])
  84.                 end
  85.             else
  86.                 m.send(from, 5, "connect" .. spaceing .. "2")
  87.                 end
  88.         end
  89.     end
  90.  
  91.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement