brackt/app/routes/leagues/$leagueId.sports-seasons.$sportsSeasonId.tsx
Chris Parsons 338979e0a8
Add FIFA World Cup 2026 support with group stage display and Monte Carlo simulator (#242)
* Add FIFA World Cup 2026 support with group stage display and Monte Carlo simulator, fixes #127

- New `groupStageMatches` table for recording group play results (W/D/L, scores, matchday, schedule)
- `computeGroupStandings()` model function: pts → GD → GF → name tiebreaker ordering
- `GroupStageStandings` component showing all 12 groups with standings table and manager column
- Admin bracket UI: group match score entry, per-group standings, "Recalculate Floors" action
- `WorldCupSimulator`: 50k Monte Carlo covering group stage + best-8 3rd-place + knockout + 3rd place game
  - Fuzzy name matching for national team Elo lookup (exact → substring → word-overlap), warns on miss
  - Partial group completion: completed matches replayed with real scores, remaining matches simulated
  - Elo priority: admin-entered sourceElo > futures odds converted to Elo > hardcoded national team ratings
- `fifa_48` bracket template: added Third Place Game round with `loserFeedsInto` on Semifinals
- Scoring rules: distinct 3rd/4th place for `fifa_48` (not averaged), QF losers share 5th–8th equally
- Floor scoring: SF participants guaranteed 4th (provisional), finalized after 3rd place game
- `recalculate-floors` admin action deletes and replays all results from scratch (fixes stale guard bug)
- Unique index on `(tournamentGroupId, participant1Id, participant2Id)` to prevent duplicate pairings
- Batch `findMatchesByGroupIds()` replacing N sequential queries in the sport season loader
- League home mini-standings now shows `actualPoints` (includes floor) instead of `totalPoints` only
- Elo ratings admin page supports World Cup (same bulk-import flow as snooker)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* Fix TypeScript errors: update GroupStandingData type to use findMatchesByGroupIds

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* Increase Node heap to 4GB for unit tests in CI to prevent OOM

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* Fix OOM in CI: make WorldCupSimulator simulation count configurable for tests

Tests now pass numSimulations=500 instead of the production default of 50,000.
Six simulator tests × 50k iterations each was exhausting the 4GB heap on GitHub
Actions runners. Also reduce simGroupMatch stat tests from 50k to 5k iterations.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

---------

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-29 10:27:47 -07:00

195 lines
7 KiB
TypeScript

import { Link } from "react-router";
import type { Route } from "./+types/$leagueId.sports-seasons.$sportsSeasonId";
import { loader } from "./$leagueId.sports-seasons.$sportsSeasonId.server";
import { SportSeasonDisplay } from "~/components/scoring/SportSeasonDisplay";
import { EventSchedule } from "~/components/sport-season/EventSchedule";
import { RegularSeasonStandings } from "~/components/sport-season/RegularSeasonStandings";
import { GroupStageStandings } from "~/components/sport-season/GroupStageStandings";
import { Button } from "~/components/ui/button";
import { Badge } from "~/components/ui/badge";
import { ArrowLeft, CheckCircle2, Clock, Zap } from "lucide-react";
export function meta({ data }: Route.MetaArgs): Route.MetaDescriptors {
return [{ title: `${data?.sportsSeason?.name ?? "Sport Season"}${data?.league?.name ?? "League"} - Brackt` }];
}
export { loader };
function getStatusBadge(status: string) {
switch (status) {
case "active":
return (
<Badge variant="outline" className="bg-emerald-500/15 text-emerald-400 border-emerald-500/30">
<Zap className="mr-1 h-3 w-3" />
Active
</Badge>
);
case "upcoming":
return (
<Badge variant="outline" className="bg-electric/15 text-electric border-electric/30">
<Clock className="mr-1 h-3 w-3" />
Upcoming
</Badge>
);
case "completed":
return (
<Badge variant="outline" className="bg-muted text-muted-foreground border-border">
<CheckCircle2 className="mr-1 h-3 w-3" />
Completed
</Badge>
);
default:
return null;
}
}
export default function SportSeasonDetail({
loaderData,
}: Route.ComponentProps) {
const {
league,
season,
sportsSeason,
scoringPattern,
playoffMatches,
playoffRounds,
preEliminatedParticipants,
participantPoints,
partialScoreParticipantIds,
seasonStandings,
qpStandings,
teamOwnerships,
userParticipantIds,
upcomingEvents,
recentEvents,
seasonIsFinalized,
regularSeasonStandings,
participantEvs,
groupStandings,
} = loaderData;
const hasBracket = playoffMatches && playoffMatches.length > 0;
const hasStandings = regularSeasonStandings && regularSeasonStandings.length > 0;
const showOtLosses = hasStandings && regularSeasonStandings.some((s) => s.otLosses !== null);
const simulatorType = sportsSeason.sport?.simulatorType;
const standingsDisplayMode =
simulatorType === "nhl_bracket" ? "nhl-divisions" :
simulatorType === "mlb_bracket" ? "mlb-divisions" :
"flat";
// NBA: 10 teams make the postseason (6 auto-qualify + 4 play-in)
// AFL: top 10 advance to the finals series (Wildcard + QF/EF + SF + PF + GF)
// NHL: handled by nhl-divisions mode (3 per div + 2 wild cards)
// MLB: handled by mlb-divisions mode (1 per div + 3 wild cards)
const playoffSpots =
simulatorType === "nba_bracket" || simulatorType === "afl_bracket" ? 10 : 8;
// Build ownership map for RegularSeasonStandings
const ownershipMap = Object.fromEntries(
teamOwnerships.map((o) => [
o.participantId,
{ teamName: o.teamName, ownerName: o.ownerName ?? "", teamId: o.teamId },
])
);
return (
<div className="container mx-auto py-8 px-4">
<div className="mb-6">
<Button variant="ghost" size="sm" asChild className="mb-4">
<Link to={`/leagues/${league.id}`}>
<ArrowLeft className="mr-2 h-4 w-4" />
Back to League
</Link>
</Button>
<div className="flex items-start justify-between gap-4 mb-4">
<div>
<h1 className="text-3xl font-bold mb-1">
{sportsSeason.sport.name}
</h1>
<p className="text-muted-foreground">
{sportsSeason.name} {league.name} {season.year} Season
</p>
</div>
{getStatusBadge(sportsSeason.status)}
</div>
</div>
{/* Regular season standings — show above bracket when no bracket exists yet */}
{hasStandings && !hasBracket && (
<div className="mb-6">
<RegularSeasonStandings
standings={regularSeasonStandings}
teamOwnerships={ownershipMap}
userParticipantIds={userParticipantIds}
showOtLosses={showOtLosses}
participantEvs={participantEvs}
displayMode={standingsDisplayMode as "flat" | "nhl-divisions" | "mlb-divisions"}
playoffSpots={playoffSpots}
/>
</div>
)}
{/* Event schedule - hidden for bracket sports (the bracket itself is the event) */}
{scoringPattern !== "playoff_bracket" &&
(upcomingEvents.length > 0 || recentEvents.length > 0) && (
<div className="mb-6">
<EventSchedule
upcomingEvents={upcomingEvents}
recentEvents={recentEvents}
/>
</div>
)}
{/* Group stage standings — shown for FIFA-style tournaments before/during group phase */}
{groupStandings.length > 0 && (
<div className="mb-6">
<GroupStageStandings groups={groupStandings} ownershipMap={ownershipMap} showEmpty={false} />
</div>
)}
{/* Hide bracket display when standings exist but no bracket matches have been set yet */}
{!(scoringPattern === "playoff_bracket" && hasStandings && !hasBracket) && <SportSeasonDisplay
scoringPattern={scoringPattern as "playoff_bracket" | "season_standings" | "qualifying_points"}
sportSeasonName={sportsSeason.name}
sportName={sportsSeason.sport.name}
playoffMatches={playoffMatches}
playoffRounds={playoffRounds}
preEliminatedParticipants={preEliminatedParticipants}
participantPoints={participantPoints}
partialScoreParticipantIds={partialScoreParticipantIds}
seasonStandings={seasonStandings}
seasonIsFinalized={seasonIsFinalized}
qpStandings={qpStandings}
qpIsFinalized={sportsSeason.qualifyingPointsFinalized || false}
totalMajors={sportsSeason.totalMajors}
majorsCompleted={sportsSeason.majorsCompleted || 0}
canFinalize={
(sportsSeason.majorsCompleted || 0) >=
(sportsSeason.totalMajors || 0) &&
!sportsSeason.qualifyingPointsFinalized
}
teamOwnerships={teamOwnerships}
userParticipantIds={userParticipantIds}
scoringRules={season}
showOwnership={true}
/>}
{/* Regular season standings — show below bracket once bracket exists */}
{hasStandings && hasBracket && (
<div className="mt-8">
<RegularSeasonStandings
standings={regularSeasonStandings}
teamOwnerships={ownershipMap}
userParticipantIds={userParticipantIds}
showOtLosses={showOtLosses}
participantEvs={participantEvs}
displayMode={standingsDisplayMode as "flat" | "nhl-divisions" | "mlb-divisions"}
playoffSpots={playoffSpots}
/>
</div>
)}
</div>
);
}