diff --git a/src/features/tournaments/components/group-stage-view.tsx b/src/features/tournaments/components/group-stage-view.tsx index 096a953..fe1f066 100644 --- a/src/features/tournaments/components/group-stage-view.tsx +++ b/src/features/tournaments/components/group-stage-view.tsx @@ -137,43 +137,103 @@ const GroupStageView: React.FC = ({ }); }); - groupMatches - .filter((match) => match.status === "ended") - .forEach((match) => { - const homeId = match.home?.id; - const awayId = match.away?.id; + const completedMatches = groupMatches.filter((match) => match.status === "ended"); - if (!homeId || !awayId) return; + completedMatches.forEach((match) => { + const homeId = match.home?.id; + const awayId = match.away?.id; - const homeStanding = standings.get(homeId); - const awayStanding = standings.get(awayId); + if (!homeId || !awayId) return; - if (!homeStanding || !awayStanding) return; + const homeStanding = standings.get(homeId); + const awayStanding = standings.get(awayId); - const homeCups = match.home_cups || 0; - const awayCups = match.away_cups || 0; + if (!homeStanding || !awayStanding) return; - homeStanding.cupsFor += homeCups; - homeStanding.cupsAgainst += awayCups; - awayStanding.cupsFor += awayCups; - awayStanding.cupsAgainst += homeCups; + const homeCups = match.home_cups || 0; + const awayCups = match.away_cups || 0; - homeStanding.cupDifference += homeCups - awayCups; - awayStanding.cupDifference += awayCups - homeCups; + homeStanding.cupsFor += homeCups; + homeStanding.cupsAgainst += awayCups; + awayStanding.cupsFor += awayCups; + awayStanding.cupsAgainst += homeCups; - if (homeCups > awayCups) { - homeStanding.wins++; - awayStanding.losses++; - } else if (awayCups > homeCups) { - awayStanding.wins++; - homeStanding.losses++; - } - }); + homeStanding.cupDifference += homeCups - awayCups; + awayStanding.cupDifference += awayCups - homeCups; + + if (homeCups > awayCups) { + homeStanding.wins++; + awayStanding.losses++; + } else if (awayCups > homeCups) { + awayStanding.wins++; + homeStanding.losses++; + } + }); + + const h2hRecords = new Map>(); + + completedMatches.forEach((match) => { + const homeId = match.home?.id; + const awayId = match.away?.id; + + if (!homeId || !awayId) return; + + if (!h2hRecords.has(homeId)) { + h2hRecords.set(homeId, new Map()); + } + if (!h2hRecords.has(awayId)) { + h2hRecords.set(awayId, new Map()); + } + + const homeH2H = h2hRecords.get(homeId)!; + const awayH2H = h2hRecords.get(awayId)!; + + if (!homeH2H.has(awayId)) { + homeH2H.set(awayId, { wins: 0, cupDiff: 0 }); + } + if (!awayH2H.has(homeId)) { + awayH2H.set(homeId, { wins: 0, cupDiff: 0 }); + } + + const homeRecord = homeH2H.get(awayId)!; + const awayRecord = awayH2H.get(homeId)!; + + const homeCups = match.home_cups || 0; + const awayCups = match.away_cups || 0; + + const cupDiff = homeCups - awayCups; + homeRecord.cupDiff += cupDiff; + awayRecord.cupDiff -= cupDiff; + + if (homeCups > awayCups) { + homeRecord.wins++; + } else if (awayCups > homeCups) { + awayRecord.wins++; + } + }); return Array.from(standings.values()).sort((a, b) => { if (b.wins !== a.wins) return b.wins - a.wins; + if (b.cupDifference !== a.cupDifference) return b.cupDifference - a.cupDifference; - return b.cupsFor - a.cupsFor; + + if (b.cupsFor !== a.cupsFor) return b.cupsFor - a.cupsFor; + + const aH2H = h2hRecords.get(a.teamId); + const bH2H = h2hRecords.get(b.teamId); + + if (aH2H && bH2H) { + const aVsB = aH2H.get(b.teamId); + const bVsA = bH2H.get(a.teamId); + + 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.teamId.localeCompare(b.teamId); }); };