From c9157e83efebf9814eb119c8a5cf83a1bd43d2e2 Mon Sep 17 00:00:00 2001 From: Thibaud Date: Wed, 25 Sep 2024 22:15:10 +0200 Subject: [PATCH] handle remote websocketserver and handle game reset by client --- client.ts | 12 +++++++++++- common.ts | 8 +++++--- server.ts | 25 +++++++++++++++++++------ 3 files changed, 35 insertions(+), 10 deletions(-) diff --git a/client.ts b/client.ts index ed5249e..9f6e35b 100644 --- a/client.ts +++ b/client.ts @@ -5,7 +5,11 @@ const GRID_SIZE = CELL_SIZE * 3; const SHAPE_SIZE = 100; const ANIMATE_DURATION = 500; // ms -const ws = new WebSocket("ws://localhost:1234"); +let address = "ws://localhost:1234"; +if (window.location.hostname !== "localhost") { + address = "wss://tic-tac-toe-ws-production.up.railway.app"; +} +const ws = new WebSocket(address); interface Point { x: number; @@ -221,6 +225,12 @@ function init() { } break; } + case "reset": { + canvasMsg = `Game reset... Id #${myId}, playing as ${mySymbol}`; + grid = new Array(9); + pendingEvts = []; + break; + } default: { console.warn("unhandled message kind:", msg.kind); break; diff --git a/common.ts b/common.ts index bf1a044..115b329 100644 --- a/common.ts +++ b/common.ts @@ -1,8 +1,8 @@ -export type MessageKind = "hello" | "update" | "endgame"; +export type MessageKind = "hello" | "update" | "endgame" | "reset"; export interface Message { kind: MessageKind, - data: Response | Hello | EndGame, + data: Response | Hello | EndGame | Reset, } export interface Request { @@ -11,7 +11,6 @@ export interface Request { } export interface Response { - grid: number[], last: { x: number, y: number, symbol: "x" | "o" } } @@ -23,3 +22,6 @@ export interface Hello { export interface EndGame { issue: "win" | "lose" | "draw" } + +type Reset = undefined; + diff --git a/server.ts b/server.ts index ab87eff..0c748d6 100644 --- a/server.ts +++ b/server.ts @@ -19,6 +19,12 @@ let id = 1; let clients: Client[] = []; let currentPlayer: Client | undefined = undefined; +function getPlayerSymbol(): "o" | "x" { + console.assert(clients.length < 2, "there should never be more than 2 clients"); + if (clients.length === 0) return "o"; + return clients[0].symbol === "o" ? "x" : "o"; +} + wss.on("connection", (ws) => { id += 1; if (clients.length == 2) { @@ -27,7 +33,7 @@ wss.on("connection", (ws) => { return; } - const symbol = clients.length == 0 ? "o" : "x"; + const symbol = getPlayerSymbol(); const helloMsg: Message = { kind: "hello", data: { id, symbol } as Hello @@ -57,7 +63,6 @@ wss.on("connection", (ws) => { const msg: Message = { kind: "update", data: { - grid, last: { x, y, symbol: player.symbol } } as Response, } @@ -92,14 +97,22 @@ wss.on("connection", (ws) => { kind: "endgame", data: { issue: "lose" } as EndGame } as Message)); - endGame = true; + endGame = true; } } }); - ws.on("close", () => { - console.log(`player #${id} disconnected`); - clients = clients.filter(x => x.id !== id); + ws.on("close", (code: number) => { + console.log(`player disconnected. Resetting game. code: ${code}`); + clients = clients.filter(x => x.ws.readyState !== 3); // 3 == CLOSED + // reset game state + grid = [0, 0, 0, 0, 0, 0, 0, 0, 0] + currentPlayer = undefined; + for (const c of clients) { + c.ws.send(JSON.stringify({ + kind: "reset" + } as Message)); + } }); });