2026-03-16 21:43:39 -07:00
|
|
|
|
/**
|
|
|
|
|
|
* NBA Playoff Simulator
|
|
|
|
|
|
*
|
2026-04-13 00:51:53 -04:00
|
|
|
|
* Two modes, auto-detected at runtime:
|
|
|
|
|
|
*
|
|
|
|
|
|
* ── Mode 1: Bracket-Aware (preferred) ─────────────────────────────────────────
|
|
|
|
|
|
* Used when a playoff_game scoring event with generated matches exists for the
|
|
|
|
|
|
* sports season. Reads the actual bracket state from the DB and simulates only
|
|
|
|
|
|
* the remaining rounds forward.
|
2026-03-16 21:43:39 -07:00
|
|
|
|
*
|
|
|
|
|
|
* Algorithm:
|
2026-04-13 00:51:53 -04:00
|
|
|
|
* 1. Find the playoff_game scoring event; load all playoffMatches
|
|
|
|
|
|
* 2. Group matches by round: Play-In Round 1/2, First Round, Conference
|
|
|
|
|
|
* Semifinals, Conference Finals, NBA Finals
|
|
|
|
|
|
* 3. Build Elo map from TEAMS_DATA (name-matched from participants table)
|
|
|
|
|
|
* 4. For each of 50k simulations:
|
|
|
|
|
|
* a. Play-In Round 1 (4 single games): use real result if isComplete, else sim
|
|
|
|
|
|
* → derives E7/E8-candidate/E9-E10-winner and their West equivalents
|
|
|
|
|
|
* b. Play-In Round 2 (2 single games): uses PIR1 results as participant
|
|
|
|
|
|
* fallbacks when DB slots are still null
|
|
|
|
|
|
* → derives E8 and W8 seeds
|
|
|
|
|
|
* c. First Round (8 best-of-7 series): uses simmed seeds as participant
|
|
|
|
|
|
* fallbacks for the 4 play-in slots; real results respected
|
|
|
|
|
|
* d. Conference Semifinals → Conference Finals → NBA Finals: same
|
|
|
|
|
|
* completed-vs-simulate pattern; standard ceil bracket path
|
|
|
|
|
|
* 5. Track integer placement counts per tier (champion/finalist/CF loser/CS loser)
|
|
|
|
|
|
* 6. Convert to probability distributions with exact denominators
|
2026-03-16 21:43:39 -07:00
|
|
|
|
*
|
2026-04-13 00:51:53 -04:00
|
|
|
|
* In-progress series: treated as a fresh best-of-7 (partial game scores are not
|
|
|
|
|
|
* used). A series only locks once isComplete + winnerId + loserId are all set.
|
2026-03-16 21:43:39 -07:00
|
|
|
|
*
|
2026-04-13 00:51:53 -04:00
|
|
|
|
* ── Mode 2: Season Projection (fallback) ──────────────────────────────────────
|
|
|
|
|
|
* Used when no bracket event / matches exist yet (pre-playoffs). Projects
|
|
|
|
|
|
* remaining regular season games → seeds → play-in → playoffs from scratch.
|
|
|
|
|
|
* See inline comments for details.
|
2026-03-21 23:37:20 -07:00
|
|
|
|
*
|
2026-04-13 00:51:53 -04:00
|
|
|
|
* Placement tiers (both modes):
|
|
|
|
|
|
* probFirst = NBA champion
|
|
|
|
|
|
* probSecond = NBA Finals loser
|
|
|
|
|
|
* probThird/Fourth = Conference Finals losers (2 per sim)
|
2026-03-16 21:43:39 -07:00
|
|
|
|
* probFifth–Eighth = Conference Semis losers (4 per sim)
|
2026-04-13 00:51:53 -04:00
|
|
|
|
* First Round / Play-In losers → all 0 (score 0 points)
|
|
|
|
|
|
* Missed playoffs (Mode 2 only) → all 0
|
2026-03-16 21:43:39 -07:00
|
|
|
|
*
|
2026-04-13 00:51:53 -04:00
|
|
|
|
* Elo ratings are hardcoded (March 2026 data).
|
2026-03-21 23:37:20 -07:00
|
|
|
|
* Source: Neil Paine Substack playoff Elo estimates (last 110 games, no regression,
|
|
|
|
|
|
* postseason games 3× weight). Update at the start of each season.
|
2026-03-16 21:43:39 -07:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
import { database } from "~/database/context";
|
2026-04-13 00:51:53 -04:00
|
|
|
|
import { eq, and } from "drizzle-orm";
|
2026-03-16 21:43:39 -07:00
|
|
|
|
import * as schema from "~/database/schema";
|
|
|
|
|
|
import type { Simulator, SimulationResult } from "./types";
|
2026-03-21 00:12:01 -07:00
|
|
|
|
import { normalizeTeamName } from "~/lib/normalize-team-name";
|
2026-03-21 23:37:20 -07:00
|
|
|
|
import { getRegularSeasonStandings } from "~/models/regular-season-standings";
|
2026-06-30 23:24:48 +00:00
|
|
|
|
import { positiveConfigNumber } from "./config-access";
|
2026-03-16 21:43:39 -07:00
|
|
|
|
|
2026-06-30 23:24:48 +00:00
|
|
|
|
// ─── Simulation parameters (defaults; overridable via season config) ───────────
|
2026-03-16 21:43:39 -07:00
|
|
|
|
|
2026-06-30 23:24:48 +00:00
|
|
|
|
const DEFAULT_NUM_SIMULATIONS = 50_000;
|
2026-03-16 21:43:39 -07:00
|
|
|
|
|
|
|
|
|
|
/**
|
2026-06-30 23:24:48 +00:00
|
|
|
|
* Elo parity factor. NBA defaults to 400 (standard formula).
|
2026-03-16 21:43:39 -07:00
|
|
|
|
* A 400-point Elo difference → ~90.9% win probability per game.
|
2026-06-30 23:24:48 +00:00
|
|
|
|
* Raising it (via the season config's `parityFactor`) flattens the distribution.
|
2026-03-16 21:43:39 -07:00
|
|
|
|
*/
|
2026-06-30 23:24:48 +00:00
|
|
|
|
const DEFAULT_PARITY_FACTOR = 400;
|
2026-03-16 21:43:39 -07:00
|
|
|
|
|
2026-03-21 23:37:20 -07:00
|
|
|
|
/** NBA regular season games per team. */
|
2026-06-30 23:24:48 +00:00
|
|
|
|
const DEFAULT_REGULAR_SEASON_GAMES = 82;
|
2026-03-16 21:43:39 -07:00
|
|
|
|
|
2026-03-21 23:37:20 -07:00
|
|
|
|
// ─── Team data (2025-26 season, as of March 2026) ─────────────────────────────
|
2026-03-16 21:43:39 -07:00
|
|
|
|
|
|
|
|
|
|
interface NbaTeamData {
|
|
|
|
|
|
conference: "Eastern" | "Western";
|
|
|
|
|
|
elo: number;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const TEAMS_DATA: Record<string, NbaTeamData> = {
|
|
|
|
|
|
// ── Eastern Conference ──────────────────────────────────────────────────────
|
|
|
|
|
|
|
2026-03-21 23:37:20 -07:00
|
|
|
|
"Detroit Pistons": { conference: "Eastern", elo: 1558 },
|
|
|
|
|
|
"Boston Celtics": { conference: "Eastern", elo: 1699 },
|
|
|
|
|
|
"New York Knicks": { conference: "Eastern", elo: 1626 },
|
|
|
|
|
|
"Cleveland Cavaliers": { conference: "Eastern", elo: 1628 },
|
|
|
|
|
|
"Orlando Magic": { conference: "Eastern", elo: 1508 },
|
|
|
|
|
|
"Miami Heat": { conference: "Eastern", elo: 1530 },
|
|
|
|
|
|
"Toronto Raptors": { conference: "Eastern", elo: 1467 },
|
|
|
|
|
|
"Atlanta Hawks": { conference: "Eastern", elo: 1496 },
|
|
|
|
|
|
"Philadelphia 76ers": { conference: "Eastern", elo: 1471 },
|
|
|
|
|
|
"Charlotte Hornets": { conference: "Eastern", elo: 1496 },
|
|
|
|
|
|
"Milwaukee Bucks": { conference: "Eastern", elo: 1442 },
|
|
|
|
|
|
"Chicago Bulls": { conference: "Eastern", elo: 1381 },
|
|
|
|
|
|
"Brooklyn Nets": { conference: "Eastern", elo: 1334 },
|
|
|
|
|
|
"Indiana Pacers": { conference: "Eastern", elo: 1433 },
|
|
|
|
|
|
"Washington Wizards": { conference: "Eastern", elo: 1255 },
|
2026-03-16 21:43:39 -07:00
|
|
|
|
|
|
|
|
|
|
// ── Western Conference ──────────────────────────────────────────────────────
|
|
|
|
|
|
|
2026-03-21 23:37:20 -07:00
|
|
|
|
"Oklahoma City Thunder": { conference: "Western", elo: 1731 },
|
|
|
|
|
|
"San Antonio Spurs": { conference: "Western", elo: 1599 },
|
|
|
|
|
|
"Houston Rockets": { conference: "Western", elo: 1564 },
|
|
|
|
|
|
"Denver Nuggets": { conference: "Western", elo: 1618 },
|
|
|
|
|
|
"LA Lakers": { conference: "Western", elo: 1569 },
|
|
|
|
|
|
"Minnesota Timberwolves":{ conference: "Western", elo: 1603 },
|
|
|
|
|
|
"Phoenix Suns": { conference: "Western", elo: 1500 },
|
|
|
|
|
|
"LA Clippers": { conference: "Western", elo: 1573 },
|
|
|
|
|
|
"Golden State Warriors": { conference: "Western", elo: 1530 },
|
|
|
|
|
|
"Portland Trail Blazers":{ conference: "Western", elo: 1426 },
|
|
|
|
|
|
"Dallas Mavericks": { conference: "Western", elo: 1473 },
|
|
|
|
|
|
"Memphis Grizzlies": { conference: "Western", elo: 1417 },
|
|
|
|
|
|
"New Orleans Pelicans": { conference: "Western", elo: 1380 },
|
|
|
|
|
|
"Sacramento Kings": { conference: "Western", elo: 1352 },
|
|
|
|
|
|
"Utah Jazz": { conference: "Western", elo: 1334 },
|
2026-03-16 21:43:39 -07:00
|
|
|
|
};
|
|
|
|
|
|
|
2026-04-13 00:51:53 -04:00
|
|
|
|
// ─── Public helpers ───────────────────────────────────────────────────────────
|
2026-03-16 21:43:39 -07:00
|
|
|
|
|
2026-03-21 00:12:01 -07:00
|
|
|
|
export { normalizeTeamName };
|
2026-03-16 21:43:39 -07:00
|
|
|
|
|
|
|
|
|
|
/** Look up team data by participant name (case-insensitive). */
|
|
|
|
|
|
export function getTeamData(name: string): NbaTeamData | undefined {
|
|
|
|
|
|
const normalized = normalizeTeamName(name);
|
|
|
|
|
|
for (const [teamName, data] of Object.entries(TEAMS_DATA)) {
|
|
|
|
|
|
if (normalizeTeamName(teamName) === normalized) return data;
|
|
|
|
|
|
}
|
|
|
|
|
|
return undefined;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Elo win probability for team A over team B.
|
|
|
|
|
|
* P(A) = 1 / (1 + 10^((eloB - eloA) / PARITY_FACTOR))
|
|
|
|
|
|
*/
|
2026-06-30 23:24:48 +00:00
|
|
|
|
export function eloWinProbability(eloA: number, eloB: number, parityFactor = DEFAULT_PARITY_FACTOR): number {
|
|
|
|
|
|
return 1 / (1 + Math.pow(10, (eloB - eloA) / parityFactor));
|
2026-03-16 21:43:39 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-13 00:51:53 -04:00
|
|
|
|
// ─── Per-position normalization ───────────────────────────────────────────────
|
2026-03-16 21:43:39 -07:00
|
|
|
|
|
2026-04-13 00:51:53 -04:00
|
|
|
|
const POSITION_KEYS = [
|
|
|
|
|
|
"probFirst", "probSecond", "probThird", "probFourth",
|
|
|
|
|
|
"probFifth", "probSixth", "probSeventh", "probEighth",
|
|
|
|
|
|
] as const;
|
|
|
|
|
|
|
|
|
|
|
|
function normalizeColumns(results: SimulationResult[]): void {
|
|
|
|
|
|
for (const key of POSITION_KEYS) {
|
|
|
|
|
|
const colSum = results.reduce((s, r) => s + r.probabilities[key], 0);
|
|
|
|
|
|
const residual = 1.0 - colSum;
|
|
|
|
|
|
if (residual !== 0) {
|
|
|
|
|
|
const maxResult = results.reduce((best, r) =>
|
|
|
|
|
|
r.probabilities[key] > best.probabilities[key] ? r : best
|
|
|
|
|
|
);
|
|
|
|
|
|
maxResult.probabilities[key] += residual;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-03-16 21:43:39 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-13 00:51:53 -04:00
|
|
|
|
// ─── Bracket-aware helpers ────────────────────────────────────────────────────
|
|
|
|
|
|
|
|
|
|
|
|
type DbMatch = typeof schema.playoffMatches.$inferSelect;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2026-06-30 23:24:48 +00:00
|
|
|
|
* Build the bracket match helpers bound to a given `parityFactor`. Returning them
|
|
|
|
|
|
* from a factory keeps every call site unchanged while letting the season config
|
|
|
|
|
|
* tune per-game variance.
|
2026-04-13 00:51:53 -04:00
|
|
|
|
*/
|
2026-06-30 23:24:48 +00:00
|
|
|
|
function makeBracketHelpers(parityFactor: number) {
|
|
|
|
|
|
/** Simulate a single-game matchup. Returns winner and loser. */
|
|
|
|
|
|
function simGame(
|
|
|
|
|
|
eloMap: Map<string, number>,
|
|
|
|
|
|
a: string,
|
|
|
|
|
|
b: string
|
|
|
|
|
|
): { winner: string; loser: string } {
|
|
|
|
|
|
const eloA = eloMap.get(a) ?? 1400;
|
|
|
|
|
|
const eloB = eloMap.get(b) ?? 1400;
|
|
|
|
|
|
const winner = Math.random() < eloWinProbability(eloA, eloB, parityFactor) ? a : b;
|
|
|
|
|
|
return { winner, loser: winner === a ? b : a };
|
2026-04-13 00:51:53 -04:00
|
|
|
|
}
|
2026-06-30 23:24:48 +00:00
|
|
|
|
|
|
|
|
|
|
/** Simulate a best-of-7 series. Returns winner and loser. */
|
|
|
|
|
|
function simSeries(
|
|
|
|
|
|
eloMap: Map<string, number>,
|
|
|
|
|
|
a: string,
|
|
|
|
|
|
b: string
|
|
|
|
|
|
): { winner: string; loser: string } {
|
|
|
|
|
|
const winProb = eloWinProbability(eloMap.get(a) ?? 1400, eloMap.get(b) ?? 1400, parityFactor);
|
|
|
|
|
|
let wA = 0;
|
|
|
|
|
|
let wB = 0;
|
|
|
|
|
|
while (wA < 4 && wB < 4) {
|
|
|
|
|
|
if (Math.random() < winProb) wA++; else wB++;
|
|
|
|
|
|
}
|
|
|
|
|
|
return wA === 4 ? { winner: a, loser: b } : { winner: b, loser: a };
|
2026-04-13 00:51:53 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-06-30 23:24:48 +00:00
|
|
|
|
/**
|
|
|
|
|
|
* Resolve a play-in single game.
|
|
|
|
|
|
* Uses the real result if the match is complete; otherwise simulates.
|
|
|
|
|
|
* p1Override / p2Override are used when the DB participant slot is still null
|
|
|
|
|
|
* (i.e. filled in by the previous round's simulated result).
|
|
|
|
|
|
*/
|
|
|
|
|
|
function resolveGame(
|
|
|
|
|
|
eloMap: Map<string, number>,
|
|
|
|
|
|
match: DbMatch | undefined,
|
|
|
|
|
|
p1Override?: string,
|
|
|
|
|
|
p2Override?: string
|
|
|
|
|
|
): { winner: string; loser: string } {
|
|
|
|
|
|
if (match?.isComplete && match.winnerId && match.loserId) {
|
|
|
|
|
|
return { winner: match.winnerId, loser: match.loserId };
|
|
|
|
|
|
}
|
|
|
|
|
|
const p1 = match?.participant1Id ?? p1Override;
|
|
|
|
|
|
const p2 = match?.participant2Id ?? p2Override;
|
|
|
|
|
|
if (!p1 || !p2) {
|
|
|
|
|
|
throw new Error(
|
|
|
|
|
|
`Cannot resolve game: missing participant(s) on match ${match?.id ?? "(undefined)"} ` +
|
|
|
|
|
|
`(p1=${p1 ?? "null"}, p2=${p2 ?? "null"}). ` +
|
|
|
|
|
|
`Ensure the bracket is fully generated before simulating.`
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
return simGame(eloMap, p1, p2);
|
2026-04-13 00:51:53 -04:00
|
|
|
|
}
|
2026-06-30 23:24:48 +00:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Resolve a playoff series.
|
|
|
|
|
|
* Uses the real result if the match is complete; otherwise simulates best-of-7.
|
|
|
|
|
|
* p1Override / p2Override fill null DB participant slots with the simulated seed.
|
|
|
|
|
|
*/
|
|
|
|
|
|
function resolveSeries(
|
|
|
|
|
|
eloMap: Map<string, number>,
|
|
|
|
|
|
match: DbMatch | undefined,
|
|
|
|
|
|
p1Override?: string,
|
|
|
|
|
|
p2Override?: string
|
|
|
|
|
|
): { winner: string; loser: string } {
|
|
|
|
|
|
if (match?.isComplete && match.winnerId && match.loserId) {
|
|
|
|
|
|
return { winner: match.winnerId, loser: match.loserId };
|
|
|
|
|
|
}
|
|
|
|
|
|
const p1 = match?.participant1Id ?? p1Override;
|
|
|
|
|
|
const p2 = match?.participant2Id ?? p2Override;
|
|
|
|
|
|
if (!p1 || !p2) {
|
|
|
|
|
|
throw new Error(
|
|
|
|
|
|
`Cannot resolve series: missing participant(s) on match ${match?.id ?? "(undefined)"} ` +
|
|
|
|
|
|
`(p1=${p1 ?? "null"}, p2=${p2 ?? "null"}). ` +
|
|
|
|
|
|
`Ensure the bracket is fully generated before simulating.`
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
return simSeries(eloMap, p1, p2);
|
2026-04-13 00:51:53 -04:00
|
|
|
|
}
|
2026-06-30 23:24:48 +00:00
|
|
|
|
|
|
|
|
|
|
return { simGame, simSeries, resolveGame, resolveSeries };
|
2026-03-21 23:37:20 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-16 21:43:39 -07:00
|
|
|
|
// ─── Simulator ────────────────────────────────────────────────────────────────
|
|
|
|
|
|
|
|
|
|
|
|
export class NBASimulator implements Simulator {
|
2026-06-30 23:24:48 +00:00
|
|
|
|
async simulate(sportsSeasonId: string, config: Record<string, unknown> = {}): Promise<SimulationResult[]> {
|
2026-03-16 21:43:39 -07:00
|
|
|
|
const db = database();
|
|
|
|
|
|
|
2026-04-13 00:51:53 -04:00
|
|
|
|
// ── Mode detection: bracket-aware if a playoff event with matches exists ──
|
|
|
|
|
|
const bracketEvent = await db.query.scoringEvents.findFirst({
|
|
|
|
|
|
where: and(
|
|
|
|
|
|
eq(schema.scoringEvents.sportsSeasonId, sportsSeasonId),
|
|
|
|
|
|
eq(schema.scoringEvents.eventType, "playoff_game")
|
|
|
|
|
|
),
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
if (bracketEvent) {
|
|
|
|
|
|
const bracketMatches = await db.query.playoffMatches.findMany({
|
|
|
|
|
|
where: eq(schema.playoffMatches.scoringEventId, bracketEvent.id),
|
|
|
|
|
|
orderBy: (m, { asc }) => [asc(m.matchNumber)],
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
if (bracketMatches.length > 0) {
|
2026-06-30 23:24:48 +00:00
|
|
|
|
return this.simulateBracketAware(sportsSeasonId, bracketMatches, config);
|
2026-04-13 00:51:53 -04:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ── Fall back to season-projection mode ───────────────────────────────────
|
2026-06-30 23:24:48 +00:00
|
|
|
|
return this.simulateSeasonProjection(sportsSeasonId, config);
|
2026-04-13 00:51:53 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ── Mode 1: Bracket-Aware ──────────────────────────────────────────────────
|
|
|
|
|
|
|
|
|
|
|
|
private async simulateBracketAware(
|
|
|
|
|
|
sportsSeasonId: string,
|
2026-06-30 23:24:48 +00:00
|
|
|
|
allMatches: DbMatch[],
|
|
|
|
|
|
config: Record<string, unknown> = {}
|
2026-04-13 00:51:53 -04:00
|
|
|
|
): Promise<SimulationResult[]> {
|
|
|
|
|
|
const db = database();
|
2026-06-30 23:24:48 +00:00
|
|
|
|
const parityFactor = positiveConfigNumber(config, "parityFactor", DEFAULT_PARITY_FACTOR);
|
|
|
|
|
|
const numSimulations = Math.round(positiveConfigNumber(config, "iterations", DEFAULT_NUM_SIMULATIONS));
|
|
|
|
|
|
const { resolveGame, resolveSeries } = makeBracketHelpers(parityFactor);
|
2026-04-13 00:51:53 -04:00
|
|
|
|
|
|
|
|
|
|
// Group matches by round.
|
|
|
|
|
|
const pir1 = allMatches.filter((m) => m.round === "Play-In Round 1")
|
|
|
|
|
|
.toSorted((a, b) => a.matchNumber - b.matchNumber);
|
|
|
|
|
|
const pir2 = allMatches.filter((m) => m.round === "Play-In Round 2")
|
|
|
|
|
|
.toSorted((a, b) => a.matchNumber - b.matchNumber);
|
|
|
|
|
|
const fr = allMatches.filter((m) => m.round === "First Round")
|
|
|
|
|
|
.toSorted((a, b) => a.matchNumber - b.matchNumber);
|
|
|
|
|
|
const cs = allMatches.filter((m) => m.round === "Conference Semifinals")
|
|
|
|
|
|
.toSorted((a, b) => a.matchNumber - b.matchNumber);
|
|
|
|
|
|
const cf = allMatches.filter((m) => m.round === "Conference Finals")
|
|
|
|
|
|
.toSorted((a, b) => a.matchNumber - b.matchNumber);
|
|
|
|
|
|
const finals = allMatches.filter((m) => m.round === "NBA Finals");
|
|
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
|
pir1.length !== 4 || pir2.length !== 2 || fr.length !== 8 ||
|
|
|
|
|
|
cs.length !== 4 || cf.length !== 2 || finals.length !== 1
|
|
|
|
|
|
) {
|
|
|
|
|
|
throw new Error(
|
|
|
|
|
|
`NBA bracket has unexpected structure. Expected PIR1×4, PIR2×2, FR×8, CS×4, CF×2, Finals×1. ` +
|
|
|
|
|
|
`Got PIR1×${pir1.length}, PIR2×${pir2.length}, FR×${fr.length}, ` +
|
|
|
|
|
|
`CS×${cs.length}, CF×${cf.length}, Finals×${finals.length}. ` +
|
|
|
|
|
|
`Ensure the bracket was generated using the nba_20 template.`
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Collect all participant IDs from the bracket.
|
|
|
|
|
|
const participantIdSet = new Set<string>();
|
|
|
|
|
|
for (const m of allMatches) {
|
|
|
|
|
|
if (m.participant1Id) participantIdSet.add(m.participant1Id);
|
|
|
|
|
|
if (m.participant2Id) participantIdSet.add(m.participant2Id);
|
|
|
|
|
|
if (m.winnerId) participantIdSet.add(m.winnerId);
|
|
|
|
|
|
if (m.loserId) participantIdSet.add(m.loserId);
|
|
|
|
|
|
}
|
|
|
|
|
|
const participantIds = [...participantIdSet];
|
|
|
|
|
|
|
Fix simulator brackets: NHL bracket-aware mode, MLB/NBA/WNBA sourceElo support (#358)
* Fix NHL bracket-aware simulation and MLB sourceElo support
NHL: NHLSimulator now checks for a populated playoff_game bracket before
falling back to season-projection mode. When a bracket exists (e.g. via
simple_16 template), it simulates the actual bracket structure, respecting
completed matches and using ELO+odds blending for the rest — matching the
pattern used by Snooker, NBA, and UCL simulators.
MLB: MLBSimulator now reads sourceElo from participantExpectedValues and
converts it to an effective RDif (via eloToRDif), overriding the hardcoded
TEAMS_DATA.rdif when present. This wires up the expected-wins admin form
(which saves sourceElo) to the simulation engine.
https://claude.ai/code/session_01139GJVg2gqgDjcUzqhnBNn
* Wire sourceElo from projected wins form into NBA, NHL, and WNBA simulators
All three simulators had entries in simulator-config.ts (enabling the projected
wins form in the admin UI) but silently ignored the sourceElo that was saved.
NBA: Both bracket-aware and season-projection modes now load sourceElo and
prefer it over hardcoded TEAMS_DATA elo. Added resolvedElo field to TeamEntry
so eloOfEntry() uses the correct value throughout.
NHL: Season-projection mode now fetches sourceElo + sourceOdds in a single
parallel query (removing the duplicate evRows query). Bracket-aware mode also
loads sourceElo alongside sourceOdds. Both modes use sourceElo > TEAMS_DATA > 1400.
WNBA: resolveElo() now accepts sourceElo as an optional highest-priority
argument. The evRows query fetches sourceElo alongside sourceOdds and passes
it through when building team entries.
https://claude.ai/code/session_01139GJVg2gqgDjcUzqhnBNn
* Add review fixes: NHL R1 validation, WNBA sourceElo tests, NHL bracket unit tests
- NHLSimulator.simulateBracket: remove unused nameById variable
- NHLSimulator.simulateBracket: validate all R1 participants before entering
Monte Carlo loop; throws with a clear message if any slot is missing
- wnba-simulator.test.ts: add 5 tests covering the sourceElo override parameter
on resolveElo() (overrides SRS, overrides futures, overrides fallback,
null/undefined fall through to normal priority chain)
- nhl-simulator.test.ts: add full bracket-aware integration suite (10 tests)
covering fallback to season-projection, structure validation errors, probability
distributions, fully-decided champion, and source tag
https://claude.ai/code/session_01139GJVg2gqgDjcUzqhnBNn
* Fix TS error: add resolvedElo to makeEntry in nhl-simulator.test.ts
TeamEntry requires resolvedElo after the simulator refactor.
https://claude.ai/code/session_01139GJVg2gqgDjcUzqhnBNn
* Fix lint: replace non-null assertions with null-safe alternatives in NHL test
https://claude.ai/code/session_01139GJVg2gqgDjcUzqhnBNn
---------
Co-authored-by: Claude <noreply@anthropic.com>
2026-04-30 08:33:57 -07:00
|
|
|
|
// Load participant names and sourceElo values in parallel.
|
|
|
|
|
|
const [participantRows, evRows] = await Promise.all([
|
|
|
|
|
|
db
|
Canonical tournament layer: schema + backfill (1/2) (#365)
* refactor(schema): rename per-window tables to season_* prefix
Renames participants, participant_expected_values, participant_qualifying_totals,
participant_results, participant_surface_elos to season_* prefixed names.
Renames event_results.participant_id to season_participant_id.
Phase 1a of canonical tournament layer migration.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
* refactor: rename participant.ts model file to season-participant.ts
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
* refactor(models): update model layer to use renamed schema exports
Updated all model files to use the renamed schema exports from Task 1:
- participants → seasonParticipants
- participantExpectedValues → seasonParticipantExpectedValues
- participantQualifyingTotals → seasonParticipantQualifyingTotals
- participantResults → seasonParticipantResults
- participantSurfaceElos → seasonParticipantSurfaceElos
- eventResults.participantId → eventResults.seasonParticipantId
- db.query relation accessors updated
- Relation field .participant → .seasonParticipant where applicable
- Import paths updated: ./participant → ./season-participant
Files updated (14 model files + 3 test files):
- draft-pick.ts
- draft-utils.ts
- event-result.ts
- group-stage-match.ts
- participant-result.ts
- qualifying-points.ts
- scoring-calculator.ts
- scoring-event.ts
- sports-season.ts
- surface-elo.ts
- team-score-events.ts
- cs2-major-stage.ts
- golf-skills.ts
- participant-expected-value.ts
- __tests__/sports-season.clone.test.ts
- __tests__/auto-pick.test.ts
- __tests__/executeAutoPick.timer.test.ts
Typecheck errors decreased: 779 → 499 (280 fewer)
All model file errors related to renamed schemas resolved.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
* refactor(routes): update route layer to use renamed schema exports
- Update model import from ~/models/participant to ~/models/season-participant
- Rename schema.participants to schema.seasonParticipants
- Rename schema.participantResults to schema.seasonParticipantResults
- Rename db.query.participants to db.query.seasonParticipants
- Update 9 route files and 1 test file
Affected files:
- admin.sports-seasons.$id.events.$eventId.bracket.server.ts
- admin.sports-seasons.$id.participants.tsx
- api/draft.force-manual-pick.ts
- api/draft.make-pick.ts
- api/draft.replace-pick.ts
- api/seasons.$seasonId.draft.ts
- leagues/$leagueId.draft-board.$seasonId.tsx
- leagues/$leagueId.sports-seasons.$sportsSeasonId.server.ts
- admin/__tests__/sports-seasons-participants.test.ts
Error count reduced from 499 to 453 (46 errors fixed).
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
* refactor(routes): update route files for schema rename
Update route imports from ~/models/participant to ~/models/season-participant
and fix references to .participant/.participantId on event results to use
.seasonParticipant/.seasonParticipantId after schema rename.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
* refactor(services): update simulators and services for renamed schema
Update all simulators, services, and server files to use renamed schema tables:
- participants → seasonParticipants
- participantExpectedValues → seasonParticipantExpectedValues
- participantResults → seasonParticipantResults
- eventResults.participantId → eventResults.seasonParticipantId
Files updated:
- 20 sport simulators (NBA, NHL, NFL, MLB, etc.)
- probability-updater.ts
- standings-sync/index.ts
- sports-data-sync.server.ts
- server/socket.ts
Typecheck errors reduced from 365 to 0.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
* migration: rename per-window tables to season_* prefix
* fix(tests): update mock query keys after participants table rename
Change mock db.query.participants to db.query.seasonParticipants in test
files to match the schema rename from commit 66145a9. This fixes
"Cannot read properties of undefined (reading 'findFirst'/'findMany')"
errors that occurred when production code queries db.query.seasonParticipants
but test mocks only defined the old participants key.
Files updated:
- app/services/simulations/__tests__/world-cup-simulator.test.ts
- app/routes/api/__tests__/draft.force-manual-pick.test.ts
- app/routes/api/__tests__/draft.force-manual-pick.timer-mode.test.ts
- app/routes/api/__tests__/draft.make-pick.timer-mode.test.ts
- server/__tests__/timer-autodraft.test.ts
- app/models/__tests__/team-score-events.test.ts
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
* fix(tests): update remaining mock paths and keys after schema rename
* fix(tests): final two mock stragglers after schema rename
- draft-pick.test.ts: assertion on db.query.participantQualifyingTotals
- process-match-result.test.ts: mock key participants → seasonParticipants
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
* chore: add post-phase1a baseline capture (temp, for diff verification)
* chore: capture pre-migration baselines
* chore: remove post-phase1a capture helper after verification
* schema: add canonical tournament & participant tables
Adds tournaments, participants (canonical), tournament_results, and
participant_surface_elos (canonical). Adds nullable tournament_id to
scoring_events and nullable participant_id to season_participants.
Phase 1b of canonical tournament layer migration.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
* feat(models): add canonical tournament, participant, result, surface-elo models
Adds CRUD modules for the canonical tables created in commit 775b905.
Each module mirrors existing app/models conventions (database() from
~/database/context, schema from ~/database/schema, mock-based tests).
Key implementation notes:
- participant.ts exports use "Canonical" prefix (CanonicalParticipant,
createCanonicalParticipant, etc.) to avoid collision with existing
season-participant.ts exports
- All four models include comprehensive unit tests following the
audit-log.test.ts pattern
- Tests use mocked db responses (no real database access)
- Upsert functions use onConflictDoUpdate for appropriate unique constraints
Part of Phase 1b of canonical tournament layer migration.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
* migration: create canonical tables, add nullable FKs
* scripts: add extractTournamentIdentity helper for backfill
Pure function that derives canonical (name, year) identity from a
scoring_events row, stripping trailing 4-digit years from the name or
falling back to eventDate. Used by the Phase 2 backfill to group
per-window events into canonical tournaments.
* scripts: add backfill orchestrator for canonical layer
Populates canonical tournaments, participants, tournament_results, and
participant_surface_elos from per-window data for qualifying-points
sports. Skips already-linked rows, is idempotent, and supports dry-run
mode.
Critical invariants enforced by the implementation:
- qualifying_points_awarded is never copied to tournament_results
- season_participant_qualifying_totals is never touched
- conflicting surface-Elo values between windows raise a loud error
(recorded in report.errors) rather than overwriting
* scripts: add backfill CLI with dry-run default
Wires backfill-canonical-layer.ts to a CLI entry point exposed as
`npm run backfill:canonical`. Defaults to --dry-run; requires --apply
to actually write. Supports --sport=<uuid> to limit to a single sport.
Exits 2 if the backfill reports errors (e.g., surface-Elo conflicts).
* fix(backfill-cli): wrap runBackfill in DatabaseContext.run
The orchestrator uses database() from ~/database/context, which requires
AsyncLocalStorage to be populated. Wrap the CLI invocation with
DatabaseContext.run(db, ...) using server/db's cached connection pool.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
* fix(backfill-cli): exit 0 on success so pg pool doesn't block
The cached postgres connection pool keeps the Node event loop open after
main() returns. Explicit process.exit(0) on success mirrors the pattern
in scripts/capture-baseline.ts.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
---------
Co-authored-by: Chris Parsons <chrisp@extrahop.com>
Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-01 20:13:18 -07:00
|
|
|
|
.select({ id: schema.seasonParticipants.id, name: schema.seasonParticipants.name })
|
|
|
|
|
|
.from(schema.seasonParticipants)
|
|
|
|
|
|
.where(eq(schema.seasonParticipants.sportsSeasonId, sportsSeasonId)),
|
Fix simulator brackets: NHL bracket-aware mode, MLB/NBA/WNBA sourceElo support (#358)
* Fix NHL bracket-aware simulation and MLB sourceElo support
NHL: NHLSimulator now checks for a populated playoff_game bracket before
falling back to season-projection mode. When a bracket exists (e.g. via
simple_16 template), it simulates the actual bracket structure, respecting
completed matches and using ELO+odds blending for the rest — matching the
pattern used by Snooker, NBA, and UCL simulators.
MLB: MLBSimulator now reads sourceElo from participantExpectedValues and
converts it to an effective RDif (via eloToRDif), overriding the hardcoded
TEAMS_DATA.rdif when present. This wires up the expected-wins admin form
(which saves sourceElo) to the simulation engine.
https://claude.ai/code/session_01139GJVg2gqgDjcUzqhnBNn
* Wire sourceElo from projected wins form into NBA, NHL, and WNBA simulators
All three simulators had entries in simulator-config.ts (enabling the projected
wins form in the admin UI) but silently ignored the sourceElo that was saved.
NBA: Both bracket-aware and season-projection modes now load sourceElo and
prefer it over hardcoded TEAMS_DATA elo. Added resolvedElo field to TeamEntry
so eloOfEntry() uses the correct value throughout.
NHL: Season-projection mode now fetches sourceElo + sourceOdds in a single
parallel query (removing the duplicate evRows query). Bracket-aware mode also
loads sourceElo alongside sourceOdds. Both modes use sourceElo > TEAMS_DATA > 1400.
WNBA: resolveElo() now accepts sourceElo as an optional highest-priority
argument. The evRows query fetches sourceElo alongside sourceOdds and passes
it through when building team entries.
https://claude.ai/code/session_01139GJVg2gqgDjcUzqhnBNn
* Add review fixes: NHL R1 validation, WNBA sourceElo tests, NHL bracket unit tests
- NHLSimulator.simulateBracket: remove unused nameById variable
- NHLSimulator.simulateBracket: validate all R1 participants before entering
Monte Carlo loop; throws with a clear message if any slot is missing
- wnba-simulator.test.ts: add 5 tests covering the sourceElo override parameter
on resolveElo() (overrides SRS, overrides futures, overrides fallback,
null/undefined fall through to normal priority chain)
- nhl-simulator.test.ts: add full bracket-aware integration suite (10 tests)
covering fallback to season-projection, structure validation errors, probability
distributions, fully-decided champion, and source tag
https://claude.ai/code/session_01139GJVg2gqgDjcUzqhnBNn
* Fix TS error: add resolvedElo to makeEntry in nhl-simulator.test.ts
TeamEntry requires resolvedElo after the simulator refactor.
https://claude.ai/code/session_01139GJVg2gqgDjcUzqhnBNn
* Fix lint: replace non-null assertions with null-safe alternatives in NHL test
https://claude.ai/code/session_01139GJVg2gqgDjcUzqhnBNn
---------
Co-authored-by: Claude <noreply@anthropic.com>
2026-04-30 08:33:57 -07:00
|
|
|
|
db
|
|
|
|
|
|
.select({
|
Canonical tournament layer: schema + backfill (1/2) (#365)
* refactor(schema): rename per-window tables to season_* prefix
Renames participants, participant_expected_values, participant_qualifying_totals,
participant_results, participant_surface_elos to season_* prefixed names.
Renames event_results.participant_id to season_participant_id.
Phase 1a of canonical tournament layer migration.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
* refactor: rename participant.ts model file to season-participant.ts
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
* refactor(models): update model layer to use renamed schema exports
Updated all model files to use the renamed schema exports from Task 1:
- participants → seasonParticipants
- participantExpectedValues → seasonParticipantExpectedValues
- participantQualifyingTotals → seasonParticipantQualifyingTotals
- participantResults → seasonParticipantResults
- participantSurfaceElos → seasonParticipantSurfaceElos
- eventResults.participantId → eventResults.seasonParticipantId
- db.query relation accessors updated
- Relation field .participant → .seasonParticipant where applicable
- Import paths updated: ./participant → ./season-participant
Files updated (14 model files + 3 test files):
- draft-pick.ts
- draft-utils.ts
- event-result.ts
- group-stage-match.ts
- participant-result.ts
- qualifying-points.ts
- scoring-calculator.ts
- scoring-event.ts
- sports-season.ts
- surface-elo.ts
- team-score-events.ts
- cs2-major-stage.ts
- golf-skills.ts
- participant-expected-value.ts
- __tests__/sports-season.clone.test.ts
- __tests__/auto-pick.test.ts
- __tests__/executeAutoPick.timer.test.ts
Typecheck errors decreased: 779 → 499 (280 fewer)
All model file errors related to renamed schemas resolved.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
* refactor(routes): update route layer to use renamed schema exports
- Update model import from ~/models/participant to ~/models/season-participant
- Rename schema.participants to schema.seasonParticipants
- Rename schema.participantResults to schema.seasonParticipantResults
- Rename db.query.participants to db.query.seasonParticipants
- Update 9 route files and 1 test file
Affected files:
- admin.sports-seasons.$id.events.$eventId.bracket.server.ts
- admin.sports-seasons.$id.participants.tsx
- api/draft.force-manual-pick.ts
- api/draft.make-pick.ts
- api/draft.replace-pick.ts
- api/seasons.$seasonId.draft.ts
- leagues/$leagueId.draft-board.$seasonId.tsx
- leagues/$leagueId.sports-seasons.$sportsSeasonId.server.ts
- admin/__tests__/sports-seasons-participants.test.ts
Error count reduced from 499 to 453 (46 errors fixed).
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
* refactor(routes): update route files for schema rename
Update route imports from ~/models/participant to ~/models/season-participant
and fix references to .participant/.participantId on event results to use
.seasonParticipant/.seasonParticipantId after schema rename.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
* refactor(services): update simulators and services for renamed schema
Update all simulators, services, and server files to use renamed schema tables:
- participants → seasonParticipants
- participantExpectedValues → seasonParticipantExpectedValues
- participantResults → seasonParticipantResults
- eventResults.participantId → eventResults.seasonParticipantId
Files updated:
- 20 sport simulators (NBA, NHL, NFL, MLB, etc.)
- probability-updater.ts
- standings-sync/index.ts
- sports-data-sync.server.ts
- server/socket.ts
Typecheck errors reduced from 365 to 0.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
* migration: rename per-window tables to season_* prefix
* fix(tests): update mock query keys after participants table rename
Change mock db.query.participants to db.query.seasonParticipants in test
files to match the schema rename from commit 66145a9. This fixes
"Cannot read properties of undefined (reading 'findFirst'/'findMany')"
errors that occurred when production code queries db.query.seasonParticipants
but test mocks only defined the old participants key.
Files updated:
- app/services/simulations/__tests__/world-cup-simulator.test.ts
- app/routes/api/__tests__/draft.force-manual-pick.test.ts
- app/routes/api/__tests__/draft.force-manual-pick.timer-mode.test.ts
- app/routes/api/__tests__/draft.make-pick.timer-mode.test.ts
- server/__tests__/timer-autodraft.test.ts
- app/models/__tests__/team-score-events.test.ts
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
* fix(tests): update remaining mock paths and keys after schema rename
* fix(tests): final two mock stragglers after schema rename
- draft-pick.test.ts: assertion on db.query.participantQualifyingTotals
- process-match-result.test.ts: mock key participants → seasonParticipants
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
* chore: add post-phase1a baseline capture (temp, for diff verification)
* chore: capture pre-migration baselines
* chore: remove post-phase1a capture helper after verification
* schema: add canonical tournament & participant tables
Adds tournaments, participants (canonical), tournament_results, and
participant_surface_elos (canonical). Adds nullable tournament_id to
scoring_events and nullable participant_id to season_participants.
Phase 1b of canonical tournament layer migration.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
* feat(models): add canonical tournament, participant, result, surface-elo models
Adds CRUD modules for the canonical tables created in commit 775b905.
Each module mirrors existing app/models conventions (database() from
~/database/context, schema from ~/database/schema, mock-based tests).
Key implementation notes:
- participant.ts exports use "Canonical" prefix (CanonicalParticipant,
createCanonicalParticipant, etc.) to avoid collision with existing
season-participant.ts exports
- All four models include comprehensive unit tests following the
audit-log.test.ts pattern
- Tests use mocked db responses (no real database access)
- Upsert functions use onConflictDoUpdate for appropriate unique constraints
Part of Phase 1b of canonical tournament layer migration.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
* migration: create canonical tables, add nullable FKs
* scripts: add extractTournamentIdentity helper for backfill
Pure function that derives canonical (name, year) identity from a
scoring_events row, stripping trailing 4-digit years from the name or
falling back to eventDate. Used by the Phase 2 backfill to group
per-window events into canonical tournaments.
* scripts: add backfill orchestrator for canonical layer
Populates canonical tournaments, participants, tournament_results, and
participant_surface_elos from per-window data for qualifying-points
sports. Skips already-linked rows, is idempotent, and supports dry-run
mode.
Critical invariants enforced by the implementation:
- qualifying_points_awarded is never copied to tournament_results
- season_participant_qualifying_totals is never touched
- conflicting surface-Elo values between windows raise a loud error
(recorded in report.errors) rather than overwriting
* scripts: add backfill CLI with dry-run default
Wires backfill-canonical-layer.ts to a CLI entry point exposed as
`npm run backfill:canonical`. Defaults to --dry-run; requires --apply
to actually write. Supports --sport=<uuid> to limit to a single sport.
Exits 2 if the backfill reports errors (e.g., surface-Elo conflicts).
* fix(backfill-cli): wrap runBackfill in DatabaseContext.run
The orchestrator uses database() from ~/database/context, which requires
AsyncLocalStorage to be populated. Wrap the CLI invocation with
DatabaseContext.run(db, ...) using server/db's cached connection pool.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
* fix(backfill-cli): exit 0 on success so pg pool doesn't block
The cached postgres connection pool keeps the Node event loop open after
main() returns. Explicit process.exit(0) on success mirrors the pattern
in scripts/capture-baseline.ts.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
---------
Co-authored-by: Chris Parsons <chrisp@extrahop.com>
Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-01 20:13:18 -07:00
|
|
|
|
participantId: schema.seasonParticipantExpectedValues.participantId,
|
|
|
|
|
|
sourceElo: schema.seasonParticipantExpectedValues.sourceElo,
|
Fix simulator brackets: NHL bracket-aware mode, MLB/NBA/WNBA sourceElo support (#358)
* Fix NHL bracket-aware simulation and MLB sourceElo support
NHL: NHLSimulator now checks for a populated playoff_game bracket before
falling back to season-projection mode. When a bracket exists (e.g. via
simple_16 template), it simulates the actual bracket structure, respecting
completed matches and using ELO+odds blending for the rest — matching the
pattern used by Snooker, NBA, and UCL simulators.
MLB: MLBSimulator now reads sourceElo from participantExpectedValues and
converts it to an effective RDif (via eloToRDif), overriding the hardcoded
TEAMS_DATA.rdif when present. This wires up the expected-wins admin form
(which saves sourceElo) to the simulation engine.
https://claude.ai/code/session_01139GJVg2gqgDjcUzqhnBNn
* Wire sourceElo from projected wins form into NBA, NHL, and WNBA simulators
All three simulators had entries in simulator-config.ts (enabling the projected
wins form in the admin UI) but silently ignored the sourceElo that was saved.
NBA: Both bracket-aware and season-projection modes now load sourceElo and
prefer it over hardcoded TEAMS_DATA elo. Added resolvedElo field to TeamEntry
so eloOfEntry() uses the correct value throughout.
NHL: Season-projection mode now fetches sourceElo + sourceOdds in a single
parallel query (removing the duplicate evRows query). Bracket-aware mode also
loads sourceElo alongside sourceOdds. Both modes use sourceElo > TEAMS_DATA > 1400.
WNBA: resolveElo() now accepts sourceElo as an optional highest-priority
argument. The evRows query fetches sourceElo alongside sourceOdds and passes
it through when building team entries.
https://claude.ai/code/session_01139GJVg2gqgDjcUzqhnBNn
* Add review fixes: NHL R1 validation, WNBA sourceElo tests, NHL bracket unit tests
- NHLSimulator.simulateBracket: remove unused nameById variable
- NHLSimulator.simulateBracket: validate all R1 participants before entering
Monte Carlo loop; throws with a clear message if any slot is missing
- wnba-simulator.test.ts: add 5 tests covering the sourceElo override parameter
on resolveElo() (overrides SRS, overrides futures, overrides fallback,
null/undefined fall through to normal priority chain)
- nhl-simulator.test.ts: add full bracket-aware integration suite (10 tests)
covering fallback to season-projection, structure validation errors, probability
distributions, fully-decided champion, and source tag
https://claude.ai/code/session_01139GJVg2gqgDjcUzqhnBNn
* Fix TS error: add resolvedElo to makeEntry in nhl-simulator.test.ts
TeamEntry requires resolvedElo after the simulator refactor.
https://claude.ai/code/session_01139GJVg2gqgDjcUzqhnBNn
* Fix lint: replace non-null assertions with null-safe alternatives in NHL test
https://claude.ai/code/session_01139GJVg2gqgDjcUzqhnBNn
---------
Co-authored-by: Claude <noreply@anthropic.com>
2026-04-30 08:33:57 -07:00
|
|
|
|
})
|
Canonical tournament layer: schema + backfill (1/2) (#365)
* refactor(schema): rename per-window tables to season_* prefix
Renames participants, participant_expected_values, participant_qualifying_totals,
participant_results, participant_surface_elos to season_* prefixed names.
Renames event_results.participant_id to season_participant_id.
Phase 1a of canonical tournament layer migration.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
* refactor: rename participant.ts model file to season-participant.ts
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
* refactor(models): update model layer to use renamed schema exports
Updated all model files to use the renamed schema exports from Task 1:
- participants → seasonParticipants
- participantExpectedValues → seasonParticipantExpectedValues
- participantQualifyingTotals → seasonParticipantQualifyingTotals
- participantResults → seasonParticipantResults
- participantSurfaceElos → seasonParticipantSurfaceElos
- eventResults.participantId → eventResults.seasonParticipantId
- db.query relation accessors updated
- Relation field .participant → .seasonParticipant where applicable
- Import paths updated: ./participant → ./season-participant
Files updated (14 model files + 3 test files):
- draft-pick.ts
- draft-utils.ts
- event-result.ts
- group-stage-match.ts
- participant-result.ts
- qualifying-points.ts
- scoring-calculator.ts
- scoring-event.ts
- sports-season.ts
- surface-elo.ts
- team-score-events.ts
- cs2-major-stage.ts
- golf-skills.ts
- participant-expected-value.ts
- __tests__/sports-season.clone.test.ts
- __tests__/auto-pick.test.ts
- __tests__/executeAutoPick.timer.test.ts
Typecheck errors decreased: 779 → 499 (280 fewer)
All model file errors related to renamed schemas resolved.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
* refactor(routes): update route layer to use renamed schema exports
- Update model import from ~/models/participant to ~/models/season-participant
- Rename schema.participants to schema.seasonParticipants
- Rename schema.participantResults to schema.seasonParticipantResults
- Rename db.query.participants to db.query.seasonParticipants
- Update 9 route files and 1 test file
Affected files:
- admin.sports-seasons.$id.events.$eventId.bracket.server.ts
- admin.sports-seasons.$id.participants.tsx
- api/draft.force-manual-pick.ts
- api/draft.make-pick.ts
- api/draft.replace-pick.ts
- api/seasons.$seasonId.draft.ts
- leagues/$leagueId.draft-board.$seasonId.tsx
- leagues/$leagueId.sports-seasons.$sportsSeasonId.server.ts
- admin/__tests__/sports-seasons-participants.test.ts
Error count reduced from 499 to 453 (46 errors fixed).
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
* refactor(routes): update route files for schema rename
Update route imports from ~/models/participant to ~/models/season-participant
and fix references to .participant/.participantId on event results to use
.seasonParticipant/.seasonParticipantId after schema rename.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
* refactor(services): update simulators and services for renamed schema
Update all simulators, services, and server files to use renamed schema tables:
- participants → seasonParticipants
- participantExpectedValues → seasonParticipantExpectedValues
- participantResults → seasonParticipantResults
- eventResults.participantId → eventResults.seasonParticipantId
Files updated:
- 20 sport simulators (NBA, NHL, NFL, MLB, etc.)
- probability-updater.ts
- standings-sync/index.ts
- sports-data-sync.server.ts
- server/socket.ts
Typecheck errors reduced from 365 to 0.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
* migration: rename per-window tables to season_* prefix
* fix(tests): update mock query keys after participants table rename
Change mock db.query.participants to db.query.seasonParticipants in test
files to match the schema rename from commit 66145a9. This fixes
"Cannot read properties of undefined (reading 'findFirst'/'findMany')"
errors that occurred when production code queries db.query.seasonParticipants
but test mocks only defined the old participants key.
Files updated:
- app/services/simulations/__tests__/world-cup-simulator.test.ts
- app/routes/api/__tests__/draft.force-manual-pick.test.ts
- app/routes/api/__tests__/draft.force-manual-pick.timer-mode.test.ts
- app/routes/api/__tests__/draft.make-pick.timer-mode.test.ts
- server/__tests__/timer-autodraft.test.ts
- app/models/__tests__/team-score-events.test.ts
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
* fix(tests): update remaining mock paths and keys after schema rename
* fix(tests): final two mock stragglers after schema rename
- draft-pick.test.ts: assertion on db.query.participantQualifyingTotals
- process-match-result.test.ts: mock key participants → seasonParticipants
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
* chore: add post-phase1a baseline capture (temp, for diff verification)
* chore: capture pre-migration baselines
* chore: remove post-phase1a capture helper after verification
* schema: add canonical tournament & participant tables
Adds tournaments, participants (canonical), tournament_results, and
participant_surface_elos (canonical). Adds nullable tournament_id to
scoring_events and nullable participant_id to season_participants.
Phase 1b of canonical tournament layer migration.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
* feat(models): add canonical tournament, participant, result, surface-elo models
Adds CRUD modules for the canonical tables created in commit 775b905.
Each module mirrors existing app/models conventions (database() from
~/database/context, schema from ~/database/schema, mock-based tests).
Key implementation notes:
- participant.ts exports use "Canonical" prefix (CanonicalParticipant,
createCanonicalParticipant, etc.) to avoid collision with existing
season-participant.ts exports
- All four models include comprehensive unit tests following the
audit-log.test.ts pattern
- Tests use mocked db responses (no real database access)
- Upsert functions use onConflictDoUpdate for appropriate unique constraints
Part of Phase 1b of canonical tournament layer migration.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
* migration: create canonical tables, add nullable FKs
* scripts: add extractTournamentIdentity helper for backfill
Pure function that derives canonical (name, year) identity from a
scoring_events row, stripping trailing 4-digit years from the name or
falling back to eventDate. Used by the Phase 2 backfill to group
per-window events into canonical tournaments.
* scripts: add backfill orchestrator for canonical layer
Populates canonical tournaments, participants, tournament_results, and
participant_surface_elos from per-window data for qualifying-points
sports. Skips already-linked rows, is idempotent, and supports dry-run
mode.
Critical invariants enforced by the implementation:
- qualifying_points_awarded is never copied to tournament_results
- season_participant_qualifying_totals is never touched
- conflicting surface-Elo values between windows raise a loud error
(recorded in report.errors) rather than overwriting
* scripts: add backfill CLI with dry-run default
Wires backfill-canonical-layer.ts to a CLI entry point exposed as
`npm run backfill:canonical`. Defaults to --dry-run; requires --apply
to actually write. Supports --sport=<uuid> to limit to a single sport.
Exits 2 if the backfill reports errors (e.g., surface-Elo conflicts).
* fix(backfill-cli): wrap runBackfill in DatabaseContext.run
The orchestrator uses database() from ~/database/context, which requires
AsyncLocalStorage to be populated. Wrap the CLI invocation with
DatabaseContext.run(db, ...) using server/db's cached connection pool.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
* fix(backfill-cli): exit 0 on success so pg pool doesn't block
The cached postgres connection pool keeps the Node event loop open after
main() returns. Explicit process.exit(0) on success mirrors the pattern
in scripts/capture-baseline.ts.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
---------
Co-authored-by: Chris Parsons <chrisp@extrahop.com>
Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-01 20:13:18 -07:00
|
|
|
|
.from(schema.seasonParticipantExpectedValues)
|
|
|
|
|
|
.where(eq(schema.seasonParticipantExpectedValues.sportsSeasonId, sportsSeasonId)),
|
Fix simulator brackets: NHL bracket-aware mode, MLB/NBA/WNBA sourceElo support (#358)
* Fix NHL bracket-aware simulation and MLB sourceElo support
NHL: NHLSimulator now checks for a populated playoff_game bracket before
falling back to season-projection mode. When a bracket exists (e.g. via
simple_16 template), it simulates the actual bracket structure, respecting
completed matches and using ELO+odds blending for the rest — matching the
pattern used by Snooker, NBA, and UCL simulators.
MLB: MLBSimulator now reads sourceElo from participantExpectedValues and
converts it to an effective RDif (via eloToRDif), overriding the hardcoded
TEAMS_DATA.rdif when present. This wires up the expected-wins admin form
(which saves sourceElo) to the simulation engine.
https://claude.ai/code/session_01139GJVg2gqgDjcUzqhnBNn
* Wire sourceElo from projected wins form into NBA, NHL, and WNBA simulators
All three simulators had entries in simulator-config.ts (enabling the projected
wins form in the admin UI) but silently ignored the sourceElo that was saved.
NBA: Both bracket-aware and season-projection modes now load sourceElo and
prefer it over hardcoded TEAMS_DATA elo. Added resolvedElo field to TeamEntry
so eloOfEntry() uses the correct value throughout.
NHL: Season-projection mode now fetches sourceElo + sourceOdds in a single
parallel query (removing the duplicate evRows query). Bracket-aware mode also
loads sourceElo alongside sourceOdds. Both modes use sourceElo > TEAMS_DATA > 1400.
WNBA: resolveElo() now accepts sourceElo as an optional highest-priority
argument. The evRows query fetches sourceElo alongside sourceOdds and passes
it through when building team entries.
https://claude.ai/code/session_01139GJVg2gqgDjcUzqhnBNn
* Add review fixes: NHL R1 validation, WNBA sourceElo tests, NHL bracket unit tests
- NHLSimulator.simulateBracket: remove unused nameById variable
- NHLSimulator.simulateBracket: validate all R1 participants before entering
Monte Carlo loop; throws with a clear message if any slot is missing
- wnba-simulator.test.ts: add 5 tests covering the sourceElo override parameter
on resolveElo() (overrides SRS, overrides futures, overrides fallback,
null/undefined fall through to normal priority chain)
- nhl-simulator.test.ts: add full bracket-aware integration suite (10 tests)
covering fallback to season-projection, structure validation errors, probability
distributions, fully-decided champion, and source tag
https://claude.ai/code/session_01139GJVg2gqgDjcUzqhnBNn
* Fix TS error: add resolvedElo to makeEntry in nhl-simulator.test.ts
TeamEntry requires resolvedElo after the simulator refactor.
https://claude.ai/code/session_01139GJVg2gqgDjcUzqhnBNn
* Fix lint: replace non-null assertions with null-safe alternatives in NHL test
https://claude.ai/code/session_01139GJVg2gqgDjcUzqhnBNn
---------
Co-authored-by: Claude <noreply@anthropic.com>
2026-04-30 08:33:57 -07:00
|
|
|
|
]);
|
2026-04-13 00:51:53 -04:00
|
|
|
|
|
|
|
|
|
|
const nameById = new Map(participantRows.map((r) => [r.id, r.name]));
|
|
|
|
|
|
|
Fix simulator brackets: NHL bracket-aware mode, MLB/NBA/WNBA sourceElo support (#358)
* Fix NHL bracket-aware simulation and MLB sourceElo support
NHL: NHLSimulator now checks for a populated playoff_game bracket before
falling back to season-projection mode. When a bracket exists (e.g. via
simple_16 template), it simulates the actual bracket structure, respecting
completed matches and using ELO+odds blending for the rest — matching the
pattern used by Snooker, NBA, and UCL simulators.
MLB: MLBSimulator now reads sourceElo from participantExpectedValues and
converts it to an effective RDif (via eloToRDif), overriding the hardcoded
TEAMS_DATA.rdif when present. This wires up the expected-wins admin form
(which saves sourceElo) to the simulation engine.
https://claude.ai/code/session_01139GJVg2gqgDjcUzqhnBNn
* Wire sourceElo from projected wins form into NBA, NHL, and WNBA simulators
All three simulators had entries in simulator-config.ts (enabling the projected
wins form in the admin UI) but silently ignored the sourceElo that was saved.
NBA: Both bracket-aware and season-projection modes now load sourceElo and
prefer it over hardcoded TEAMS_DATA elo. Added resolvedElo field to TeamEntry
so eloOfEntry() uses the correct value throughout.
NHL: Season-projection mode now fetches sourceElo + sourceOdds in a single
parallel query (removing the duplicate evRows query). Bracket-aware mode also
loads sourceElo alongside sourceOdds. Both modes use sourceElo > TEAMS_DATA > 1400.
WNBA: resolveElo() now accepts sourceElo as an optional highest-priority
argument. The evRows query fetches sourceElo alongside sourceOdds and passes
it through when building team entries.
https://claude.ai/code/session_01139GJVg2gqgDjcUzqhnBNn
* Add review fixes: NHL R1 validation, WNBA sourceElo tests, NHL bracket unit tests
- NHLSimulator.simulateBracket: remove unused nameById variable
- NHLSimulator.simulateBracket: validate all R1 participants before entering
Monte Carlo loop; throws with a clear message if any slot is missing
- wnba-simulator.test.ts: add 5 tests covering the sourceElo override parameter
on resolveElo() (overrides SRS, overrides futures, overrides fallback,
null/undefined fall through to normal priority chain)
- nhl-simulator.test.ts: add full bracket-aware integration suite (10 tests)
covering fallback to season-projection, structure validation errors, probability
distributions, fully-decided champion, and source tag
https://claude.ai/code/session_01139GJVg2gqgDjcUzqhnBNn
* Fix TS error: add resolvedElo to makeEntry in nhl-simulator.test.ts
TeamEntry requires resolvedElo after the simulator refactor.
https://claude.ai/code/session_01139GJVg2gqgDjcUzqhnBNn
* Fix lint: replace non-null assertions with null-safe alternatives in NHL test
https://claude.ai/code/session_01139GJVg2gqgDjcUzqhnBNn
---------
Co-authored-by: Claude <noreply@anthropic.com>
2026-04-30 08:33:57 -07:00
|
|
|
|
const dbEloMap = new Map<string, number>();
|
|
|
|
|
|
for (const row of evRows) {
|
|
|
|
|
|
if (row.sourceElo !== null && row.sourceElo !== undefined) {
|
|
|
|
|
|
dbEloMap.set(row.participantId, row.sourceElo);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Build Elo map: sourceElo > TEAMS_DATA > fallback 1400.
|
2026-04-13 00:51:53 -04:00
|
|
|
|
const eloMap = new Map<string, number>();
|
|
|
|
|
|
for (const id of participantIds) {
|
|
|
|
|
|
const name = nameById.get(id) ?? "";
|
Fix simulator brackets: NHL bracket-aware mode, MLB/NBA/WNBA sourceElo support (#358)
* Fix NHL bracket-aware simulation and MLB sourceElo support
NHL: NHLSimulator now checks for a populated playoff_game bracket before
falling back to season-projection mode. When a bracket exists (e.g. via
simple_16 template), it simulates the actual bracket structure, respecting
completed matches and using ELO+odds blending for the rest — matching the
pattern used by Snooker, NBA, and UCL simulators.
MLB: MLBSimulator now reads sourceElo from participantExpectedValues and
converts it to an effective RDif (via eloToRDif), overriding the hardcoded
TEAMS_DATA.rdif when present. This wires up the expected-wins admin form
(which saves sourceElo) to the simulation engine.
https://claude.ai/code/session_01139GJVg2gqgDjcUzqhnBNn
* Wire sourceElo from projected wins form into NBA, NHL, and WNBA simulators
All three simulators had entries in simulator-config.ts (enabling the projected
wins form in the admin UI) but silently ignored the sourceElo that was saved.
NBA: Both bracket-aware and season-projection modes now load sourceElo and
prefer it over hardcoded TEAMS_DATA elo. Added resolvedElo field to TeamEntry
so eloOfEntry() uses the correct value throughout.
NHL: Season-projection mode now fetches sourceElo + sourceOdds in a single
parallel query (removing the duplicate evRows query). Bracket-aware mode also
loads sourceElo alongside sourceOdds. Both modes use sourceElo > TEAMS_DATA > 1400.
WNBA: resolveElo() now accepts sourceElo as an optional highest-priority
argument. The evRows query fetches sourceElo alongside sourceOdds and passes
it through when building team entries.
https://claude.ai/code/session_01139GJVg2gqgDjcUzqhnBNn
* Add review fixes: NHL R1 validation, WNBA sourceElo tests, NHL bracket unit tests
- NHLSimulator.simulateBracket: remove unused nameById variable
- NHLSimulator.simulateBracket: validate all R1 participants before entering
Monte Carlo loop; throws with a clear message if any slot is missing
- wnba-simulator.test.ts: add 5 tests covering the sourceElo override parameter
on resolveElo() (overrides SRS, overrides futures, overrides fallback,
null/undefined fall through to normal priority chain)
- nhl-simulator.test.ts: add full bracket-aware integration suite (10 tests)
covering fallback to season-projection, structure validation errors, probability
distributions, fully-decided champion, and source tag
https://claude.ai/code/session_01139GJVg2gqgDjcUzqhnBNn
* Fix TS error: add resolvedElo to makeEntry in nhl-simulator.test.ts
TeamEntry requires resolvedElo after the simulator refactor.
https://claude.ai/code/session_01139GJVg2gqgDjcUzqhnBNn
* Fix lint: replace non-null assertions with null-safe alternatives in NHL test
https://claude.ai/code/session_01139GJVg2gqgDjcUzqhnBNn
---------
Co-authored-by: Claude <noreply@anthropic.com>
2026-04-30 08:33:57 -07:00
|
|
|
|
eloMap.set(id, dbEloMap.get(id) ?? getTeamData(name)?.elo ?? 1400);
|
2026-04-13 00:51:53 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Build per-round lookup maps for O(1) access in the hot loop.
|
|
|
|
|
|
const pir1ByNum = new Map(pir1.map((m) => [m.matchNumber, m]));
|
|
|
|
|
|
const pir2ByNum = new Map(pir2.map((m) => [m.matchNumber, m]));
|
|
|
|
|
|
const frByNum = new Map(fr.map((m) => [m.matchNumber, m]));
|
|
|
|
|
|
const csByNum = new Map(cs.map((m) => [m.matchNumber, m]));
|
|
|
|
|
|
const cfByNum = new Map(cf.map((m) => [m.matchNumber, m]));
|
|
|
|
|
|
const finalMatch = finals[0];
|
|
|
|
|
|
|
|
|
|
|
|
// Integer placement counts — avoids fractional accumulation error.
|
|
|
|
|
|
const championCounts = new Map(participantIds.map((id) => [id, 0]));
|
|
|
|
|
|
const finalistCounts = new Map(participantIds.map((id) => [id, 0]));
|
|
|
|
|
|
const cfLoserCounts = new Map(participantIds.map((id) => [id, 0]));
|
|
|
|
|
|
const csLoserCounts = new Map(participantIds.map((id) => [id, 0]));
|
|
|
|
|
|
|
|
|
|
|
|
// ── Monte Carlo loop ──────────────────────────────────────────────────────
|
2026-06-30 23:24:48 +00:00
|
|
|
|
for (let s = 0; s < numSimulations; s++) {
|
2026-04-13 00:51:53 -04:00
|
|
|
|
|
|
|
|
|
|
// ── Play-In Round 1 (4 single games) ─────────────────────────────────
|
|
|
|
|
|
// M1: East 7 vs 8 → winner = E7 seed; loser enters PIR2 as p1
|
|
|
|
|
|
// M2: East 9 vs 10 → winner enters PIR2 as p2; loser eliminated
|
|
|
|
|
|
// M3: West 7 vs 8 → winner = W7 seed; loser enters PIR2 as p1
|
|
|
|
|
|
// M4: West 9 vs 10 → winner enters PIR2 as p2; loser eliminated
|
|
|
|
|
|
const pir1M1 = resolveGame(eloMap, pir1ByNum.get(1));
|
|
|
|
|
|
const pir1M2 = resolveGame(eloMap, pir1ByNum.get(2));
|
|
|
|
|
|
const pir1M3 = resolveGame(eloMap, pir1ByNum.get(3));
|
|
|
|
|
|
const pir1M4 = resolveGame(eloMap, pir1ByNum.get(4));
|
|
|
|
|
|
|
|
|
|
|
|
const e7Seed = pir1M1.winner;
|
|
|
|
|
|
const e8Cand = pir1M1.loser; // plays PIR2 M1 p1
|
|
|
|
|
|
const e9e10W = pir1M2.winner; // plays PIR2 M1 p2
|
|
|
|
|
|
const w7Seed = pir1M3.winner;
|
|
|
|
|
|
const w8Cand = pir1M3.loser; // plays PIR2 M2 p1
|
|
|
|
|
|
const w9w10W = pir1M4.winner; // plays PIR2 M2 p2
|
|
|
|
|
|
|
|
|
|
|
|
// ── Play-In Round 2 (2 single games) ─────────────────────────────────
|
|
|
|
|
|
// M1: E8 candidate vs E9/10 winner → winner = E8 seed
|
|
|
|
|
|
// M2: W8 candidate vs W9/10 winner → winner = W8 seed
|
|
|
|
|
|
// Use DB participant slots when already filled (after PIR1 locked in),
|
|
|
|
|
|
// otherwise fall back to simulated values from above.
|
|
|
|
|
|
const pir2M1 = resolveGame(eloMap, pir2ByNum.get(1), e8Cand, e9e10W);
|
|
|
|
|
|
const pir2M2 = resolveGame(eloMap, pir2ByNum.get(2), w8Cand, w9w10W);
|
|
|
|
|
|
|
|
|
|
|
|
const e8Seed = pir2M1.winner;
|
|
|
|
|
|
const w8Seed = pir2M2.winner;
|
|
|
|
|
|
|
|
|
|
|
|
// ── First Round (8 best-of-7 series) ─────────────────────────────────
|
|
|
|
|
|
// Bracket order (matches correct Conference Semis pairings):
|
|
|
|
|
|
// M1: E1 vs E8 M2: E4 vs E5 M3: E2 vs E7 M4: E3 vs E6
|
|
|
|
|
|
// M5: W1 vs W8 M6: W4 vs W5 M7: W2 vs W7 M8: W3 vs W6
|
|
|
|
|
|
//
|
|
|
|
|
|
// Slots that stay null until play-in resolves use simmed seeds as fallback.
|
|
|
|
|
|
// Once the play-in result is recorded, the DB slot is filled, so participant
|
|
|
|
|
|
// coalescence (DB ?? simmed) always produces the correct team.
|
|
|
|
|
|
const frM1 = resolveSeries(eloMap, frByNum.get(1), undefined, e8Seed); // E1(DB) vs E8
|
|
|
|
|
|
const frM2 = resolveSeries(eloMap, frByNum.get(2)); // E4(DB) vs E5(DB)
|
2026-04-13 01:25:32 -04:00
|
|
|
|
const frM3 = resolveSeries(eloMap, frByNum.get(3), undefined, e7Seed); // E2(DB) vs E7
|
2026-04-13 00:51:53 -04:00
|
|
|
|
const frM4 = resolveSeries(eloMap, frByNum.get(4)); // E3(DB) vs E6(DB)
|
|
|
|
|
|
const frM5 = resolveSeries(eloMap, frByNum.get(5), undefined, w8Seed); // W1(DB) vs W8
|
|
|
|
|
|
const frM6 = resolveSeries(eloMap, frByNum.get(6)); // W4(DB) vs W5(DB)
|
2026-04-13 01:25:32 -04:00
|
|
|
|
const frM7 = resolveSeries(eloMap, frByNum.get(7), undefined, w7Seed); // W2(DB) vs W7
|
2026-04-13 00:51:53 -04:00
|
|
|
|
const frM8 = resolveSeries(eloMap, frByNum.get(8)); // W3(DB) vs W6(DB)
|
|
|
|
|
|
|
|
|
|
|
|
// ── Conference Semifinals (4 best-of-7 series) ────────────────────────
|
|
|
|
|
|
// Standard ceil bracket path:
|
|
|
|
|
|
// CS M1: FR M1/M2 winners (East upper: E1/8 vs E4/5)
|
|
|
|
|
|
// CS M2: FR M3/M4 winners (East lower: E2/7 vs E3/6)
|
|
|
|
|
|
// CS M3: FR M5/M6 winners (West upper: W1/8 vs W4/5)
|
|
|
|
|
|
// CS M4: FR M7/M8 winners (West lower: W2/7 vs W3/6)
|
|
|
|
|
|
const csM1 = resolveSeries(eloMap, csByNum.get(1), frM1.winner, frM2.winner);
|
|
|
|
|
|
const csM2 = resolveSeries(eloMap, csByNum.get(2), frM3.winner, frM4.winner);
|
|
|
|
|
|
const csM3 = resolveSeries(eloMap, csByNum.get(3), frM5.winner, frM6.winner);
|
|
|
|
|
|
const csM4 = resolveSeries(eloMap, csByNum.get(4), frM7.winner, frM8.winner);
|
|
|
|
|
|
|
|
|
|
|
|
csLoserCounts.set(csM1.loser, (csLoserCounts.get(csM1.loser) ?? 0) + 1);
|
|
|
|
|
|
csLoserCounts.set(csM2.loser, (csLoserCounts.get(csM2.loser) ?? 0) + 1);
|
|
|
|
|
|
csLoserCounts.set(csM3.loser, (csLoserCounts.get(csM3.loser) ?? 0) + 1);
|
|
|
|
|
|
csLoserCounts.set(csM4.loser, (csLoserCounts.get(csM4.loser) ?? 0) + 1);
|
|
|
|
|
|
|
|
|
|
|
|
// ── Conference Finals (2 best-of-7 series) ────────────────────────────
|
|
|
|
|
|
// CF M1: East champion (CS M1/M2 winners)
|
|
|
|
|
|
// CF M2: West champion (CS M3/M4 winners)
|
|
|
|
|
|
const cfM1 = resolveSeries(eloMap, cfByNum.get(1), csM1.winner, csM2.winner);
|
|
|
|
|
|
const cfM2 = resolveSeries(eloMap, cfByNum.get(2), csM3.winner, csM4.winner);
|
|
|
|
|
|
|
|
|
|
|
|
cfLoserCounts.set(cfM1.loser, (cfLoserCounts.get(cfM1.loser) ?? 0) + 1);
|
|
|
|
|
|
cfLoserCounts.set(cfM2.loser, (cfLoserCounts.get(cfM2.loser) ?? 0) + 1);
|
|
|
|
|
|
|
|
|
|
|
|
// ── NBA Finals ────────────────────────────────────────────────────────
|
|
|
|
|
|
const nbaFinals = resolveSeries(eloMap, finalMatch, cfM1.winner, cfM2.winner);
|
|
|
|
|
|
|
|
|
|
|
|
championCounts.set(nbaFinals.winner, (championCounts.get(nbaFinals.winner) ?? 0) + 1);
|
|
|
|
|
|
finalistCounts.set(nbaFinals.loser, (finalistCounts.get(nbaFinals.loser) ?? 0) + 1);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ── Convert counts to probability distributions ───────────────────────────
|
|
|
|
|
|
// Exact denominators guarantee column sums of 1.0 by construction:
|
|
|
|
|
|
// probFirst/Second → N total (1 per sim)
|
|
|
|
|
|
// probThird/Fourth → cfLoserCounts / (2×N) — 2 CF losers per sim
|
|
|
|
|
|
// probFifth–Eighth → csLoserCounts / (4×N) — 4 CS losers per sim
|
2026-06-30 23:24:48 +00:00
|
|
|
|
const N = numSimulations;
|
2026-04-13 00:51:53 -04:00
|
|
|
|
const results: SimulationResult[] = participantIds.map((participantId) => {
|
|
|
|
|
|
const c = championCounts.get(participantId) ?? 0;
|
|
|
|
|
|
const f = finalistCounts.get(participantId) ?? 0;
|
|
|
|
|
|
const cfCount = cfLoserCounts.get(participantId) ?? 0;
|
|
|
|
|
|
const csCount = csLoserCounts.get(participantId) ?? 0;
|
|
|
|
|
|
return {
|
|
|
|
|
|
participantId,
|
|
|
|
|
|
probabilities: {
|
|
|
|
|
|
probFirst: c / N,
|
|
|
|
|
|
probSecond: f / N,
|
|
|
|
|
|
probThird: cfCount / (2 * N),
|
|
|
|
|
|
probFourth: cfCount / (2 * N),
|
|
|
|
|
|
probFifth: csCount / (4 * N),
|
|
|
|
|
|
probSixth: csCount / (4 * N),
|
|
|
|
|
|
probSeventh: csCount / (4 * N),
|
|
|
|
|
|
probEighth: csCount / (4 * N),
|
|
|
|
|
|
},
|
|
|
|
|
|
source: "nba_bracket_monte_carlo",
|
|
|
|
|
|
};
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
normalizeColumns(results);
|
|
|
|
|
|
return results;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ── Mode 2: Season Projection ──────────────────────────────────────────────
|
|
|
|
|
|
|
2026-06-30 23:24:48 +00:00
|
|
|
|
private async simulateSeasonProjection(sportsSeasonId: string, config: Record<string, unknown> = {}): Promise<SimulationResult[]> {
|
2026-04-13 00:51:53 -04:00
|
|
|
|
const db = database();
|
2026-06-30 23:24:48 +00:00
|
|
|
|
const parityFactor = positiveConfigNumber(config, "parityFactor", DEFAULT_PARITY_FACTOR);
|
|
|
|
|
|
const numSimulations = Math.round(positiveConfigNumber(config, "iterations", DEFAULT_NUM_SIMULATIONS));
|
|
|
|
|
|
const seasonGames = Math.round(positiveConfigNumber(config, "seasonGames", DEFAULT_REGULAR_SEASON_GAMES));
|
2026-04-13 00:51:53 -04:00
|
|
|
|
|
Fix simulator brackets: NHL bracket-aware mode, MLB/NBA/WNBA sourceElo support (#358)
* Fix NHL bracket-aware simulation and MLB sourceElo support
NHL: NHLSimulator now checks for a populated playoff_game bracket before
falling back to season-projection mode. When a bracket exists (e.g. via
simple_16 template), it simulates the actual bracket structure, respecting
completed matches and using ELO+odds blending for the rest — matching the
pattern used by Snooker, NBA, and UCL simulators.
MLB: MLBSimulator now reads sourceElo from participantExpectedValues and
converts it to an effective RDif (via eloToRDif), overriding the hardcoded
TEAMS_DATA.rdif when present. This wires up the expected-wins admin form
(which saves sourceElo) to the simulation engine.
https://claude.ai/code/session_01139GJVg2gqgDjcUzqhnBNn
* Wire sourceElo from projected wins form into NBA, NHL, and WNBA simulators
All three simulators had entries in simulator-config.ts (enabling the projected
wins form in the admin UI) but silently ignored the sourceElo that was saved.
NBA: Both bracket-aware and season-projection modes now load sourceElo and
prefer it over hardcoded TEAMS_DATA elo. Added resolvedElo field to TeamEntry
so eloOfEntry() uses the correct value throughout.
NHL: Season-projection mode now fetches sourceElo + sourceOdds in a single
parallel query (removing the duplicate evRows query). Bracket-aware mode also
loads sourceElo alongside sourceOdds. Both modes use sourceElo > TEAMS_DATA > 1400.
WNBA: resolveElo() now accepts sourceElo as an optional highest-priority
argument. The evRows query fetches sourceElo alongside sourceOdds and passes
it through when building team entries.
https://claude.ai/code/session_01139GJVg2gqgDjcUzqhnBNn
* Add review fixes: NHL R1 validation, WNBA sourceElo tests, NHL bracket unit tests
- NHLSimulator.simulateBracket: remove unused nameById variable
- NHLSimulator.simulateBracket: validate all R1 participants before entering
Monte Carlo loop; throws with a clear message if any slot is missing
- wnba-simulator.test.ts: add 5 tests covering the sourceElo override parameter
on resolveElo() (overrides SRS, overrides futures, overrides fallback,
null/undefined fall through to normal priority chain)
- nhl-simulator.test.ts: add full bracket-aware integration suite (10 tests)
covering fallback to season-projection, structure validation errors, probability
distributions, fully-decided champion, and source tag
https://claude.ai/code/session_01139GJVg2gqgDjcUzqhnBNn
* Fix TS error: add resolvedElo to makeEntry in nhl-simulator.test.ts
TeamEntry requires resolvedElo after the simulator refactor.
https://claude.ai/code/session_01139GJVg2gqgDjcUzqhnBNn
* Fix lint: replace non-null assertions with null-safe alternatives in NHL test
https://claude.ai/code/session_01139GJVg2gqgDjcUzqhnBNn
---------
Co-authored-by: Claude <noreply@anthropic.com>
2026-04-30 08:33:57 -07:00
|
|
|
|
const [participantRows, standings, evRows] = await Promise.all([
|
2026-03-21 23:37:20 -07:00
|
|
|
|
db
|
Canonical tournament layer: schema + backfill (1/2) (#365)
* refactor(schema): rename per-window tables to season_* prefix
Renames participants, participant_expected_values, participant_qualifying_totals,
participant_results, participant_surface_elos to season_* prefixed names.
Renames event_results.participant_id to season_participant_id.
Phase 1a of canonical tournament layer migration.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
* refactor: rename participant.ts model file to season-participant.ts
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
* refactor(models): update model layer to use renamed schema exports
Updated all model files to use the renamed schema exports from Task 1:
- participants → seasonParticipants
- participantExpectedValues → seasonParticipantExpectedValues
- participantQualifyingTotals → seasonParticipantQualifyingTotals
- participantResults → seasonParticipantResults
- participantSurfaceElos → seasonParticipantSurfaceElos
- eventResults.participantId → eventResults.seasonParticipantId
- db.query relation accessors updated
- Relation field .participant → .seasonParticipant where applicable
- Import paths updated: ./participant → ./season-participant
Files updated (14 model files + 3 test files):
- draft-pick.ts
- draft-utils.ts
- event-result.ts
- group-stage-match.ts
- participant-result.ts
- qualifying-points.ts
- scoring-calculator.ts
- scoring-event.ts
- sports-season.ts
- surface-elo.ts
- team-score-events.ts
- cs2-major-stage.ts
- golf-skills.ts
- participant-expected-value.ts
- __tests__/sports-season.clone.test.ts
- __tests__/auto-pick.test.ts
- __tests__/executeAutoPick.timer.test.ts
Typecheck errors decreased: 779 → 499 (280 fewer)
All model file errors related to renamed schemas resolved.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
* refactor(routes): update route layer to use renamed schema exports
- Update model import from ~/models/participant to ~/models/season-participant
- Rename schema.participants to schema.seasonParticipants
- Rename schema.participantResults to schema.seasonParticipantResults
- Rename db.query.participants to db.query.seasonParticipants
- Update 9 route files and 1 test file
Affected files:
- admin.sports-seasons.$id.events.$eventId.bracket.server.ts
- admin.sports-seasons.$id.participants.tsx
- api/draft.force-manual-pick.ts
- api/draft.make-pick.ts
- api/draft.replace-pick.ts
- api/seasons.$seasonId.draft.ts
- leagues/$leagueId.draft-board.$seasonId.tsx
- leagues/$leagueId.sports-seasons.$sportsSeasonId.server.ts
- admin/__tests__/sports-seasons-participants.test.ts
Error count reduced from 499 to 453 (46 errors fixed).
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
* refactor(routes): update route files for schema rename
Update route imports from ~/models/participant to ~/models/season-participant
and fix references to .participant/.participantId on event results to use
.seasonParticipant/.seasonParticipantId after schema rename.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
* refactor(services): update simulators and services for renamed schema
Update all simulators, services, and server files to use renamed schema tables:
- participants → seasonParticipants
- participantExpectedValues → seasonParticipantExpectedValues
- participantResults → seasonParticipantResults
- eventResults.participantId → eventResults.seasonParticipantId
Files updated:
- 20 sport simulators (NBA, NHL, NFL, MLB, etc.)
- probability-updater.ts
- standings-sync/index.ts
- sports-data-sync.server.ts
- server/socket.ts
Typecheck errors reduced from 365 to 0.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
* migration: rename per-window tables to season_* prefix
* fix(tests): update mock query keys after participants table rename
Change mock db.query.participants to db.query.seasonParticipants in test
files to match the schema rename from commit 66145a9. This fixes
"Cannot read properties of undefined (reading 'findFirst'/'findMany')"
errors that occurred when production code queries db.query.seasonParticipants
but test mocks only defined the old participants key.
Files updated:
- app/services/simulations/__tests__/world-cup-simulator.test.ts
- app/routes/api/__tests__/draft.force-manual-pick.test.ts
- app/routes/api/__tests__/draft.force-manual-pick.timer-mode.test.ts
- app/routes/api/__tests__/draft.make-pick.timer-mode.test.ts
- server/__tests__/timer-autodraft.test.ts
- app/models/__tests__/team-score-events.test.ts
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
* fix(tests): update remaining mock paths and keys after schema rename
* fix(tests): final two mock stragglers after schema rename
- draft-pick.test.ts: assertion on db.query.participantQualifyingTotals
- process-match-result.test.ts: mock key participants → seasonParticipants
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
* chore: add post-phase1a baseline capture (temp, for diff verification)
* chore: capture pre-migration baselines
* chore: remove post-phase1a capture helper after verification
* schema: add canonical tournament & participant tables
Adds tournaments, participants (canonical), tournament_results, and
participant_surface_elos (canonical). Adds nullable tournament_id to
scoring_events and nullable participant_id to season_participants.
Phase 1b of canonical tournament layer migration.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
* feat(models): add canonical tournament, participant, result, surface-elo models
Adds CRUD modules for the canonical tables created in commit 775b905.
Each module mirrors existing app/models conventions (database() from
~/database/context, schema from ~/database/schema, mock-based tests).
Key implementation notes:
- participant.ts exports use "Canonical" prefix (CanonicalParticipant,
createCanonicalParticipant, etc.) to avoid collision with existing
season-participant.ts exports
- All four models include comprehensive unit tests following the
audit-log.test.ts pattern
- Tests use mocked db responses (no real database access)
- Upsert functions use onConflictDoUpdate for appropriate unique constraints
Part of Phase 1b of canonical tournament layer migration.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
* migration: create canonical tables, add nullable FKs
* scripts: add extractTournamentIdentity helper for backfill
Pure function that derives canonical (name, year) identity from a
scoring_events row, stripping trailing 4-digit years from the name or
falling back to eventDate. Used by the Phase 2 backfill to group
per-window events into canonical tournaments.
* scripts: add backfill orchestrator for canonical layer
Populates canonical tournaments, participants, tournament_results, and
participant_surface_elos from per-window data for qualifying-points
sports. Skips already-linked rows, is idempotent, and supports dry-run
mode.
Critical invariants enforced by the implementation:
- qualifying_points_awarded is never copied to tournament_results
- season_participant_qualifying_totals is never touched
- conflicting surface-Elo values between windows raise a loud error
(recorded in report.errors) rather than overwriting
* scripts: add backfill CLI with dry-run default
Wires backfill-canonical-layer.ts to a CLI entry point exposed as
`npm run backfill:canonical`. Defaults to --dry-run; requires --apply
to actually write. Supports --sport=<uuid> to limit to a single sport.
Exits 2 if the backfill reports errors (e.g., surface-Elo conflicts).
* fix(backfill-cli): wrap runBackfill in DatabaseContext.run
The orchestrator uses database() from ~/database/context, which requires
AsyncLocalStorage to be populated. Wrap the CLI invocation with
DatabaseContext.run(db, ...) using server/db's cached connection pool.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
* fix(backfill-cli): exit 0 on success so pg pool doesn't block
The cached postgres connection pool keeps the Node event loop open after
main() returns. Explicit process.exit(0) on success mirrors the pattern
in scripts/capture-baseline.ts.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
---------
Co-authored-by: Chris Parsons <chrisp@extrahop.com>
Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-01 20:13:18 -07:00
|
|
|
|
.select({ id: schema.seasonParticipants.id, name: schema.seasonParticipants.name })
|
|
|
|
|
|
.from(schema.seasonParticipants)
|
|
|
|
|
|
.where(eq(schema.seasonParticipants.sportsSeasonId, sportsSeasonId)),
|
2026-03-21 23:37:20 -07:00
|
|
|
|
getRegularSeasonStandings(sportsSeasonId),
|
Fix simulator brackets: NHL bracket-aware mode, MLB/NBA/WNBA sourceElo support (#358)
* Fix NHL bracket-aware simulation and MLB sourceElo support
NHL: NHLSimulator now checks for a populated playoff_game bracket before
falling back to season-projection mode. When a bracket exists (e.g. via
simple_16 template), it simulates the actual bracket structure, respecting
completed matches and using ELO+odds blending for the rest — matching the
pattern used by Snooker, NBA, and UCL simulators.
MLB: MLBSimulator now reads sourceElo from participantExpectedValues and
converts it to an effective RDif (via eloToRDif), overriding the hardcoded
TEAMS_DATA.rdif when present. This wires up the expected-wins admin form
(which saves sourceElo) to the simulation engine.
https://claude.ai/code/session_01139GJVg2gqgDjcUzqhnBNn
* Wire sourceElo from projected wins form into NBA, NHL, and WNBA simulators
All three simulators had entries in simulator-config.ts (enabling the projected
wins form in the admin UI) but silently ignored the sourceElo that was saved.
NBA: Both bracket-aware and season-projection modes now load sourceElo and
prefer it over hardcoded TEAMS_DATA elo. Added resolvedElo field to TeamEntry
so eloOfEntry() uses the correct value throughout.
NHL: Season-projection mode now fetches sourceElo + sourceOdds in a single
parallel query (removing the duplicate evRows query). Bracket-aware mode also
loads sourceElo alongside sourceOdds. Both modes use sourceElo > TEAMS_DATA > 1400.
WNBA: resolveElo() now accepts sourceElo as an optional highest-priority
argument. The evRows query fetches sourceElo alongside sourceOdds and passes
it through when building team entries.
https://claude.ai/code/session_01139GJVg2gqgDjcUzqhnBNn
* Add review fixes: NHL R1 validation, WNBA sourceElo tests, NHL bracket unit tests
- NHLSimulator.simulateBracket: remove unused nameById variable
- NHLSimulator.simulateBracket: validate all R1 participants before entering
Monte Carlo loop; throws with a clear message if any slot is missing
- wnba-simulator.test.ts: add 5 tests covering the sourceElo override parameter
on resolveElo() (overrides SRS, overrides futures, overrides fallback,
null/undefined fall through to normal priority chain)
- nhl-simulator.test.ts: add full bracket-aware integration suite (10 tests)
covering fallback to season-projection, structure validation errors, probability
distributions, fully-decided champion, and source tag
https://claude.ai/code/session_01139GJVg2gqgDjcUzqhnBNn
* Fix TS error: add resolvedElo to makeEntry in nhl-simulator.test.ts
TeamEntry requires resolvedElo after the simulator refactor.
https://claude.ai/code/session_01139GJVg2gqgDjcUzqhnBNn
* Fix lint: replace non-null assertions with null-safe alternatives in NHL test
https://claude.ai/code/session_01139GJVg2gqgDjcUzqhnBNn
---------
Co-authored-by: Claude <noreply@anthropic.com>
2026-04-30 08:33:57 -07:00
|
|
|
|
db
|
|
|
|
|
|
.select({
|
Canonical tournament layer: schema + backfill (1/2) (#365)
* refactor(schema): rename per-window tables to season_* prefix
Renames participants, participant_expected_values, participant_qualifying_totals,
participant_results, participant_surface_elos to season_* prefixed names.
Renames event_results.participant_id to season_participant_id.
Phase 1a of canonical tournament layer migration.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
* refactor: rename participant.ts model file to season-participant.ts
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
* refactor(models): update model layer to use renamed schema exports
Updated all model files to use the renamed schema exports from Task 1:
- participants → seasonParticipants
- participantExpectedValues → seasonParticipantExpectedValues
- participantQualifyingTotals → seasonParticipantQualifyingTotals
- participantResults → seasonParticipantResults
- participantSurfaceElos → seasonParticipantSurfaceElos
- eventResults.participantId → eventResults.seasonParticipantId
- db.query relation accessors updated
- Relation field .participant → .seasonParticipant where applicable
- Import paths updated: ./participant → ./season-participant
Files updated (14 model files + 3 test files):
- draft-pick.ts
- draft-utils.ts
- event-result.ts
- group-stage-match.ts
- participant-result.ts
- qualifying-points.ts
- scoring-calculator.ts
- scoring-event.ts
- sports-season.ts
- surface-elo.ts
- team-score-events.ts
- cs2-major-stage.ts
- golf-skills.ts
- participant-expected-value.ts
- __tests__/sports-season.clone.test.ts
- __tests__/auto-pick.test.ts
- __tests__/executeAutoPick.timer.test.ts
Typecheck errors decreased: 779 → 499 (280 fewer)
All model file errors related to renamed schemas resolved.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
* refactor(routes): update route layer to use renamed schema exports
- Update model import from ~/models/participant to ~/models/season-participant
- Rename schema.participants to schema.seasonParticipants
- Rename schema.participantResults to schema.seasonParticipantResults
- Rename db.query.participants to db.query.seasonParticipants
- Update 9 route files and 1 test file
Affected files:
- admin.sports-seasons.$id.events.$eventId.bracket.server.ts
- admin.sports-seasons.$id.participants.tsx
- api/draft.force-manual-pick.ts
- api/draft.make-pick.ts
- api/draft.replace-pick.ts
- api/seasons.$seasonId.draft.ts
- leagues/$leagueId.draft-board.$seasonId.tsx
- leagues/$leagueId.sports-seasons.$sportsSeasonId.server.ts
- admin/__tests__/sports-seasons-participants.test.ts
Error count reduced from 499 to 453 (46 errors fixed).
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
* refactor(routes): update route files for schema rename
Update route imports from ~/models/participant to ~/models/season-participant
and fix references to .participant/.participantId on event results to use
.seasonParticipant/.seasonParticipantId after schema rename.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
* refactor(services): update simulators and services for renamed schema
Update all simulators, services, and server files to use renamed schema tables:
- participants → seasonParticipants
- participantExpectedValues → seasonParticipantExpectedValues
- participantResults → seasonParticipantResults
- eventResults.participantId → eventResults.seasonParticipantId
Files updated:
- 20 sport simulators (NBA, NHL, NFL, MLB, etc.)
- probability-updater.ts
- standings-sync/index.ts
- sports-data-sync.server.ts
- server/socket.ts
Typecheck errors reduced from 365 to 0.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
* migration: rename per-window tables to season_* prefix
* fix(tests): update mock query keys after participants table rename
Change mock db.query.participants to db.query.seasonParticipants in test
files to match the schema rename from commit 66145a9. This fixes
"Cannot read properties of undefined (reading 'findFirst'/'findMany')"
errors that occurred when production code queries db.query.seasonParticipants
but test mocks only defined the old participants key.
Files updated:
- app/services/simulations/__tests__/world-cup-simulator.test.ts
- app/routes/api/__tests__/draft.force-manual-pick.test.ts
- app/routes/api/__tests__/draft.force-manual-pick.timer-mode.test.ts
- app/routes/api/__tests__/draft.make-pick.timer-mode.test.ts
- server/__tests__/timer-autodraft.test.ts
- app/models/__tests__/team-score-events.test.ts
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
* fix(tests): update remaining mock paths and keys after schema rename
* fix(tests): final two mock stragglers after schema rename
- draft-pick.test.ts: assertion on db.query.participantQualifyingTotals
- process-match-result.test.ts: mock key participants → seasonParticipants
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
* chore: add post-phase1a baseline capture (temp, for diff verification)
* chore: capture pre-migration baselines
* chore: remove post-phase1a capture helper after verification
* schema: add canonical tournament & participant tables
Adds tournaments, participants (canonical), tournament_results, and
participant_surface_elos (canonical). Adds nullable tournament_id to
scoring_events and nullable participant_id to season_participants.
Phase 1b of canonical tournament layer migration.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
* feat(models): add canonical tournament, participant, result, surface-elo models
Adds CRUD modules for the canonical tables created in commit 775b905.
Each module mirrors existing app/models conventions (database() from
~/database/context, schema from ~/database/schema, mock-based tests).
Key implementation notes:
- participant.ts exports use "Canonical" prefix (CanonicalParticipant,
createCanonicalParticipant, etc.) to avoid collision with existing
season-participant.ts exports
- All four models include comprehensive unit tests following the
audit-log.test.ts pattern
- Tests use mocked db responses (no real database access)
- Upsert functions use onConflictDoUpdate for appropriate unique constraints
Part of Phase 1b of canonical tournament layer migration.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
* migration: create canonical tables, add nullable FKs
* scripts: add extractTournamentIdentity helper for backfill
Pure function that derives canonical (name, year) identity from a
scoring_events row, stripping trailing 4-digit years from the name or
falling back to eventDate. Used by the Phase 2 backfill to group
per-window events into canonical tournaments.
* scripts: add backfill orchestrator for canonical layer
Populates canonical tournaments, participants, tournament_results, and
participant_surface_elos from per-window data for qualifying-points
sports. Skips already-linked rows, is idempotent, and supports dry-run
mode.
Critical invariants enforced by the implementation:
- qualifying_points_awarded is never copied to tournament_results
- season_participant_qualifying_totals is never touched
- conflicting surface-Elo values between windows raise a loud error
(recorded in report.errors) rather than overwriting
* scripts: add backfill CLI with dry-run default
Wires backfill-canonical-layer.ts to a CLI entry point exposed as
`npm run backfill:canonical`. Defaults to --dry-run; requires --apply
to actually write. Supports --sport=<uuid> to limit to a single sport.
Exits 2 if the backfill reports errors (e.g., surface-Elo conflicts).
* fix(backfill-cli): wrap runBackfill in DatabaseContext.run
The orchestrator uses database() from ~/database/context, which requires
AsyncLocalStorage to be populated. Wrap the CLI invocation with
DatabaseContext.run(db, ...) using server/db's cached connection pool.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
* fix(backfill-cli): exit 0 on success so pg pool doesn't block
The cached postgres connection pool keeps the Node event loop open after
main() returns. Explicit process.exit(0) on success mirrors the pattern
in scripts/capture-baseline.ts.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
---------
Co-authored-by: Chris Parsons <chrisp@extrahop.com>
Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-01 20:13:18 -07:00
|
|
|
|
participantId: schema.seasonParticipantExpectedValues.participantId,
|
|
|
|
|
|
sourceElo: schema.seasonParticipantExpectedValues.sourceElo,
|
Fix simulator brackets: NHL bracket-aware mode, MLB/NBA/WNBA sourceElo support (#358)
* Fix NHL bracket-aware simulation and MLB sourceElo support
NHL: NHLSimulator now checks for a populated playoff_game bracket before
falling back to season-projection mode. When a bracket exists (e.g. via
simple_16 template), it simulates the actual bracket structure, respecting
completed matches and using ELO+odds blending for the rest — matching the
pattern used by Snooker, NBA, and UCL simulators.
MLB: MLBSimulator now reads sourceElo from participantExpectedValues and
converts it to an effective RDif (via eloToRDif), overriding the hardcoded
TEAMS_DATA.rdif when present. This wires up the expected-wins admin form
(which saves sourceElo) to the simulation engine.
https://claude.ai/code/session_01139GJVg2gqgDjcUzqhnBNn
* Wire sourceElo from projected wins form into NBA, NHL, and WNBA simulators
All three simulators had entries in simulator-config.ts (enabling the projected
wins form in the admin UI) but silently ignored the sourceElo that was saved.
NBA: Both bracket-aware and season-projection modes now load sourceElo and
prefer it over hardcoded TEAMS_DATA elo. Added resolvedElo field to TeamEntry
so eloOfEntry() uses the correct value throughout.
NHL: Season-projection mode now fetches sourceElo + sourceOdds in a single
parallel query (removing the duplicate evRows query). Bracket-aware mode also
loads sourceElo alongside sourceOdds. Both modes use sourceElo > TEAMS_DATA > 1400.
WNBA: resolveElo() now accepts sourceElo as an optional highest-priority
argument. The evRows query fetches sourceElo alongside sourceOdds and passes
it through when building team entries.
https://claude.ai/code/session_01139GJVg2gqgDjcUzqhnBNn
* Add review fixes: NHL R1 validation, WNBA sourceElo tests, NHL bracket unit tests
- NHLSimulator.simulateBracket: remove unused nameById variable
- NHLSimulator.simulateBracket: validate all R1 participants before entering
Monte Carlo loop; throws with a clear message if any slot is missing
- wnba-simulator.test.ts: add 5 tests covering the sourceElo override parameter
on resolveElo() (overrides SRS, overrides futures, overrides fallback,
null/undefined fall through to normal priority chain)
- nhl-simulator.test.ts: add full bracket-aware integration suite (10 tests)
covering fallback to season-projection, structure validation errors, probability
distributions, fully-decided champion, and source tag
https://claude.ai/code/session_01139GJVg2gqgDjcUzqhnBNn
* Fix TS error: add resolvedElo to makeEntry in nhl-simulator.test.ts
TeamEntry requires resolvedElo after the simulator refactor.
https://claude.ai/code/session_01139GJVg2gqgDjcUzqhnBNn
* Fix lint: replace non-null assertions with null-safe alternatives in NHL test
https://claude.ai/code/session_01139GJVg2gqgDjcUzqhnBNn
---------
Co-authored-by: Claude <noreply@anthropic.com>
2026-04-30 08:33:57 -07:00
|
|
|
|
})
|
Canonical tournament layer: schema + backfill (1/2) (#365)
* refactor(schema): rename per-window tables to season_* prefix
Renames participants, participant_expected_values, participant_qualifying_totals,
participant_results, participant_surface_elos to season_* prefixed names.
Renames event_results.participant_id to season_participant_id.
Phase 1a of canonical tournament layer migration.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
* refactor: rename participant.ts model file to season-participant.ts
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
* refactor(models): update model layer to use renamed schema exports
Updated all model files to use the renamed schema exports from Task 1:
- participants → seasonParticipants
- participantExpectedValues → seasonParticipantExpectedValues
- participantQualifyingTotals → seasonParticipantQualifyingTotals
- participantResults → seasonParticipantResults
- participantSurfaceElos → seasonParticipantSurfaceElos
- eventResults.participantId → eventResults.seasonParticipantId
- db.query relation accessors updated
- Relation field .participant → .seasonParticipant where applicable
- Import paths updated: ./participant → ./season-participant
Files updated (14 model files + 3 test files):
- draft-pick.ts
- draft-utils.ts
- event-result.ts
- group-stage-match.ts
- participant-result.ts
- qualifying-points.ts
- scoring-calculator.ts
- scoring-event.ts
- sports-season.ts
- surface-elo.ts
- team-score-events.ts
- cs2-major-stage.ts
- golf-skills.ts
- participant-expected-value.ts
- __tests__/sports-season.clone.test.ts
- __tests__/auto-pick.test.ts
- __tests__/executeAutoPick.timer.test.ts
Typecheck errors decreased: 779 → 499 (280 fewer)
All model file errors related to renamed schemas resolved.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
* refactor(routes): update route layer to use renamed schema exports
- Update model import from ~/models/participant to ~/models/season-participant
- Rename schema.participants to schema.seasonParticipants
- Rename schema.participantResults to schema.seasonParticipantResults
- Rename db.query.participants to db.query.seasonParticipants
- Update 9 route files and 1 test file
Affected files:
- admin.sports-seasons.$id.events.$eventId.bracket.server.ts
- admin.sports-seasons.$id.participants.tsx
- api/draft.force-manual-pick.ts
- api/draft.make-pick.ts
- api/draft.replace-pick.ts
- api/seasons.$seasonId.draft.ts
- leagues/$leagueId.draft-board.$seasonId.tsx
- leagues/$leagueId.sports-seasons.$sportsSeasonId.server.ts
- admin/__tests__/sports-seasons-participants.test.ts
Error count reduced from 499 to 453 (46 errors fixed).
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
* refactor(routes): update route files for schema rename
Update route imports from ~/models/participant to ~/models/season-participant
and fix references to .participant/.participantId on event results to use
.seasonParticipant/.seasonParticipantId after schema rename.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
* refactor(services): update simulators and services for renamed schema
Update all simulators, services, and server files to use renamed schema tables:
- participants → seasonParticipants
- participantExpectedValues → seasonParticipantExpectedValues
- participantResults → seasonParticipantResults
- eventResults.participantId → eventResults.seasonParticipantId
Files updated:
- 20 sport simulators (NBA, NHL, NFL, MLB, etc.)
- probability-updater.ts
- standings-sync/index.ts
- sports-data-sync.server.ts
- server/socket.ts
Typecheck errors reduced from 365 to 0.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
* migration: rename per-window tables to season_* prefix
* fix(tests): update mock query keys after participants table rename
Change mock db.query.participants to db.query.seasonParticipants in test
files to match the schema rename from commit 66145a9. This fixes
"Cannot read properties of undefined (reading 'findFirst'/'findMany')"
errors that occurred when production code queries db.query.seasonParticipants
but test mocks only defined the old participants key.
Files updated:
- app/services/simulations/__tests__/world-cup-simulator.test.ts
- app/routes/api/__tests__/draft.force-manual-pick.test.ts
- app/routes/api/__tests__/draft.force-manual-pick.timer-mode.test.ts
- app/routes/api/__tests__/draft.make-pick.timer-mode.test.ts
- server/__tests__/timer-autodraft.test.ts
- app/models/__tests__/team-score-events.test.ts
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
* fix(tests): update remaining mock paths and keys after schema rename
* fix(tests): final two mock stragglers after schema rename
- draft-pick.test.ts: assertion on db.query.participantQualifyingTotals
- process-match-result.test.ts: mock key participants → seasonParticipants
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
* chore: add post-phase1a baseline capture (temp, for diff verification)
* chore: capture pre-migration baselines
* chore: remove post-phase1a capture helper after verification
* schema: add canonical tournament & participant tables
Adds tournaments, participants (canonical), tournament_results, and
participant_surface_elos (canonical). Adds nullable tournament_id to
scoring_events and nullable participant_id to season_participants.
Phase 1b of canonical tournament layer migration.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
* feat(models): add canonical tournament, participant, result, surface-elo models
Adds CRUD modules for the canonical tables created in commit 775b905.
Each module mirrors existing app/models conventions (database() from
~/database/context, schema from ~/database/schema, mock-based tests).
Key implementation notes:
- participant.ts exports use "Canonical" prefix (CanonicalParticipant,
createCanonicalParticipant, etc.) to avoid collision with existing
season-participant.ts exports
- All four models include comprehensive unit tests following the
audit-log.test.ts pattern
- Tests use mocked db responses (no real database access)
- Upsert functions use onConflictDoUpdate for appropriate unique constraints
Part of Phase 1b of canonical tournament layer migration.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
* migration: create canonical tables, add nullable FKs
* scripts: add extractTournamentIdentity helper for backfill
Pure function that derives canonical (name, year) identity from a
scoring_events row, stripping trailing 4-digit years from the name or
falling back to eventDate. Used by the Phase 2 backfill to group
per-window events into canonical tournaments.
* scripts: add backfill orchestrator for canonical layer
Populates canonical tournaments, participants, tournament_results, and
participant_surface_elos from per-window data for qualifying-points
sports. Skips already-linked rows, is idempotent, and supports dry-run
mode.
Critical invariants enforced by the implementation:
- qualifying_points_awarded is never copied to tournament_results
- season_participant_qualifying_totals is never touched
- conflicting surface-Elo values between windows raise a loud error
(recorded in report.errors) rather than overwriting
* scripts: add backfill CLI with dry-run default
Wires backfill-canonical-layer.ts to a CLI entry point exposed as
`npm run backfill:canonical`. Defaults to --dry-run; requires --apply
to actually write. Supports --sport=<uuid> to limit to a single sport.
Exits 2 if the backfill reports errors (e.g., surface-Elo conflicts).
* fix(backfill-cli): wrap runBackfill in DatabaseContext.run
The orchestrator uses database() from ~/database/context, which requires
AsyncLocalStorage to be populated. Wrap the CLI invocation with
DatabaseContext.run(db, ...) using server/db's cached connection pool.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
* fix(backfill-cli): exit 0 on success so pg pool doesn't block
The cached postgres connection pool keeps the Node event loop open after
main() returns. Explicit process.exit(0) on success mirrors the pattern
in scripts/capture-baseline.ts.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
---------
Co-authored-by: Chris Parsons <chrisp@extrahop.com>
Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-01 20:13:18 -07:00
|
|
|
|
.from(schema.seasonParticipantExpectedValues)
|
|
|
|
|
|
.where(eq(schema.seasonParticipantExpectedValues.sportsSeasonId, sportsSeasonId)),
|
2026-03-21 23:37:20 -07:00
|
|
|
|
]);
|
2026-03-16 21:43:39 -07:00
|
|
|
|
|
|
|
|
|
|
if (participantRows.length === 0) {
|
|
|
|
|
|
throw new Error(
|
|
|
|
|
|
`No participants found for sports season ${sportsSeasonId}. ` +
|
|
|
|
|
|
`Add NBA teams as participants before running simulation.`
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-21 23:37:20 -07:00
|
|
|
|
const standingsMap = new Map(standings.map((s) => [s.participantId, s]));
|
2026-03-16 21:43:39 -07:00
|
|
|
|
const participantIds = participantRows.map((r) => r.id);
|
|
|
|
|
|
|
Fix simulator brackets: NHL bracket-aware mode, MLB/NBA/WNBA sourceElo support (#358)
* Fix NHL bracket-aware simulation and MLB sourceElo support
NHL: NHLSimulator now checks for a populated playoff_game bracket before
falling back to season-projection mode. When a bracket exists (e.g. via
simple_16 template), it simulates the actual bracket structure, respecting
completed matches and using ELO+odds blending for the rest — matching the
pattern used by Snooker, NBA, and UCL simulators.
MLB: MLBSimulator now reads sourceElo from participantExpectedValues and
converts it to an effective RDif (via eloToRDif), overriding the hardcoded
TEAMS_DATA.rdif when present. This wires up the expected-wins admin form
(which saves sourceElo) to the simulation engine.
https://claude.ai/code/session_01139GJVg2gqgDjcUzqhnBNn
* Wire sourceElo from projected wins form into NBA, NHL, and WNBA simulators
All three simulators had entries in simulator-config.ts (enabling the projected
wins form in the admin UI) but silently ignored the sourceElo that was saved.
NBA: Both bracket-aware and season-projection modes now load sourceElo and
prefer it over hardcoded TEAMS_DATA elo. Added resolvedElo field to TeamEntry
so eloOfEntry() uses the correct value throughout.
NHL: Season-projection mode now fetches sourceElo + sourceOdds in a single
parallel query (removing the duplicate evRows query). Bracket-aware mode also
loads sourceElo alongside sourceOdds. Both modes use sourceElo > TEAMS_DATA > 1400.
WNBA: resolveElo() now accepts sourceElo as an optional highest-priority
argument. The evRows query fetches sourceElo alongside sourceOdds and passes
it through when building team entries.
https://claude.ai/code/session_01139GJVg2gqgDjcUzqhnBNn
* Add review fixes: NHL R1 validation, WNBA sourceElo tests, NHL bracket unit tests
- NHLSimulator.simulateBracket: remove unused nameById variable
- NHLSimulator.simulateBracket: validate all R1 participants before entering
Monte Carlo loop; throws with a clear message if any slot is missing
- wnba-simulator.test.ts: add 5 tests covering the sourceElo override parameter
on resolveElo() (overrides SRS, overrides futures, overrides fallback,
null/undefined fall through to normal priority chain)
- nhl-simulator.test.ts: add full bracket-aware integration suite (10 tests)
covering fallback to season-projection, structure validation errors, probability
distributions, fully-decided champion, and source tag
https://claude.ai/code/session_01139GJVg2gqgDjcUzqhnBNn
* Fix TS error: add resolvedElo to makeEntry in nhl-simulator.test.ts
TeamEntry requires resolvedElo after the simulator refactor.
https://claude.ai/code/session_01139GJVg2gqgDjcUzqhnBNn
* Fix lint: replace non-null assertions with null-safe alternatives in NHL test
https://claude.ai/code/session_01139GJVg2gqgDjcUzqhnBNn
---------
Co-authored-by: Claude <noreply@anthropic.com>
2026-04-30 08:33:57 -07:00
|
|
|
|
const dbEloMap = new Map<string, number>();
|
|
|
|
|
|
for (const row of evRows) {
|
|
|
|
|
|
if (row.sourceElo !== null && row.sourceElo !== undefined) {
|
|
|
|
|
|
dbEloMap.set(row.participantId, row.sourceElo);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-13 00:51:53 -04:00
|
|
|
|
interface TeamEntry {
|
|
|
|
|
|
id: string;
|
|
|
|
|
|
name: string;
|
|
|
|
|
|
data: NbaTeamData | undefined;
|
|
|
|
|
|
conference: "Eastern" | "Western";
|
|
|
|
|
|
currentWins: number;
|
|
|
|
|
|
remainingGames: number;
|
|
|
|
|
|
winProb: number;
|
Fix simulator brackets: NHL bracket-aware mode, MLB/NBA/WNBA sourceElo support (#358)
* Fix NHL bracket-aware simulation and MLB sourceElo support
NHL: NHLSimulator now checks for a populated playoff_game bracket before
falling back to season-projection mode. When a bracket exists (e.g. via
simple_16 template), it simulates the actual bracket structure, respecting
completed matches and using ELO+odds blending for the rest — matching the
pattern used by Snooker, NBA, and UCL simulators.
MLB: MLBSimulator now reads sourceElo from participantExpectedValues and
converts it to an effective RDif (via eloToRDif), overriding the hardcoded
TEAMS_DATA.rdif when present. This wires up the expected-wins admin form
(which saves sourceElo) to the simulation engine.
https://claude.ai/code/session_01139GJVg2gqgDjcUzqhnBNn
* Wire sourceElo from projected wins form into NBA, NHL, and WNBA simulators
All three simulators had entries in simulator-config.ts (enabling the projected
wins form in the admin UI) but silently ignored the sourceElo that was saved.
NBA: Both bracket-aware and season-projection modes now load sourceElo and
prefer it over hardcoded TEAMS_DATA elo. Added resolvedElo field to TeamEntry
so eloOfEntry() uses the correct value throughout.
NHL: Season-projection mode now fetches sourceElo + sourceOdds in a single
parallel query (removing the duplicate evRows query). Bracket-aware mode also
loads sourceElo alongside sourceOdds. Both modes use sourceElo > TEAMS_DATA > 1400.
WNBA: resolveElo() now accepts sourceElo as an optional highest-priority
argument. The evRows query fetches sourceElo alongside sourceOdds and passes
it through when building team entries.
https://claude.ai/code/session_01139GJVg2gqgDjcUzqhnBNn
* Add review fixes: NHL R1 validation, WNBA sourceElo tests, NHL bracket unit tests
- NHLSimulator.simulateBracket: remove unused nameById variable
- NHLSimulator.simulateBracket: validate all R1 participants before entering
Monte Carlo loop; throws with a clear message if any slot is missing
- wnba-simulator.test.ts: add 5 tests covering the sourceElo override parameter
on resolveElo() (overrides SRS, overrides futures, overrides fallback,
null/undefined fall through to normal priority chain)
- nhl-simulator.test.ts: add full bracket-aware integration suite (10 tests)
covering fallback to season-projection, structure validation errors, probability
distributions, fully-decided champion, and source tag
https://claude.ai/code/session_01139GJVg2gqgDjcUzqhnBNn
* Fix TS error: add resolvedElo to makeEntry in nhl-simulator.test.ts
TeamEntry requires resolvedElo after the simulator refactor.
https://claude.ai/code/session_01139GJVg2gqgDjcUzqhnBNn
* Fix lint: replace non-null assertions with null-safe alternatives in NHL test
https://claude.ai/code/session_01139GJVg2gqgDjcUzqhnBNn
---------
Co-authored-by: Claude <noreply@anthropic.com>
2026-04-30 08:33:57 -07:00
|
|
|
|
resolvedElo: number;
|
2026-04-13 00:51:53 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-21 23:37:20 -07:00
|
|
|
|
const teams: TeamEntry[] = participantRows.map((r) => {
|
|
|
|
|
|
const standing = standingsMap.get(r.id);
|
|
|
|
|
|
const data = getTeamData(r.name);
|
|
|
|
|
|
const gamesPlayed = standing?.gamesPlayed ?? 0;
|
|
|
|
|
|
const conf = standing?.conference;
|
|
|
|
|
|
const conference: "Eastern" | "Western" =
|
|
|
|
|
|
conf === "Eastern" || conf === "Western"
|
|
|
|
|
|
? conf
|
|
|
|
|
|
: (data?.conference ?? "Eastern");
|
Fix simulator brackets: NHL bracket-aware mode, MLB/NBA/WNBA sourceElo support (#358)
* Fix NHL bracket-aware simulation and MLB sourceElo support
NHL: NHLSimulator now checks for a populated playoff_game bracket before
falling back to season-projection mode. When a bracket exists (e.g. via
simple_16 template), it simulates the actual bracket structure, respecting
completed matches and using ELO+odds blending for the rest — matching the
pattern used by Snooker, NBA, and UCL simulators.
MLB: MLBSimulator now reads sourceElo from participantExpectedValues and
converts it to an effective RDif (via eloToRDif), overriding the hardcoded
TEAMS_DATA.rdif when present. This wires up the expected-wins admin form
(which saves sourceElo) to the simulation engine.
https://claude.ai/code/session_01139GJVg2gqgDjcUzqhnBNn
* Wire sourceElo from projected wins form into NBA, NHL, and WNBA simulators
All three simulators had entries in simulator-config.ts (enabling the projected
wins form in the admin UI) but silently ignored the sourceElo that was saved.
NBA: Both bracket-aware and season-projection modes now load sourceElo and
prefer it over hardcoded TEAMS_DATA elo. Added resolvedElo field to TeamEntry
so eloOfEntry() uses the correct value throughout.
NHL: Season-projection mode now fetches sourceElo + sourceOdds in a single
parallel query (removing the duplicate evRows query). Bracket-aware mode also
loads sourceElo alongside sourceOdds. Both modes use sourceElo > TEAMS_DATA > 1400.
WNBA: resolveElo() now accepts sourceElo as an optional highest-priority
argument. The evRows query fetches sourceElo alongside sourceOdds and passes
it through when building team entries.
https://claude.ai/code/session_01139GJVg2gqgDjcUzqhnBNn
* Add review fixes: NHL R1 validation, WNBA sourceElo tests, NHL bracket unit tests
- NHLSimulator.simulateBracket: remove unused nameById variable
- NHLSimulator.simulateBracket: validate all R1 participants before entering
Monte Carlo loop; throws with a clear message if any slot is missing
- wnba-simulator.test.ts: add 5 tests covering the sourceElo override parameter
on resolveElo() (overrides SRS, overrides futures, overrides fallback,
null/undefined fall through to normal priority chain)
- nhl-simulator.test.ts: add full bracket-aware integration suite (10 tests)
covering fallback to season-projection, structure validation errors, probability
distributions, fully-decided champion, and source tag
https://claude.ai/code/session_01139GJVg2gqgDjcUzqhnBNn
* Fix TS error: add resolvedElo to makeEntry in nhl-simulator.test.ts
TeamEntry requires resolvedElo after the simulator refactor.
https://claude.ai/code/session_01139GJVg2gqgDjcUzqhnBNn
* Fix lint: replace non-null assertions with null-safe alternatives in NHL test
https://claude.ai/code/session_01139GJVg2gqgDjcUzqhnBNn
---------
Co-authored-by: Claude <noreply@anthropic.com>
2026-04-30 08:33:57 -07:00
|
|
|
|
const resolvedElo = dbEloMap.get(r.id) ?? data?.elo ?? 1400;
|
2026-03-21 23:37:20 -07:00
|
|
|
|
return {
|
|
|
|
|
|
id: r.id,
|
|
|
|
|
|
name: r.name,
|
|
|
|
|
|
data,
|
|
|
|
|
|
conference,
|
|
|
|
|
|
currentWins: standing?.wins ?? 0,
|
2026-06-30 23:24:48 +00:00
|
|
|
|
remainingGames: Math.max(0, seasonGames - gamesPlayed),
|
|
|
|
|
|
winProb: eloWinProbability(resolvedElo, 1500, parityFactor),
|
Fix simulator brackets: NHL bracket-aware mode, MLB/NBA/WNBA sourceElo support (#358)
* Fix NHL bracket-aware simulation and MLB sourceElo support
NHL: NHLSimulator now checks for a populated playoff_game bracket before
falling back to season-projection mode. When a bracket exists (e.g. via
simple_16 template), it simulates the actual bracket structure, respecting
completed matches and using ELO+odds blending for the rest — matching the
pattern used by Snooker, NBA, and UCL simulators.
MLB: MLBSimulator now reads sourceElo from participantExpectedValues and
converts it to an effective RDif (via eloToRDif), overriding the hardcoded
TEAMS_DATA.rdif when present. This wires up the expected-wins admin form
(which saves sourceElo) to the simulation engine.
https://claude.ai/code/session_01139GJVg2gqgDjcUzqhnBNn
* Wire sourceElo from projected wins form into NBA, NHL, and WNBA simulators
All three simulators had entries in simulator-config.ts (enabling the projected
wins form in the admin UI) but silently ignored the sourceElo that was saved.
NBA: Both bracket-aware and season-projection modes now load sourceElo and
prefer it over hardcoded TEAMS_DATA elo. Added resolvedElo field to TeamEntry
so eloOfEntry() uses the correct value throughout.
NHL: Season-projection mode now fetches sourceElo + sourceOdds in a single
parallel query (removing the duplicate evRows query). Bracket-aware mode also
loads sourceElo alongside sourceOdds. Both modes use sourceElo > TEAMS_DATA > 1400.
WNBA: resolveElo() now accepts sourceElo as an optional highest-priority
argument. The evRows query fetches sourceElo alongside sourceOdds and passes
it through when building team entries.
https://claude.ai/code/session_01139GJVg2gqgDjcUzqhnBNn
* Add review fixes: NHL R1 validation, WNBA sourceElo tests, NHL bracket unit tests
- NHLSimulator.simulateBracket: remove unused nameById variable
- NHLSimulator.simulateBracket: validate all R1 participants before entering
Monte Carlo loop; throws with a clear message if any slot is missing
- wnba-simulator.test.ts: add 5 tests covering the sourceElo override parameter
on resolveElo() (overrides SRS, overrides futures, overrides fallback,
null/undefined fall through to normal priority chain)
- nhl-simulator.test.ts: add full bracket-aware integration suite (10 tests)
covering fallback to season-projection, structure validation errors, probability
distributions, fully-decided champion, and source tag
https://claude.ai/code/session_01139GJVg2gqgDjcUzqhnBNn
* Fix TS error: add resolvedElo to makeEntry in nhl-simulator.test.ts
TeamEntry requires resolvedElo after the simulator refactor.
https://claude.ai/code/session_01139GJVg2gqgDjcUzqhnBNn
* Fix lint: replace non-null assertions with null-safe alternatives in NHL test
https://claude.ai/code/session_01139GJVg2gqgDjcUzqhnBNn
---------
Co-authored-by: Claude <noreply@anthropic.com>
2026-04-30 08:33:57 -07:00
|
|
|
|
resolvedElo,
|
2026-03-21 23:37:20 -07:00
|
|
|
|
};
|
|
|
|
|
|
});
|
2026-03-16 21:43:39 -07:00
|
|
|
|
|
2026-03-21 23:37:20 -07:00
|
|
|
|
const easternTeams = teams.filter((t) => t.conference === "Eastern");
|
|
|
|
|
|
const westernTeams = teams.filter((t) => t.conference === "Western");
|
2026-03-16 21:43:39 -07:00
|
|
|
|
|
|
|
|
|
|
if (easternTeams.length < 10 || westernTeams.length < 10) {
|
|
|
|
|
|
throw new Error(
|
|
|
|
|
|
`Each conference needs at least 10 participants (got East: ${easternTeams.length}, ` +
|
|
|
|
|
|
`West: ${westernTeams.length}). Add all 30 NBA teams before running simulation.`
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-13 00:51:53 -04:00
|
|
|
|
const simTeamGame = (a: TeamEntry, b: TeamEntry): TeamEntry =>
|
2026-06-30 23:24:48 +00:00
|
|
|
|
Math.random() < eloWinProbability(eloOfEntry(a), eloOfEntry(b), parityFactor) ? a : b;
|
2026-03-16 21:43:39 -07:00
|
|
|
|
|
2026-04-13 00:51:53 -04:00
|
|
|
|
const simTeamSeries = (a: TeamEntry, b: TeamEntry): { winner: TeamEntry; loser: TeamEntry } => {
|
2026-06-30 23:24:48 +00:00
|
|
|
|
const winProb = eloWinProbability(eloOfEntry(a), eloOfEntry(b), parityFactor);
|
2026-04-13 00:51:53 -04:00
|
|
|
|
let wA = 0;
|
|
|
|
|
|
let wB = 0;
|
|
|
|
|
|
while (wA < 4 && wB < 4) {
|
|
|
|
|
|
if (Math.random() < winProb) wA++; else wB++;
|
2026-03-16 21:43:39 -07:00
|
|
|
|
}
|
2026-04-13 00:51:53 -04:00
|
|
|
|
return wA === 4 ? { winner: a, loser: b } : { winner: b, loser: a };
|
2026-03-16 21:43:39 -07:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const simPlayIn = ([s7, s8, s9, s10]: [TeamEntry, TeamEntry, TeamEntry, TeamEntry]): [TeamEntry, TeamEntry] => {
|
2026-04-13 00:51:53 -04:00
|
|
|
|
const game1Winner = simTeamGame(s7, s8);
|
2026-03-16 21:43:39 -07:00
|
|
|
|
const game1Loser = game1Winner === s7 ? s8 : s7;
|
2026-04-13 00:51:53 -04:00
|
|
|
|
const game2Winner = simTeamGame(s9, s10);
|
|
|
|
|
|
return [game1Winner, simTeamGame(game1Loser, game2Winner)];
|
2026-03-16 21:43:39 -07:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const buildConferenceBracket = (confTeams: TeamEntry[]): TeamEntry[] => {
|
2026-03-21 23:37:20 -07:00
|
|
|
|
const projected = confTeams.map((t) => ({
|
2026-03-16 21:43:39 -07:00
|
|
|
|
team: t,
|
2026-03-21 23:37:20 -07:00
|
|
|
|
projectedWins: simulateProjectedWins(t),
|
2026-03-16 21:43:39 -07:00
|
|
|
|
tiebreaker: Math.random(),
|
|
|
|
|
|
}));
|
2026-04-13 00:51:53 -04:00
|
|
|
|
const ranked = projected.toSorted((a, b) => b.projectedWins - a.projectedWins || b.tiebreaker - a.tiebreaker);
|
|
|
|
|
|
const top6 = ranked.slice(0, 6).map((x) => x.team);
|
|
|
|
|
|
const playIn = ranked.slice(6, 10).map((x) => x.team) as
|
2026-03-16 21:43:39 -07:00
|
|
|
|
[TeamEntry, TeamEntry, TeamEntry, TeamEntry];
|
|
|
|
|
|
const [seed7, seed8] = simPlayIn(playIn);
|
|
|
|
|
|
return [...top6, seed7, seed8];
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const simR1 = ([s1, s2, s3, s4, s5, s6, s7, s8]: TeamEntry[]): TeamEntry[] => [
|
2026-04-13 00:51:53 -04:00
|
|
|
|
simTeamSeries(s1, s8).winner,
|
|
|
|
|
|
simTeamSeries(s4, s5).winner,
|
|
|
|
|
|
simTeamSeries(s2, s7).winner,
|
|
|
|
|
|
simTeamSeries(s3, s6).winner,
|
2026-03-16 21:43:39 -07:00
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
const simR2 = ([w0, w1, w2, w3]: TeamEntry[]): { winners: TeamEntry[]; losers: TeamEntry[] } => {
|
2026-04-13 00:51:53 -04:00
|
|
|
|
const m1 = simTeamSeries(w0, w1);
|
|
|
|
|
|
const m2 = simTeamSeries(w2, w3);
|
2026-03-16 21:43:39 -07:00
|
|
|
|
return { winners: [m1.winner, m2.winner], losers: [m1.loser, m2.loser] };
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const championCounts = new Map<string, number>(participantIds.map((id) => [id, 0]));
|
|
|
|
|
|
const finalistCounts = new Map<string, number>(participantIds.map((id) => [id, 0]));
|
|
|
|
|
|
const confFinalLoserCounts = new Map<string, number>(participantIds.map((id) => [id, 0]));
|
|
|
|
|
|
const confSemiLoserCounts = new Map<string, number>(participantIds.map((id) => [id, 0]));
|
|
|
|
|
|
|
2026-06-30 23:24:48 +00:00
|
|
|
|
for (let s = 0; s < numSimulations; s++) {
|
2026-03-16 21:43:39 -07:00
|
|
|
|
const eastBracket = buildConferenceBracket(easternTeams);
|
|
|
|
|
|
const westBracket = buildConferenceBracket(westernTeams);
|
|
|
|
|
|
|
|
|
|
|
|
const { winners: eastR2Winners, losers: eastR2Losers } = simR2(simR1(eastBracket));
|
2026-04-13 00:51:53 -04:00
|
|
|
|
const { winner: eastChamp, loser: eastCFLoser } = simTeamSeries(eastR2Winners[0], eastR2Winners[1]);
|
2026-03-16 21:43:39 -07:00
|
|
|
|
|
|
|
|
|
|
const { winners: westR2Winners, losers: westR2Losers } = simR2(simR1(westBracket));
|
2026-04-13 00:51:53 -04:00
|
|
|
|
const { winner: westChamp, loser: westCFLoser } = simTeamSeries(westR2Winners[0], westR2Winners[1]);
|
2026-03-16 21:43:39 -07:00
|
|
|
|
|
2026-04-13 00:51:53 -04:00
|
|
|
|
const { winner: champion, loser: finalist } = simTeamSeries(eastChamp, westChamp);
|
2026-03-16 21:43:39 -07:00
|
|
|
|
|
2026-03-21 23:37:20 -07:00
|
|
|
|
championCounts.set(champion.id, (championCounts.get(champion.id) ?? 0) + 1);
|
|
|
|
|
|
finalistCounts.set(finalist.id, (finalistCounts.get(finalist.id) ?? 0) + 1);
|
Fix oxlint warnings: no-shadow, consistent-function-scoping, no-non-null-assertion, and others (#196)
* Fix no-shadow and consistent-function-scoping lint violations
Resolves all 11 no-shadow and 16 consistent-function-scoping oxlint
warnings and promotes both rules to errors in .oxlintrc.json.
no-shadow: renamed Drizzle callback params (sports→s, matches→m,
seasons→s) to avoid shadowing outer imports; removed shadowed
destructures (eq, inArray) from where callbacks; renamed inner
template→bracketTemplate, prev→currentTimers, season→ss, name→teamName
(with name: teamName fix to preserve semantics).
consistent-function-scoping: moved formatDate, getRankBadge,
getMovementIndicator, getPositionBadge, getStatusBadge, toDateStr,
elo (×2), weightedPick, sortByMatchNumber (×2) to module scope;
moved formatTime (×2), isValidLeagueName, getDraftTimes,
makeSeasonQueues to file scope in test files.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
* Fix no-non-null-assertion lint violations and promote to error
Eliminates all 208 no-non-null-assertion warnings across 38 files.
Promotes typescript/no-non-null-assertion from warn to error in
.oxlintrc.json.
Fix patterns applied:
- Map.get(key)! after .has() check → extract with get() + null guard
- Map.get(key)! on pre-populated count maps → ?? 0 default
- .set(id, map.get(id)! + 1) increment → ?? 0 before adding
- participant1Id!/participant2Id! on DB matches → ?? "" fallback
- array.find()! in tests → guard + throw or expect().toBeDefined()
- bracketTemplateCache.get(id)! → null guard extract
- Various nullable field accesses → optional chain or ?? default
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
* Fix prefer-add-event-listener, no-unassigned-import, require-module-specifiers
Resolves all 9 remaining non-console lint warnings and promotes all
three rules to errors in .oxlintrc.json.
- prefer-add-event-listener: converted onchange/onclick/onload
assignments to addEventListener in useDraftNotifications.ts and
admin.data-sync.tsx; stored changeHandler ref for proper cleanup
with removeEventListener
- no-unassigned-import: configured rule with allow list for legitimate
side-effect imports (*.css, @testing-library/jest-dom,
@testing-library/cypress/add-commands)
- require-module-specifiers: removed redundant `export {}` from
cypress/support/e2e.ts (file already has an import)
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
* Fix TypeScript errors from no-non-null-assertion fixes
Two fixes introduced by the non-null assertion cleanup produced type
errors:
- scoring-event.ts: `?? ""` was wrong type for a participant object map;
restructured to explicit null guards so TypeScript can narrow correctly
- standings-sync/index.ts: `?? null` after name-match lookup lost the
truthy guarantee, causing TS18047 on the write-back block; added
`participant &&` guard before accessing its properties
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
* Add npm run typecheck as Stop hook in Claude settings
Runs a full project typecheck at the end of each Claude turn so type
errors surface as feedback before the next message.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
---------
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-21 10:59:51 -07:00
|
|
|
|
confFinalLoserCounts.set(eastCFLoser.id, (confFinalLoserCounts.get(eastCFLoser.id) ?? 0) + 1);
|
|
|
|
|
|
confFinalLoserCounts.set(westCFLoser.id, (confFinalLoserCounts.get(westCFLoser.id) ?? 0) + 1);
|
2026-03-16 21:43:39 -07:00
|
|
|
|
for (const loser of [...eastR2Losers, ...westR2Losers]) {
|
Fix oxlint warnings: no-shadow, consistent-function-scoping, no-non-null-assertion, and others (#196)
* Fix no-shadow and consistent-function-scoping lint violations
Resolves all 11 no-shadow and 16 consistent-function-scoping oxlint
warnings and promotes both rules to errors in .oxlintrc.json.
no-shadow: renamed Drizzle callback params (sports→s, matches→m,
seasons→s) to avoid shadowing outer imports; removed shadowed
destructures (eq, inArray) from where callbacks; renamed inner
template→bracketTemplate, prev→currentTimers, season→ss, name→teamName
(with name: teamName fix to preserve semantics).
consistent-function-scoping: moved formatDate, getRankBadge,
getMovementIndicator, getPositionBadge, getStatusBadge, toDateStr,
elo (×2), weightedPick, sortByMatchNumber (×2) to module scope;
moved formatTime (×2), isValidLeagueName, getDraftTimes,
makeSeasonQueues to file scope in test files.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
* Fix no-non-null-assertion lint violations and promote to error
Eliminates all 208 no-non-null-assertion warnings across 38 files.
Promotes typescript/no-non-null-assertion from warn to error in
.oxlintrc.json.
Fix patterns applied:
- Map.get(key)! after .has() check → extract with get() + null guard
- Map.get(key)! on pre-populated count maps → ?? 0 default
- .set(id, map.get(id)! + 1) increment → ?? 0 before adding
- participant1Id!/participant2Id! on DB matches → ?? "" fallback
- array.find()! in tests → guard + throw or expect().toBeDefined()
- bracketTemplateCache.get(id)! → null guard extract
- Various nullable field accesses → optional chain or ?? default
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
* Fix prefer-add-event-listener, no-unassigned-import, require-module-specifiers
Resolves all 9 remaining non-console lint warnings and promotes all
three rules to errors in .oxlintrc.json.
- prefer-add-event-listener: converted onchange/onclick/onload
assignments to addEventListener in useDraftNotifications.ts and
admin.data-sync.tsx; stored changeHandler ref for proper cleanup
with removeEventListener
- no-unassigned-import: configured rule with allow list for legitimate
side-effect imports (*.css, @testing-library/jest-dom,
@testing-library/cypress/add-commands)
- require-module-specifiers: removed redundant `export {}` from
cypress/support/e2e.ts (file already has an import)
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
* Fix TypeScript errors from no-non-null-assertion fixes
Two fixes introduced by the non-null assertion cleanup produced type
errors:
- scoring-event.ts: `?? ""` was wrong type for a participant object map;
restructured to explicit null guards so TypeScript can narrow correctly
- standings-sync/index.ts: `?? null` after name-match lookup lost the
truthy guarantee, causing TS18047 on the write-back block; added
`participant &&` guard before accessing its properties
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
* Add npm run typecheck as Stop hook in Claude settings
Runs a full project typecheck at the end of each Claude turn so type
errors surface as feedback before the next message.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
---------
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-21 10:59:51 -07:00
|
|
|
|
confSemiLoserCounts.set(loser.id, (confSemiLoserCounts.get(loser.id) ?? 0) + 1);
|
2026-03-16 21:43:39 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-06-30 23:24:48 +00:00
|
|
|
|
const N = numSimulations;
|
2026-03-16 21:43:39 -07:00
|
|
|
|
const results: SimulationResult[] = participantIds.map((participantId) => {
|
Fix oxlint warnings: no-shadow, consistent-function-scoping, no-non-null-assertion, and others (#196)
* Fix no-shadow and consistent-function-scoping lint violations
Resolves all 11 no-shadow and 16 consistent-function-scoping oxlint
warnings and promotes both rules to errors in .oxlintrc.json.
no-shadow: renamed Drizzle callback params (sports→s, matches→m,
seasons→s) to avoid shadowing outer imports; removed shadowed
destructures (eq, inArray) from where callbacks; renamed inner
template→bracketTemplate, prev→currentTimers, season→ss, name→teamName
(with name: teamName fix to preserve semantics).
consistent-function-scoping: moved formatDate, getRankBadge,
getMovementIndicator, getPositionBadge, getStatusBadge, toDateStr,
elo (×2), weightedPick, sortByMatchNumber (×2) to module scope;
moved formatTime (×2), isValidLeagueName, getDraftTimes,
makeSeasonQueues to file scope in test files.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
* Fix no-non-null-assertion lint violations and promote to error
Eliminates all 208 no-non-null-assertion warnings across 38 files.
Promotes typescript/no-non-null-assertion from warn to error in
.oxlintrc.json.
Fix patterns applied:
- Map.get(key)! after .has() check → extract with get() + null guard
- Map.get(key)! on pre-populated count maps → ?? 0 default
- .set(id, map.get(id)! + 1) increment → ?? 0 before adding
- participant1Id!/participant2Id! on DB matches → ?? "" fallback
- array.find()! in tests → guard + throw or expect().toBeDefined()
- bracketTemplateCache.get(id)! → null guard extract
- Various nullable field accesses → optional chain or ?? default
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
* Fix prefer-add-event-listener, no-unassigned-import, require-module-specifiers
Resolves all 9 remaining non-console lint warnings and promotes all
three rules to errors in .oxlintrc.json.
- prefer-add-event-listener: converted onchange/onclick/onload
assignments to addEventListener in useDraftNotifications.ts and
admin.data-sync.tsx; stored changeHandler ref for proper cleanup
with removeEventListener
- no-unassigned-import: configured rule with allow list for legitimate
side-effect imports (*.css, @testing-library/jest-dom,
@testing-library/cypress/add-commands)
- require-module-specifiers: removed redundant `export {}` from
cypress/support/e2e.ts (file already has an import)
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
* Fix TypeScript errors from no-non-null-assertion fixes
Two fixes introduced by the non-null assertion cleanup produced type
errors:
- scoring-event.ts: `?? ""` was wrong type for a participant object map;
restructured to explicit null guards so TypeScript can narrow correctly
- standings-sync/index.ts: `?? null` after name-match lookup lost the
truthy guarantee, causing TS18047 on the write-back block; added
`participant &&` guard before accessing its properties
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
* Add npm run typecheck as Stop hook in Claude settings
Runs a full project typecheck at the end of each Claude turn so type
errors surface as feedback before the next message.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
---------
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-21 10:59:51 -07:00
|
|
|
|
const c = championCounts.get(participantId) ?? 0;
|
|
|
|
|
|
const f = finalistCounts.get(participantId) ?? 0;
|
|
|
|
|
|
const cf = confFinalLoserCounts.get(participantId) ?? 0;
|
|
|
|
|
|
const cs = confSemiLoserCounts.get(participantId) ?? 0;
|
2026-03-16 21:43:39 -07:00
|
|
|
|
return {
|
|
|
|
|
|
participantId,
|
|
|
|
|
|
probabilities: {
|
2026-04-13 00:51:53 -04:00
|
|
|
|
probFirst: c / N,
|
|
|
|
|
|
probSecond: f / N,
|
|
|
|
|
|
probThird: cf / (2 * N),
|
|
|
|
|
|
probFourth: cf / (2 * N),
|
|
|
|
|
|
probFifth: cs / (4 * N),
|
|
|
|
|
|
probSixth: cs / (4 * N),
|
|
|
|
|
|
probSeventh: cs / (4 * N),
|
|
|
|
|
|
probEighth: cs / (4 * N),
|
2026-03-16 21:43:39 -07:00
|
|
|
|
},
|
|
|
|
|
|
source: "nba_bracket_monte_carlo",
|
|
|
|
|
|
};
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2026-04-13 00:51:53 -04:00
|
|
|
|
normalizeColumns(results);
|
2026-03-16 21:43:39 -07:00
|
|
|
|
return results;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-04-13 00:51:53 -04:00
|
|
|
|
|
|
|
|
|
|
// ─── Season-projection helpers (used only in Mode 2) ─────────────────────────
|
|
|
|
|
|
|
|
|
|
|
|
// TeamEntryBase is a structural subset of TeamEntry (the private interface defined inside
|
|
|
|
|
|
// simulateSeasonProjection). eloOfEntry is module-level so the linter doesn't flag it for
|
|
|
|
|
|
// "consistent-function-scoping" (it doesn't close over any local variables).
|
|
|
|
|
|
interface TeamEntryBase {
|
Fix simulator brackets: NHL bracket-aware mode, MLB/NBA/WNBA sourceElo support (#358)
* Fix NHL bracket-aware simulation and MLB sourceElo support
NHL: NHLSimulator now checks for a populated playoff_game bracket before
falling back to season-projection mode. When a bracket exists (e.g. via
simple_16 template), it simulates the actual bracket structure, respecting
completed matches and using ELO+odds blending for the rest — matching the
pattern used by Snooker, NBA, and UCL simulators.
MLB: MLBSimulator now reads sourceElo from participantExpectedValues and
converts it to an effective RDif (via eloToRDif), overriding the hardcoded
TEAMS_DATA.rdif when present. This wires up the expected-wins admin form
(which saves sourceElo) to the simulation engine.
https://claude.ai/code/session_01139GJVg2gqgDjcUzqhnBNn
* Wire sourceElo from projected wins form into NBA, NHL, and WNBA simulators
All three simulators had entries in simulator-config.ts (enabling the projected
wins form in the admin UI) but silently ignored the sourceElo that was saved.
NBA: Both bracket-aware and season-projection modes now load sourceElo and
prefer it over hardcoded TEAMS_DATA elo. Added resolvedElo field to TeamEntry
so eloOfEntry() uses the correct value throughout.
NHL: Season-projection mode now fetches sourceElo + sourceOdds in a single
parallel query (removing the duplicate evRows query). Bracket-aware mode also
loads sourceElo alongside sourceOdds. Both modes use sourceElo > TEAMS_DATA > 1400.
WNBA: resolveElo() now accepts sourceElo as an optional highest-priority
argument. The evRows query fetches sourceElo alongside sourceOdds and passes
it through when building team entries.
https://claude.ai/code/session_01139GJVg2gqgDjcUzqhnBNn
* Add review fixes: NHL R1 validation, WNBA sourceElo tests, NHL bracket unit tests
- NHLSimulator.simulateBracket: remove unused nameById variable
- NHLSimulator.simulateBracket: validate all R1 participants before entering
Monte Carlo loop; throws with a clear message if any slot is missing
- wnba-simulator.test.ts: add 5 tests covering the sourceElo override parameter
on resolveElo() (overrides SRS, overrides futures, overrides fallback,
null/undefined fall through to normal priority chain)
- nhl-simulator.test.ts: add full bracket-aware integration suite (10 tests)
covering fallback to season-projection, structure validation errors, probability
distributions, fully-decided champion, and source tag
https://claude.ai/code/session_01139GJVg2gqgDjcUzqhnBNn
* Fix TS error: add resolvedElo to makeEntry in nhl-simulator.test.ts
TeamEntry requires resolvedElo after the simulator refactor.
https://claude.ai/code/session_01139GJVg2gqgDjcUzqhnBNn
* Fix lint: replace non-null assertions with null-safe alternatives in NHL test
https://claude.ai/code/session_01139GJVg2gqgDjcUzqhnBNn
---------
Co-authored-by: Claude <noreply@anthropic.com>
2026-04-30 08:33:57 -07:00
|
|
|
|
resolvedElo: number;
|
2026-04-13 00:51:53 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function eloOfEntry(entry: TeamEntryBase): number {
|
Fix simulator brackets: NHL bracket-aware mode, MLB/NBA/WNBA sourceElo support (#358)
* Fix NHL bracket-aware simulation and MLB sourceElo support
NHL: NHLSimulator now checks for a populated playoff_game bracket before
falling back to season-projection mode. When a bracket exists (e.g. via
simple_16 template), it simulates the actual bracket structure, respecting
completed matches and using ELO+odds blending for the rest — matching the
pattern used by Snooker, NBA, and UCL simulators.
MLB: MLBSimulator now reads sourceElo from participantExpectedValues and
converts it to an effective RDif (via eloToRDif), overriding the hardcoded
TEAMS_DATA.rdif when present. This wires up the expected-wins admin form
(which saves sourceElo) to the simulation engine.
https://claude.ai/code/session_01139GJVg2gqgDjcUzqhnBNn
* Wire sourceElo from projected wins form into NBA, NHL, and WNBA simulators
All three simulators had entries in simulator-config.ts (enabling the projected
wins form in the admin UI) but silently ignored the sourceElo that was saved.
NBA: Both bracket-aware and season-projection modes now load sourceElo and
prefer it over hardcoded TEAMS_DATA elo. Added resolvedElo field to TeamEntry
so eloOfEntry() uses the correct value throughout.
NHL: Season-projection mode now fetches sourceElo + sourceOdds in a single
parallel query (removing the duplicate evRows query). Bracket-aware mode also
loads sourceElo alongside sourceOdds. Both modes use sourceElo > TEAMS_DATA > 1400.
WNBA: resolveElo() now accepts sourceElo as an optional highest-priority
argument. The evRows query fetches sourceElo alongside sourceOdds and passes
it through when building team entries.
https://claude.ai/code/session_01139GJVg2gqgDjcUzqhnBNn
* Add review fixes: NHL R1 validation, WNBA sourceElo tests, NHL bracket unit tests
- NHLSimulator.simulateBracket: remove unused nameById variable
- NHLSimulator.simulateBracket: validate all R1 participants before entering
Monte Carlo loop; throws with a clear message if any slot is missing
- wnba-simulator.test.ts: add 5 tests covering the sourceElo override parameter
on resolveElo() (overrides SRS, overrides futures, overrides fallback,
null/undefined fall through to normal priority chain)
- nhl-simulator.test.ts: add full bracket-aware integration suite (10 tests)
covering fallback to season-projection, structure validation errors, probability
distributions, fully-decided champion, and source tag
https://claude.ai/code/session_01139GJVg2gqgDjcUzqhnBNn
* Fix TS error: add resolvedElo to makeEntry in nhl-simulator.test.ts
TeamEntry requires resolvedElo after the simulator refactor.
https://claude.ai/code/session_01139GJVg2gqgDjcUzqhnBNn
* Fix lint: replace non-null assertions with null-safe alternatives in NHL test
https://claude.ai/code/session_01139GJVg2gqgDjcUzqhnBNn
---------
Co-authored-by: Claude <noreply@anthropic.com>
2026-04-30 08:33:57 -07:00
|
|
|
|
return entry.resolvedElo;
|
2026-04-13 00:51:53 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
interface TeamForProjection {
|
|
|
|
|
|
currentWins: number;
|
|
|
|
|
|
remainingGames: number;
|
|
|
|
|
|
winProb: number;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function simulateProjectedWins(entry: TeamForProjection): number {
|
|
|
|
|
|
let extra = 0;
|
|
|
|
|
|
for (let g = 0; g < entry.remainingGames; g++) {
|
|
|
|
|
|
if (Math.random() < entry.winProb) extra++;
|
|
|
|
|
|
}
|
|
|
|
|
|
return entry.currentWins + extra;
|
|
|
|
|
|
}
|