Squanou

ClientEdition

Jun 23rd, 2024 (edited)
427
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.81 KB | None | 0 0
  1. local computerId = os.getComputerID()
  2. local monitor = peripheral.find("monitor")
  3. local modem = peripheral.find("modem")
  4.  
  5. if modem == nil then
  6.     error("Aucun modem disponible.")
  7.     return
  8. end
  9.  
  10. local function findModem()
  11.     local sides = { "top", "bottom", "left", "right", "back", "front" }
  12.  
  13.     for _, side in ipairs(sides) do
  14.         if peripheral.isPresent(side) and peripheral.getType(side) == "modem" then
  15.             return side
  16.         end
  17.     end
  18.  
  19.     return nil
  20. end
  21.  
  22. local function connectToServer(protocol, hostname)
  23.     local modemSide = findModem()
  24.     rednet.open(modemSide)
  25.     term.clear()
  26.     print(string.format("Tentative de connexion à %s sur le protocol : %s...", hostname, protocol))
  27.     local serverId = rednet.lookup(protocol)
  28.  
  29.     if serverId then
  30.         rednet.send(serverId, "Demande de connexion", protocol) -- Envoyer une demande de connexion au serveur
  31.         return serverId
  32.     else
  33.         monitor.clear()
  34.         monitor.setCursorPos(1, 1)
  35.         monitor.setTextScale(1)
  36.  
  37.         local title = string.format("[ECHEC] Client NetTchat Computer #%s :", os.getComputerID())
  38.         local underline = string.rep("-", #title)
  39.  
  40.         monitor.write(title)
  41.         monitor.setCursorPos(1, 2)
  42.         monitor.write(underline)
  43.  
  44.         monitor.setCursorPos(1, 4)
  45.         monitor.write(string.format("Connexion : Aucune"))
  46.  
  47.         monitor.setCursorPos(1, 5)
  48.         monitor.write(string.format("Erreur : Aucun serveur trouvé pour le protocole : " .. protocol))
  49.         print("Aucun serveur trouvé pour le protocole : " .. protocol)
  50.         return false
  51.     end
  52. end
  53.  
  54. local function displayStartupMessage()
  55.     local message = "NetTchat Client Edition, created and designed by _Tsyke_ | Ver: 0.1"
  56.     local scale = .8
  57.     monitor.setTextScale(scale)
  58.     monitor.clear()
  59.     local width, height = monitor.getSize()
  60.     local x = math.floor((width - #message) / 2)
  61.     local y = math.floor(height / 2)
  62.     monitor.setCursorPos(x, y)
  63.     monitor.write(message)
  64.     sleep(5)
  65.     monitor.clear()
  66.     monitor.setTextScale(1)
  67. end
  68.  
  69. local function updateMonitor(hostname, protocol)
  70.     monitor.clear()
  71.     monitor.setCursorPos(1, 1)
  72.     monitor.setTextScale(1)
  73.  
  74.     local title = string.format("Client NetTchat Computer #%s :", os.getComputerID())
  75.     local underline = string.rep("-", #title)
  76.  
  77.     monitor.write(title)
  78.     monitor.setCursorPos(1, 2)
  79.     monitor.write(underline)
  80.  
  81.     monitor.setCursorPos(1, 4)
  82.     monitor.write(string.format("Hostname : %s", hostname))
  83.  
  84.     monitor.setCursorPos(1, 5)
  85.     monitor.write(string.format("Protocol : %s", protocol))
  86. end
  87.  
  88. local function startOnClient()
  89.     displayStartupMessage()
  90.  
  91.     term.clear()
  92.     term.setCursorPos(1, 1)
  93.  
  94.     print("=== Configuration ===")
  95.     print("Entrez le nom du protocole :")
  96.     term.setCursorPos(1, 4)
  97.     write("> ")
  98.     local selectedProtocol = read()
  99.  
  100.     print("Entrez le nom d'hôte du serveur:")
  101.     term.setCursorPos(1, 6)
  102.     write("> ")
  103.     local selectedHostname = read()
  104.     local serverId = connectToServer(selectedProtocol, selectedHostname)
  105.  
  106.     if serverId then
  107.         print("Connexion établie avec le serveur #" .. serverId)
  108.         updateMonitor(selectedHostname, selectedProtocol)
  109.  
  110.         while true do
  111.             rednet.send(serverId, "Ping", selectedProtocol)
  112.  
  113.             local senderId, message, protocol = rednet.receive(selectedProtocol, 5)
  114.             if senderId and message == "Pong" then
  115.                 print(string.format("Réponse reçue de %d : %s", senderId, message))
  116.             else
  117.                 print("Aucune réponse reçue dans les 5 secondes. Déconnexion...")
  118.                 return
  119.             end
  120.  
  121.             sleep(5)
  122.         end
  123.     else
  124.         print("Impossible de se connecter au serveur.")
  125.     end
  126. end
  127.  
  128. startOnClient()
  129.  
Advertisement
Add Comment
Please, Sign In to add comment