Modern Apps git

← Modern-Apps.git

games: race standings rank by total progress, not the display-clamped lap

Commit
d5d819dc7a3fac6cd69c87d4ef8b3b40dfb0d85c
Author
Vayun Mathur <[email protected]>
When
2026-12-01 13:10 UTC (5 days ago)
Parents
0917a41
standings() clamped lap to RACE_LAPS and frac to 1.0
BEFORE sorting, discarding laps past the finish: seat a
(lap 4, s=10) vs b (lap 3, half track) — total progress
4.013 vs 3.5 laps — ranked b first, and winner() reads
standings().first(), so it would crown the kart a lap
behind. The tick itself orders by unclamped progress_key;
the HUD/winner path disagreed with it.

standings now sorts descending by progress_key (stable, so
ties keep seat order) and keeps the clamp for display only
(the lap-99-to-3 display test still passes). Regression
test pins this exact field plus the clamped display values
and winner(). Physics, items and the Kotlin placeholder
race were audited and cleared. Verified: cargo test 129
passed; :games:kart:testDevUnitTest 77 tests and lint BUILD
SUCCESSFUL.

Diff

games/kart/src/main/rust/src/race.rs
diff --git a/games/kart/src/main/rust/src/race.rs b/games/kart/src/main/rust/src/race.rsindex 0e18111826..5e463948fe 100644--- a/games/kart/src/main/rust/src/race.rs+++ b/games/kart/src/main/rust/src/race.rs@@ -238,22 +238,25 @@ impl Race {     }      /// Standings: (seat id, lap, 0..1 lap progress), leaders first.+    ///+    /// Ranking is by total progress ([`progress_key`]: laps completed plus+    /// the lap fraction, unclamped), the same key the tick orders by. The+    /// returned lap and fraction are clamped to `RACE_LAPS` / 1.0 for the+    /// HUD only: clamping before the sort would discard laps past the+    /// finish, so a kart a full lap beyond `RACE_LAPS` would rank behind a+    /// finisher halfway into that lap despite being ahead on track.     pub fn standings(&self) -> Vec<(String, u32, f32)> {-        let mut v: Vec<(String, u32, f32)> = self+        let mut v: Vec<(String, u32, f32, f32)> = self             .seats             .iter()             .map(|s| {                 let lap = s.kart.lap.min(RACE_LAPS);                 let frac = (s.kart.track_s / track::TRACK_LENGTH).clamp(0.0, 1.0);-                (s.id.clone(), lap, frac)+                (s.id.clone(), lap, frac, progress_key(&s.kart))             })             .collect();-        v.sort_by(|a, b| {-            (b.1, b.2)-                .partial_cmp(&(a.1, a.2))-                .unwrap_or(std::cmp::Ordering::Equal)-        });-        v+        v.sort_by(|a, b| b.3.partial_cmp(&a.3).unwrap_or(std::cmp::Ordering::Equal));+        v.into_iter().map(|(id, lap, frac, _key)| (id, lap, frac)).collect()     }      /// Winner once finished: the first across the line.
games/kart/src/main/rust/src/race/race_tests.rs
diff --git a/games/kart/src/main/rust/src/race/race_tests.rs b/games/kart/src/main/rust/src/race/race_tests.rsindex bee31a2f34..d78c3a0729 100644--- a/games/kart/src/main/rust/src/race/race_tests.rs+++ b/games/kart/src/main/rust/src/race/race_tests.rs@@ -114,6 +114,31 @@ fn standings_clamp_lap_and_progress() {     } } +#[test]+fn standings_rank_by_total_progress_beyond_race_laps() {+    // Display laps clamp to RACE_LAPS, but ranking must not: a kart a full+    // lap past the finish still leads a finisher halfway into that lap.+    // Total progress (see progress_key, which the tick orders by) is+    // 4 + 10/LEN for "a" against 3.5 for "b", so "a" is the leader even+    // though both display lap RACE_LAPS and "b" has the larger lap fraction.+    let mut race = Race::new(&["a".to_string(), "b".to_string()]);+    race.seats[0].kart.lap = RACE_LAPS + 1;+    race.seats[0].kart.track_s = 10.0;+    race.seats[1].kart.lap = RACE_LAPS;+    race.seats[1].kart.track_s = track::TRACK_LENGTH * 0.5;+    let st = race.standings();+    assert_eq!(st[0].0, "a", "total progress must rank first: {st:?}");+    assert_eq!(st[1].0, "b");+    // Display values stay clamped for the HUD.+    assert_eq!(st[0].1, RACE_LAPS);+    assert_eq!(st[1].1, RACE_LAPS);+    assert!((st[0].2 - 10.0 / track::TRACK_LENGTH).abs() < 1e-6);+    assert!((st[1].2 - 0.5).abs() < 1e-6);+    // The winner follows the same ranking once finished.+    race.finished = true;+    assert_eq!(race.winner().as_deref(), Some("a"));+}+ #[test] fn standings_ties_keep_seat_order() {     let mut race = Race::new(&["a".to_string(), "b".to_string()]);