Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local computerId = os.getComputerID()
- local monitor = peripheral.find("monitor")
- local modem = peripheral.find("modem")
- if modem == nil then
- error("Aucun modem disponible.")
- return
- end
- local function findModem()
- local sides = { "top", "bottom", "left", "right", "back", "front" }
- for _, side in ipairs(sides) do
- if peripheral.isPresent(side) and peripheral.getType(side) == "modem" then
- return side
- end
- end
- return nil
- end
- local function promptQuestion(question, options)
- term.clear()
- term.setCursorPos(1, 1)
- print(question)
- local selectedOption = 1
- local numOptions = #options
- while true do
- for i, option in ipairs(options) do
- if i == selectedOption then
- print("> " .. option)
- else
- print(" " .. option)
- end
- end
- local _, key = os.pullEvent("key")
- if key == keys.up then
- selectedOption = selectedOption - 1
- if selectedOption < 1 then
- selectedOption = numOptions
- end
- elseif key == keys.down then
- selectedOption = selectedOption + 1
- if selectedOption > numOptions then
- selectedOption = 1
- end
- elseif key == keys.enter then
- break
- end
- term.clear()
- term.setCursorPos(1, 1)
- print(question)
- end
- return selectedOption
- end
- local function generateRandomHostname()
- local randomSuffix = ""
- local randomLength = 5
- local characters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
- for i = 1, randomLength do
- local randomIndex = math.random(1, #characters)
- randomSuffix = randomSuffix .. string.sub(characters, randomIndex, randomIndex)
- end
- local hostname = "NetTchat-" .. randomSuffix
- return hostname
- end
- local function connectToServer(protocol, hostname)
- local modemSide = findModem()
- rednet.open(modemSide)
- term.clear()
- print(string.format("Tentative de connexion à %s sur le protocol : %s...", hostname, protocol))
- local serverId = rednet.lookup(protocol)
- if serverId then
- rednet.send(serverId, "Demande de connexion", protocol) -- Envoyer une demande de connexion au serveur
- print("Connexion établie avec le serveur #" .. serverId)
- return true
- else
- print("Aucun serveur trouvé pour le protocole : " .. protocol)
- return false
- end
- end
- local function startOnClient()
- term.clear()
- term.setCursorPos(1, 1)
- print("=== Configuration ===")
- print("Entrez le nom du protocole :")
- term.setCursorPos(1, 4)
- write("> ")
- local selectedProtocol = read()
- print("Entrez le nom d'hôte du serveur:")
- term.setCursorPos(1, 6)
- write("> ")
- local selectedHostname = read()
- if connectToServer(selectedProtocol, selectedHostname) then
- -- Gérer la suite de votre application une fois connecté au serveur
- else
- print("Impossible de se connecter au serveur.")
- end
- end
- local function startOnServer()
- term.clear()
- local protocol = "chat"
- local hostname = generateRandomHostname()
- local credits = "NetTchat, created and designed by _Tsyke_ | Ver: 0.1"
- local creditsScale = 0.5
- local width, height = monitor.getSize()
- local creditsWidth = #credits * creditsScale
- local creditsX = math.floor(width - creditsWidth + 1)
- local creditsY = height - 1
- monitor.setCursorPos(creditsX, creditsY)
- monitor.setTextScale(creditsScale)
- monitor.write(credits)
- os.sleep(2)
- monitor.clear()
- monitor.setCursorPos(1, 1)
- monitor.setTextScale(1)
- local title = string.format("Serveur NetTchat démarré sur le Computer #%s :", os.getComputerID())
- local underline = string.rep("-", #title)
- monitor.write(title)
- monitor.setCursorPos(1, 2)
- monitor.write(underline)
- monitor.setCursorPos(1, 4)
- monitor.write(string.format("Hostname : %s", hostname))
- monitor.setCursorPos(1, 5)
- monitor.write(string.format("Protocol : %s", protocol))
- local modemSide = findModem()
- rednet.open(modemSide)
- rednet.host(protocol, hostname)
- print("Serveur démarré. Attente des connexions...")
- while true do
- local senderId, message, receivedProtocol = rednet.receive(protocol)
- if receivedProtocol == protocol then
- print("Message reçu de #" .. senderId .. ": " .. message)
- end
- end
- end
- local function start()
- local question = "Choisissez une option :"
- local options = {
- string.format("Utiliser \"Computer #%s\" comme serveur", computerId),
- string.format("Utiliser \"Computer #%s\" comme client", computerId)
- }
- term.clear()
- local selected = promptQuestion(question, options)
- if selected == 1 then
- startOnServer()
- elseif selected == 2 then
- startOnClient()
- end
- end
- start()
Advertisement
Add Comment
Please, Sign In to add comment