Add leaderboard for all tracks and track calendar

This commit is contained in:
2026-07-15 11:53:35 +04:00
parent 4a894a4399
commit a56841237d
20 changed files with 1394 additions and 25 deletions
+72
View File
@@ -740,6 +740,78 @@ func (s *Service) GetLeaderboard(ctx context.Context, trackID string, year int,
}, nil
}
// GetCalendarLeaderboard returns the leaderboard details of all scheduled tracks.
func (s *Service) GetCalendarLeaderboard(ctx context.Context, year int, currentDriverID string) ([]TrackCalendarLeaderboard, error) {
if year == 0 {
year = s.now().Year()
}
calendarInfo, err := s.pg.GetCalendarLeaderboardTracks(ctx)
if err != nil {
return nil, err
}
now := s.now()
var out []TrackCalendarLeaderboard
// Cache driver profile fallback if we need it
var fallbackProfile *LeaderboardEntry
if currentDriverID != "" {
fallbackProfile, err = s.pg.GetDriverLeaderboardProfile(ctx, currentDriverID)
if err != nil {
return nil, err
}
if fallbackProfile != nil {
fallbackProfile.Points = 0
fallbackProfile.Rank = 0
}
}
for _, cal := range calendarInfo {
status := "planned"
if now.After(cal.EndAt) {
status = "finished"
} else if (now.Equal(cal.StartAt) || now.After(cal.StartAt)) && now.Before(cal.EndAt) {
status = "active"
}
// Get top 3
podium, _, err := s.pg.GetTrackLeaderboard(ctx, cal.TrackID, year, 3, 0)
if err != nil {
return nil, err
}
if podium == nil {
podium = []LeaderboardEntry{}
}
// Get current driver
var curDriver *LeaderboardEntry
if currentDriverID != "" {
curDriver, err = s.pg.GetTrackLeaderboardDriver(ctx, cal.TrackID, year, currentDriverID)
if err != nil {
return nil, err
}
if curDriver == nil && fallbackProfile != nil {
// Clone the fallback profile so we don't share pointer/modify original
profileCopy := *fallbackProfile
curDriver = &profileCopy
}
}
out = append(out, TrackCalendarLeaderboard{
TrackID: cal.TrackID,
TrackName: cal.TrackName,
StartAt: cal.StartAt,
EndAt: cal.EndAt,
Status: status,
Podium: podium,
CurrentDriver: curDriver,
})
}
return out, nil
}
// ---------------------------------------------------------------------------
// Scheduler
// ---------------------------------------------------------------------------