Fix empty entries when reconnecting to server

This commit is contained in:
hodasemi 2023-01-20 06:56:26 +01:00
parent 8095d866c3
commit 5d7e771ba1
2 changed files with 86 additions and 53 deletions

View file

@ -119,11 +119,12 @@ impl LeaderBoardEntry {
pub fn update_place(&mut self, place: u8) -> Result<()> { pub fn update_place(&mut self, place: u8) -> Result<()> {
if self.place != place { if self.place != place {
self.place_updated = true; self.place_updated = true;
}
self.place = place; self.place = place;
self.place_label.set_text(self.place)?;
}
self.place_label.set_text(self.place) Ok(())
} }
pub fn reset_time(&mut self) -> Result<()> { pub fn reset_time(&mut self) -> Result<()> {
@ -134,10 +135,7 @@ impl LeaderBoardEntry {
self.time_label.set_text("---") self.time_label.set_text("---")
} }
pub fn update_time_behind_leader(&mut self, behind: BehindLeader) -> Result<()> { pub fn force_display_behind_leader(&mut self) -> Result<()> {
if self.behind != behind {
self.behind = behind;
match self.behind { match self.behind {
BehindLeader::Time(time_behind) => { BehindLeader::Time(time_behind) => {
// check if we are leader // check if we are leader
@ -160,15 +158,21 @@ impl LeaderBoardEntry {
self.time_label.set_text(format!("+{}", 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;
self.force_display_behind_leader()?;
} }
Ok(()) Ok(())
} }
pub fn update_best_lap(&mut self, time: f64) -> Result<()> { pub fn force_display_best_lap(&mut self) -> Result<()> {
if self.best_lap != time {
self.best_lap = time;
if self.best_lap <= 0.0 { if self.best_lap <= 0.0 {
self.time_label.set_text("---")?; self.time_label.set_text("---")?;
} else { } else {
@ -183,14 +187,21 @@ impl LeaderBoardEntry {
self.time_label.set_text(text)?; self.time_label.set_text(text)?;
} }
Ok(())
}
pub fn update_best_lap(&mut self, time: f64) -> Result<()> {
if self.best_lap != time {
self.best_lap = time;
self.force_display_best_lap()?;
} }
Ok(()) Ok(())
} }
pub fn update_time_behind_next(&mut self, time: f64) -> Result<()> { pub fn force_display_behind_next(&mut self) -> Result<()> {
self.time_behind_next = time;
let text = if self.time_behind_next > 60.0 { let text = if self.time_behind_next > 60.0 {
let full_minutes = (self.time_behind_next / 60.0).floor(); let full_minutes = (self.time_behind_next / 60.0).floor();
let remainder = self.time_behind_next - (full_minutes * 60.0); let remainder = self.time_behind_next - (full_minutes * 60.0);
@ -203,6 +214,16 @@ impl LeaderBoardEntry {
self.time_label.set_text(text) 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 { pub fn needs_resorting(&self) -> bool {
self.place_updated self.place_updated
} }

View file

@ -78,13 +78,15 @@ impl LeaderBoard {
.to_string() .to_string()
} }
fn update_leaderboard<F>( fn update_leaderboard<F, D>(
&mut self, &mut self,
vehicle_scorings: &[VehicleScoringInfoV01], vehicle_scorings: &[VehicleScoringInfoV01],
f: F, f: F,
d: D,
) -> Result<()> ) -> Result<()>
where where
F: Fn(&mut LeaderBoardEntry, &VehicleScoringInfoV01) -> Result<()>, F: Fn(&mut LeaderBoardEntry, &VehicleScoringInfoV01) -> Result<()>,
D: Fn(&mut LeaderBoardEntry) -> 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);
@ -95,6 +97,7 @@ impl LeaderBoard {
.iter_mut() .iter_mut()
.find(|entry| vehicle_scoring.mID == entry.id()) .find(|entry| vehicle_scoring.mID == entry.id())
{ {
// update existing entry
Some(entry) => { Some(entry) => {
if entry.name() != driver_name { if entry.name() != driver_name {
entry.change_name(driver_name)?; entry.change_name(driver_name)?;
@ -104,8 +107,9 @@ impl LeaderBoard {
f(entry, vehicle_scoring)?; f(entry, vehicle_scoring)?;
} }
// add new entry if not found
None => { None => {
let entry = LeaderBoardEntry::new( let mut entry = LeaderBoardEntry::new(
&self.gui_handler, &self.gui_handler,
vehicle_scoring.mID, vehicle_scoring.mID,
driver_name, driver_name,
@ -123,6 +127,8 @@ impl LeaderBoard {
vehicle_scoring.mBestLapTime, vehicle_scoring.mBestLapTime,
)?; )?;
d(&mut entry)?;
self.leaderboard_entries.push(entry); self.leaderboard_entries.push(entry);
} }
} }
@ -251,7 +257,9 @@ impl LeaderBoard {
} }
fn race_leaderboard(&mut self, vehicle_scorings: &[VehicleScoringInfoV01]) -> Result<()> { fn race_leaderboard(&mut self, vehicle_scorings: &[VehicleScoringInfoV01]) -> Result<()> {
self.update_leaderboard(vehicle_scorings, |entry, scoring| { self.update_leaderboard(
vehicle_scorings,
|entry, scoring| {
let laps_behind = scoring.mLapsBehindLeader; let laps_behind = scoring.mLapsBehindLeader;
if laps_behind != 0 { if laps_behind != 0 {
@ -259,13 +267,17 @@ impl LeaderBoard {
} else { } else {
entry.update_time_behind_leader(BehindLeader::Time(scoring.mTimeBehindLeader)) entry.update_time_behind_leader(BehindLeader::Time(scoring.mTimeBehindLeader))
} }
}) },
|entry| entry.force_display_behind_leader(),
)
} }
fn quali_leaderboard(&mut self, vehicle_scorings: &[VehicleScoringInfoV01]) -> Result<()> { fn quali_leaderboard(&mut self, vehicle_scorings: &[VehicleScoringInfoV01]) -> Result<()> {
self.update_leaderboard(vehicle_scorings, |entry, scoring| { self.update_leaderboard(
entry.update_best_lap(scoring.mBestLapTime) vehicle_scorings,
}) |entry, scoring| entry.update_best_lap(scoring.mBestLapTime),
|entry| entry.force_display_best_lap(),
)
} }
} }