fixed b2b

This commit is contained in:
yohlo
2026-03-01 17:52:47 -06:00
parent 5d6136a445
commit 938418cec5

View File

@@ -473,54 +473,73 @@ export function createBadgesService(pb: PocketBase) {
}
if (criteria.consecutive_wins !== undefined) {
const tournaments = await pb.collection("tournaments").getFullList({
const allTournaments = await pb.collection("tournaments").getFullList({
filter: 'regional = false || regional = null',
sort: 'start_time',
});
let consecutiveWins = 0;
let maxConsecutiveWins = 0;
const tournamentResults: { tournament: any; playerWon: boolean }[] = [];
for (const tournament of tournaments) {
const tournamentMatches = await pb.collection("matches").getFullList({
for (const tournament of allTournaments) {
const matches = await pb.collection("matches").getFullList({
filter: `tournament = "${tournament.id}" && status = "ended"`,
expand: 'home,away,home.players,away.players',
});
if (tournamentMatches.length === 0) {
if (matches.length === 0) {
continue;
}
const winnersMatches = tournamentMatches.filter(m => !m.is_losers_bracket);
const winnersMatches = matches.filter(m => !m.is_losers_bracket);
if (winnersMatches.length === 0) {
continue;
}
const finalsMatch = winnersMatches.reduce((highest: any, current: any) =>
(!highest || current.lid > highest.lid) ? current : highest, null);
const finalsMatch = winnersMatches.reduce((highest: any, current: any) => {
if (!highest) return current;
return (current.lid > highest.lid) ? current : highest;
}, null);
if (!finalsMatch || finalsMatch.status !== 'ended') {
continue;
}
const finalsWinnerId = (finalsMatch.home_cups > finalsMatch.away_cups) ? finalsMatch.home : finalsMatch.away;
const winningTeamId = (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 winningTeam = finalsMatch.expand?.[winningTeamId === finalsMatch.home ? 'home' : 'away'];
if (!winningTeam) {
continue;
}
const playerWon = winningPlayers.some((p: any) =>
(typeof p === 'string' ? p : p.id) === playerId
);
const winningPlayers = winningTeam.expand?.players || winningTeam.players || [];
if (playerWon) {
consecutiveWins++;
maxConsecutiveWins = Math.max(maxConsecutiveWins, consecutiveWins);
const playerWon = winningPlayers.some((p: any) => {
const pid = (typeof p === 'string') ? p : p.id;
return pid === playerId;
});
tournamentResults.push({
tournament,
playerWon,
});
}
let currentStreak = 0;
let maxStreak = 0;
for (const result of tournamentResults) {
if (result.playerWon) {
currentStreak++;
maxStreak = Math.max(maxStreak, currentStreak);
} else {
consecutiveWins = 0;
currentStreak = 0;
}
}
return maxConsecutiveWins >= criteria.consecutive_wins ? 1 : 0;
return maxStreak >= criteria.consecutive_wins ? 1 : 0;
}
if (criteria.undefeated_group_stage !== undefined) {