A review of this branch found four problems, all downstream of one decision: calling runSportsSeasonSimulation from inside the result path. That function does three jobs — recompute probabilities, recalculate standings, write the daily EV snapshot — and the result path wants only the first. 1. The Discord standings post was silently suppressed on every scored match, for all 13 bracket-aware sports. recalculateAffectedLeagues detects change by snapshotting teamStandings, recalculating, then diffing; changedTeamIds gates the notification. But processMatchResult runs updateProbabilitiesAfterResult first, which now reached the runner's own recalculateStandings. The new totals were therefore already written when the "before" snapshot was taken, the diff came back empty, and the post never fired. previousRank went the same way: recalculateStandings rolls it forward on every call, so the extra one erased rank movement. runSportsSeasonSimulation now takes skipStandingsRecalc / skipSnapshots and the probability updater passes both. The snapshot is skipped because it is a per-day series keyed by snapshotDate — writing it per match result just overwrites the day's row with intra-day values. 2. finalizeQualifyingPoints marks the season completed immediately before calling the updater, and the runner rejects a completed season outright. With the ICM fallback gone that failed every time, stranding anyone still in the unfinished set on permanently stale probabilities — reachable for cs2_major_qualifying_points, the one bracket-aware qualifying-points sport. A completed season is not this branch's case rather than a failure: every placement is final and the floor it protects can no longer be contradicted. shouldRerunSimulator now excludes it and it falls through to ICM as before. The genuine failure modes still leave probabilities alone rather than falling back to the path being replaced. 3. match-sync calls processMatchResult in a per-match loop with no skipSideEffects, so each synced match ran a full Monte Carlo plus EV rewrite, snapshot and standings recalc. processMatchResult gains skipProbabilities — mirroring the option processPlayoffEvent already takes, and narrower than skipSideEffects — which match-sync passes in the loop before refreshing once at the end. Per-match standings and Discord posts are unchanged. 4. A partially-seeded afl_10 bracket now throws from inside the result path. The throw is correct and stays; the concern was that it was silent, which the error surfacing in 2 covers. Two claims from the review did not hold up and were left alone: the batch bracket route already passes skipSideEffects per match and refreshes once after the loop, and autoCompleteRoundIfDone already passes skipProbabilities, so there is no double run per round completion. Tests: the runner honors both skip flags and still writes EVs; the updater asks for probabilities only; a completed season takes the ICM path without erroring; processMatchResult skips the refresh but still announces. The two behavioral ones were confirmed to fail against the previous behavior. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01VPxDnSEKVoKx9HFcQ6gmcS
203 lines
7.5 KiB
TypeScript
203 lines
7.5 KiB
TypeScript
import { eq } from "drizzle-orm";
|
|
import { database } from "~/database/context";
|
|
import * as schema from "~/database/schema";
|
|
import { DEFAULT_SCORING_RULES } from "~/lib/scoring-types";
|
|
import type { ScoringRules } from "~/services/ev-calculator";
|
|
import { batchUpsertParticipantEvSnapshots } from "~/models/ev-snapshot";
|
|
import { batchUpsertParticipantEVs } from "~/models/participant-expected-value";
|
|
import { recalculateStandings } from "~/models/scoring-calculator";
|
|
import { findParticipantsBySportsSeasonId } from "~/models/season-participant";
|
|
import { findSportsSeasonById, updateSportsSeason } from "~/models/sports-season";
|
|
import { calculateEV } from "~/services/ev-calculator";
|
|
import { getSimulator, type SimulatorType } from "~/services/simulations/registry";
|
|
import { normalizeSimulationResultColumns } from "~/services/simulations/simulation-probabilities";
|
|
import {
|
|
getSportsSeasonSimulatorConfig,
|
|
prepareSimulatorInputsForRun,
|
|
validateSimulatorReadiness,
|
|
} from "~/models/simulator";
|
|
|
|
const ZERO_PROBS = {
|
|
probFirst: 0,
|
|
probSecond: 0,
|
|
probThird: 0,
|
|
probFourth: 0,
|
|
probFifth: 0,
|
|
probSixth: 0,
|
|
probSeventh: 0,
|
|
probEighth: 0,
|
|
};
|
|
|
|
async function getPersistenceContext(
|
|
simulatorType: SimulatorType,
|
|
sportsSeason: Awaited<ReturnType<typeof findSportsSeasonById>>
|
|
): Promise<{ scoringRules: ScoringRules; source: "elo_simulation" | "performance_model" }> {
|
|
if (simulatorType !== "brackt") {
|
|
return { scoringRules: DEFAULT_SCORING_RULES, source: "elo_simulation" };
|
|
}
|
|
|
|
if (!sportsSeason?.fantasySeasonId) {
|
|
throw new Error("Brackt simulations must run against a private per-league sports season, not the global Brackt template.");
|
|
}
|
|
|
|
const fantasySeason = await database().query.seasons.findFirst({
|
|
where: eq(schema.seasons.id, sportsSeason.fantasySeasonId),
|
|
});
|
|
if (!fantasySeason) {
|
|
throw new Error("Linked fantasy season not found for Brackt simulation.");
|
|
}
|
|
|
|
return {
|
|
source: "performance_model",
|
|
scoringRules: {
|
|
pointsFor1st: fantasySeason.pointsFor1st,
|
|
pointsFor2nd: fantasySeason.pointsFor2nd,
|
|
pointsFor3rd: fantasySeason.pointsFor3rd,
|
|
pointsFor4th: fantasySeason.pointsFor4th,
|
|
pointsFor5th: fantasySeason.pointsFor5th,
|
|
pointsFor6th: fantasySeason.pointsFor6th,
|
|
pointsFor7th: fantasySeason.pointsFor7th,
|
|
pointsFor8th: fantasySeason.pointsFor8th,
|
|
},
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Side effects a caller can opt out of.
|
|
*
|
|
* A simulation run does three jobs — recompute probabilities, recalculate standings, and record
|
|
* the day's EV snapshot. `updateProbabilitiesAfterResult` wants only the first: it runs inside
|
|
* the result path, where the caller recalculates standings itself immediately afterwards.
|
|
*
|
|
* Letting the run recalculate there is not merely redundant, it is wrong.
|
|
* recalculateAffectedLeagues detects change by snapshotting teamStandings, recalculating, then
|
|
* diffing, and that diff gates the Discord standings post; a recalculation slipped in
|
|
* beforehand makes the diff empty and silently suppresses the notification. recalculateStandings
|
|
* also rolls previousRank forward on every call, so an extra one erases rank movement.
|
|
*/
|
|
export interface RunSportsSeasonSimulationOptions {
|
|
/** Leave standings to the caller. */
|
|
skipStandingsRecalc?: boolean;
|
|
/**
|
|
* Skip the daily EV snapshot. The snapshot is a per-day series keyed by snapshotDate, so
|
|
* writing it on every match result just overwrites the day's row with intra-day values.
|
|
*/
|
|
skipSnapshots?: boolean;
|
|
}
|
|
|
|
export interface RunSportsSeasonSimulationResult {
|
|
sportsSeasonId: string;
|
|
simulatorType: SimulatorType;
|
|
simulatedParticipants: number;
|
|
zeroedParticipants: number;
|
|
snapshotDate: string;
|
|
}
|
|
|
|
export async function runSportsSeasonSimulation(
|
|
sportsSeasonId: string,
|
|
options: RunSportsSeasonSimulationOptions = {}
|
|
): Promise<RunSportsSeasonSimulationResult> {
|
|
const sportsSeason = await findSportsSeasonById(sportsSeasonId);
|
|
if (!sportsSeason) {
|
|
throw new Error("Sports season not found");
|
|
}
|
|
if (sportsSeason.status === "completed") {
|
|
throw new Error("Completed sports seasons cannot be simulated.");
|
|
}
|
|
|
|
const simulatorConfig = await getSportsSeasonSimulatorConfig(sportsSeasonId);
|
|
if (!simulatorConfig) {
|
|
throw new Error(
|
|
"This sport has no simulator type configured. Set a simulator type on the sport in the admin panel before running a simulation."
|
|
);
|
|
}
|
|
|
|
if (sportsSeason.simulationStatus === "running") {
|
|
throw new Error("A simulation is already running for this sports season. Please wait for it to complete.");
|
|
}
|
|
|
|
const readiness = await validateSimulatorReadiness(sportsSeasonId);
|
|
if (!readiness.canRun) {
|
|
throw new Error(
|
|
`Simulator is not ready: ${readiness.missingInputs.join(", ") || "missing required setup"}.`
|
|
);
|
|
}
|
|
|
|
await prepareSimulatorInputsForRun(sportsSeasonId);
|
|
|
|
await updateSportsSeason(sportsSeasonId, { simulationStatus: "running" });
|
|
|
|
try {
|
|
const simulator = getSimulator(simulatorConfig.simulatorType);
|
|
const results = await simulator.simulate(sportsSeasonId, simulatorConfig.config);
|
|
|
|
if (results.length === 0) {
|
|
throw new Error("Simulation returned no results. Check that participants have simulator input data.");
|
|
}
|
|
|
|
normalizeSimulationResultColumns(results);
|
|
|
|
const allSeasonParticipants = await findParticipantsBySportsSeasonId(sportsSeasonId);
|
|
const simulatedIds = new Set(results.map((r) => r.participantId));
|
|
const zeroedParticipants = allSeasonParticipants.filter((p) => !simulatedIds.has(p.id));
|
|
const persistence = await getPersistenceContext(simulatorConfig.simulatorType, sportsSeason);
|
|
|
|
await batchUpsertParticipantEVs([
|
|
...results.map((r) => ({
|
|
participantId: r.participantId,
|
|
sportsSeasonId,
|
|
probabilities: r.probabilities,
|
|
scoringRules: persistence.scoringRules,
|
|
source: persistence.source,
|
|
})),
|
|
...zeroedParticipants.map((p) => ({
|
|
participantId: p.id,
|
|
sportsSeasonId,
|
|
probabilities: ZERO_PROBS,
|
|
scoringRules: persistence.scoringRules,
|
|
source: persistence.source,
|
|
})),
|
|
]);
|
|
|
|
if (!options.skipStandingsRecalc) {
|
|
const seasonSports = await database().query.seasonSports.findMany({
|
|
where: eq(schema.seasonSports.sportsSeasonId, sportsSeasonId),
|
|
});
|
|
await Promise.all(seasonSports.map(({ seasonId }) => recalculateStandings(seasonId)));
|
|
}
|
|
|
|
const snapshotDate = new Date().toISOString().slice(0, 10);
|
|
if (!options.skipSnapshots) {
|
|
await batchUpsertParticipantEvSnapshots(
|
|
results.map((r) => ({
|
|
participantId: r.participantId,
|
|
sportsSeasonId,
|
|
snapshotDate,
|
|
probFirst: r.probabilities.probFirst,
|
|
probSecond: r.probabilities.probSecond,
|
|
probThird: r.probabilities.probThird,
|
|
probFourth: r.probabilities.probFourth,
|
|
probFifth: r.probabilities.probFifth,
|
|
probSixth: r.probabilities.probSixth,
|
|
probSeventh: r.probabilities.probSeventh,
|
|
probEighth: r.probabilities.probEighth,
|
|
calculatedEV: calculateEV(r.probabilities, persistence.scoringRules),
|
|
source: r.source,
|
|
}))
|
|
);
|
|
}
|
|
|
|
await updateSportsSeason(sportsSeasonId, { simulationStatus: "idle" });
|
|
|
|
return {
|
|
sportsSeasonId,
|
|
simulatorType: simulatorConfig.simulatorType,
|
|
simulatedParticipants: results.length,
|
|
zeroedParticipants: zeroedParticipants.length,
|
|
snapshotDate,
|
|
};
|
|
} catch (error) {
|
|
await updateSportsSeason(sportsSeasonId, { simulationStatus: "failed" });
|
|
throw error;
|
|
}
|
|
}
|