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;