diff --git a/Cargo.lock b/Cargo.lock index b8715ff..ebf5481 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1550,6 +1550,15 @@ dependencies = [ "syn", ] +[[package]] +name = "num_threads" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2819ce041d2ee131036f4fc9d6ae7ae125a3a40e97ba64d04fe799ad9dabbb44" +dependencies = [ + "libc", +] + [[package]] name = "objc" version = "0.2.7" @@ -2187,6 +2196,7 @@ dependencies = [ "scrap", "serde", "serde_json", + "time", "tokio", ] @@ -2255,6 +2265,17 @@ dependencies = [ "weezl", ] +[[package]] +name = "time" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c2702e08a7a860f005826c6815dcac101b19b5eb330c27fe4a5928fec1d20ddd" +dependencies = [ + "itoa 1.0.2", + "libc", + "num_threads", +] + [[package]] name = "tinyvec" version = "1.6.0" diff --git a/Cargo.toml b/Cargo.toml index 6cbb633..bcca88e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,4 +24,5 @@ reqwest = { version = "0.11", features = ["json"] } img_hash = "3" -csv = "1" \ No newline at end of file +csv = "1" +time = { version = "0.3", features = ["formatting"] } \ No newline at end of file diff --git a/src/control_loop.rs b/src/control_loop.rs index 3755094..dbd8865 100644 --- a/src/control_loop.rs +++ b/src/control_loop.rs @@ -1,7 +1,7 @@ use std::{ collections::HashMap, thread, - time::{Duration, Instant}, + time::{Duration, Instant, SystemTime}, }; use anyhow::Result; @@ -74,6 +74,7 @@ fn handle_new_frame(state: &mut AppState, frame: ParsedFrame, image: &RgbImage) ) .expect("failed to save screenshot"), ); + race.race_time = Some(SystemTime::now()); state.current_race = Some(race); } } else { @@ -93,6 +94,15 @@ fn handle_new_frame(state: &mut AppState, frame: ParsedFrame, image: &RgbImage) } if let Some(race) = state.current_race.as_mut() { + if let Some(prev_lap) = race.laps.last() { + if prev_lap.lap == merged.lap { + // Could be final lap, bump by one + merged.lap = prev_lap.lap.map(|l| l + 1); + } + } + if merged.lap.is_none() { + merged.lap = Some(race.laps.len() + 1); + } race.laps.push(merged); race.last_lap_record_time = Some(Instant::now()); } @@ -108,7 +118,6 @@ fn add_saved_frame( saved_frames: &mut HashMap, frame: &RgbImage, region: &Region, - config: &Config, ) { let extracted = extract_and_filter(frame, region); let retained = @@ -141,10 +150,10 @@ fn run_loop_once(capturer: &mut Capturer, state: &SharedAppState) -> Result<()> if state.lock().unwrap().debug_frames { for region in &config.ocr_regions { - add_saved_frame(&mut saved_frames, &frame, region, config.as_ref()); + add_saved_frame(&mut saved_frames, &frame, region); } if let Some(track_region) = &config.track_region { - add_saved_frame(&mut saved_frames, &frame, track_region, config.as_ref()); + add_saved_frame(&mut saved_frames, &frame, track_region); } } { diff --git a/src/state.rs b/src/state.rs index 2cdd0eb..4c505ef 100644 --- a/src/state.rs +++ b/src/state.rs @@ -1,4 +1,4 @@ -use std::{sync::{Arc, Mutex, RwLock}, time::{Duration, Instant}, collections::{HashMap, VecDeque}}; +use std::{sync::{Arc, Mutex, RwLock}, time::{Duration, Instant, SystemTime}, collections::{HashMap, VecDeque}}; use egui_extras::RetainedImage; use image::RgbImage; @@ -59,6 +59,7 @@ impl ParsedFrame { #[derive(Default)] pub struct RaceState { + pub race_time: Option, pub laps: Vec, pub last_lap_record_time: Option, diff --git a/src/stats_writer.rs b/src/stats_writer.rs index 3b663b2..c78dc22 100644 --- a/src/stats_writer.rs +++ b/src/stats_writer.rs @@ -1,13 +1,18 @@ use std::{ io::BufWriter, - time::{Duration, Instant}, + time::{Duration, Instant, SystemTime}, }; use crate::state::RaceState; use anyhow::Result; +use time::{OffsetDateTime, format_description}; pub fn export_race_stats(race_stats: &mut RaceState) -> Result<()> { + let race_time = race_stats.race_time.unwrap_or_else(|| SystemTime::now()); + let race_time: OffsetDateTime = race_time.into(); + let race_time: String = race_time.format(&format_description::parse("[year]-[month]-[day]-[hour]:[minute]")?)?; + let file = std::fs::OpenOptions::new() .create(true) .append(true) @@ -17,8 +22,12 @@ pub fn export_race_stats(race_stats: &mut RaceState) -> Result<()> { for lap in &race_stats.laps { csv_writer.write_record(vec![ + race_time.clone(), race_stats.track.clone(), race_stats.car.clone(), + lap.lap + .map(|x| x.to_string()) + .unwrap_or_else(|| "?".to_owned()), format!( "{:.3}", lap.lap_time.unwrap_or(Duration::from_secs(0)).as_secs_f64()