From 966414a17cc990d7fefd95bfe73471b35cfb8bd5 Mon Sep 17 00:00:00 2001 From: hodasemi Date: Wed, 18 Jan 2023 08:04:47 +0100 Subject: [PATCH] Improve leaderboard --- src/overlay/elements/leaderboard.rs | 62 ++++++++++++++++--- .../elements/ui_files/leaderboard_entry.xml | 6 +- .../elements/ui_files/leaderboard_grid.xml | 2 +- 3 files changed, 56 insertions(+), 14 deletions(-) diff --git a/src/overlay/elements/leaderboard.rs b/src/overlay/elements/leaderboard.rs index 754166e..df881a1 100644 --- a/src/overlay/elements/leaderboard.rs +++ b/src/overlay/elements/leaderboard.rs @@ -9,6 +9,7 @@ use ui::prelude::*; use utilities::prelude::Color; use crate::overlay::{rfactor_data::DataReceiver, UiOverlay}; +use crate::write_log; pub struct LeaderBoard { gui_handler: Arc, @@ -18,6 +19,7 @@ pub struct LeaderBoard { entries: Vec, entry_backgrounds: [Color; 2], + player_background: Color, } impl LeaderBoard { @@ -37,22 +39,38 @@ impl LeaderBoard { entries: Vec::new(), entry_backgrounds: [Color::try_from("#838383")?, Color::try_from("#545454")?], + player_background: Color::try_from("#b4bf26")?, }) } + + fn c_char_to_string(c: [c_char; 32usize]) -> String { + unsafe { CStr::from_ptr(&c as *const c_char) } + .to_str() + .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 ==================="); + for vehicle_scoring in vehicle_scorings { + let driver_name = Self::c_char_to_string(vehicle_scoring.mDriverName); + // check driver list match self .entries .iter_mut() - .find(|entry| vehicle_scoring.mID == entry.id) + .find(|entry| vehicle_scoring.mID == entry.id()) { Some(entry) => { + if entry.name() != driver_name { + entry.change_name(driver_name)?; + } + entry.update_place(vehicle_scoring.mPlace)?; entry.update_time_behind_leader(vehicle_scoring.mTimeBehindLeader)?; entry.update_time_behind_next(vehicle_scoring.mTimeBehindNext)?; @@ -61,10 +79,7 @@ impl DataReceiver for LeaderBoard { let entry = LeaderBoardEntry::new( &self.gui_handler, vehicle_scoring.mID, - unsafe { CStr::from_ptr(&vehicle_scoring.mDriverName as *const c_char) } - .to_str() - .unwrap() - .to_string(), + driver_name, vehicle_scoring.mPlace, vehicle_scoring.mTimeBehindLeader, vehicle_scoring.mTimeBehindNext, @@ -75,11 +90,14 @@ impl DataReceiver for LeaderBoard { } } + write_log!("create entries"); + // check if entry count in grid is the same as the gathered entries - let force_update = if self - .main_grid - .child_at(0, self.entries.len() - 1)? - .is_none() + let force_update = if !self.entries.is_empty() + && self + .main_grid + .child_at(0, self.entries.len() - 1)? + .is_none() { for i in 0..self.entries.len() { self.main_grid.detach(0, i)?; @@ -92,6 +110,8 @@ impl DataReceiver for LeaderBoard { // check if any entry needs resorting if force_update || self.entries.iter().any(|entry| entry.needs_resorting()) { + write_log!("leader board update required"); + self.entries .sort_by(|lhs, rhs| lhs.place().cmp(&rhs.place())); @@ -109,14 +129,22 @@ impl DataReceiver for LeaderBoard { self.gui.enable()?; } + write_log!("leader board update finished"); + Ok(()) } fn telemetry_update( &mut self, - _player_id: Option, + player_id: Option, _telemetries: &[rF2VehicleTelemetry], ) -> Result<()> { + if let Some(player_id) = player_id { + if let Some(entry) = self.entries.iter().find(|entry| entry.id() == player_id) { + entry.change_background_color(self.player_background)?; + } + } + Ok(()) } } @@ -178,10 +206,24 @@ impl LeaderBoardEntry { }) } + pub fn id(&self) -> i32 { + self.id + } + pub fn place(&self) -> u8 { self.place } + pub fn name(&self) -> &str { + &self.name + } + + pub fn change_name(&mut self, name: String) -> Result<()> { + self.name = name; + + self.name_label.set_text(&self.name) + } + pub fn snippet(&self) -> Arc { self.snippet.clone() } diff --git a/src/overlay/elements/ui_files/leaderboard_entry.xml b/src/overlay/elements/ui_files/leaderboard_entry.xml index 2d6d04c..78de1e5 100644 --- a/src/overlay/elements/ui_files/leaderboard_entry.xml +++ b/src/overlay/elements/ui_files/leaderboard_entry.xml @@ -1,7 +1,7 @@ - + - - + + \ No newline at end of file diff --git a/src/overlay/elements/ui_files/leaderboard_grid.xml b/src/overlay/elements/ui_files/leaderboard_grid.xml index 7d59f19..5a2a63d 100644 --- a/src/overlay/elements/ui_files/leaderboard_grid.xml +++ b/src/overlay/elements/ui_files/leaderboard_grid.xml @@ -1,5 +1,5 @@ - \ No newline at end of file