tiebreakers

This commit is contained in:
yohlo
2026-03-01 19:41:34 -06:00
parent 873ca3e4c4
commit c7487e37f2

View File

@@ -436,10 +436,64 @@ async function calculateGroupStandings(groupId: string): Promise<GroupStanding[]
standing.cup_differential = standing.cups_for - standing.cups_against;
}
const h2hRecords = new Map<string, Map<string, { wins: number; cupDiff: number }>>();
for (const match of completedMatches) {
if (!match.home || !match.away) continue;
if (!h2hRecords.has(match.home.id)) {
h2hRecords.set(match.home.id, new Map());
}
if (!h2hRecords.has(match.away.id)) {
h2hRecords.set(match.away.id, new Map());
}
const homeH2H = h2hRecords.get(match.home.id)!;
const awayH2H = h2hRecords.get(match.away.id)!;
if (!homeH2H.has(match.away.id)) {
homeH2H.set(match.away.id, { wins: 0, cupDiff: 0 });
}
if (!awayH2H.has(match.home.id)) {
awayH2H.set(match.home.id, { wins: 0, cupDiff: 0 });
}
const homeRecord = homeH2H.get(match.away.id)!;
const awayRecord = awayH2H.get(match.home.id)!;
const cupDiff = match.home_cups - match.away_cups;
homeRecord.cupDiff += cupDiff;
awayRecord.cupDiff -= cupDiff;
if (match.home_cups > match.away_cups) {
homeRecord.wins++;
} else {
awayRecord.wins++;
}
}
const sortedStandings = Array.from(standings.values()).sort((a, b) => {
if (b.wins !== a.wins) return b.wins - a.wins;
if (b.cup_differential !== a.cup_differential) return b.cup_differential - a.cup_differential;
return b.cups_for - a.cups_for;
if (b.cups_for !== a.cups_for) return b.cups_for - a.cups_for;
const aH2H = h2hRecords.get(a.team.id);
const bH2H = h2hRecords.get(b.team.id);
if (aH2H && bH2H) {
const aVsB = aH2H.get(b.team.id);
const bVsA = bH2H.get(a.team.id);
if (aVsB && bVsA) {
if (aVsB.wins !== bVsA.wins) return bVsA.wins - aVsB.wins;
if (aVsB.cupDiff !== -bVsA.cupDiff) return aVsB.cupDiff - (-bVsA.cupDiff);
}
}
return a.team.id.localeCompare(b.team.id);
});
sortedStandings.forEach((standing, index) => {