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
+62 -5
View File
@@ -236,10 +236,54 @@ func (r *Runner) Run(ctx context.Context, opt Options) (Summary, error) {
s.PlansInserted++
}
if err := r.seedTrackCalendar(ctx); err != nil {
return s, fmt.Errorf("seed track calendar: %w", err)
}
s.Duration = time.Since(start)
return s, nil
}
func (r *Runner) seedTrackCalendar(ctx context.Context) error {
now := r.now
if len(r.trackIDs) == 0 {
return nil
}
tracksMap := make(map[string]bool)
for _, id := range r.trackIDs {
tracksMap[id] = true
}
schedule := []struct {
trackID string
startAt time.Time
endAt time.Time
}{
{"monaco", now.Add(-14 * 24 * time.Hour), now.Add(-7 * 24 * time.Hour)},
{"barcelona-catalunya", now.Add(-2 * 24 * time.Hour), now.Add(12 * 24 * time.Hour)},
{"austria-redbull-ring", now.Add(14 * 24 * time.Hour), now.Add(28 * 24 * time.Hour)},
{"great-britain-silverstone", now.Add(28 * 24 * time.Hour), now.Add(42 * 24 * time.Hour)},
{"belgium-spa", now.Add(42 * 24 * time.Hour), now.Add(56 * 24 * time.Hour)},
}
for _, s := range schedule {
if !tracksMap[s.trackID] {
continue
}
_, err := r.pg.Exec(ctx, `
INSERT INTO track_calendar (track_id, start_at, end_at)
VALUES ($1, $2, $3)
`, s.trackID, s.startAt, s.endAt)
if err != nil {
return fmt.Errorf("insert calendar entry: %w", err)
}
}
return nil
}
type planShape struct {
intervalS int
count int
@@ -251,18 +295,31 @@ type planShape struct {
// cascade), plus the plan and queue tables. Drivers and clans are
// kept (they're independent of races).
func (r *Runner) resetAll(ctx context.Context) error {
if _, err := r.pg.Exec(ctx,
`DELETE FROM races WHERE status IN ('finished','cancelled')`); err != nil {
if _, err := r.pg.Exec(ctx, `DELETE FROM race_queue`); err != nil {
return err
}
if _, err := r.pg.Exec(ctx,
`TRUNCATE TABLE race_plans, race_queue, drivers, clans CASCADE`); err != nil {
if _, err := r.pg.Exec(ctx, `DELETE FROM races`); err != nil {
return err
}
if _, err := r.pg.Exec(ctx, `DELETE FROM race_plans`); err != nil {
return err
}
if _, err := r.pg.Exec(ctx, `DELETE FROM track_calendar`); err != nil {
return err
}
if _, err := r.pg.Exec(ctx, `DELETE FROM drivers`); err != nil {
return err
}
if _, err := r.pg.Exec(ctx, `DELETE FROM clans`); err != nil {
return err
}
if _, err := r.pg.Exec(ctx, `ALTER SEQUENCE IF EXISTS track_calendar_id_seq RESTART WITH 1`); err != nil {
return err
}
for _, m := range r.lb.ListRaces() {
_ = r.lb.DeleteRace(m.ID)
}
if _, err := r.pg.Exec(ctx, `TRUNCATE TABLE lobby_drivers`); err != nil {
if _, err := r.pg.Exec(ctx, `DELETE FROM lobby_drivers`); err != nil {
return err
}
return nil