Squanou

ServerEdition

Jun 23rd, 2024 (edited)
440
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.13 KB | None | 0 0
  1. local computerId = os.getComputerID()
  2. local monitor = peripheral.find("monitor")
  3. local modem = peripheral.find("modem")
  4.  
  5. local function generateRandomHostname()
  6.     local randomSuffix = ""
  7.     local randomLength = 5
  8.     local characters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
  9.  
  10.     for i = 1, randomLength do
  11.         local randomIndex = math.random(1, #characters)
  12.         randomSuffix = randomSuffix .. string.sub(characters, randomIndex, randomIndex)
  13.     end
  14.  
  15.     local hostname = "NetTchat-" .. randomSuffix
  16.     return hostname
  17. end
  18.  
  19. local function findEnderModem()
  20.     for _, side in ipairs({"left", "right", "top", "bottom", "front", "back"}) do
  21.         if peripheral.isPresent(side) and peripheral.getType(side) == "modem" then
  22.             return side
  23.         end
  24.     end
  25.     error("No modem found")
  26. end
  27.  
  28. local function displayServerInfo(monitor, computerId, hostname, protocol)
  29.     monitor.clear()
  30.     monitor.setCursorPos(1, 1)
  31.     monitor.setTextScale(1)
  32.  
  33.     local title = string.format("Serveur NetTchat démarré sur le Computer #%s :", computerId)
  34.     local underline = string.rep("-", #title)
  35.  
  36.     monitor.write(title)
  37.     monitor.setCursorPos(1, 2)
  38.     monitor.write(underline)
  39.  
  40.     monitor.setCursorPos(1, 4)
  41.     monitor.write(string.format("Hostname : %s", hostname))
  42.  
  43.     monitor.setCursorPos(1, 5)
  44.     monitor.write(string.format("Protocol : %s", protocol))
  45. end
  46.  
  47. local function updateClientList(monitor, clients)
  48.     monitor.clear()
  49.     monitor.setCursorPos(1, 1)
  50.     monitor.setTextScale(1)
  51.  
  52.     local title = string.format("Serveur NetTchat démarré sur le Computer #%s :", computerId)
  53.     local underline = string.rep("-", #title)
  54.  
  55.     monitor.write(title)
  56.     monitor.setCursorPos(1, 2)
  57.     monitor.write(underline)
  58.  
  59.     monitor.setCursorPos(1, 4)
  60.     monitor.write(string.format("Hostname : %s", hostname))
  61.  
  62.     monitor.setCursorPos(1, 5)
  63.     monitor.write(string.format("Protocol : %s", protocol))
  64.  
  65.     monitor.setCursorPos(1, 7)
  66.     monitor.write("Clients connectés:")
  67.     monitor.setCursorPos(1, 8)
  68.     monitor.write(string.rep("-", 20))
  69.    
  70.     local y = 9
  71.     for clientId, client in pairs(clients) do
  72.         monitor.setCursorPos(1, y)
  73.         if client.connected then
  74.             monitor.write(client.name .. " (Online)")
  75.         else
  76.             monitor.write(client.name .. " (Disconnected)")
  77.         end
  78.         y = y + 1
  79.     end
  80. end
  81.  
  82.  
  83. local function startOnServer()
  84.     term.clear()
  85.     local protocol = "chat"
  86.     local hostname = generateRandomHostname()
  87.  
  88.     local credits = "NetTchat Server Edition, created and designed by _Tsyke_ | Ver: 0.3"
  89.     local creditsScale = 0.8
  90.     monitor.setTextScale(creditsScale)
  91.  
  92.     local width, height = monitor.getSize()
  93.     local creditsX = math.floor((width - #credits) / 2)
  94.     local creditsY = math.floor(height / 2)
  95.  
  96.     monitor.clear()
  97.     monitor.setCursorPos(creditsX, creditsY)
  98.     monitor.write(credits)
  99.  
  100.     os.sleep(5)
  101.  
  102.     displayServerInfo(monitor, computerId, hostname, protocol)
  103.  
  104.     local modemSide = findEnderModem()
  105.     rednet.open(modemSide)
  106.     rednet.host(protocol, hostname)
  107.  
  108.     print("Serveur démarré. Attente des connexions...")
  109.     print("Protocole: " .. protocol)
  110.     print("Hostname: " .. hostname)
  111.     print("Modem sur: " .. modemSide)
  112.  
  113.     local clients = {}
  114.     local clientLastPing = {}
  115.  
  116.     local function checkClientPing()
  117.         while true do
  118.             local currentTime = os.time()
  119.             for clientId, client in pairs(clients) do
  120.                 if client.connected then
  121.                     rednet.send(clientId, "Ping", protocol)
  122.                     local _, message = rednet.receive(protocol, 2)  -- Attend une réponse pendant 2 secondes
  123.                    
  124.                     if not message or message ~= "Pong" then
  125.                         print("Client déconnecté: #" .. clientId)
  126.                         clients[clientId] = nil
  127.                         updateClientList(monitor, clients)  -- Mettre à jour la liste des clients sur le moniteur
  128.                     end
  129.                 end
  130.             end
  131.             os.sleep(5)
  132.         end
  133.     end
  134.    
  135.  
  136.     parallel.waitForAll(
  137.         function()
  138.             while true do
  139.                 local senderId, message, receivedProtocol = rednet.receive(protocol)
  140.                
  141.                 if receivedProtocol == protocol then
  142.                     if message == "Demande de connexion" then
  143.                         clients[senderId] = { name = "Client #" .. senderId, connected = true }
  144.                         updateClientList(monitor, clients)
  145.                         print("Client connecté: #" .. senderId)
  146.                         rednet.send(senderId, "Connexion acceptée", protocol)
  147.                     elseif message == "Ping" then
  148.                         -- Le serveur ne devrait pas recevoir "Ping" directement ici, donc rien n'est fait ici.
  149.                     else
  150.                         print("Message reçu de #" .. senderId .. ": " .. message)
  151.                     end
  152.                 end
  153.             end
  154.         end,
  155.         checkClientPing
  156.     )
  157. end
  158.  
  159. startOnServer()
  160.  
Advertisement
Add Comment
Please, Sign In to add comment