Check for GamePhase

This commit is contained in:
hodasemi 2023-01-18 08:55:43 +01:00
parent edd17b3c73
commit e6ae0e7bee
5 changed files with 92 additions and 16 deletions

View file

@ -8,7 +8,10 @@ use rfactor_sm_reader::{rF2VehicleTelemetry, VehicleScoringInfoV01};
use ui::prelude::*; use ui::prelude::*;
use utilities::prelude::Color; use utilities::prelude::Color;
use crate::overlay::{rfactor_data::DataReceiver, UiOverlay}; use crate::overlay::{
rfactor_data::{DataReceiver, GamePhase},
UiOverlay,
};
use crate::write_log; use crate::write_log;
pub struct LeaderBoard { pub struct LeaderBoard {
@ -49,14 +52,8 @@ impl LeaderBoard {
.unwrap() .unwrap()
.to_string() .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 { for vehicle_scoring in vehicle_scorings {
let driver_name = Self::c_char_to_string(vehicle_scoring.mDriverName); let driver_name = Self::c_char_to_string(vehicle_scoring.mDriverName);
@ -129,6 +126,30 @@ impl DataReceiver for LeaderBoard {
self.gui.enable()?; 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"); write_log!("leader board update finished");
Ok(()) Ok(())

View file

@ -4,7 +4,10 @@ use anyhow::Result;
use rfactor_sm_reader::{rF2VehicleTelemetry, VehicleScoringInfoV01}; use rfactor_sm_reader::{rF2VehicleTelemetry, VehicleScoringInfoV01};
use ui::prelude::*; use ui::prelude::*;
use crate::overlay::{rfactor_data::DataReceiver, UiOverlay}; use crate::overlay::{
rfactor_data::{DataReceiver, GamePhase},
UiOverlay,
};
pub struct Pedals { pub struct Pedals {
gui: Arc<GuiBuilder>, gui: Arc<GuiBuilder>,
@ -39,7 +42,11 @@ impl Pedals {
impl UiOverlay for Pedals {} impl UiOverlay for Pedals {}
impl DataReceiver 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(()) Ok(())
} }

View file

@ -9,7 +9,11 @@ use std::sync::{Arc, Mutex};
use super::pipeline::SingleColorPipeline; use super::pipeline::SingleColorPipeline;
use crate::{ use crate::{
overlay::{rendering::Rendering, rfactor_data::DataReceiver, UiOverlay}, overlay::{
rendering::Rendering,
rfactor_data::{DataReceiver, GamePhase},
UiOverlay,
},
write_log, write_log,
}; };
@ -296,7 +300,11 @@ impl Radar {
impl UiOverlay for Radar {} impl UiOverlay for Radar {}
impl DataReceiver 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(()) Ok(())
} }

View file

@ -4,7 +4,10 @@ use anyhow::Result;
use rfactor_sm_reader::{rF2VehicleTelemetry, VehicleScoringInfoV01}; use rfactor_sm_reader::{rF2VehicleTelemetry, VehicleScoringInfoV01};
use ui::prelude::*; use ui::prelude::*;
use crate::overlay::{rfactor_data::DataReceiver, UiOverlay}; use crate::overlay::{
rfactor_data::{DataReceiver, GamePhase},
UiOverlay,
};
pub struct Watermark { pub struct Watermark {
_gui: Arc<GuiBuilder>, _gui: Arc<GuiBuilder>,
@ -25,7 +28,11 @@ impl Watermark {
impl UiOverlay for Watermark {} impl UiOverlay for Watermark {}
impl DataReceiver 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(()) Ok(())
} }

View file

@ -8,7 +8,11 @@ use crate::write_log;
use super::UiOverlay; use super::UiOverlay;
pub trait DataReceiver { 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( fn telemetry_update(
&mut self, &mut self,
@ -17,6 +21,31 @@ pub trait DataReceiver {
) -> Result<()>; ) -> Result<()>;
} }
#[derive(Clone, Copy, Debug)]
pub enum GamePhase {
TestDay,
Practice,
Qualifying,
Warmup,
Race,
}
impl TryFrom<i32> for GamePhase {
type Error = anyhow::Error;
fn try_from(value: i32) -> Result<Self> {
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 { pub struct RFactorData {
// rf2 memory mapped data // rf2 memory mapped data
telemetry_reader: TelemetryReader, telemetry_reader: TelemetryReader,
@ -78,8 +107,12 @@ impl RFactorData {
} }
} }
let phase = GamePhase::try_from(scoring_info.mSession)?;
for receiver in self.receivers.iter() { for receiver in self.receivers.iter() {
receiver.borrow_mut().scoring_update(&vehicle_scorings)?; receiver
.borrow_mut()
.scoring_update(phase, &vehicle_scorings)?;
} }
} }