This commit is contained in:
Kunagisa 2025-05-28 17:45:10 +08:00
parent 479b6c38a8
commit 7217c2dfcd

View File

@ -180,7 +180,6 @@ const generateSingleEliminationBracket = async () => {
const totalPlayers = Math.pow(2, tournament.value.rounds); const totalPlayers = Math.pow(2, tournament.value.rounds);
const shuffled = [...tournament.value.participants].sort(() => Math.random() - 0.5); const shuffled = [...tournament.value.participants].sort(() => Math.random() - 0.5);
//
const latestData = await getSignUpResultList(); const latestData = await getSignUpResultList();
const filteredData = latestData.filter(item => item.tournament_id == tournament.value.id); const filteredData = latestData.filter(item => item.tournament_id == tournament.value.id);
@ -189,21 +188,25 @@ const generateSingleEliminationBracket = async () => {
} }
// //
const firstRoundMatches = totalPlayers / 2; const firstRoundMatchesCount = totalPlayers / 2;
for (let i = 0; i < firstRoundMatches; i++) { for (let i = 0; i < firstRoundMatchesCount; i++) {
const p1 = shuffled[i * 2]; const p1 = shuffled[i * 2];
const p2 = shuffled[i * 2 + 1]; const p2 = shuffled[i * 2 + 1];
let winner = null; let winner = null;
let decided = false; let decided = false;
if (p1 && !p2) {
if (p1 && !p2) { // p1 gets a bye
winner = p1; winner = p1;
decided = true; decided = true;
} else if (!p1 && p2) { } else if (!p1 && p2) { // p2 gets a bye
winner = p2; winner = p2;
decided = true; decided = true;
} else if (!p1 && !p2) { // double bye
winner = null; // Winner is null for a double bye
decided = true;
} }
// If p1 && p2, winner is null, decided is false (normal match)
//
const p1Latest = p1 ? filteredData.find(item => item.id === p1.id) : null; const p1Latest = p1 ? filteredData.find(item => item.id === p1.id) : null;
const p2Latest = p2 ? filteredData.find(item => item.id === p2.id) : null; const p2Latest = p2 ? filteredData.find(item => item.id === p2.id) : null;
@ -214,9 +217,10 @@ const generateSingleEliminationBracket = async () => {
participant1: p1, participant1: p1,
participant2: p2, participant2: p2,
winner, winner,
score1: decided && p1 && !p2 ? 1 : (p1Latest?.win || 0), score1: decided && winner === p1 ? 1 : (p1Latest?.win || 0), // Score 1 for bye winner, else existing or 0
score2: decided && !p1 && p2 ? 1 : (p2Latest?.win || 0), score2: decided && winner === p2 ? 1 : (p2Latest?.win || 0), // Score 1 for bye winner, else existing or 0
decided decided,
// participantXResolved flags are not needed for round 1 matches as they are resolved by definition or play
}); });
} }
@ -228,8 +232,10 @@ const generateSingleEliminationBracket = async () => {
id: `${r}-${i + 1}`, id: `${r}-${i + 1}`,
round: r, round: r,
matchNumber: i + 1, matchNumber: i + 1,
participant1: null, participant1: null, // Placeholder, will be filled by previous round's winner
participant2: null, participant2: null, // Placeholder
participant1Resolved: false, // New flag
participant2Resolved: false, // New flag
winner: null, winner: null,
score1: null, score1: null,
score2: null, score2: null,
@ -237,6 +243,14 @@ const generateSingleEliminationBracket = async () => {
}); });
} }
} }
//
const firstRoundActualMatches = tournament.value.matches.filter(m => m.round === 1);
for (const match of firstRoundActualMatches) {
if (match.decided) { // If the match was decided (bye, double bye)
updateNextRound(match); // Propagate its result (winner could be player or null)
}
}
}; };
const drawConnections = () => { const drawConnections = () => {
@ -357,37 +371,72 @@ async function confirmScore(match) {
emit('refreshPlayers'); emit('refreshPlayers');
} }
const updateNextRound = (match) => { const updateNextRound = (currentDecidedMatch) => {
if (!match.decided) return; if (!currentDecidedMatch.decided) { // Winner can be null (e.g. double bye)
console.warn("updateNextRound called on non-decided match", currentDecidedMatch);
if (match.round === tournament.value.rounds) {
return; return;
} }
const nextRound = match.round + 1; if (currentDecidedMatch.round === tournament.value.rounds) {
const nextMatchIndex = Math.floor((match.matchNumber - 1) / 2); return; // This was the final match or a match in the final round
const nextMatch = tournament.value.matches.find(m => m.round === nextRound && m.matchNumber === nextMatchIndex + 1);
if (!nextMatch) return;
if ((match.matchNumber - 1) % 2 === 0) {
nextMatch.participant1 = match.winner;
} else {
nextMatch.participant2 = match.winner;
} }
if ((nextMatch.participant1 && !nextMatch.participant2) || (!nextMatch.participant1 && nextMatch.participant2)) { const nextRound = currentDecidedMatch.round + 1;
nextMatch.winner = nextMatch.participant1 || nextMatch.participant2; const nextMatchIndexInRound = Math.floor((currentDecidedMatch.matchNumber - 1) / 2);
nextMatch.score1 = nextMatch.participant1 ? 1 : 0; const nextMatch = tournament.value.matches.find(m => m.round === nextRound && m.matchNumber === nextMatchIndexInRound + 1);
nextMatch.score2 = nextMatch.participant2 ? 1 : 0;
if (!nextMatch) {
console.error("Could not find next match for", currentDecidedMatch);
return;
}
const isFeederForParticipant1 = (currentDecidedMatch.matchNumber - 1) % 2 === 0;
if (isFeederForParticipant1) {
nextMatch.participant1 = currentDecidedMatch.winner; // Winner might be null
nextMatch.participant1Resolved = true;
} else {
nextMatch.participant2 = currentDecidedMatch.winner; // Winner might be null
nextMatch.participant2Resolved = true;
}
// Only proceed if both participant slots for nextMatch are resolved
if (nextMatch.participant1Resolved && nextMatch.participant2Resolved) {
const p1 = nextMatch.participant1;
const p2 = nextMatch.participant2;
if (p1 && !p2) { // p1 advances due to p2 being a bye (null)
nextMatch.winner = p1;
nextMatch.score1 = 1; // Convention for bye win
nextMatch.score2 = 0;
nextMatch.decided = true; nextMatch.decided = true;
updateNextRound(nextMatch); } else if (!p1 && p2) { // p2 advances due to p1 being a bye (null)
} else { nextMatch.winner = p2;
nextMatch.score1 = 0;
nextMatch.score2 = 1; // Convention for bye win
nextMatch.decided = true;
} else if (!p1 && !p2) { // Double bye in this nextMatch
nextMatch.winner = null; // No winner advances
nextMatch.score1 = 0;
nextMatch.score2 = 0;
nextMatch.decided = true; // This "match" is decided as a double bye
} else if (p1 && p2) { // Both participants are actual players, match is ready to be played
nextMatch.winner = null; nextMatch.winner = null;
nextMatch.score1 = null; nextMatch.score1 = null; // Or 0, depending on display preference for undecided matches
nextMatch.score2 = null; nextMatch.score2 = null; // Or 0
nextMatch.decided = false; nextMatch.decided = false;
} else {
// This case should ideally not be reached if logic is correct
console.warn("Unexpected state in updateNextRound for nextMatch:", nextMatch);
nextMatch.decided = false; // Default to not decided to prevent erroneous propagation
} }
// If nextMatch itself was decided (due to byes), propagate its result further
if (nextMatch.decided) {
updateNextRound(nextMatch);
}
}
// If only one participant is resolved, wait for the other feeder match to complete.
}; };
const calculateFinalRanking = () => { const calculateFinalRanking = () => {