Fix: Update socket ID refs on reconnect (leader buttons bug)
Some checks failed
CI/CD - Francia Ocupada (La Resistencia) / build-and-deploy (push) Failing after 6s
Some checks failed
CI/CD - Francia Ocupada (La Resistencia) / build-and-deploy (push) Failing after 6s
This commit is contained in:
@@ -405,8 +405,8 @@ io.on('connection', (socket) => {
|
|||||||
const existingPlayer = game.state.players.find(p => p.name === playerName);
|
const existingPlayer = game.state.players.find(p => p.name === playerName);
|
||||||
|
|
||||||
if (existingPlayer) {
|
if (existingPlayer) {
|
||||||
// Actualizar el socket ID del jugador
|
// Actualizar el socket ID del jugador y referencias
|
||||||
existingPlayer.id = socket.id;
|
game.updatePlayerSocket(existingPlayer.id, socket.id);
|
||||||
|
|
||||||
// Unir al socket a la sala
|
// Unir al socket a la sala
|
||||||
socket.join(roomId);
|
socket.join(roomId);
|
||||||
|
|||||||
@@ -415,4 +415,44 @@ export class Game {
|
|||||||
// Mantener solo los últimos 50 mensajes
|
// Mantener solo los últimos 50 mensajes
|
||||||
if (this.state.history.length > 50) this.state.history.shift();
|
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.`);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user