Badge images!

This commit is contained in:
yohlo
2025-10-06 02:13:51 -05:00
parent af0ec85811
commit afd0b692fa
27 changed files with 111 additions and 37 deletions

View File

@@ -170,6 +170,38 @@ export function createBadgesService(pb: PocketBase) {
return tournamentsAttended;
}
if (criteria.won_tournament !== undefined) {
const tournamentId = criteria.won_tournament;
try {
const tournamentMatches = await pb.collection("matches").getFullList({
filter: `tournament = "${tournamentId}" && 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
);
return playerWon ? 1 : 0;
}
return 0;
} catch (error) {
return 0;
}
}
if (criteria.tournament_wins !== undefined) {
if (tournamentIds.size === 0) return 0;
@@ -342,26 +374,28 @@ export function createBadgesService(pb: PocketBase) {
const criteria = badge.criteria;
return (
criteria.matches_played ||
criteria.tournament_wins ||
criteria.tournaments_attended ||
criteria.overtime_matches ||
criteria.overtime_wins ||
criteria.consecutive_wins ||
1
);
// Use explicit checks to handle 0 values correctly
if (criteria.matches_played !== undefined) return criteria.matches_played;
if (criteria.tournament_wins !== undefined) return criteria.tournament_wins;
if (criteria.tournaments_attended !== undefined) return criteria.tournaments_attended;
if (criteria.overtime_matches !== undefined) return criteria.overtime_matches;
if (criteria.overtime_wins !== undefined) return criteria.overtime_wins;
if (criteria.consecutive_wins !== undefined) return criteria.consecutive_wins;
if (criteria.won_tournament !== undefined) return 1;
if (criteria.placement !== undefined) return 1;
if (criteria.margin_of_victory !== undefined) return 1;
if (criteria.tournament_record !== undefined) return 1;
return 1;
},
async awardManualBadge(playerId: string, badgeId: string): Promise<BadgeProgress> {
// Get or create badge progress record
const existingProgress = await pb.collection("badge_progress").getFirstListItem(
`player = "${playerId}" && badge = "${badgeId}"`,
{ expand: 'badge' }
).catch(() => null);
if (existingProgress) {
// Update existing progress to mark as earned
const updated = await pb.collection("badge_progress").update(existingProgress.id, {
progress: 1,
earned: true,
@@ -369,7 +403,6 @@ export function createBadgesService(pb: PocketBase) {
return transformBadgeProgress(updated);
}
// Create new progress record
const created = await pb.collection("badge_progress").create({
badge: badgeId,
player: playerId,
@@ -401,7 +434,11 @@ export function createBadgesService(pb: PocketBase) {
try {
const progress = await this.calculateBadgeProgress(playerId, badge);
const target = this.getTargetProgress(badge);
const earned = progress >= target;
const isPlacementBadge = badge.criteria.placement !== undefined;
const earned = badge.progressive || isPlacementBadge
? progress >= target
: progress === target;
if (progress > 0 || earned) {
await this.createBadgeProgress({