From b68f4e9ff5172f2dd336334428add8088064c45c Mon Sep 17 00:00:00 2001 From: Resistencia Dev Date: Sat, 27 Dec 2025 22:45:17 +0100 Subject: [PATCH] Fix: Update socket ID refs on reconnect (leader buttons bug) --- server/src/index.ts | 4 ++-- server/src/models/Game.ts | 40 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/server/src/index.ts b/server/src/index.ts index c630429..7da2e9b 100644 --- a/server/src/index.ts +++ b/server/src/index.ts @@ -405,8 +405,8 @@ io.on('connection', (socket) => { const existingPlayer = game.state.players.find(p => p.name === playerName); if (existingPlayer) { - // Actualizar el socket ID del jugador - existingPlayer.id = socket.id; + // Actualizar el socket ID del jugador y referencias + game.updatePlayerSocket(existingPlayer.id, socket.id); // Unir al socket a la sala socket.join(roomId); diff --git a/server/src/models/Game.ts b/server/src/models/Game.ts index f47e7ff..eb6995c 100644 --- a/server/src/models/Game.ts +++ b/server/src/models/Game.ts @@ -415,4 +415,44 @@ export class Game { // Mantener solo los últimos 50 mensajes if (this.state.history.length > 50) this.state.history.shift(); } + + updatePlayerSocket(oldId: string, newId: string) { + const player = this.state.players.find(p => p.id === oldId); + if (!player) return; + + // Actualizar ID del jugador + player.id = newId; + + // Actualizar referencias en el estado + + // 1. Host + if (this.hostId === oldId) { + this.hostId = newId; + this.state.hostId = newId; + } + + // 2. Líder actual + if (this.state.currentLeaderId === oldId) { + this.state.currentLeaderId = newId; + } + + // 3. Votos de Líder (leaderVotes) + if (this.state.leaderVotes && this.state.leaderVotes[oldId] !== undefined) { + this.state.leaderVotes[newId] = this.state.leaderVotes[oldId]; + delete this.state.leaderVotes[oldId]; + } + + // 4. Votos de Equipo (teamVotes) + if (this.state.teamVotes && this.state.teamVotes[oldId] !== undefined) { + this.state.teamVotes[newId] = this.state.teamVotes[oldId]; + delete this.state.teamVotes[oldId]; + } + + // 5. Equipo Propuesto (proposedTeam) + if (this.state.proposedTeam && this.state.proposedTeam.includes(oldId)) { + this.state.proposedTeam = this.state.proposedTeam.map(id => id === oldId ? newId : id); + } + + this.log(`Jugador ${player.name} reconectado. ID actualizado.`); + } }