From 044fc9646a5e7fbd66a57381b361fe3d1270cb9e Mon Sep 17 00:00:00 2001 From: ThiML10 <157675695+ThiML10@users.noreply.github.com> Date: Sat, 27 Apr 2024 03:42:40 +0000 Subject: [PATCH] commit1 --- html/welcome.js | 65 ------------------------------------------------- 1 file changed, 65 deletions(-) delete mode 100644 html/welcome.js diff --git a/html/welcome.js b/html/welcome.js deleted file mode 100644 index 1f82d72..0000000 --- a/html/welcome.js +++ /dev/null @@ -1,65 +0,0 @@ -document.addEventListener("DOMContentLoaded", function() { - const nickInput = document.getElementById("nickInput"); - const nickForm = document.getElementById("nickForm"); - const errorElement = document.getElementById("error"); - const welcomeContainer = document.getElementById("welcomeContainer"); - const lobbyContainer = document.getElementById("lobbyContainer"); - - // Setup WebSocket connection - const connection = new WebSocket("ws://" + window.location.hostname + ":9105"); - - connection.onopen = function () { - console.log('WebSocket connection established!'); - }; - - connection.onerror = function (error) { - console.error('WebSocket Error:', error); - }; - - connection.onmessage = function (e) { - console.log('Server:', e.data); - const response = JSON.parse(e.data); - handleServerResponse(response); - }; - - nickForm.addEventListener('submit', function(event) { - event.preventDefault(); - validateNickname(nickInput.value.trim()); - }); - - function validateNickname(nickname) { - if (!nickname) { - showError("Please enter a nickname."); - return; - } - - let message = { - screen: "welcome", - type: "validateNickname", - nickname: nickname - }; - - // Send the message object as a string via WebSocket - connection.send(JSON.stringify(message)); - } - - function handleServerResponse(response) { - if (response.type === "validateNicknameResponse") { - if (response.isValid) { - enterLobby(); - } else { - showError("Your nick is already taken, please reenter."); - } - } - } - - function enterLobby() { - welcomeContainer.style.display = 'none'; - lobbyContainer.style.display = 'block'; - } - - function showError(message) { - errorElement.textContent = message; - errorElement.style.display = 'block'; - } -});