fix(championship): drop cancelled rounds and declare a clinched title (#110)

* fix(championship): drop cancelled rounds and declare a clinched title

The championship hub counted cancelled meetings (2026 Bahrain/Saudi) as
rounds, reporting 25 total / 6 left instead of 23 / 4. Title math then used
6 x 25 = 150 pts as the ceiling, so a 119-pt gap still looked "alive" after
Antonelli had mathematically clinched the title.

- Skip cancelled meetings and cancelled Race/Sprint sessions in both the
  local (query) and OpenF1 aggregation paths.
- Track pending sprints and return sprints_left + max_points_left
  (25/race + 8/sprint) so title math is sprint-aware.
- Frontend: use max_points_left for alive/out, gap annotations and the
  simulator; show a CHAMPION state, "has clinched" title math, and a
  Champion stat tile once P2 cannot catch the leader.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(championship): resolve ties on countback in title math

Review feedback:
- Local standings broke points ties by driver number, so a season ending
  level could crown the wrong driver. Order ties by FIA countback (most GP
  wins, then P2s, ...) before falling back to driver number.
- An exact-gap lead was always treated as open. It is a clinch when the
  leader still wins the countback even if the rival takes every remaining
  GP. Every rival, not just P2, is now checked.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Aman TahilianiandClaude Opus 5.5 authored and GitHub committed 2026-11-01 18:40:37 -08:00
1 parent 21b29bec6a
commit 09bbccff93
12 files changed
+442 -36

No files matched your search

+56 -2
View File
@@ -15,6 +15,11 @@ type ChampionshipRace struct {
RaceSessionKey int
Results []models.SessionResult
Grid []models.StartingGrid
// HasSprint reports whether the meeting has a (non-cancelled) Sprint
// session; SprintDone whether that sprint already has results. Together
// they let the hub count the sprint points still available.
HasSprint bool
SprintDone bool
}
// ChampionshipInputs are local-first inputs for the championship hub aggregation.
@@ -49,11 +54,19 @@ func (s *Service) GetChampionshipInputs(year int) (ChampionshipInputs, error) {
}
pointsByDriver := map[int]float64{}
// Grand Prix finishing-position counts per driver, for countback.
finishesByDriver := map[int]map[int]int{}
pointsByTeam := map[string]float64{}
latestSessionKey := 0
latestMeetingKey := 0
for _, meeting := range meetings {
// Cancelled meetings (e.g. 2026 Bahrain/Saudi) are not championship
// rounds: counting them inflates total/remaining rounds and the
// title-math "max points left" bound.
if meeting.IsCancelled {
continue
}
sessions, err := s.ListSessionsByMeeting(int(meeting.MeetingKey))
if err != nil {
return ChampionshipInputs{}, err
@@ -62,6 +75,9 @@ func (s *Service) GetChampionshipInputs(year int) (ChampionshipInputs, error) {
raceKey := 0
sprintKey := 0
for _, sess := range sessions {
if sess.IsCancelled {
continue
}
if strings.EqualFold(sess.SessionName, "Race") {
raceKey = sess.SessionKey
} else if strings.EqualFold(sess.SessionName, "Sprint") {
@@ -98,6 +114,14 @@ func (s *Service) GetChampionshipInputs(year int) (ChampionshipInputs, error) {
for _, entry := range grid {
race.Grid = append(race.Grid, gridToModel(entry))
}
if sprintKey != 0 {
race.HasSprint = true
sprintResults, err := s.store.ListSessionResults(sprintKey)
if err != nil {
return ChampionshipInputs{}, err
}
race.SprintDone = len(sprintResults) > 0
}
inputs.Races = append(inputs.Races, race)
raceResults = race.Results
@@ -113,6 +137,12 @@ func (s *Service) GetChampionshipInputs(year int) (ChampionshipInputs, error) {
}
for _, result := range results {
pointsByDriver[result.DriverNumber] += result.Points
if result.Position > 0 {
if finishesByDriver[result.DriverNumber] == nil {
finishesByDriver[result.DriverNumber] = map[int]int{}
}
finishesByDriver[result.DriverNumber][result.Position]++
}
team := inputs.DriverMap[result.DriverNumber].TeamName
if team != "" {
pointsByTeam[team] += result.Points
@@ -157,7 +187,7 @@ func (s *Service) GetChampionshipInputs(year int) (ChampionshipInputs, error) {
_ = raceGrid
}
inputs.Champ = derivedDriverStandings(pointsByDriver, latestMeetingKey, latestSessionKey)
inputs.Champ = derivedDriverStandings(pointsByDriver, finishesByDriver, latestMeetingKey, latestSessionKey)
inputs.Teams = derivedTeamStandings(pointsByTeam, latestMeetingKey, latestSessionKey)
return inputs, nil
}
@@ -182,7 +212,10 @@ func (s *Service) driversForSession(sessionKey int) ([]models.Driver, error) {
return drivers, nil
}
func derivedDriverStandings(points map[int]float64, meetingKey, sessionKey int) []models.ChampionshipDriver {
// derivedDriverStandings orders drivers by points, breaking ties with the
// FIA countback (most Grand Prix wins, then most P2s, and so on), then by
// driver number for a stable order when countback cannot separate them.
func derivedDriverStandings(points map[int]float64, finishes map[int]map[int]int, meetingKey, sessionKey int) []models.ChampionshipDriver {
drivers := make([]models.ChampionshipDriver, 0, len(points))
for num, pts := range points {
drivers = append(drivers, models.ChampionshipDriver{
@@ -194,6 +227,9 @@ func derivedDriverStandings(points map[int]float64, meetingKey, sessionKey int)
}
sort.Slice(drivers, func(i, j int) bool {
if drivers[i].PointsCurrent == drivers[j].PointsCurrent {
if c := countbackCompare(finishes[drivers[i].DriverNumber], finishes[drivers[j].DriverNumber]); c != 0 {
return c > 0
}
return drivers[i].DriverNumber < drivers[j].DriverNumber
}
return drivers[i].PointsCurrent > drivers[j].PointsCurrent
@@ -204,6 +240,24 @@ func derivedDriverStandings(points map[int]float64, meetingKey, sessionKey int)
return drivers
}
// countbackCompare returns >0 if a beats b on countback, <0 if b beats a and
// 0 if their finishing records are identical.
func countbackCompare(a, b map[int]int) int {
maxPos := 0
for p := range a {
maxPos = max(maxPos, p)
}
for p := range b {
maxPos = max(maxPos, p)
}
for p := 1; p <= maxPos; p++ {
if a[p] != b[p] {
return a[p] - b[p]
}
}
return 0
}
func derivedTeamStandings(points map[string]float64, meetingKey, sessionKey int) []models.ChampionshipTeam {
teams := make([]models.ChampionshipTeam, 0, len(points))
for name, pts := range points {