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