Skip to content

Commit

Permalink
refactor: Rename WebSocket variables in agentplayer.js for clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
SverreNystad committed Sep 16, 2024
1 parent 0e2a8ef commit 9c6d280
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 32 deletions.
21 changes: 12 additions & 9 deletions frontend/agentplayer.js
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -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);
};
}
Expand Down
17 changes: 5 additions & 12 deletions frontend/index.html
Original file line number Diff line number Diff line change
@@ -1,26 +1,19 @@
<!DOCTYPE html>
<html>
<head>
<title>Tetris AI Demo</title>
<title>TetrisAI</title>
</head>
<body>
<h1>Tetris AI Demo</h1>
<h1>TetrisAI</h1>

<label for="agent-select">Select Agent:</label>
<select id="agent-select">
<!-- Agents will be dynamically populated -->
</select>
<!-- Get the options from endpoint -->
<select id="agent-select"></select>
<button id="start-demo">Start Demo</button>

<canvas id="game-canvas" width="400" height="920"></canvas>

<!-- Include the common shared logic -->

<script src="tetris-common.js"></script>

<!-- Include the single player logic -->
<script src="singleplayer.js"></script>

<!-- Include the agent demo logic -->
<script src="agentplayer.js"></script>
</body>
</html>
Empty file added frontend/routes.js
Empty file.
22 changes: 11 additions & 11 deletions frontend/singleplayer.js
Original file line number Diff line number Diff line change
@@ -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");
}
});

0 comments on commit 9c6d280

Please sign in to comment.