1
0
mirror of https://github.com/thib8956/tic-tac-toe-ws.git synced 2026-01-29 06:13:22 +00:00

fix: reset game state

This commit is contained in:
2026-01-27 13:51:58 +01:00
parent eb6011fa9d
commit 8ba37e58b7
2 changed files with 8 additions and 7 deletions

View File

@@ -1,4 +1,4 @@
import { Click, Update, Message, Hello, EndGame, Spectate } from "common.js";
import type { Click, Update, Message, Hello, EndGame, Spectate } from "common.js";
const CELL_SIZE = 150;
const GRID_SIZE = CELL_SIZE * 3;

View File

@@ -1,4 +1,4 @@
import { Message, Update, Hello, EndGame, Symbol, SymbolWithHue } from "common.js"
import type { Message, Update, Hello, EndGame, Symbol, SymbolWithHue } from "common.js"
import { WebSocket, WebSocketServer, MessageEvent } from "ws";
const port = 1234
@@ -64,7 +64,7 @@ wss.on("connection", (ws, req) => {
ws.addEventListener("message", (event: MessageEvent) => {
const message = JSON.parse(event.data as string);
const {x, y} = message;
const hue = Math.floor(Math.random() * 255);
const hue = Math.floor(Math.random() * 361); // hue is a value in degrees
const player = clients.find(x => x.ws === ws);
if (!player) throw new Error("player not found");
console.log("received message", message, "player", player!.id, "currentPlayer", currentPlayer?.id);
@@ -77,6 +77,7 @@ wss.on("connection", (ws, req) => {
console.log("player", currentPlayer!.id, "reset the game");
// reset game state
grid = [0, 0, 0, 0, 0, 0, 0, 0, 0];
hues = [0, 0, 0, 0, 0, 0, 0, 0, 0];
currentPlayer = undefined;
endGame = false;
for (const c of clients) {
@@ -151,14 +152,14 @@ wss.on("connection", (ws, req) => {
});
ws.on("close", (code: number) => {
// readyState 3 == CLOSED
spectators = spectators.filter(s => s.readyState !== 3);
const isClientDisconnect = clients.findIndex(c => c.ws.readyState !== 3)
spectators = spectators.filter(s => s.readyState !== WebSocket.CLOSED);
const isClientDisconnect = clients.some(c => c.ws === ws);
if (isClientDisconnect) {
clients = clients.filter(x => x.ws.readyState !== 3);
clients = clients.filter(x => x.ws.readyState !== WebSocket.CLOSED);
console.log(`player disconnected. Resetting game. Total clients ${clients.length}`);
// reset game state
grid = [0, 0, 0, 0, 0, 0, 0, 0, 0];
hues = [0, 0, 0, 0, 0, 0, 0, 0, 0];
currentPlayer = undefined;
endGame = false;
for (const c of clients) {