2025-11-21 22:05:50 -08:00
|
|
|
/**
|
|
|
|
|
* Probability Updater Service
|
|
|
|
|
*
|
|
|
|
|
* Updates probability distributions when real results come in.
|
|
|
|
|
*
|
|
|
|
|
* Key behaviors:
|
|
|
|
|
* - Finished participants: Set to 100% at their placement, 0% elsewhere
|
|
|
|
|
* - Unfinished participants: Re-run ICM calculation with remaining participants
|
|
|
|
|
* - Handles partial results correctly
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import { findParticipantResultsBySportsSeasonId } from "~/models/participant-result";
|
|
|
|
|
import {
|
|
|
|
|
getAllParticipantEVsForSeason,
|
|
|
|
|
upsertParticipantEV,
|
|
|
|
|
type ParticipantEV,
|
|
|
|
|
} from "~/models/participant-expected-value";
|
|
|
|
|
import { calculateICMFromOdds } from "./icm-calculator";
|
|
|
|
|
import type { ProbabilityDistribution } from "./ev-calculator";
|
|
|
|
|
import { database } from "~/database/context";
|
|
|
|
|
import * as schema from "~/database/schema";
|
|
|
|
|
import { eq } from "drizzle-orm";
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Result of probability update operation
|
|
|
|
|
*/
|
|
|
|
|
export interface ProbabilityUpdateResult {
|
|
|
|
|
finishedParticipants: number;
|
|
|
|
|
unfishedParticipants: number;
|
|
|
|
|
updated: number;
|
|
|
|
|
errors: string[];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Before/after probabilities for display
|
|
|
|
|
*/
|
|
|
|
|
export interface ProbabilityComparison {
|
|
|
|
|
participantId: string;
|
|
|
|
|
participantName: string;
|
|
|
|
|
before: number[]; // [P(1st), P(2nd), ..., P(8th)]
|
|
|
|
|
after: number[]; // [P(1st), P(2nd), ..., P(8th)]
|
|
|
|
|
status: 'finished' | 'recalculated' | 'unchanged';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Convert probability array to ProbabilityDistribution
|
|
|
|
|
*/
|
|
|
|
|
function arrayToProbabilityDistribution(probs: number[]): ProbabilityDistribution {
|
|
|
|
|
return {
|
|
|
|
|
probFirst: probs[0],
|
|
|
|
|
probSecond: probs[1],
|
|
|
|
|
probThird: probs[2],
|
|
|
|
|
probFourth: probs[3],
|
|
|
|
|
probFifth: probs[4],
|
|
|
|
|
probSixth: probs[5],
|
|
|
|
|
probSeventh: probs[6],
|
|
|
|
|
probEighth: probs[7],
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Convert ParticipantEV to probability array
|
|
|
|
|
*/
|
|
|
|
|
function evToProbabilityArray(ev: ParticipantEV): number[] {
|
|
|
|
|
return [
|
|
|
|
|
parseFloat(ev.probFirst),
|
|
|
|
|
parseFloat(ev.probSecond),
|
|
|
|
|
parseFloat(ev.probThird),
|
|
|
|
|
parseFloat(ev.probFourth),
|
|
|
|
|
parseFloat(ev.probFifth),
|
|
|
|
|
parseFloat(ev.probSixth),
|
|
|
|
|
parseFloat(ev.probSeventh),
|
|
|
|
|
parseFloat(ev.probEighth),
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create a probability distribution where participant finished at a specific position
|
|
|
|
|
*
|
|
|
|
|
* @param finalPosition The position where participant finished
|
|
|
|
|
* - 1-8: 100% at that position, 0% elsewhere
|
|
|
|
|
* - 0: Eliminated (didn't make playoffs) - 0% for all positions
|
|
|
|
|
* - >8: Finished outside scoring - 0% for all positions
|
|
|
|
|
* @returns Array of probabilities with 100% at finalPosition (if 1-8), or all 0%
|
|
|
|
|
*/
|
|
|
|
|
function createFinishedProbabilities(finalPosition: number): number[] {
|
|
|
|
|
const probs = [0, 0, 0, 0, 0, 0, 0, 0];
|
|
|
|
|
|
|
|
|
|
// Handle positions 1-8
|
|
|
|
|
if (finalPosition >= 1 && finalPosition <= 8) {
|
|
|
|
|
probs[finalPosition - 1] = 1.0; // 100% at their position
|
|
|
|
|
}
|
|
|
|
|
// finalPosition = 0 (eliminated) or > 8 (finished outside top 8)
|
|
|
|
|
// Keep all probabilities at 0%
|
|
|
|
|
|
|
|
|
|
return probs;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Update probabilities for a sports season after results come in
|
|
|
|
|
*
|
|
|
|
|
* Process:
|
|
|
|
|
* 1. Get all participant results (finished participants)
|
|
|
|
|
* 2. Get all existing participant EVs
|
|
|
|
|
* 3. For finished participants: set 100% at their placement
|
|
|
|
|
* 4. For unfinished participants: recalculate using ICM with remaining participants
|
|
|
|
|
*
|
|
|
|
|
* @param sportsSeasonId Sports season to update
|
|
|
|
|
* @param recalculateUnfinished Whether to recalculate unfinished participants (default true)
|
|
|
|
|
* @returns Update result summary
|
|
|
|
|
*/
|
|
|
|
|
export async function updateProbabilitiesAfterResult(
|
|
|
|
|
sportsSeasonId: string,
|
|
|
|
|
recalculateUnfinished: boolean = true
|
|
|
|
|
): Promise<ProbabilityUpdateResult> {
|
|
|
|
|
const errors: string[] = [];
|
|
|
|
|
let updated = 0;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// Get all results (finished participants)
|
|
|
|
|
const results = await findParticipantResultsBySportsSeasonId(sportsSeasonId);
|
|
|
|
|
|
|
|
|
|
// Get all existing EVs
|
|
|
|
|
const existingEVs = await getAllParticipantEVsForSeason(sportsSeasonId);
|
|
|
|
|
|
|
|
|
|
// Create map of participantId -> finalPosition
|
|
|
|
|
const finishedMap = new Map(
|
|
|
|
|
results
|
|
|
|
|
.filter(r => r.finalPosition !== null)
|
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
|
|
|
.map(r => [r.participantId, r.finalPosition ?? 0])
|
2025-11-21 22:05:50 -08:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Update finished participants
|
|
|
|
|
// Use default scoring rules (we only care about setting probabilities, not EV for finished)
|
|
|
|
|
const defaultScoringRules = {
|
|
|
|
|
pointsFor1st: 100,
|
|
|
|
|
pointsFor2nd: 70,
|
|
|
|
|
pointsFor3rd: 50,
|
|
|
|
|
pointsFor4th: 40,
|
|
|
|
|
pointsFor5th: 25,
|
|
|
|
|
pointsFor6th: 25,
|
|
|
|
|
pointsFor7th: 15,
|
|
|
|
|
pointsFor8th: 15,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
for (const [participantId, finalPosition] of finishedMap.entries()) {
|
|
|
|
|
try {
|
|
|
|
|
const probs = createFinishedProbabilities(finalPosition);
|
|
|
|
|
const probabilities = arrayToProbabilityDistribution(probs);
|
|
|
|
|
|
|
|
|
|
await upsertParticipantEV({
|
|
|
|
|
participantId,
|
|
|
|
|
sportsSeasonId,
|
|
|
|
|
probabilities,
|
|
|
|
|
scoringRules: defaultScoringRules,
|
|
|
|
|
source: 'manual', // Result is from actual outcome
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
updated++;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
errors.push(`Failed to update participant ${participantId}: ${error}`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Recalculate unfinished participants if requested
|
|
|
|
|
if (recalculateUnfinished) {
|
|
|
|
|
const unfinishedEVs = existingEVs.filter(
|
|
|
|
|
ev => !finishedMap.has(ev.participantId)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (unfinishedEVs.length > 0) {
|
|
|
|
|
// Get their current championship probabilities (use existing P(1st) as proxy)
|
|
|
|
|
const unfinishedOdds = unfinishedEVs.map(ev => {
|
|
|
|
|
const pFirst = parseFloat(ev.probFirst);
|
|
|
|
|
// Convert probability back to odds (approximate)
|
|
|
|
|
// probability = 100 / (odds + 100) => odds = (100 / probability) - 100
|
|
|
|
|
const odds = pFirst > 0 ? Math.round((100 / pFirst) - 100) : 100000;
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
participantId: ev.participantId,
|
|
|
|
|
odds: odds,
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Recalculate ICM for unfinished participants
|
|
|
|
|
const icmResults = calculateICMFromOdds(unfinishedOdds);
|
|
|
|
|
|
|
|
|
|
// Update each unfinished participant
|
|
|
|
|
for (const [participantId, icmResult] of icmResults.entries()) {
|
|
|
|
|
try {
|
|
|
|
|
const probs = [
|
|
|
|
|
icmResult.probabilities.first,
|
|
|
|
|
icmResult.probabilities.second,
|
|
|
|
|
icmResult.probabilities.third,
|
|
|
|
|
icmResult.probabilities.fourth,
|
|
|
|
|
icmResult.probabilities.fifth,
|
|
|
|
|
icmResult.probabilities.sixth,
|
|
|
|
|
icmResult.probabilities.seventh,
|
|
|
|
|
icmResult.probabilities.eighth,
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
const probabilities = arrayToProbabilityDistribution(probs);
|
|
|
|
|
|
|
|
|
|
await upsertParticipantEV({
|
|
|
|
|
participantId,
|
|
|
|
|
sportsSeasonId,
|
|
|
|
|
probabilities,
|
|
|
|
|
scoringRules: defaultScoringRules,
|
|
|
|
|
source: 'futures_odds', // Recalculated from remaining odds
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
updated++;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
errors.push(`Failed to recalculate participant ${participantId}: ${error}`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
finishedParticipants: finishedMap.size,
|
|
|
|
|
unfishedParticipants: existingEVs.length - finishedMap.size,
|
|
|
|
|
updated,
|
|
|
|
|
errors,
|
|
|
|
|
};
|
|
|
|
|
} catch (error) {
|
|
|
|
|
errors.push(`Failed to update probabilities: ${error}`);
|
|
|
|
|
return {
|
|
|
|
|
finishedParticipants: 0,
|
|
|
|
|
unfishedParticipants: 0,
|
|
|
|
|
updated,
|
|
|
|
|
errors,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get before/after comparison of probabilities for display
|
|
|
|
|
*
|
|
|
|
|
* Shows what will change when updateProbabilitiesAfterResult runs.
|
|
|
|
|
* Useful for preview before committing changes.
|
|
|
|
|
*
|
|
|
|
|
* @param sportsSeasonId Sports season to preview
|
|
|
|
|
* @returns Array of probability comparisons
|
|
|
|
|
*/
|
|
|
|
|
export async function previewProbabilityUpdate(
|
|
|
|
|
sportsSeasonId: string
|
|
|
|
|
): Promise<ProbabilityComparison[]> {
|
|
|
|
|
const db = database();
|
|
|
|
|
|
|
|
|
|
// Get all results (finished participants)
|
|
|
|
|
const results = await findParticipantResultsBySportsSeasonId(sportsSeasonId);
|
|
|
|
|
|
|
|
|
|
// Get all existing EVs with participant names
|
|
|
|
|
const existingEVs = await db.query.participantExpectedValues.findMany({
|
|
|
|
|
where: eq(schema.participantExpectedValues.sportsSeasonId, sportsSeasonId),
|
|
|
|
|
with: {
|
|
|
|
|
participant: true,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Create map of participantId -> finalPosition
|
|
|
|
|
const finishedMap = new Map(
|
|
|
|
|
results
|
|
|
|
|
.filter(r => r.finalPosition !== null)
|
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
|
|
|
.map(r => [r.participantId, r.finalPosition ?? 0])
|
2025-11-21 22:05:50 -08:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const comparisons: ProbabilityComparison[] = [];
|
|
|
|
|
|
|
|
|
|
for (const ev of existingEVs) {
|
|
|
|
|
const before = evToProbabilityArray(ev);
|
|
|
|
|
const finalPosition = finishedMap.get(ev.participantId);
|
|
|
|
|
|
|
|
|
|
let after: number[];
|
|
|
|
|
let status: 'finished' | 'recalculated' | 'unchanged';
|
|
|
|
|
|
|
|
|
|
if (finalPosition !== undefined) {
|
|
|
|
|
// Finished participant
|
|
|
|
|
after = createFinishedProbabilities(finalPosition);
|
|
|
|
|
status = 'finished';
|
|
|
|
|
} else {
|
|
|
|
|
// For preview, we'd need to recalculate - for now just show unchanged
|
|
|
|
|
// In a real implementation, we'd run the ICM calculation here too
|
|
|
|
|
after = before;
|
|
|
|
|
status = 'unchanged';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
comparisons.push({
|
|
|
|
|
participantId: ev.participantId,
|
|
|
|
|
participantName: ev.participant?.name || 'Unknown',
|
|
|
|
|
before,
|
|
|
|
|
after,
|
|
|
|
|
status,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return comparisons;
|
|
|
|
|
}
|