From 9c6d28075e532e8018b3d9f4bf297fd3ca2fe6b9 Mon Sep 17 00:00:00 2001 From: SverreNystad Date: Tue, 17 Sep 2024 00:57:38 +0200 Subject: [PATCH] refactor: Rename WebSocket variables in agentplayer.js for clarity --- frontend/agentplayer.js | 21 ++++++++++++--------- frontend/index.html | 17 +++++------------ frontend/routes.js | 0 frontend/singleplayer.js | 22 +++++++++++----------- 4 files changed, 28 insertions(+), 32 deletions(-) create mode 100644 frontend/routes.js diff --git a/frontend/agentplayer.js b/frontend/agentplayer.js index 3ac9cda..054216d 100644 --- a/frontend/agentplayer.js +++ b/frontend/agentplayer.js @@ -1,6 +1,6 @@ const agentSelect = document.getElementById("agent-select"); const startDemoBtn = document.getElementById("start-demo"); -let wsAgent = null; // WebSocket connection for agent demo +let agentWebSocket = null; // Fetch available agents from the server and populate the dropdown async function loadAgents() { @@ -20,27 +20,30 @@ function startDemo() { const selectedAgent = agentSelect.value; // Close the existing WebSocket connection if any - if (wsAgent) { - wsAgent.close(); + if (agentWebSocket) { + agentWebSocket.close(); } - wsAgent = new WebSocket(`ws://127.0.0.1:8000/ws/demo/${selectedAgent}`); + agentWebSocket = new WebSocket( + `ws://127.0.0.1:8000/ws/demo/${selectedAgent}` + ); - wsAgent.onopen = function () { + agentWebSocket.onopen = function () { console.log(`WebSocket connection established with ${selectedAgent} agent`); }; - wsAgent.onmessage = function (event) { + agentWebSocket.onmessage = function (event) { const gameState = JSON.parse(event.data); console.log("Game state received from server:", gameState); - drawBoard(gameState.board); // Render the updated board using shared function + // Render the updated board using shared function + drawBoard(gameState.board); }; - wsAgent.onclose = function () { + agentWebSocket.onclose = function () { console.log("WebSocket connection closed"); }; - wsAgent.onerror = function (error) { + agentWebSocket.onerror = function (error) { console.error("WebSocket error:", error); }; } diff --git a/frontend/index.html b/frontend/index.html index 30257b4..0ea3635 100644 --- a/frontend/index.html +++ b/frontend/index.html @@ -1,26 +1,19 @@ - Tetris AI Demo + TetrisAI -

Tetris AI Demo

+

TetrisAI

- + + - - - + - - - - diff --git a/frontend/routes.js b/frontend/routes.js new file mode 100644 index 0000000..e69de29 diff --git a/frontend/singleplayer.js b/frontend/singleplayer.js index 4d96d5f..9046d80 100644 --- a/frontend/singleplayer.js +++ b/frontend/singleplayer.js @@ -1,35 +1,35 @@ // WebSocket connection for single-player mode -const wsSingleplayer = new WebSocket("ws://127.0.0.1:8000/ws/game"); +const singleplayerWebSocket = new WebSocket("ws://127.0.0.1:8000/ws/game"); -wsSingleplayer.onopen = function () { +singleplayerWebSocket.onopen = () => { console.log("Single-player WebSocket connection established"); }; -wsSingleplayer.onmessage = function (event) { +singleplayerWebSocket.onmessage = (event) => { const gameState = JSON.parse(event.data); console.log("Game state received from server:", gameState); drawBoard(gameState.board); // Render the updated board using shared function }; -wsSingleplayer.onclose = function () { +singleplayerWebSocket.onclose = () => { console.log("Single-player WebSocket connection closed"); }; -wsSingleplayer.onerror = function (error) { +singleplayerWebSocket.onerror = (error) => { console.error("WebSocket error:", error); }; // Sending input events to the server -window.addEventListener("keydown", function (e) { +window.addEventListener("keydown", (e) => { if (e.key === "ArrowDown") { - wsSingleplayer.send("SOFT_DROP"); + singleplayerWebSocket.send("SOFT_DROP"); } else if (e.key === "ArrowLeft") { - wsSingleplayer.send("MOVE_LEFT"); + singleplayerWebSocket.send("MOVE_LEFT"); } else if (e.key === "ArrowRight") { - wsSingleplayer.send("MOVE_RIGHT"); + singleplayerWebSocket.send("MOVE_RIGHT"); } else if (e.key === " ") { - wsSingleplayer.send("HARD_DROP"); + singleplayerWebSocket.send("HARD_DROP"); } else if (e.key === "ArrowUp") { - wsSingleplayer.send("ROTATE_CLOCKWISE"); + singleplayerWebSocket.send("ROTATE_CLOCKWISE"); } });