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 <noreply@anthropic.com>
This commit is contained in:
Michess
2026-04-15 12:54:08 +02:00
parent 0b3228c9d7
commit bbf26826c6

View File

@@ -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;