Fix automatic advance bug
All checks were successful
Publish Docker image / Push Docker image to Docker Hub (push) Successful in 2m3s

This commit is contained in:
2025-05-14 19:27:21 +02:00
parent 9f4ebf379f
commit f2712bdcec
4 changed files with 90 additions and 25 deletions

View File

@ -539,13 +539,19 @@ class GameManager {
return { error: 'No active battle' };
}
// For bye battles (automatic advancement), only allow the host to vote/advance
if (lobby.currentBattle.bye === true && playerId !== lobby.hostId) {
return { error: 'Only the host can advance bye rounds' };
}
// Check if player has already voted in this battle
if (lobby.currentBattle.votes.has(playerId)) {
if (lobby.currentBattle.votes && lobby.currentBattle.votes.has(playerId)) {
return { error: 'Already voted in this battle' };
}
// Check if the voted song is part of the current battle
if (songId !== lobby.currentBattle.song1.id && songId !== lobby.currentBattle.song2.id) {
if (songId !== lobby.currentBattle.song1.id &&
(lobby.currentBattle.song2 && songId !== lobby.currentBattle.song2.id)) {
return { error: 'Invalid song ID' };
}
@ -553,6 +559,11 @@ class GameManager {
const player = lobby.players.find(p => p.id === playerId);
const playerName = player ? player.name : 'Unknown Player';
// Initialize votes Map if it doesn't exist
if (!lobby.currentBattle.votes) {
lobby.currentBattle.votes = new Map();
}
// Record the vote with player name for UI display
lobby.currentBattle.votes.set(playerId, {
songId,
@ -561,15 +572,38 @@ class GameManager {
// Update vote counts
if (songId === lobby.currentBattle.song1.id) {
lobby.currentBattle.song1Votes++;
} else {
lobby.currentBattle.song2Votes++;
lobby.currentBattle.song1Votes = (lobby.currentBattle.song1Votes || 0) + 1;
} else if (lobby.currentBattle.song2) {
lobby.currentBattle.song2Votes = (lobby.currentBattle.song2Votes || 0) + 1;
}
// Add a voteCount attribute for easier UI rendering
lobby.currentBattle.voteCount = lobby.currentBattle.votes.size;
// Check if all connected players have voted
// For bye battles, the host's vote is all that's needed
if (lobby.currentBattle.bye === true && playerId === lobby.hostId) {
// Determine winner (in bye battles, it's always song1)
const winnerSongId = lobby.currentBattle.song1.id;
lobby.currentBattle.winner = winnerSongId;
// Save battle to history
lobby.battles.push({
round: lobby.currentBattle.round,
song1: lobby.currentBattle.song1,
song2: lobby.currentBattle.song2,
song1Votes: lobby.currentBattle.song1Votes,
song2Votes: 0,
winner: winnerSongId
});
// Move to next battle or finish tournament
this._moveToNextBattle(lobby);
return { lobby, lobbyId };
}
// For regular battles, check if all connected players have voted
const connectedPlayers = lobby.players.filter(p => p.isConnected).length;
const voteCount = lobby.currentBattle.votes.size;
@ -728,9 +762,10 @@ class GameManager {
song1: byeSong,
song2: null, // Bye
bye: true,
winner: byeSong.id,
winner: null, // Set to null until host advances it
song1Votes: 0,
song2Votes: 0
song2Votes: 0,
votes: new Map() // Initialize votes as a Map
});
}
@ -825,9 +860,10 @@ class GameManager {
song1: byeSong,
song2: null,
bye: true,
winner: byeSong.id,
winner: null, // Set to null until host advances it
song1Votes: 0,
song2Votes: 0
song2Votes: 0,
votes: new Map() // Initialize votes as a Map
});
}
@ -853,6 +889,14 @@ class GameManager {
// Set first battle of new round
if (nextRound.length > 0) {
lobby.currentBattle = nextRound[0];
// Ensure votes is initialized as a Map
if (!lobby.currentBattle.votes) {
lobby.currentBattle.votes = new Map();
}
// Initialize vote count for UI
lobby.currentBattle.voteCount = 0;
}
}

View File

@ -315,7 +315,7 @@ io.on('connection', (socket) => {
});
// If battle is finished, notify about new battle
if (result.lobby.currentBattle) {
if (result.lobby.currentBattle && result.lobby.currentBattle !== result.lobby.battles[result.lobby.battles.length - 1]) {
io.to(result.lobbyId).emit('new_battle', {
battle: result.lobby.currentBattle
});