Advertisement
TomWaa

[Lua][WIP] CC Server thing idk

Mar 26th, 2022
1,206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.79 KB | None | 0 0
  1. -- Start setup
  2. print("Opening network and hosting from mainServerComs")
  3. rednet.open("top")
  4. rednet.host("mainServerComs", "main_server")
  5.  
  6. local computers = {}
  7.  
  8. function dump(o)
  9.     if type(o) == 'table' then
  10.        local s = '{ '
  11.        for k,v in pairs(o) do
  12.           if type(k) ~= 'number' then k = '"'..k..'"' end
  13.           s = s .. '['..k..'] = ' .. dump(v) .. ','
  14.        end
  15.        return s .. '} '
  16.     else
  17.        return tostring(o)
  18.     end
  19. end
  20.  
  21. while true do
  22.     local eventValues = { os.pullEvent() }
  23.  
  24.     if eventValues[1] == "key_up" then
  25.         if eventValues[2] == keys.q then
  26.             -- TODO: Broadcast that server is shutting down
  27.             break
  28.         end
  29.    
  30.     elseif eventValues[1] == "rednet_message" and eventValues[4] == "mainServerComs" then
  31.         if tostring(eventValues[3]) == "connected" then
  32.             print("Computer "..eventValues[2].." connected!")
  33.             table.insert(computers, eventValues[2])
  34.        
  35.         elseif tostring(eventValues[3]) == "disconnected" then
  36.             print("Computer "..eventValues[2].." disconnected!")
  37.             for index, comp in pairs(computers) do
  38.                 if comp == eventValues[2] then
  39.                     table.remove(computers, index)
  40.                     break
  41.                 end
  42.             end
  43.  
  44.         elseif tostring(eventValues[3]) == "lights" then
  45.             if rs.getOutput("right") then
  46.                 rs.setOutput("right", false)
  47.             else
  48.                 rs.setOutput("right", true)
  49.             end
  50.         end
  51.  
  52.         print()
  53.         print(#computers.." connected computers")
  54.         print(dump(computers))
  55.     end
  56. end
  57.  
  58. -- Cleanup
  59. print("Unhosting from mainServerComs and closing network")
  60. rednet.unhost("mainServerComs", "main_server")
  61. rednet.close("top")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement