Squanou

index

Jun 23rd, 2024 (edited)
516
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.06 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 promptQuestion(question, options)
  23.     term.clear()
  24.     term.setCursorPos(1, 1)
  25.     print(question)
  26.  
  27.     local selectedOption = 1
  28.     local numOptions = #options
  29.  
  30.     while true do
  31.         for i, option in ipairs(options) do
  32.             if i == selectedOption then
  33.                 print("> " .. option)
  34.             else
  35.                 print("  " .. option)
  36.             end
  37.         end
  38.  
  39.         local _, key = os.pullEvent("key")
  40.  
  41.         if key == keys.up then
  42.             selectedOption = selectedOption - 1
  43.             if selectedOption < 1 then
  44.                 selectedOption = numOptions
  45.             end
  46.         elseif key == keys.down then
  47.             selectedOption = selectedOption + 1
  48.             if selectedOption > numOptions then
  49.                 selectedOption = 1
  50.             end
  51.         elseif key == keys.enter then
  52.             break
  53.         end
  54.  
  55.         term.clear()
  56.         term.setCursorPos(1, 1)
  57.         print(question)
  58.     end
  59.  
  60.     return selectedOption
  61. end
  62.  
  63. local function generateRandomHostname()
  64.     local randomSuffix = ""
  65.     local randomLength = 5
  66.     local characters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
  67.  
  68.     for i = 1, randomLength do
  69.         local randomIndex = math.random(1, #characters)
  70.         randomSuffix = randomSuffix .. string.sub(characters, randomIndex, randomIndex)
  71.     end
  72.  
  73.     local hostname = "NetTchat-" .. randomSuffix
  74.     return hostname
  75. end
  76.  
  77. local function connectToServer(protocol, hostname)
  78.     local modemSide = findModem()
  79.     rednet.open(modemSide)
  80.     term.clear()
  81.     print(string.format("Tentative de connexion à %s sur le protocol : %s...", hostname, protocol))
  82.     local serverId = rednet.lookup(protocol)
  83.  
  84.     if serverId then
  85.         rednet.send(serverId, "Demande de connexion", protocol) -- Envoyer une demande de connexion au serveur
  86.         print("Connexion établie avec le serveur #" .. serverId)
  87.         return true
  88.     else
  89.         print("Aucun serveur trouvé pour le protocole : " .. protocol)
  90.         return false
  91.     end
  92. end
  93.  
  94. local function startOnClient()
  95.     term.clear()
  96.     term.setCursorPos(1, 1)
  97.  
  98.     print("=== Configuration ===")
  99.     print("Entrez le nom du protocole :")
  100.     term.setCursorPos(1, 4)
  101.     write("> ")
  102.     local selectedProtocol = read()
  103.  
  104.     print("Entrez le nom d'hôte du serveur:")
  105.     term.setCursorPos(1, 6)
  106.     write("> ")
  107.     local selectedHostname = read()
  108.  
  109.     if connectToServer(selectedProtocol, selectedHostname) then
  110.         -- Gérer la suite de votre application une fois connecté au serveur
  111.     else
  112.         print("Impossible de se connecter au serveur.")
  113.     end
  114. end
  115.  
  116. local function startOnServer()
  117.     term.clear()
  118.     local protocol = "chat"
  119.     local hostname = generateRandomHostname()
  120.  
  121.     local credits = "NetTchat, created and designed by _Tsyke_ | Ver: 0.1"
  122.     local creditsScale = 0.5
  123.     local width, height = monitor.getSize()
  124.  
  125.     local creditsWidth = #credits * creditsScale
  126.     local creditsX = math.floor(width - creditsWidth + 1)
  127.     local creditsY = height - 1
  128.  
  129.     monitor.setCursorPos(creditsX, creditsY)
  130.     monitor.setTextScale(creditsScale)
  131.     monitor.write(credits)
  132.  
  133.     os.sleep(2)
  134.  
  135.     monitor.clear()
  136.     monitor.setCursorPos(1, 1)
  137.     monitor.setTextScale(1)
  138.  
  139.     local title = string.format("Serveur NetTchat démarré sur le Computer #%s :", os.getComputerID())
  140.     local underline = string.rep("-", #title)
  141.  
  142.     monitor.write(title)
  143.     monitor.setCursorPos(1, 2)
  144.     monitor.write(underline)
  145.  
  146.     monitor.setCursorPos(1, 4)
  147.     monitor.write(string.format("Hostname : %s", hostname))
  148.  
  149.     monitor.setCursorPos(1, 5)
  150.     monitor.write(string.format("Protocol : %s", protocol))
  151.  
  152.     local modemSide = findModem()
  153.     rednet.open(modemSide)
  154.     rednet.host(protocol, hostname)
  155.  
  156.     print("Serveur démarré. Attente des connexions...")
  157.  
  158.     while true do
  159.         local senderId, message, receivedProtocol = rednet.receive(protocol)
  160.  
  161.         if receivedProtocol == protocol then
  162.             print("Message reçu de #" .. senderId .. ": " .. message)
  163.         end
  164.     end
  165. end
  166.  
  167. local function start()
  168.     local question = "Choisissez une option :"
  169.     local options = {
  170.         string.format("Utiliser \"Computer #%s\" comme serveur", computerId),
  171.         string.format("Utiliser \"Computer #%s\" comme client", computerId)
  172.     }
  173.  
  174.     term.clear()
  175.  
  176.     local selected = promptQuestion(question, options)
  177.     if selected == 1 then
  178.         startOnServer()
  179.     elseif selected == 2 then
  180.         startOnClient()
  181.     end
  182. end
  183.  
  184. start()
  185.  
Advertisement
Add Comment
Please, Sign In to add comment