From c45ed8d12827704a61ce8778dfb0904403a2a320 Mon Sep 17 00:00:00 2001 From: hodasemi Date: Wed, 18 Jan 2023 08:55:43 +0100 Subject: [PATCH] Check for GamePhase --- src/overlay/elements/leaderboard.rs | 37 ++++++++++++++++++++++------- src/overlay/elements/pedals.rs | 11 +++++++-- src/overlay/elements/radar.rs | 12 ++++++++-- src/overlay/elements/watermark.rs | 11 +++++++-- src/overlay/rfactor_data.rs | 37 +++++++++++++++++++++++++++-- 5 files changed, 92 insertions(+), 16 deletions(-) diff --git a/src/overlay/elements/leaderboard.rs b/src/overlay/elements/leaderboard.rs index df881a1..2d0abb0 100644 --- a/src/overlay/elements/leaderboard.rs +++ b/src/overlay/elements/leaderboard.rs @@ -8,7 +8,10 @@ use rfactor_sm_reader::{rF2VehicleTelemetry, VehicleScoringInfoV01}; use ui::prelude::*; use utilities::prelude::Color; -use crate::overlay::{rfactor_data::DataReceiver, UiOverlay}; +use crate::overlay::{ + rfactor_data::{DataReceiver, GamePhase}, + UiOverlay, +}; use crate::write_log; pub struct LeaderBoard { @@ -49,14 +52,8 @@ impl LeaderBoard { .unwrap() .to_string() } -} - -impl UiOverlay for LeaderBoard {} - -impl DataReceiver for LeaderBoard { - fn scoring_update(&mut self, vehicle_scorings: &[VehicleScoringInfoV01]) -> Result<()> { - write_log!("=================== leader board: scoring update ==================="); + fn race_leaderboard(&mut self, vehicle_scorings: &[VehicleScoringInfoV01]) -> Result<()> { for vehicle_scoring in vehicle_scorings { let driver_name = Self::c_char_to_string(vehicle_scoring.mDriverName); @@ -129,6 +126,30 @@ impl DataReceiver for LeaderBoard { self.gui.enable()?; } + Ok(()) + } +} + +impl UiOverlay for LeaderBoard {} + +impl DataReceiver for LeaderBoard { + fn scoring_update( + &mut self, + phase: GamePhase, + vehicle_scorings: &[VehicleScoringInfoV01], + ) -> Result<()> { + write_log!("=================== leader board: scoring update ==================="); + + match phase { + GamePhase::TestDay => self.gui.disable()?, + GamePhase::Practice => self.gui.disable()?, + GamePhase::Qualifying => self.gui.disable()?, + GamePhase::Warmup => self.gui.disable()?, + GamePhase::Race => { + self.race_leaderboard(vehicle_scorings)?; + } + } + write_log!("leader board update finished"); Ok(()) diff --git a/src/overlay/elements/pedals.rs b/src/overlay/elements/pedals.rs index 1c659e6..a735ac0 100644 --- a/src/overlay/elements/pedals.rs +++ b/src/overlay/elements/pedals.rs @@ -4,7 +4,10 @@ use anyhow::Result; use rfactor_sm_reader::{rF2VehicleTelemetry, VehicleScoringInfoV01}; use ui::prelude::*; -use crate::overlay::{rfactor_data::DataReceiver, UiOverlay}; +use crate::overlay::{ + rfactor_data::{DataReceiver, GamePhase}, + UiOverlay, +}; pub struct Pedals { gui: Arc, @@ -39,7 +42,11 @@ impl Pedals { impl UiOverlay for Pedals {} impl DataReceiver for Pedals { - fn scoring_update(&mut self, _vehicle_scoring: &[VehicleScoringInfoV01]) -> Result<()> { + fn scoring_update( + &mut self, + _phase: GamePhase, + _vehicle_scoring: &[VehicleScoringInfoV01], + ) -> Result<()> { Ok(()) } diff --git a/src/overlay/elements/radar.rs b/src/overlay/elements/radar.rs index 05d756b..04d9a45 100644 --- a/src/overlay/elements/radar.rs +++ b/src/overlay/elements/radar.rs @@ -9,7 +9,11 @@ use std::sync::{Arc, Mutex}; use super::pipeline::SingleColorPipeline; use crate::{ - overlay::{rendering::Rendering, rfactor_data::DataReceiver, UiOverlay}, + overlay::{ + rendering::Rendering, + rfactor_data::{DataReceiver, GamePhase}, + UiOverlay, + }, write_log, }; @@ -296,7 +300,11 @@ impl Radar { impl UiOverlay for Radar {} impl DataReceiver for Radar { - fn scoring_update(&mut self, _vehicle_scoring: &[VehicleScoringInfoV01]) -> Result<()> { + fn scoring_update( + &mut self, + _phase: GamePhase, + _vehicle_scoring: &[VehicleScoringInfoV01], + ) -> Result<()> { Ok(()) } diff --git a/src/overlay/elements/watermark.rs b/src/overlay/elements/watermark.rs index 4e58b1f..608be46 100644 --- a/src/overlay/elements/watermark.rs +++ b/src/overlay/elements/watermark.rs @@ -4,7 +4,10 @@ use anyhow::Result; use rfactor_sm_reader::{rF2VehicleTelemetry, VehicleScoringInfoV01}; use ui::prelude::*; -use crate::overlay::{rfactor_data::DataReceiver, UiOverlay}; +use crate::overlay::{ + rfactor_data::{DataReceiver, GamePhase}, + UiOverlay, +}; pub struct Watermark { _gui: Arc, @@ -25,7 +28,11 @@ impl Watermark { impl UiOverlay for Watermark {} impl DataReceiver for Watermark { - fn scoring_update(&mut self, _vehicle_scoring: &[VehicleScoringInfoV01]) -> Result<()> { + fn scoring_update( + &mut self, + _phase: GamePhase, + _vehicle_scoring: &[VehicleScoringInfoV01], + ) -> Result<()> { Ok(()) } diff --git a/src/overlay/rfactor_data.rs b/src/overlay/rfactor_data.rs index dbb974d..d65e62a 100644 --- a/src/overlay/rfactor_data.rs +++ b/src/overlay/rfactor_data.rs @@ -8,7 +8,11 @@ use crate::write_log; use super::UiOverlay; pub trait DataReceiver { - fn scoring_update(&mut self, vehicle_scoring: &[VehicleScoringInfoV01]) -> Result<()>; + fn scoring_update( + &mut self, + phase: GamePhase, + vehicle_scoring: &[VehicleScoringInfoV01], + ) -> Result<()>; fn telemetry_update( &mut self, @@ -17,6 +21,31 @@ pub trait DataReceiver { ) -> Result<()>; } +#[derive(Clone, Copy, Debug)] +pub enum GamePhase { + TestDay, + Practice, + Qualifying, + Warmup, + Race, +} + +impl TryFrom for GamePhase { + type Error = anyhow::Error; + + fn try_from(value: i32) -> Result { + Ok(match value { + 0 => Self::TestDay, + 1..=4 => Self::Practice, + 5..=8 => Self::Qualifying, + 9 => Self::Warmup, + 10..=13 => Self::Race, + + _ => return Err(anyhow::anyhow!("Failed to parse GamePhase from: {}", value)), + }) + } +} + pub struct RFactorData { // rf2 memory mapped data telemetry_reader: TelemetryReader, @@ -78,8 +107,12 @@ impl RFactorData { } } + let phase = GamePhase::try_from(scoring_info.mSession)?; + for receiver in self.receivers.iter() { - receiver.borrow_mut().scoring_update(&vehicle_scorings)?; + receiver + .borrow_mut() + .scoring_update(phase, &vehicle_scorings)?; } }