diff --git a/src/lib/pocketbase/services/badges.ts b/src/lib/pocketbase/services/badges.ts index 3e396df..19b3487 100644 --- a/src/lib/pocketbase/services/badges.ts +++ b/src/lib/pocketbase/services/badges.ts @@ -377,9 +377,31 @@ export function createBadgesService(pb: PocketBase) { for (const tournament of tournaments) { if (!tournamentIds.has(tournament.id)) continue; - if (tournament.winner_id === playerId) { - consecutiveWins++; - maxConsecutiveWins = Math.max(maxConsecutiveWins, consecutiveWins); + const tournamentMatches = await pb.collection("matches").getFullList({ + filter: `tournament = "${tournament.id}" && status = "ended"`, + expand: 'home,away,home.players,away.players', + }); + + const winnersMatches = tournamentMatches.filter(m => !m.is_losers_bracket); + const finalsMatch = winnersMatches.reduce((highest: any, current: any) => + (!highest || current.lid > highest.lid) ? current : highest, null); + + if (finalsMatch && finalsMatch.status === 'ended') { + const finalsWinnerId = (finalsMatch.home_cups > finalsMatch.away_cups) ? finalsMatch.home : finalsMatch.away; + + const winningTeam = finalsMatch.expand?.[finalsWinnerId === finalsMatch.home ? 'home' : 'away']; + const winningPlayers = winningTeam?.expand?.players || winningTeam?.players || []; + + const playerWon = winningPlayers.some((p: any) => + (typeof p === 'string' ? p : p.id) === playerId + ); + + if (playerWon) { + consecutiveWins++; + maxConsecutiveWins = Math.max(maxConsecutiveWins, consecutiveWins); + } else { + consecutiveWins = 0; + } } else { consecutiveWins = 0; }