- Added loader and action functions for managing expected values in sports seasons. - Implemented UI for recalculating probabilities based on participant results. - Created a service to update probabilities after results are finalized, including handling finished and unfinished participants. - Developed tests for the probability updater service to ensure correct functionality. - Introduced a preview feature to show potential changes before applying updates.
92 lines
3.1 KiB
TypeScript
92 lines
3.1 KiB
TypeScript
import type { Route } from "./+types/admin.sports-seasons.$id.expected-values";
|
|
import { findSportsSeasonById } from "~/models/sports-season";
|
|
import { findParticipantsBySportsSeasonId } from "~/models/participant";
|
|
import {
|
|
upsertParticipantEV,
|
|
getAllParticipantEVsForSeason
|
|
} from "~/models/participant-expected-value";
|
|
import type { ProbabilitySource } from "~/models/participant-expected-value";
|
|
|
|
export async function loader({ params }: Route.LoaderArgs) {
|
|
const sportsSeason = await findSportsSeasonById(params.id);
|
|
|
|
if (!sportsSeason) {
|
|
throw new Response("Sports season not found", { status: 404 });
|
|
}
|
|
|
|
const participants = await findParticipantsBySportsSeasonId(params.id);
|
|
const existingEVs = await getAllParticipantEVsForSeason(params.id);
|
|
|
|
// Create a map of participant ID to EV data
|
|
const evMap = new Map(existingEVs.map(ev => [ev.participantId, ev]));
|
|
|
|
return {
|
|
sportsSeason: sportsSeason as typeof sportsSeason & { sport: { id: string; name: string; type: string; slug: string } },
|
|
participants,
|
|
existingEVs: evMap,
|
|
};
|
|
}
|
|
|
|
export async function action({ request, params }: Route.ActionArgs) {
|
|
const formData = await request.formData();
|
|
const participantId = formData.get("participantId");
|
|
const source = (formData.get("source") || "manual") as ProbabilitySource;
|
|
|
|
if (typeof participantId !== "string") {
|
|
return { error: "Participant ID is required" };
|
|
}
|
|
|
|
// Get probability values (entered as percentages, convert to decimals)
|
|
const probFirst = parseFloat(formData.get("probFirst") as string || "0") / 100;
|
|
const probSecond = parseFloat(formData.get("probSecond") as string || "0") / 100;
|
|
const probThird = parseFloat(formData.get("probThird") as string || "0") / 100;
|
|
const probFourth = parseFloat(formData.get("probFourth") as string || "0") / 100;
|
|
const probFifth = parseFloat(formData.get("probFifth") as string || "0") / 100;
|
|
const probSixth = parseFloat(formData.get("probSixth") as string || "0") / 100;
|
|
const probSeventh = parseFloat(formData.get("probSeventh") as string || "0") / 100;
|
|
const probEighth = parseFloat(formData.get("probEighth") as string || "0") / 100;
|
|
|
|
// Use default scoring rules
|
|
// Note: Actual league seasons may have different scoring rules
|
|
// EVs will be recalculated when used in a specific league
|
|
const scoringRules = {
|
|
pointsFor1st: 100,
|
|
pointsFor2nd: 70,
|
|
pointsFor3rd: 50,
|
|
pointsFor4th: 40,
|
|
pointsFor5th: 25,
|
|
pointsFor6th: 25,
|
|
pointsFor7th: 15,
|
|
pointsFor8th: 15,
|
|
};
|
|
|
|
try {
|
|
const result = await upsertParticipantEV({
|
|
participantId,
|
|
sportsSeasonId: params.id,
|
|
probabilities: {
|
|
probFirst,
|
|
probSecond,
|
|
probThird,
|
|
probFourth,
|
|
probFifth,
|
|
probSixth,
|
|
probSeventh,
|
|
probEighth,
|
|
},
|
|
scoringRules,
|
|
source,
|
|
});
|
|
|
|
return {
|
|
success: true,
|
|
participantId,
|
|
expectedValue: parseFloat(result.expectedValue),
|
|
};
|
|
} catch (error) {
|
|
console.error("Error saving probabilities:", error);
|
|
return {
|
|
error: error instanceof Error ? error.message : "Failed to save probabilities"
|
|
};
|
|
}
|
|
}
|