add race time

This commit is contained in:
Scott Pruett 2022-05-22 16:38:20 -04:00
parent 9af050dde3
commit 9aaff38d96
5 changed files with 48 additions and 7 deletions

21
Cargo.lock generated
View File

@ -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"

View File

@ -25,3 +25,4 @@ reqwest = { version = "0.11", features = ["json"] }
img_hash = "3"
csv = "1"
time = { version = "0.3", features = ["formatting"] }

View File

@ -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<String, DebugOcrFrame>,
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);
}
}
{

View File

@ -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<SystemTime>,
pub laps: Vec<ParsedFrame>,
pub last_lap_record_time: Option<Instant>,

View File

@ -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()