fix: only set winnerUsername when winning team's score actually changed
Previously, winnerUsername was set for any drafted winner regardless of whether points were actually scored. This caused R64 wins (where the scoring system may not award points until later rounds) to appear in Discord's Scored Matches section even when no Brackt points were earned. Now winnerUsername is only set when the winner's team's totalPoints changed after recalculation. loserUsername (eliminations) is always set when the loser is drafted, since being knocked out is always notable. https://claude.ai/code/session_01FciwfdG9Sfr5ZrXUHPeB18
This commit is contained in:
parent
be9f30753f
commit
b0c43ef2d3
1 changed files with 25 additions and 1 deletions
|
|
@ -1357,6 +1357,17 @@ export async function recalculateAffectedLeagues(
|
||||||
const teamIdByParticipantId = new Map(
|
const teamIdByParticipantId = new Map(
|
||||||
draftPicks.map((p) => [p.participantId, p.teamId])
|
draftPicks.map((p) => [p.participantId, p.teamId])
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Teams whose score actually changed after recalculation.
|
||||||
|
const changedTeamIds = new Set(
|
||||||
|
afterStandings
|
||||||
|
.filter((s) => {
|
||||||
|
const prev = previousPointsById.get(s.teamId);
|
||||||
|
return prev === undefined || prev !== parseFloat(s.totalPoints);
|
||||||
|
})
|
||||||
|
.map((s) => s.teamId)
|
||||||
|
);
|
||||||
|
|
||||||
const usernameForParticipant = (participantId: string | null | undefined) => {
|
const usernameForParticipant = (participantId: string | null | undefined) => {
|
||||||
if (!participantId) return undefined;
|
if (!participantId) return undefined;
|
||||||
const teamId = teamIdByParticipantId.get(participantId);
|
const teamId = teamIdByParticipantId.get(participantId);
|
||||||
|
|
@ -1366,12 +1377,25 @@ export async function recalculateAffectedLeagues(
|
||||||
return usernameByClerkId.get(ownerId);
|
return usernameByClerkId.get(ownerId);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Only set winnerUsername if the winner's owner actually scored points
|
||||||
|
// this round. This prevents listing matches where a drafted team won
|
||||||
|
// but the scoring system didn't award points yet (e.g. R64 wins when
|
||||||
|
// points only trigger at Sweet 16+).
|
||||||
|
const winnerUsernameForParticipant = (participantId: string | null | undefined) => {
|
||||||
|
if (!participantId) return undefined;
|
||||||
|
const teamId = teamIdByParticipantId.get(participantId);
|
||||||
|
if (!teamId || !changedTeamIds.has(teamId)) return undefined;
|
||||||
|
const ownerId = ownerIdByTeamId.get(teamId);
|
||||||
|
if (!ownerId) return undefined;
|
||||||
|
return usernameByClerkId.get(ownerId);
|
||||||
|
};
|
||||||
|
|
||||||
scoredMatches = relevant
|
scoredMatches = relevant
|
||||||
.filter((m) => m.winnerName && m.loserName)
|
.filter((m) => m.winnerName && m.loserName)
|
||||||
.map((m) => ({
|
.map((m) => ({
|
||||||
winnerName: m.winnerName!,
|
winnerName: m.winnerName!,
|
||||||
loserName: m.loserName!,
|
loserName: m.loserName!,
|
||||||
winnerUsername: usernameForParticipant(m.winnerId),
|
winnerUsername: winnerUsernameForParticipant(m.winnerId),
|
||||||
loserUsername: usernameForParticipant(m.loserId),
|
loserUsername: usernameForParticipant(m.loserId),
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue