reduce int sizing

This commit is contained in:
Scott Pruett 2022-06-11 14:07:59 -04:00
parent 0dee7fdd71
commit 86a1df04ad
4 changed files with 32 additions and 32 deletions

View File

@ -158,8 +158,8 @@ fn handle_new_frame(state: &mut AppState, lap_state: LapState, image: &RgbImage)
merged.lap = prev_lap.lap.map(|l| l + 1); merged.lap = prev_lap.lap.map(|l| l + 1);
} }
} }
if merged.lap.is_none() { if merged.lap.is_none() || merged.total_laps.is_none() {
merged.lap = Some(race.laps.len() + 1); merged.lap = Some((race.laps.len() + 1) as u16);
} }
crate::tts::lap_summary(&state.tts, state.config.as_ref(), &merged, race); crate::tts::lap_summary(&state.tts, state.config.as_ref(), &merged, race);

View File

@ -78,11 +78,11 @@ fn main() -> anyhow::Result<()> {
); );
} }
fn show_optional_usize_with_diff( fn show_optional_u8_with_diff(
ui: &mut Ui, ui: &mut Ui,
color: Color32, color: Color32,
old: &Option<usize>, old: &Option<u8>,
v: &Option<usize>, v: &Option<u8>,
) { ) {
let diff_string = match (old, v) { let diff_string = match (old, v) {
(Some(old), Some(v)) => { (Some(old), Some(v)) => {
@ -217,8 +217,8 @@ fn show_race_state(
let fastest_lap = race.fastest_lap(); let fastest_lap = race.fastest_lap();
for (i, lap) in race.laps.iter_mut().enumerate() { for (i, lap) in race.laps.iter_mut().enumerate() {
if let Some(prev) = &prev_lap { if let Some(prev) = &prev_lap {
if lap.gas.unwrap_or_default() > prev.gas.unwrap_or(usize::MAX) if lap.gas.unwrap_or_default() > prev.gas.unwrap_or(u8::MAX)
|| lap.tyres.unwrap_or_default() > prev.tyres.unwrap_or(usize::MAX) || lap.tyres.unwrap_or_default() > prev.tyres.unwrap_or(u8::MAX)
{ {
ui.separator(); ui.separator();
ui.separator(); ui.separator();
@ -230,7 +230,7 @@ fn show_race_state(
} }
} }
if let Some(lap_time) = lap.lap_time { if let Some(lap_time) = lap.lap_time {
ui.label(format!("#{}", lap.lap.unwrap_or(i + 1))); ui.label(format!("#{}", lap.lap.unwrap_or((i + 1) as u16)));
ui.label(format!("P{}", lap.position.unwrap_or_default())); ui.label(format!("P{}", lap.position.unwrap_or_default()));
match lap match lap
@ -263,19 +263,19 @@ fn show_race_state(
label_time_delta(ui, lap_time, prev_lap.and_then(|p| p.lap_time)); label_time_delta(ui, lap_time, prev_lap.and_then(|p| p.lap_time));
label_time_delta(ui, lap_time, fastest_lap); label_time_delta(ui, lap_time, fastest_lap);
show_optional_usize_with_diff( show_optional_u8_with_diff(
ui, ui,
Color32::RED, Color32::RED,
&prev_lap.and_then(|p| p.health), &prev_lap.and_then(|p| p.health),
&lap.health, &lap.health,
); );
show_optional_usize_with_diff( show_optional_u8_with_diff(
ui, ui,
Color32::LIGHT_BLUE, Color32::LIGHT_BLUE,
&prev_lap.and_then(|p| p.gas), &prev_lap.and_then(|p| p.gas),
&lap.gas, &lap.gas,
); );
show_optional_usize_with_diff( show_optional_u8_with_diff(
ui, ui,
Color32::GREEN, Color32::GREEN,
&prev_lap.and_then(|p| p.tyres), &prev_lap.and_then(|p| p.tyres),

View File

@ -13,15 +13,15 @@ use crate::{config::Config, learned_tracks::LearnedTracks, ocr_db::OcrDatabase,
#[derive(Debug, Clone, Default, Serialize, Deserialize)] #[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct LapState { pub struct LapState {
pub lap: Option<usize>, pub lap: Option<u16>,
pub total_laps: Option<usize>, pub total_laps: Option<u16>,
pub position: Option<usize>, pub position: Option<u16>,
pub total_positions: Option<usize>, pub total_positions: Option<u16>,
pub health: Option<usize>, pub health: Option<u8>,
pub gas: Option<usize>, pub gas: Option<u8>,
pub tyres: Option<usize>, pub tyres: Option<u8>,
pub best_time: Option<Duration>, pub best_time: Option<Duration>,
pub lap_time: Option<Duration>, pub lap_time: Option<Duration>,
@ -48,7 +48,7 @@ fn parse_to_duration(time: Option<&String>) -> Option<Duration> {
parse_duration(time?) parse_duration(time?)
} }
fn check_0_100(v: usize) -> Option<usize> { fn check_0_100(v: u8) -> Option<u8> {
if v > 100 { if v > 100 {
None None
} else { } else {
@ -56,11 +56,11 @@ fn check_0_100(v: usize) -> Option<usize> {
} }
} }
fn parse_to_0_100(v: Option<&String>) -> Option<usize> { fn parse_to_0_100(v: Option<&String>) -> Option<u8> {
check_0_100(v?.parse::<usize>().ok()?) check_0_100(v?.parse::<u8>().ok()?)
} }
fn parse_with_slash(v: Option<&String>) -> (Option<usize>, Option<usize>) { fn parse_with_slash(v: Option<&String>) -> (Option<u16>, Option<u16>) {
let v = match v { let v = match v {
Some(v) => v, Some(v) => v,
None => return (None, None), None => return (None, None),
@ -90,7 +90,7 @@ impl LapState {
} }
} }
fn wear_values(values: &[Option<usize>]) -> Vec<usize> { fn wear_values(values: &[Option<u8>]) -> Vec<u8> {
let mut wear_values = Vec::new(); let mut wear_values = Vec::new();
let mut last_value = 100; let mut last_value = 100;
for val in values.iter().flatten() { for val in values.iter().flatten() {
@ -102,7 +102,7 @@ fn wear_values(values: &[Option<usize>]) -> Vec<usize> {
wear_values wear_values
} }
fn median_wear(values: Vec<Option<usize>>) -> Option<usize> { fn median_wear(values: Vec<Option<u8>>) -> Option<u8> {
let mut wear_values = wear_values(&values); let mut wear_values = wear_values(&values);
wear_values.sort_unstable(); wear_values.sort_unstable();
wear_values.get(wear_values.len() / 2).cloned() wear_values.get(wear_values.len() / 2).cloned()
@ -115,9 +115,9 @@ pub struct RaceState {
#[serde(skip)] #[serde(skip)]
pub last_lap_record_time: Option<Instant>, pub last_lap_record_time: Option<Instant>,
pub starting_position: usize, pub starting_position: u16,
pub total_laps: usize, pub total_laps: u16,
pub total_positions: usize, pub total_positions: u16,
#[serde(skip)] #[serde(skip)]
pub screencap: Option<RetainedImage>, pub screencap: Option<RetainedImage>,
@ -154,15 +154,15 @@ impl RaceState {
self.laps.iter().filter(|lap| !lap.striked).filter_map(|lap| lap.lap_time).min() self.laps.iter().filter(|lap| !lap.striked).filter_map(|lap| lap.lap_time).min()
} }
pub fn tyre_wear(&self) -> Option<usize> { pub fn tyre_wear(&self) -> Option<u8> {
median_wear(self.laps.iter().map(|lap| lap.tyres).collect()) median_wear(self.laps.iter().map(|lap| lap.tyres).collect())
} }
pub fn gas_per_lap(&self) -> Option<usize> { pub fn gas_per_lap(&self) -> Option<u8> {
median_wear(self.laps.iter().map(|lap| lap.gas).collect()) median_wear(self.laps.iter().map(|lap| lap.gas).collect())
} }
pub fn latest_gas_diff(&self, to: usize) -> Option<usize> { pub fn latest_gas_diff(&self, to: u8) -> Option<u8> {
let last_gas = self.laps.iter().last().map(|l| l.gas).unwrap_or(Some(100))?; let last_gas = self.laps.iter().last().map(|l| l.gas).unwrap_or(Some(100))?;
if to < last_gas { if to < last_gas {
Some(last_gas - to) Some(last_gas - to)
@ -170,7 +170,7 @@ impl RaceState {
None None
} }
} }
pub fn latest_tyre_diff(&self, to: usize) -> Option<usize> { pub fn latest_tyre_diff(&self, to: u8) -> Option<u8> {
let last_tyres = self.laps.iter().last().map(|l| l.tyres).unwrap_or(Some(100))?; let last_tyres = self.laps.iter().last().map(|l| l.tyres).unwrap_or(Some(100))?;
if to < last_tyres { if to < last_tyres {
Some(last_tyres - to) Some(last_tyres - to)

View File

@ -82,7 +82,7 @@ pub fn lap_summary(tts: &Tts, config: &Config, lap_state: &LapState, race: &Race
if lap == 1 { if lap == 1 {
let laps_per_tank = 100 / gas_wear; let laps_per_tank = 100 / gas_wear;
let pit_stops = race.total_laps / laps_per_tank; let pit_stops = race.total_laps / (laps_per_tank as u16);
tts.speak( tts.speak(
config, config,
TtsKind::FirstLap, TtsKind::FirstLap,