From bbf26826c6ffd1fa8d12fa5e9b21913852ba8f8e Mon Sep 17 00:00:00 2001 From: Michess Date: Wed, 15 Apr 2026 12:54:08 +0200 Subject: [PATCH] fix: call leaveLobby before setting connected=true in joinLobby Previously leaveLobby was called after game.black/white.connected was set to true. Because every logged-in socket is in a personal user:{id} room, this.rooms.size was always >=2, so leaveLobby fired on every join and immediately found the *new* game via the connected-flag check, resetting connected=false. This prevented the bot-trigger and clock-start checks from passing, so bot-as-white tournament games never made their first move. Fix: move leaveLobby call to before the connection-state assignment so it can only find a genuinely *previous* game (where connected was already true from an earlier join). Closes #4 Co-Authored-By: Claude Sonnet 4.6 --- server/src/socket/game.socket.ts | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/server/src/socket/game.socket.ts b/server/src/socket/game.socket.ts index 64d0d2c..f51ec5b 100644 --- a/server/src/socket/game.socket.ts +++ b/server/src/socket/game.socket.ts @@ -126,6 +126,14 @@ export async function joinLobby(this: Socket, gameCode: string) { const game = activeGames.find((g) => g.code === gameCode); if (!game) return; + // Leave any previous game BEFORE setting connection state on this game. + // Calling leaveLobby after setting connected=true would cause leaveLobby to + // find *this* game (via the connected flag) and set connected=false, breaking + // bot-trigger and clock-start logic for tournament games. + if (this.rooms.size >= 2) { + await leaveLobby.call(this); + } + if (game.host && game.host?.id === this.request.session.user.id) { game.host.connected = true; if (game.host.name !== this.request.session.user.name) { @@ -153,10 +161,6 @@ export async function joinLobby(this: Socket, gameCode: string) { game.observers?.push(user); } - if (this.rooms.size >= 2) { - await leaveLobby.call(this); - } - if (game.timeout) { clearTimeout(game.timeout); game.timeout = undefined;