import { Form } from "react-router"; import { Button } from "~/components/ui/button"; import { Card, CardContent, CardDescription, CardHeader, CardTitle, } from "~/components/ui/card"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from "~/components/ui/table"; import { Trophy, CheckCircle2 } from "lucide-react"; interface QPStanding { id: string; totalQualifyingPoints: string; eventsScored: number; finalRanking: number | null; participant: { id: string; name: string; }; } interface ScoringRules { pointsFor1st: number; pointsFor2nd: number; pointsFor3rd: number; pointsFor4th: number; pointsFor5th: number; pointsFor6th: number; pointsFor7th: number; pointsFor8th: number; } interface QualifyingPointsStandingsProps { standings: QPStanding[]; scoringRules: ScoringRules | null; isFinalized: boolean; totalMajors?: number | null; majorsCompleted?: number; canFinalize: boolean; // Whether all majors are complete } export function QualifyingPointsStandings({ standings, scoringRules, isFinalized, totalMajors, majorsCompleted = 0, canFinalize, }: QualifyingPointsStandingsProps) { // Calculate projected fantasy points for each standing (Q6) const calculateProjectedPoints = (currentRank: number): number => { if (!scoringRules || currentRank < 1 || currentRank > 8) return 0; const pointsMap: Record = { 1: scoringRules.pointsFor1st, 2: scoringRules.pointsFor2nd, 3: scoringRules.pointsFor3rd, 4: scoringRules.pointsFor4th, 5: scoringRules.pointsFor5th, 6: scoringRules.pointsFor6th, 7: scoringRules.pointsFor7th, 8: scoringRules.pointsFor8th, }; return pointsMap[currentRank] || 0; }; // Assign current ranks with tie handling const rankedStandings: Array = []; let previousRank = 0; let previousQP = -1; for (let index = 0; index < standings.length; index++) { const standing = standings[index]; const currentQP = parseFloat(standing.totalQualifyingPoints); // Check if this is a tie with the previous participant let currentRank: number; if (index > 0 && Math.abs(currentQP - previousQP) < 0.001) { // Same QP as previous = same rank currentRank = previousRank; } else { // Different QP = new rank (1-based position) currentRank = index + 1; } rankedStandings.push({ ...standing, currentRank, }); previousRank = currentRank; previousQP = currentQP; } return ( Qualifying Points Standings {isFinalized ? ( Finalized - Fantasy points have been assigned ) : ( <> Current standings based on {majorsCompleted} of {totalMajors || '?'} major tournaments completed. {scoringRules ? ( Projected fantasy points show what participants would earn if finalized now. ) : ( Projected points will be shown when linked to a fantasy season. )} )} {standings.length === 0 ? (

No qualifying points awarded yet. Complete a qualifying event to see standings.

) : ( <> Rank Participant Total QP Events {scoringRules && !isFinalized && ( Projected Points )} {rankedStandings.map((standing, idx) => { const projectedPoints = calculateProjectedPoints(standing.currentRank); const isTop8 = standing.currentRank <= 8; // Check if this is a tie (same rank as another participant) const isTied = rankedStandings.some((other, otherIdx) => otherIdx !== idx && other.currentRank === standing.currentRank ); return (
{standing.currentRank === 1 && !isTied && 🥇} {standing.currentRank === 2 && !isTied && 🥈} {standing.currentRank === 3 && !isTied && 🥉} {isTied && "T"} {standing.currentRank} {standing.currentRank === 1 ? "st" : standing.currentRank === 2 ? "nd" : standing.currentRank === 3 ? "rd" : "th"}
{standing.participant.name} {parseFloat(standing.totalQualifyingPoints).toFixed(2)} QP {standing.eventsScored} {scoringRules && !isFinalized && ( {isTop8 ? ( {projectedPoints} pts ) : ( 0 pts )} )}
); })}
{!isFinalized && canFinalize && (

Ready to finalize?

All {totalMajors} major tournaments are complete. Finalizing will:

  • Convert the top 8 participants to fantasy placements 1-8
  • Award fantasy points based on league scoring rules
  • Update all league standings
  • Lock the qualifying points (no further changes)
)} {!isFinalized && !canFinalize && (

Not ready to finalize yet. Complete all {totalMajors} major tournaments before finalizing qualifying points. ({majorsCompleted} of {totalMajors} completed)

)} )}
); }