Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions latest.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"math"
"net/http"
"sort"
"strings"
"time"

"github.com/labstack/echo/v4"
Expand Down Expand Up @@ -140,7 +139,7 @@ func (a *app) fetchSessionResults(key string, at, now time.Time) ([]map[string]a
driver := fmt.Sprint(r.DriverNumber)
row := map[string]any{"position": nil, "driver": driver, "surname": driver, "team": "", "laps": r.Laps}
if i, ok := byNumber[r.DriverNumber]; ok {
row["driver"], row["surname"], row["team"] = drivers[i].Acronym, drivers[i].LastName, formatTeamName(strings.ReplaceAll(strings.ToLower(drivers[i].Team), " ", "_"))
row["driver"], row["surname"], row["team"] = drivers[i].Acronym, drivers[i].LastName, shortTeamName(drivers[i].Team)
}
if r.Position != nil {
row["position"] = *r.Position
Expand Down Expand Up @@ -209,3 +208,17 @@ func formatSessionDuration(seconds float64) string {
}
return fmt.Sprintf("%d:%02d.%03d", m, s, ms)
}

// shortTeamName maps OpenF1's team names onto the short names the rest of
// the API uses (from Ergast constructor ids), so tiles read consistently.
func shortTeamName(name string) string {
switch name {
case "Red Bull Racing":
return "Red Bull"
case "Racing Bulls":
return "RB"
case "Haas F1 Team":
return "Haas"
}
return name
}
15 changes: 14 additions & 1 deletion main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ func TestLatestSessionContract(t *testing.T) {
result := decode(t, request(t, newServer(testApp(t, mock)), "/f1/latest_session/"))
rows := result["results"].([]any)
first, second, third := rows[0].(map[string]any), rows[1].(map[string]any), rows[2].(map[string]any)
if result["session"] != "Free Practice 1" || result["raceName"] != "Test Grand Prix" || first["surname"] != "Verstappen" || first["team"] != "Red Bull Racing" || first["time"] != "1:17.738" || second["time"] != "+0.162" || third["time"] != "DNF" {
if result["session"] != "Free Practice 1" || result["raceName"] != "Test Grand Prix" || first["surname"] != "Verstappen" || first["team"] != "Red Bull" || first["time"] != "1:17.738" || second["time"] != "+0.162" || third["time"] != "DNF" {
t.Fatalf("unexpected response: %#v", result)
}
}
Expand Down Expand Up @@ -263,3 +263,16 @@ func TestNextRaceHandsOverWhenRaceEnds(t *testing.T) {
t.Fatalf("finished race should hand over: %#v", after)
}
}

func TestTeamNames(t *testing.T) {
for id, want := range map[string]string{"mclaren": "McLaren", "rb": "RB", "red_bull": "Red Bull", "aston_martin": "Aston Martin"} {
if got := formatTeamName(id); got != want {
t.Errorf("formatTeamName(%q) = %q, want %q", id, got, want)
}
}
for name, want := range map[string]string{"Red Bull Racing": "Red Bull", "Racing Bulls": "RB", "Haas F1 Team": "Haas", "McLaren": "McLaren"} {
if got := shortTeamName(name); got != want {
t.Errorf("shortTeamName(%q) = %q, want %q", name, got, want)
}
}
}
7 changes: 5 additions & 2 deletions standings.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,12 @@ func normalizeNationality(value string) string {
}
return ""
}

var teamNameOverrides = map[string]string{"rb": "RB", "mclaren": "McLaren"}

func formatTeamName(value string) string {
if value == "rb" {
return "RB"
if name, ok := teamNameOverrides[value]; ok {
return name
}
words := strings.Fields(strings.ReplaceAll(value, "_", " "))
for i := range words {
Expand Down
Loading