mirror of
https://github.com/AmanTahiliani/box-box.git
synced 2026-08-07 19:56:18 -04:00
Add race control visuals and driver names in laps view
This commit is contained in:
@@ -557,12 +557,13 @@ func (s *Server) handleChampionshipDrivers(w http.ResponseWriter, r *http.Reques
|
||||
}
|
||||
|
||||
drivers, _ := s.client.GetDriversForSession(champ[0].SessionKey)
|
||||
driverMap := buildDriverMap(drivers)
|
||||
driverMap := buildDriverMapFirst(drivers)
|
||||
|
||||
enriched := make([]champDriverWithInfo, 0, len(champ))
|
||||
for _, c := range champ {
|
||||
e := champDriverWithInfo{ChampionshipDriver: c}
|
||||
if d, ok := driverMap[c.DriverNumber]; ok {
|
||||
d, ok := s.championshipDriverInfo(c.SessionKey, c.DriverNumber, driverMap)
|
||||
if ok {
|
||||
e.NameAcronym = d.NameAcronym
|
||||
e.FullName = d.FullName
|
||||
e.TeamName = d.TeamName
|
||||
@@ -573,6 +574,14 @@ func (s *Server) handleChampionshipDrivers(w http.ResponseWriter, r *http.Reques
|
||||
writeJSON(w, enriched)
|
||||
}
|
||||
|
||||
func (s *Server) championshipDriverInfo(sessionKey, driverNumber int, fallback map[int]models.Driver) (models.Driver, bool) {
|
||||
if d, err := s.client.GetDriver(sessionKey, driverNumber); err == nil && d != nil {
|
||||
return *d, true
|
||||
}
|
||||
d, ok := fallback[driverNumber]
|
||||
return d, ok
|
||||
}
|
||||
|
||||
// --- /api/v1/championship/teams ---
|
||||
|
||||
func (s *Server) handleChampionshipTeams(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -1026,6 +1035,16 @@ func buildDriverMap(drivers []models.Driver) map[int]models.Driver {
|
||||
return m
|
||||
}
|
||||
|
||||
func buildDriverMapFirst(drivers []models.Driver) map[int]models.Driver {
|
||||
m := make(map[int]models.Driver, len(drivers))
|
||||
for _, d := range drivers {
|
||||
if _, exists := m[d.DriverNumber]; !exists {
|
||||
m[d.DriverNumber] = d
|
||||
}
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
func enrichedResultsToAPI(results []query.EnrichedResult) []resultWithDriver {
|
||||
out := make([]resultWithDriver, 0, len(results))
|
||||
for _, res := range results {
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/AmanTahiliani/box-box/internal/api"
|
||||
"github.com/AmanTahiliani/box-box/internal/models"
|
||||
"github.com/AmanTahiliani/box-box/internal/query"
|
||||
"github.com/AmanTahiliani/box-box/internal/store"
|
||||
)
|
||||
@@ -151,6 +152,19 @@ func TestHandleRaceHubRequiresSessionKey(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildDriverMapFirstKeepsFirstDuplicateDriverNumber(t *testing.T) {
|
||||
drivers := []models.Driver{
|
||||
{DriverNumber: 4, NameAcronym: "NOR", TeamName: "McLaren"},
|
||||
{DriverNumber: 4, NameAcronym: "NOR", TeamName: "Red Bull Racing"},
|
||||
}
|
||||
|
||||
driverMap := buildDriverMapFirst(drivers)
|
||||
driver := driverMap[4]
|
||||
if driver.TeamName != "McLaren" {
|
||||
t.Fatalf("team = %q, want McLaren", driver.TeamName)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleSeasonsEmpty(t *testing.T) {
|
||||
srv := testServer(t, nil)
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/v1/seasons", nil)
|
||||
|
||||
Reference in New Issue
Block a user