Fix empty entries when reconnecting to server
This commit is contained in:
parent
8095d866c3
commit
5d7e771ba1
2 changed files with 86 additions and 53 deletions
|
@ -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_label.set_text(self.place)
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn reset_time(&mut self) -> Result<()> {
|
||||
|
@ -134,10 +135,7 @@ impl LeaderBoardEntry {
|
|||
self.time_label.set_text("---")
|
||||
}
|
||||
|
||||
pub fn update_time_behind_leader(&mut self, behind: BehindLeader) -> Result<()> {
|
||||
if self.behind != behind {
|
||||
self.behind = behind;
|
||||
|
||||
pub fn force_display_behind_leader(&mut self) -> Result<()> {
|
||||
match self.behind {
|
||||
BehindLeader::Time(time_behind) => {
|
||||
// check if we are leader
|
||||
|
@ -160,15 +158,21 @@ impl LeaderBoardEntry {
|
|||
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(())
|
||||
}
|
||||
|
||||
pub fn update_best_lap(&mut self, time: f64) -> Result<()> {
|
||||
if self.best_lap != time {
|
||||
self.best_lap = time;
|
||||
|
||||
pub fn force_display_best_lap(&mut self) -> Result<()> {
|
||||
if self.best_lap <= 0.0 {
|
||||
self.time_label.set_text("---")?;
|
||||
} else {
|
||||
|
@ -183,14 +187,21 @@ impl LeaderBoardEntry {
|
|||
|
||||
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(())
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
|
|
@ -78,13 +78,15 @@ impl LeaderBoard {
|
|||
.to_string()
|
||||
}
|
||||
|
||||
fn update_leaderboard<F>(
|
||||
fn update_leaderboard<F, D>(
|
||||
&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,7 +257,9 @@ impl LeaderBoard {
|
|||
}
|
||||
|
||||
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;
|
||||
|
||||
if laps_behind != 0 {
|
||||
|
@ -259,13 +267,17 @@ impl LeaderBoard {
|
|||
} 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(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue