From a10698962eb4c94406d9efb9b71fc09227a77afa Mon Sep 17 00:00:00 2001 From: hodasemi Date: Wed, 18 Jan 2023 17:02:20 +0100 Subject: [PATCH 1/6] Add GamePhase logic --- .../leaderboard/leaderboard_entry.xml | 6 +- .../elements/leaderboard/leaderboard_grid.xml | 2 +- src/overlay/elements/leaderboard/mod.rs | 113 +++++++++++++----- src/overlay/elements/pedals/mod.rs | 13 ++ src/overlay/elements/radar/mod.rs | 13 ++ src/overlay/elements/watermark/mod.rs | 20 +++- src/overlay/rfactor_data.rs | 41 +++++-- 7 files changed, 163 insertions(+), 45 deletions(-) diff --git a/src/overlay/elements/leaderboard/leaderboard_entry.xml b/src/overlay/elements/leaderboard/leaderboard_entry.xml index a61afb4..b3c182e 100644 --- a/src/overlay/elements/leaderboard/leaderboard_entry.xml +++ b/src/overlay/elements/leaderboard/leaderboard_entry.xml @@ -1,12 +1,12 @@ - + + id="time" + x_slot="7" y_slot="0" x_size="4" text_color="black" text_alignment="right"> \ No newline at end of file diff --git a/src/overlay/elements/leaderboard/leaderboard_grid.xml b/src/overlay/elements/leaderboard/leaderboard_grid.xml index 0ad24dc..cab3c03 100644 --- a/src/overlay/elements/leaderboard/leaderboard_grid.xml +++ b/src/overlay/elements/leaderboard/leaderboard_grid.xml @@ -1,5 +1,5 @@ - \ No newline at end of file diff --git a/src/overlay/elements/leaderboard/mod.rs b/src/overlay/elements/leaderboard/mod.rs index f189029..e453eee 100644 --- a/src/overlay/elements/leaderboard/mod.rs +++ b/src/overlay/elements/leaderboard/mod.rs @@ -53,7 +53,14 @@ impl LeaderBoard { .to_string() } - fn race_leaderboard(&mut self, vehicle_scorings: &[VehicleScoringInfoV01]) -> Result<()> { + fn update_leaderboard( + &mut self, + vehicle_scorings: &[VehicleScoringInfoV01], + f: F, + ) -> Result<()> + where + F: Fn(&mut LeaderBoardEntry, &VehicleScoringInfoV01) -> Result<()>, + { for vehicle_scoring in vehicle_scorings { let driver_name = Self::c_char_to_string(vehicle_scoring.mDriverName); @@ -69,8 +76,8 @@ impl LeaderBoard { } entry.update_place(vehicle_scoring.mPlace)?; - entry.update_time_behind_leader(vehicle_scoring.mTimeBehindLeader)?; - entry.update_time_behind_next(vehicle_scoring.mTimeBehindNext)?; + + f(entry, vehicle_scoring)?; } None => { let entry = LeaderBoardEntry::new( @@ -80,6 +87,7 @@ impl LeaderBoard { vehicle_scoring.mPlace, vehicle_scoring.mTimeBehindLeader, vehicle_scoring.mTimeBehindNext, + vehicle_scoring.mBestLapTime, )?; self.entries.push(entry); @@ -120,19 +128,39 @@ impl LeaderBoard { } } - if self.entries.is_empty() { - self.gui.disable()?; - } else { - self.gui.enable()?; - } - Ok(()) } + + fn race_leaderboard(&mut self, vehicle_scorings: &[VehicleScoringInfoV01]) -> Result<()> { + self.update_leaderboard(vehicle_scorings, |entry, scoring| { + entry.update_time_behind_leader(scoring.mTimeBehindLeader) + }) + } + + fn quali_leaderboard(&mut self, vehicle_scorings: &[VehicleScoringInfoV01]) -> Result<()> { + self.update_leaderboard(vehicle_scorings, |entry, scoring| { + entry.update_best_lap(scoring.mBestLapTime) + }) + } } impl UiOverlay for LeaderBoard {} impl DataReceiver for LeaderBoard { + fn game_phase_change(&mut self, phase: GamePhase) -> Result<()> { + match phase { + GamePhase::Practice | GamePhase::Qualifying | GamePhase::Race => self.gui.enable(), + _ => self.gui.disable(), + } + } + + fn update_for_phase(&self, phase: GamePhase) -> bool { + match phase { + GamePhase::Practice | GamePhase::Qualifying | GamePhase::Race => true, + _ => false, + } + } + fn scoring_update( &mut self, phase: GamePhase, @@ -141,13 +169,13 @@ impl DataReceiver for LeaderBoard { 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)?; + GamePhase::Practice | GamePhase::Qualifying => { + self.quali_leaderboard(vehicle_scorings)? } + + GamePhase::Race => self.race_leaderboard(vehicle_scorings)?, + + _ => (), } write_log!("leader board update finished"); @@ -177,13 +205,14 @@ struct LeaderBoardEntry { place: u8, time_behind_leader: f64, time_behind_next: f64, + best_lap: f64, snippet: Arc, grid: Arc, name_label: Arc