mirror of
https://github.com/AmanTahiliani/box-box.git
synced 2026-08-07 19:56:18 -04:00
Compare commits
5 Commits
f4f5071e26
...
be7eb3c034
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
be7eb3c034 | ||
|
|
71523641ad | ||
|
|
e51cabba62 | ||
|
|
fc6fc36d8f | ||
|
|
16d82afad5 |
@@ -26,6 +26,15 @@ type ChampionshipInputs struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetChampionshipInputs derives championship hub inputs from ingested season data.
|
// GetChampionshipInputs derives championship hub inputs from ingested season data.
|
||||||
|
//
|
||||||
|
// 2026 note: Bahrain (meeting 1282 / session 11261) and Saudi Arabia (1283 / 11269)
|
||||||
|
// are cancelled due to Force Majeure (regional conflict) — OpenF1 returns no results
|
||||||
|
// for those sessions. This is intentional, which is why 2026 shows Round 9/24 after
|
||||||
|
// Silverstone instead of Round 11.
|
||||||
|
//
|
||||||
|
// Points: F1 Sprint points (SessionName == "Sprint") must be included alongside Race
|
||||||
|
// points. Both appear as SessionType "Race" in OpenF1 /api/v1/sessions, so we match
|
||||||
|
// on SessionName. Fix for #57.
|
||||||
func (s *Service) GetChampionshipInputs(year int) (ChampionshipInputs, error) {
|
func (s *Service) GetChampionshipInputs(year int) (ChampionshipInputs, error) {
|
||||||
meetings, err := s.ListMeetingsByYear(year)
|
meetings, err := s.ListMeetingsByYear(year)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -51,16 +60,24 @@ func (s *Service) GetChampionshipInputs(year int) (ChampionshipInputs, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
raceKey := 0
|
raceKey := 0
|
||||||
|
sprintKey := 0
|
||||||
for _, sess := range sessions {
|
for _, sess := range sessions {
|
||||||
if strings.EqualFold(sess.SessionName, "Race") {
|
if strings.EqualFold(sess.SessionName, "Race") {
|
||||||
raceKey = sess.SessionKey
|
raceKey = sess.SessionKey
|
||||||
break
|
} else if strings.EqualFold(sess.SessionName, "Sprint") {
|
||||||
|
sprintKey = sess.SessionKey
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if raceKey == 0 {
|
if raceKey == 0 && sprintKey == 0 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Always build Races slice from the Race session only (for UI),
|
||||||
|
// but include sprint points for standings.
|
||||||
|
var raceResults []models.SessionResult
|
||||||
|
var raceGrid []models.StartingGrid
|
||||||
|
|
||||||
|
if raceKey != 0 {
|
||||||
results, err := s.store.ListSessionResults(raceKey)
|
results, err := s.store.ListSessionResults(raceKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ChampionshipInputs{}, err
|
return ChampionshipInputs{}, err
|
||||||
@@ -69,7 +86,6 @@ func (s *Service) GetChampionshipInputs(year int) (ChampionshipInputs, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return ChampionshipInputs{}, err
|
return ChampionshipInputs{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
race := ChampionshipRace{
|
race := ChampionshipRace{
|
||||||
Meeting: meeting,
|
Meeting: meeting,
|
||||||
RaceSessionKey: raceKey,
|
RaceSessionKey: raceKey,
|
||||||
@@ -83,11 +99,9 @@ func (s *Service) GetChampionshipInputs(year int) (ChampionshipInputs, error) {
|
|||||||
race.Grid = append(race.Grid, gridToModel(entry))
|
race.Grid = append(race.Grid, gridToModel(entry))
|
||||||
}
|
}
|
||||||
inputs.Races = append(inputs.Races, race)
|
inputs.Races = append(inputs.Races, race)
|
||||||
|
raceResults = race.Results
|
||||||
|
|
||||||
if len(results) == 0 {
|
if len(results) > 0 {
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
latestSessionKey = raceKey
|
latestSessionKey = raceKey
|
||||||
latestMeetingKey = int(meeting.MeetingKey)
|
latestMeetingKey = int(meeting.MeetingKey)
|
||||||
drivers, err := s.driversForSession(raceKey)
|
drivers, err := s.driversForSession(raceKey)
|
||||||
@@ -97,7 +111,6 @@ func (s *Service) GetChampionshipInputs(year int) (ChampionshipInputs, error) {
|
|||||||
for _, d := range drivers {
|
for _, d := range drivers {
|
||||||
inputs.DriverMap[d.DriverNumber] = d
|
inputs.DriverMap[d.DriverNumber] = d
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, result := range results {
|
for _, result := range results {
|
||||||
pointsByDriver[result.DriverNumber] += result.Points
|
pointsByDriver[result.DriverNumber] += result.Points
|
||||||
team := inputs.DriverMap[result.DriverNumber].TeamName
|
team := inputs.DriverMap[result.DriverNumber].TeamName
|
||||||
@@ -106,6 +119,43 @@ func (s *Service) GetChampionshipInputs(year int) (ChampionshipInputs, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Include sprint points if present.
|
||||||
|
// Note: 2026 has 4 sprints up to British GP (Chinese GP 11240, Miami 11275,
|
||||||
|
// Canadian 11286, British 11321) totaling e.g. ANT 21 = 4+3+6+8, RUS 26.
|
||||||
|
if sprintKey != 0 {
|
||||||
|
sprintResults, err := s.store.ListSessionResults(sprintKey)
|
||||||
|
if err != nil {
|
||||||
|
return ChampionshipInputs{}, err
|
||||||
|
}
|
||||||
|
if len(sprintResults) > 0 {
|
||||||
|
// Ensure driver map includes sprint-only drivers if any.
|
||||||
|
sprintDrivers, err := s.driversForSession(sprintKey)
|
||||||
|
if err == nil {
|
||||||
|
for _, d := range sprintDrivers {
|
||||||
|
if _, ok := inputs.DriverMap[d.DriverNumber]; !ok {
|
||||||
|
inputs.DriverMap[d.DriverNumber] = d
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, result := range sprintResults {
|
||||||
|
pointsByDriver[result.DriverNumber] += result.Points
|
||||||
|
team := inputs.DriverMap[result.DriverNumber].TeamName
|
||||||
|
if team != "" {
|
||||||
|
pointsByTeam[team] += result.Points
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// If this meeting had no race results (edge), still track latest.
|
||||||
|
if len(raceResults) == 0 {
|
||||||
|
latestSessionKey = sprintKey
|
||||||
|
latestMeetingKey = int(meeting.MeetingKey)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ = raceResults
|
||||||
|
_ = raceGrid
|
||||||
|
}
|
||||||
|
|
||||||
inputs.Champ = derivedDriverStandings(pointsByDriver, latestMeetingKey, latestSessionKey)
|
inputs.Champ = derivedDriverStandings(pointsByDriver, latestMeetingKey, latestSessionKey)
|
||||||
inputs.Teams = derivedTeamStandings(pointsByTeam, latestMeetingKey, latestSessionKey)
|
inputs.Teams = derivedTeamStandings(pointsByTeam, latestMeetingKey, latestSessionKey)
|
||||||
|
|||||||
@@ -375,3 +375,56 @@ func TestGetWeekendWithSessions(t *testing.T) {
|
|||||||
t.Fatalf("first session drivers = %+v, want available", weekend.Sessions[0].Datasets["drivers"])
|
t.Fatalf("first session drivers = %+v, want available", weekend.Sessions[0].Datasets["drivers"])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGetChampionshipInputsIncludesSprintPoints(t *testing.T) {
|
||||||
|
// Regression for #57: Race-only aggregation dropped Sprint points.
|
||||||
|
// Setup: same meeting 1229 has Race (9472) 25pts + Sprint (9473) 8pts => total 33.
|
||||||
|
svc := openTestService(t)
|
||||||
|
seedRaceHubData(t, svc.store)
|
||||||
|
|
||||||
|
// Add sprint session for same meeting
|
||||||
|
if err := svc.store.UpsertSession(store.Session{
|
||||||
|
SessionKey: 9473,
|
||||||
|
MeetingKey: 1229,
|
||||||
|
SessionName: "Sprint",
|
||||||
|
SessionType: "Race", // OpenF1 uses SessionType Race even for Sprint
|
||||||
|
DateStart: "2025-05-24T13:00:00+00:00",
|
||||||
|
}); err != nil {
|
||||||
|
t.Fatalf("UpsertSession sprint error = %v", err)
|
||||||
|
}
|
||||||
|
if err := svc.store.UpsertSessionDriver(store.SessionDriver{
|
||||||
|
SessionKey: 9473, DriverNumber: 1, MeetingKey: 1229, TeamName: "Red Bull Racing",
|
||||||
|
}); err != nil {
|
||||||
|
t.Fatalf("UpsertSessionDriver sprint error = %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Race points 25
|
||||||
|
if err := svc.store.UpsertSessionResult(store.SessionResult{
|
||||||
|
SessionKey: 9472, DriverNumber: 1, MeetingKey: 1229, Position: 1, Points: 25,
|
||||||
|
}); err != nil {
|
||||||
|
t.Fatalf("UpsertSessionResult race error = %v", err)
|
||||||
|
}
|
||||||
|
// Sprint points 8
|
||||||
|
if err := svc.store.UpsertSessionResult(store.SessionResult{
|
||||||
|
SessionKey: 9473, DriverNumber: 1, MeetingKey: 1229, Position: 1, Points: 8,
|
||||||
|
}); err != nil {
|
||||||
|
t.Fatalf("UpsertSessionResult sprint error = %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
inputs, err := svc.GetChampionshipInputs(2025)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("GetChampionshipInputs() error = %v", err)
|
||||||
|
}
|
||||||
|
if len(inputs.Races) != 1 {
|
||||||
|
t.Fatalf("Races len = %d, want 1 race entry", len(inputs.Races))
|
||||||
|
}
|
||||||
|
if len(inputs.Champ) != 1 {
|
||||||
|
t.Fatalf("Champ len = %d, want 1", len(inputs.Champ))
|
||||||
|
}
|
||||||
|
if inputs.Champ[0].PointsCurrent != 33 {
|
||||||
|
t.Fatalf("PointsCurrent = %v, want 33 (25 race + 8 sprint)", inputs.Champ[0].PointsCurrent)
|
||||||
|
}
|
||||||
|
if len(inputs.Teams) != 1 || inputs.Teams[0].PointsCurrent != 33 {
|
||||||
|
t.Fatalf("Teams = %+v, want Red Bull 33", inputs.Teams)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user