Fix empty entries when reconnecting to server

This commit is contained in:
hodasemi 2023-01-20 06:56:26 +01:00
parent bd2470d7c7
commit 95e7ec72a1
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_label.set_text(self.place)?;
} }
self.place = place; Ok(())
self.place_label.set_text(self.place)
} }
pub fn reset_time(&mut self) -> Result<()> { pub fn reset_time(&mut self) -> Result<()> {
@ -134,32 +135,57 @@ impl LeaderBoardEntry {
self.time_label.set_text("---") 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<()> { pub fn update_time_behind_leader(&mut self, behind: BehindLeader) -> Result<()> {
if self.behind != behind { if self.behind != behind {
self.behind = behind; self.behind = behind;
match self.behind { self.force_display_behind_leader()?;
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) Ok(())
} else { }
format!("+{:.3}", time_behind)
};
self.time_label.set_text(text)?; pub fn force_display_best_lap(&mut self) -> Result<()> {
} if self.best_lap <= 0.0 {
} self.time_label.set_text("---")?;
BehindLeader::Laps(laps_behind) => { } else {
self.time_label.set_text(format!("+{}", laps_behind))?; 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(()) Ok(())
@ -169,28 +195,13 @@ impl LeaderBoardEntry {
if self.best_lap != time { if self.best_lap != time {
self.best_lap = time; self.best_lap = time;
if self.best_lap <= 0.0 { self.force_display_best_lap()?;
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(()) 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,21 +257,27 @@ 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(
let laps_behind = scoring.mLapsBehindLeader; vehicle_scorings,
|entry, scoring| {
let laps_behind = scoring.mLapsBehindLeader;
if laps_behind != 0 { if laps_behind != 0 {
entry.update_time_behind_leader(BehindLeader::Laps(laps_behind)) entry.update_time_behind_leader(BehindLeader::Laps(laps_behind))
} 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(),
)
} }
} }