scoring notifications #115

Merged
chrisp merged 2 commits from claude/scoring-notification-display-jooaym into main 2026-06-30 01:58:20 +00:00
3 changed files with 79 additions and 16 deletions
Showing only changes of commit 4a9a70ca7c - Show all commits

View file

@ -59,7 +59,7 @@ function makeDb(existingResult?: { id: string; isPartialScore: boolean }) {
}; };
} }
import { processMatchResult, isLoserNotifiable, processPlayoffEvent } from "../scoring-calculator"; import { processMatchResult, isLoserNotifiable, isMatchNotable, processPlayoffEvent } from "../scoring-calculator";
import { doesLoserAdvance } from "../playoff-match"; import { doesLoserAdvance } from "../playoff-match";
import { updateProbabilitiesAfterResult } from "~/services/probability-updater"; import { updateProbabilitiesAfterResult } from "~/services/probability-updater";
@ -399,6 +399,24 @@ describe("isLoserNotifiable", () => {
}); });
}); });
describe("isMatchNotable", () => {
it("returns true when winner scored (loser not notifiable)", () => {
expect(isMatchNotable(true, false)).toBe(true);
});
it("returns true when loser is notifiable (winner did not score)", () => {
expect(isMatchNotable(false, true)).toBe(true);
});
it("returns true when both winner scored and loser is notifiable", () => {
expect(isMatchNotable(true, true)).toBe(true);
});
it("returns false when winner only advanced (no points) and loser is not notifiable", () => {
expect(isMatchNotable(false, false)).toBe(false);
});
});
describe("processPlayoffEvent - NBA Play-In Round 1 loserAdvances fix", () => { describe("processPlayoffEvent - NBA Play-In Round 1 loserAdvances fix", () => {
function makePlayInDb(matches: object[]) { function makePlayInDb(matches: object[]) {
const insertedRows: Array<{ participantId: string; finalPosition: number; isPartialScore: boolean }> = []; const insertedRows: Array<{ participantId: string; finalPosition: number; isPartialScore: boolean }> = [];

View file

@ -623,6 +623,25 @@ export function isLoserNotifiable(
return scoreChanged || finalizedLoserIds.has(loserId); return scoreChanged || finalizedLoserIds.has(loserId);
} }
/**
* Returns true if a scored match should be displayed in a Discord standings
* notification at all.
*
* A match is shown only when the winner earned points this round or the loser is
* notifiable (eliminated / scored see {@link isLoserNotifiable}). A drafted
* winner that merely advanced without earning points is NOT enough on its own.
*
* The winner's owner is still credited on any match that passes this gate (even
* a points-less advance) this function only decides whether the match line
* appears, not which managers are labelled within it.
*/
export function isMatchNotable(
winnerScoreChanged: boolean,
loserNotifiable: boolean
): boolean {
return winnerScoreChanged || loserNotifiable;
}
export function getGuaranteedMinimumPosition( export function getGuaranteedMinimumPosition(
round: string, round: string,
bracketTemplateId: string | null | undefined, bracketTemplateId: string | null | undefined,
@ -1826,12 +1845,13 @@ export async function recalculateAffectedLeagues(
); );
// Build scored matches for the notification. // Build scored matches for the notification.
// Winners only appear if their team's score changed (they earned points this round). // A match is included only when it is "notable": either the winner earned points this
// Losers appear if their team's score changed OR they were definitively eliminated // round OR the loser is notifiable (eliminated / scored). A drafted winner that merely
// (finalPosition set, non-partial) — the latter catches 0-pt eliminations that // advanced without earning points is NOT enough on its own (see isMatchNotable).
// produce no score delta. Losers who advance to another match (loserAdvances=true, // On any match that IS included, the winning team's owner is always credited — even
// e.g. NBA 7v8 → PIR2) have no finalized result and no score change, so they're // when the winner only advanced. Losers appear if their score changed or they were
// correctly suppressed. // definitively eliminated (finalPosition set, non-partial); losers who advance to
// another match (loserAdvances=true, e.g. NBA 7v8 → PIR2) are correctly suppressed.
let scoredMatches: ScoredMatch[] | undefined; let scoredMatches: ScoredMatch[] | undefined;
if (allCompletedMatches.length > 0) { if (allCompletedMatches.length > 0) {
const relevant = allCompletedMatches.filter( const relevant = allCompletedMatches.filter(
@ -1857,15 +1877,17 @@ export async function recalculateAffectedLeagues(
const winnerOwnerId = lookupOwner(m.winnerId); const winnerOwnerId = lookupOwner(m.winnerId);
const loserOwnerId = lookupOwner(m.loserId); const loserOwnerId = lookupOwner(m.loserId);
const showLoser = isLoserNotifiable(m.loserId, loserTeamId, changedTeamIds, finalizedLoserIds); const showLoser = isLoserNotifiable(m.loserId, loserTeamId, changedTeamIds, finalizedLoserIds);
return { return { m, winnerScoreChanged, showLoser, winnerOwnerId, loserOwnerId };
winnerName: m.winnerName ?? "", })
loserName: m.loserName ?? "", .filter((x) => isMatchNotable(x.winnerScoreChanged, x.showLoser))
winnerUsername: winnerScoreChanged && winnerOwnerId ? usernameByUserId.get(winnerOwnerId) : undefined, .map((x) => ({
loserUsername: showLoser && loserOwnerId ? usernameByUserId.get(loserOwnerId) : undefined, winnerName: x.m.winnerName ?? "",
winnerDiscordUserId: winnerScoreChanged && winnerOwnerId ? discordIdByUserId.get(winnerOwnerId) : undefined, loserName: x.m.loserName ?? "",
loserDiscordUserId: showLoser && loserOwnerId ? discordIdByUserId.get(loserOwnerId) : undefined, winnerUsername: x.winnerOwnerId ? usernameByUserId.get(x.winnerOwnerId) : undefined,
}; loserUsername: x.showLoser && x.loserOwnerId ? usernameByUserId.get(x.loserOwnerId) : undefined,
}); winnerDiscordUserId: x.winnerOwnerId ? discordIdByUserId.get(x.winnerOwnerId) : undefined,
loserDiscordUserId: x.showLoser && x.loserOwnerId ? discordIdByUserId.get(x.loserOwnerId) : undefined,
}));
} }
} }

View file

@ -186,6 +186,29 @@ describe("sendStandingsUpdateNotification", () => {
expect(desc).toContain("• **Sporting (christhrowsrocks)** def. Bodø/Glimt (apatel)"); expect(desc).toContain("• **Sporting (christhrowsrocks)** def. Bodø/Glimt (apatel)");
}); });
it("shows winner's owner even when winner only advanced (loser was eliminated)", async () => {
// Mirrors the Brazil/Japan scenario: Brazil advanced without earning points,
// Japan was eliminated. The calculator now sets winnerUsername unconditionally
// on any match that passes the notability gate.
await sendStandingsUpdateNotification({
webhookUrl: WEBHOOK_URL,
seasonName: "Rumble League 2026",
standings: [{ teamId: "a", teamName: "Alpha", totalPoints: 100, rank: 1 }],
previousStandings: new Map([["a", 100]]),
scoredMatches: [
{
winnerName: "Brazil",
loserName: "Japan",
winnerUsername: "someManager",
loserUsername: "ikyn",
},
],
});
const desc = getDescription();
expect(desc).toContain("• **Brazil (someManager)** def. Japan (ikyn)");
});
it("omits scored matches where neither side has a username", async () => { it("omits scored matches where neither side has a username", async () => {
await sendStandingsUpdateNotification({ await sendStandingsUpdateNotification({
webhookUrl: WEBHOOK_URL, webhookUrl: WEBHOOK_URL,