From 5d7e771ba1e70fa2e7c7add02c01ac022845dacc Mon Sep 17 00:00:00 2001 From: hodasemi Date: Fri, 20 Jan 2023 06:56:26 +0100 Subject: [PATCH] Fix empty entries when reconnecting to server --- .../elements/leaderboard/leaderboard_entry.rs | 101 +++++++++++------- src/overlay/elements/leaderboard/mod.rs | 38 ++++--- 2 files changed, 86 insertions(+), 53 deletions(-) diff --git a/src/overlay/elements/leaderboard/leaderboard_entry.rs b/src/overlay/elements/leaderboard/leaderboard_entry.rs index 01bdf30..7ce89dc 100644 --- a/src/overlay/elements/leaderboard/leaderboard_entry.rs +++ b/src/overlay/elements/leaderboard/leaderboard_entry.rs @@ -119,11 +119,12 @@ impl LeaderBoardEntry { pub fn update_place(&mut self, place: u8) -> Result<()> { if self.place != place { self.place_updated = true; + + self.place = place; + self.place_label.set_text(self.place)?; } - self.place = place; - - self.place_label.set_text(self.place) + Ok(()) } pub fn reset_time(&mut self) -> Result<()> { @@ -134,32 +135,57 @@ impl LeaderBoardEntry { self.time_label.set_text("---") } + pub fn force_display_behind_leader(&mut self) -> Result<()> { + match self.behind { + BehindLeader::Time(time_behind) => { + // check if we are leader + if time_behind == 0.0 { + self.time_label.set_text("---")?; + } else { + let text = if time_behind > 60.0 { + let full_minutes = (self.best_lap / 60.0).floor(); + let remainder = self.best_lap - (full_minutes * 60.0); + + format!("+{:.0}:{:.0}", full_minutes, remainder) + } else { + format!("+{:.3}", time_behind) + }; + + self.time_label.set_text(text)?; + } + } + BehindLeader::Laps(laps_behind) => { + self.time_label.set_text(format!("+{}", laps_behind))?; + } + } + + Ok(()) + } + pub fn update_time_behind_leader(&mut self, behind: BehindLeader) -> Result<()> { if self.behind != behind { self.behind = behind; - match self.behind { - BehindLeader::Time(time_behind) => { - // check if we are leader - if time_behind == 0.0 { - self.time_label.set_text("---")?; - } else { - let text = if time_behind > 60.0 { - let full_minutes = (self.best_lap / 60.0).floor(); - let remainder = self.best_lap - (full_minutes * 60.0); + self.force_display_behind_leader()?; + } - format!("+{:.0}:{:.0}", full_minutes, remainder) - } else { - format!("+{:.3}", time_behind) - }; + Ok(()) + } - self.time_label.set_text(text)?; - } - } - BehindLeader::Laps(laps_behind) => { - self.time_label.set_text(format!("+{}", laps_behind))?; - } - } + pub fn force_display_best_lap(&mut self) -> Result<()> { + if self.best_lap <= 0.0 { + self.time_label.set_text("---")?; + } else { + let text = if self.best_lap > 60.0 { + let full_minutes = (self.best_lap / 60.0).floor(); + let remainder = self.best_lap - (full_minutes * 60.0); + + format!("{:.0}:{:.3}", full_minutes, remainder) + } else { + format!("{:.3}", self.best_lap) + }; + + self.time_label.set_text(text)?; } Ok(()) @@ -169,28 +195,13 @@ impl LeaderBoardEntry { if self.best_lap != time { self.best_lap = time; - if self.best_lap <= 0.0 { - self.time_label.set_text("---")?; - } else { - let text = if self.best_lap > 60.0 { - let full_minutes = (self.best_lap / 60.0).floor(); - let remainder = self.best_lap - (full_minutes * 60.0); - - format!("{:.0}:{:.3}", full_minutes, remainder) - } else { - format!("{:.3}", self.best_lap) - }; - - self.time_label.set_text(text)?; - } + self.force_display_best_lap()?; } Ok(()) } - pub fn update_time_behind_next(&mut self, time: f64) -> Result<()> { - self.time_behind_next = time; - + pub fn force_display_behind_next(&mut self) -> Result<()> { let text = if self.time_behind_next > 60.0 { let full_minutes = (self.time_behind_next / 60.0).floor(); let remainder = self.time_behind_next - (full_minutes * 60.0); @@ -203,6 +214,16 @@ impl LeaderBoardEntry { self.time_label.set_text(text) } + pub fn update_time_behind_next(&mut self, time: f64) -> Result<()> { + if self.time_behind_next != time { + self.time_behind_next = time; + + self.force_display_behind_next()?; + } + + Ok(()) + } + pub fn needs_resorting(&self) -> bool { self.place_updated } diff --git a/src/overlay/elements/leaderboard/mod.rs b/src/overlay/elements/leaderboard/mod.rs index 141e3a6..3b27fc9 100644 --- a/src/overlay/elements/leaderboard/mod.rs +++ b/src/overlay/elements/leaderboard/mod.rs @@ -78,13 +78,15 @@ impl LeaderBoard { .to_string() } - fn update_leaderboard( + fn update_leaderboard( &mut self, vehicle_scorings: &[VehicleScoringInfoV01], f: F, + d: D, ) -> Result<()> where F: Fn(&mut LeaderBoardEntry, &VehicleScoringInfoV01) -> Result<()>, + D: Fn(&mut LeaderBoardEntry) -> Result<()>, { for vehicle_scoring in vehicle_scorings { let driver_name = Self::c_char_to_string(vehicle_scoring.mDriverName); @@ -95,6 +97,7 @@ impl LeaderBoard { .iter_mut() .find(|entry| vehicle_scoring.mID == entry.id()) { + // update existing entry Some(entry) => { if entry.name() != driver_name { entry.change_name(driver_name)?; @@ -104,8 +107,9 @@ impl LeaderBoard { f(entry, vehicle_scoring)?; } + // add new entry if not found None => { - let entry = LeaderBoardEntry::new( + let mut entry = LeaderBoardEntry::new( &self.gui_handler, vehicle_scoring.mID, driver_name, @@ -123,6 +127,8 @@ impl LeaderBoard { vehicle_scoring.mBestLapTime, )?; + d(&mut entry)?; + self.leaderboard_entries.push(entry); } } @@ -251,21 +257,27 @@ impl LeaderBoard { } fn race_leaderboard(&mut self, vehicle_scorings: &[VehicleScoringInfoV01]) -> Result<()> { - self.update_leaderboard(vehicle_scorings, |entry, scoring| { - let laps_behind = scoring.mLapsBehindLeader; + self.update_leaderboard( + vehicle_scorings, + |entry, scoring| { + let laps_behind = scoring.mLapsBehindLeader; - if laps_behind != 0 { - entry.update_time_behind_leader(BehindLeader::Laps(laps_behind)) - } else { - entry.update_time_behind_leader(BehindLeader::Time(scoring.mTimeBehindLeader)) - } - }) + if laps_behind != 0 { + entry.update_time_behind_leader(BehindLeader::Laps(laps_behind)) + } else { + entry.update_time_behind_leader(BehindLeader::Time(scoring.mTimeBehindLeader)) + } + }, + |entry| entry.force_display_behind_leader(), + ) } fn quali_leaderboard(&mut self, vehicle_scorings: &[VehicleScoringInfoV01]) -> Result<()> { - self.update_leaderboard(vehicle_scorings, |entry, scoring| { - entry.update_best_lap(scoring.mBestLapTime) - }) + self.update_leaderboard( + vehicle_scorings, + |entry, scoring| entry.update_best_lap(scoring.mBestLapTime), + |entry| entry.force_display_best_lap(), + ) } }