feat: add normalized config tracking and improve command

- Add normalize_to_final_build() to map tune configs to final builds (323/233/332)
- Add normalized_config field to BestCarRecord
- Change CarCounts.by_rid key from String to (String, String) for per-config tracking
- Change CarCounts.guid_to_rid to HashMap<String, HashSet<(String, String)>>
- Extract owned tune configs from my_cars.json (engineMajor/weightMajor/chassisMajor)
- Add improve command: owned cars not yet at final build, sorted by track count
- Strip version prefixes (v2c_/v1c_) from config keys at parse time
- Update dedup check in update_best_car to consider normalized_config
- Add atomic_write helper for safe CSV output
- Delete completed plans/code-review-v2.md
- Apply rustfmt formatting across codebase
This commit is contained in:
Jan-Peter von Hunnius 2026-05-11 16:23:04 +02:00
parent 67a79f190a
commit bf21d3d1f6
25 changed files with 735 additions and 759 deletions

View file

@ -3,7 +3,7 @@
//! Contains the mutable analysis state (`AnalysisContext`) and the
//! aggregated per-car win counts (`CarCounts`) computed from it.
use std::collections::HashMap;
use std::collections::{HashMap, HashSet};
use std::sync::Arc;
use dashmap::DashMap;
@ -33,24 +33,29 @@ impl Default for AnalysisContext {
impl AnalysisContext {
/// Create a new, empty analysis context.
pub fn new() -> Self {
AnalysisContext {
best_cars: Arc::new(DashMap::new()),
}
AnalysisContext { best_cars: Arc::new(DashMap::new()) }
}
}
/// Aggregated per-car win counts across all track/RQ/condition combinations.
/// Computed once from `AnalysisContext` and shared across all output functions.
///
/// The composite key `(car_rid, normalized_config)` ensures that the same car
/// with different final builds (e.g., "323" vs "332") is tracked separately.
pub struct CarCounts {
/// Map from car_rid → (BestCarRecord, count of tracks where this car is best)
pub by_rid: HashMap<String, (BestCarRecord, usize)>,
/// Map from guid → car_rid (for O(1) ownership/wishlist lookups)
pub guid_to_rid: HashMap<String, String>,
/// Map from (car_rid, normalized_config) → (BestCarRecord, count of tracks where this car+config is best)
pub by_rid: HashMap<(String, String), (BestCarRecord, usize)>,
/// Map from guid → set of (car_rid, normalized_config) for O(1) ownership/wishlist lookups.
/// A guid may map to multiple entries if the same car is best with different configs.
pub guid_to_rid: HashMap<String, HashSet<(String, String)>>,
/// Map from guid → Car (for O(1) lookups of car metadata by GUID).
/// Built once in `compute_car_counts()` and reused by trash/wishlist output.
pub car_map: HashMap<String, Car>,
/// Full car list from the database (kept for any callers that need iteration).
pub car_list: Vec<Car>,
/// Actual tune config for each owned car (guid → config string from playerDeck).
/// Used by the improve command to determine which cars are not yet at a final build.
pub owned_configs: HashMap<String, String>,
}
/// Computes per-car win counts from the analysis context.
@ -58,39 +63,46 @@ pub struct CarCounts {
/// Returns a `CarCounts` struct that can be shared across all output functions.
/// This function cannot fail — it only performs `DashMap` iteration and
/// `HashMap` inserts.
pub fn compute_car_counts(ctx: &AnalysisContext, car_list: Vec<Car>) -> CarCounts {
let mut by_rid: HashMap<String, (BestCarRecord, usize)> = HashMap::new();
let mut guid_to_rid: HashMap<String, String> = HashMap::new();
///
/// Groups by `(car_rid, normalized_config)` so that the same car with different
/// final builds is counted as separate entries.
pub fn compute_car_counts(
ctx: &AnalysisContext,
car_list: Vec<Car>,
owned_configs: HashMap<String, String>,
) -> CarCounts {
let mut by_rid: HashMap<(String, String), (BestCarRecord, usize)> = HashMap::new();
let mut guid_to_rid: HashMap<String, HashSet<(String, String)>> = HashMap::new();
for entry in ctx.best_cars.iter() {
for best_car in entry.value() {
let rid_entry = by_rid
.entry(best_car.car_rid.clone())
.or_insert_with(|| (best_car.clone(), 0usize));
let key = (best_car.car_rid.clone(), best_car.normalized_config.clone());
let rid_entry = by_rid.entry(key.clone()).or_insert_with(|| (best_car.clone(), 0usize));
rid_entry.1 += 1;
// Keep the record with the best (lowest) time for this car.
// Keep the record with the best (lowest) time for this car+config.
if best_car.time < rid_entry.0.time {
rid_entry.0 = best_car.clone();
}
// HashMap iteration order is non-deterministic, but car_rid and guid
// have a 1:1 relationship in the data model, so any mapping is correct.
guid_to_rid.entry(best_car.guid.clone())
.or_insert_with(|| best_car.car_rid.clone());
// Track all (rid, config) pairs for each guid.
guid_to_rid.entry(best_car.guid.clone()).or_default().insert(key);
}
}
// Build guid → Car lookup once, reused by trash/wishlist output functions.
let car_map: HashMap<String, Car> = car_list.iter()
.map(|c| (c.guid.clone(), c.clone()))
.collect();
let car_map: HashMap<String, Car> =
car_list.iter().map(|c| (c.guid.clone(), c.clone())).collect();
CarCounts { by_rid, guid_to_rid, car_map, car_list }
CarCounts { by_rid, guid_to_rid, car_map, car_list, owned_configs }
}
#[cfg(test)]
mod tests {
use super::*;
fn empty_owned_configs() -> HashMap<String, String> {
HashMap::new()
}
#[test]
fn test_compute_car_counts_basic() {
let ctx = AnalysisContext::new();
@ -101,6 +113,7 @@ mod tests {
year: 2020,
rq: 85,
config: "111".to_string(),
normalized_config: "332".to_string(),
time: 100.0,
owned: true,
prize: false,
@ -108,14 +121,10 @@ mod tests {
};
// Manually insert into best_cars
ctx.best_cars.insert(
("Laguna Seca".to_string(), 85, "Dry".to_string()),
vec![record.clone()],
);
ctx.best_cars.insert(
("Monaco Streets".to_string(), 85, "Dry".to_string()),
vec![record.clone()],
);
ctx.best_cars
.insert(("Laguna Seca".to_string(), 85, "Dry".to_string()), vec![record.clone()]);
ctx.best_cars
.insert(("Monaco Streets".to_string(), 85, "Dry".to_string()), vec![record.clone()]);
let car_list = vec![Car {
rid: "Test_Car".to_string(),
@ -127,16 +136,19 @@ mod tests {
guid: "guid_test".to_string(),
}];
let counts = compute_car_counts(&ctx, car_list);
let my_data = empty_owned_configs();
let counts = compute_car_counts(&ctx, car_list, my_data);
// Should have exactly 1 entry in by_rid
assert_eq!(counts.by_rid.len(), 1);
let (rec, count) = counts.by_rid.get("Test_Car").unwrap();
let (rec, count) = counts.by_rid.get(&("Test_Car".to_string(), "332".to_string())).unwrap();
assert_eq!(count, &2); // best on 2 tracks
assert_eq!(rec.car_rid, "Test_Car");
// guid_to_rid should map guid → rid
assert_eq!(counts.guid_to_rid.get("guid_test"), Some(&"Test_Car".to_string()));
// guid_to_rid should map guid → set of (rid, config)
let guid_entries = counts.guid_to_rid.get("guid_test").unwrap();
assert_eq!(guid_entries.len(), 1);
assert!(guid_entries.contains(&("Test_Car".to_string(), "332".to_string())));
// car_map should have the car
assert!(counts.car_map.contains_key("guid_test"));
@ -154,6 +166,7 @@ mod tests {
year: 2020,
rq: 85,
config: "111".to_string(),
normalized_config: "332".to_string(),
time: 90.0,
owned: false,
prize: false,
@ -166,24 +179,27 @@ mod tests {
year: 2020,
rq: 85,
config: "233".to_string(),
normalized_config: "233".to_string(),
time: 110.0,
owned: false,
prize: false,
guid: "guid_a".to_string(),
};
ctx.best_cars.insert(
("Track1".to_string(), 85, "Dry".to_string()),
vec![record_fast],
);
ctx.best_cars.insert(
("Track2".to_string(), 85, "Dry".to_string()),
vec![record_slow],
);
ctx.best_cars.insert(("Track1".to_string(), 85, "Dry".to_string()), vec![record_fast]);
ctx.best_cars.insert(("Track2".to_string(), 85, "Dry".to_string()), vec![record_slow]);
let counts = compute_car_counts(&ctx, vec![]);
let (rec, count) = counts.by_rid.get("Car_A").unwrap();
assert_eq!(*count, 2);
assert_eq!(rec.time, 90.0); // Should keep the faster time
let my_data = empty_owned_configs();
let counts = compute_car_counts(&ctx, vec![], my_data);
// Different normalized configs → separate entries
assert_eq!(counts.by_rid.len(), 2);
let (rec_fast, count_fast) =
counts.by_rid.get(&("Car_A".to_string(), "332".to_string())).unwrap();
assert_eq!(*count_fast, 1);
assert_eq!(rec_fast.time, 90.0);
let (rec_slow, count_slow) =
counts.by_rid.get(&("Car_A".to_string(), "233".to_string())).unwrap();
assert_eq!(*count_slow, 1);
assert_eq!(rec_slow.time, 110.0);
}
}