Skip to content

Fix leaderboard showing wrong order after the race ends (#309) - #312

Open
TemRevil wants to merge 1 commit into
IAmTomShaw:mainfrom
TemRevil:fix/leaderboard-final-order
Open

TemRevil wants to merge 1 commit into
IAmTomShaw:mainfrom
TemRevil:fix/leaderboard-final-order

Conversation

@TemRevil

Copy link
Copy Markdown

Fixes #309.

What was happening

The sidebar leaderboard is rebuilt every frame in race_replay.py by sorting drivers on a live progress metric: (lap - 1) * lap_length + projected_track_distance, largest first. That is a fine proxy for position while the cars are running, but the same sort keeps running after the checkered flag. At the finish it cannot be right:

  • post-race penalties change the classified order without changing on-track distance,
  • lapped cars are ordered by raw projected distance rather than by laps down,
  • once a car parks, its telemetry freezes, so the proxy gets noisy exactly when the order should be final.

I confirmed this against real data before writing anything. I ran the full 2024 Spanish GP through get_race_telemetry and looked at the last frame. The live order was:

HUL, SAI, HAM, OCO, VER, NOR, RUS, PIA, GAS, LEC, ...

The actual result was VER, NOR, HAM, RUS, LEC, SAI, ..., so the winner was being shown fifth and Hulkenberg was on top. That matches the report.

How I approached it

My first instinct was to just re-sort in the draw method when the race is over, but that method lives inside the arcade window, which I cannot open headlessly on this machine, so I could not have tested it. So I pulled the ordering decision out into a small module with no arcade or FastF1 imports, src/lib/leaderboard.py:

  • official_finishing_order(results) reads the classified order straight from session.results, which FastF1 already sorts by Position (that column reflects penalties, lapped cars and retirements, with retired drivers classified at the back).
  • order_leaderboard_codes(live_order, official_order, race_finished) returns the live order untouched during the race, and the official order once it is over. It never drops a driver: anyone present live but missing from the classification is appended.

The window derives the official order once from session.results at startup and applies it only on the frozen final frame (frame_index at the last frame). Mid-race ordering is byte-for-byte unchanged, so there is no risk to the normal replay.

Testing

  • tests/test_leaderboard.py (8 cases) covers ordering by Position regardless of row order, retirements and unclassified rows sorting to the back, the live/finished switch, and the "never drop a driver" behaviour. Runs offline with synthetic frames.
  • Verified official_finishing_order against real sessions: exact match on 2024 Spain, and on 2024 Australia the retirements (Hamilton, Verstappen) correctly land at the back.
  • Ran the full 2024 Spain pipeline end to end: the live final-frame order differed from the result (as above) and the fix reproduced the official classification exactly.

I could not drive the arcade window itself without a display, so the rendering side is covered by the unit tests plus the end-to-end ordering check rather than by clicking through the GUI.

For transparency: I paired with Claude on this one. It was not fully autonomous, but the investigation, the design decision to keep the ordering testable, and the verification against real sessions were done together rather than blindly generated.

The sidebar leaderboard is ordered every frame by a live on-track
progress proxy (lap plus projected track distance). That sort keeps
running after the checkered flag, where it no longer matches the
classified result: post-race penalties, lapped cars and retirements
change the finishing order without changing on-track progress, and the
telemetry proxy is noisy once cars have parked.

Add a GUI-free src/lib/leaderboard.py with two small helpers:
official_finishing_order(results) reads the classified order from
session.results, and order_leaderboard_codes(...) keeps the live order
during the race but switches to the official order once it is over,
without ever dropping a driver from the board.

The replay window derives the official order from session.results at
startup and applies it to the frozen final frame only, so mid-race
ordering is untouched. Keeping the policy out of the arcade window lets
it be unit tested headlessly.

Add tests/test_leaderboard.py covering position ordering, retirements,
unclassified rows, and the live/finished switch.
@TemRevil

TemRevil commented Nov 2, 2026

Copy link
Copy Markdown
Author

Friendly bump on this when you have a minute. It still applies cleanly to main as far as I can tell. If you'd like it split up or changed, tell me and I'll update it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Leaderboard shows incorrect final order after race ends

1 participant