Probable fix for time display

This commit is contained in:
hodasemi 2023-01-23 09:14:53 +01:00
parent 3a463f2bec
commit ee75183765

View file

@ -164,40 +164,41 @@ impl LeaderBoardEntry {
self.time_label.set_text("---") self.time_label.set_text("---")
} }
pub fn force_display_behind_leader(&mut self) -> Result<()> { fn split_minute(time: f64) -> (f64, f64) {
match self.behind { let full_minutes = (time / 60.0).floor();
let remainder = time - (full_minutes * 60.0);
(full_minutes, remainder)
}
fn calculate_behind_leader(behind: BehindLeader) -> String {
match behind {
BehindLeader::Time(time_behind) => { BehindLeader::Time(time_behind) => {
// check if we are leader // check if we are leader
if time_behind <= 0.0 { if time_behind <= 0.0 {
self.time_label.set_text("---")?; "---".to_string()
} else { } else {
let text = if time_behind > 60.0 { if time_behind > 60.0 {
let full_minutes = (self.best_lap / 60.0).floor(); let (full_minutes, remainder) = Self::split_minute(time_behind);
let remainder = self.best_lap - (full_minutes * 60.0);
format!("+{:.0}:{:.0}", full_minutes, remainder) format!("+{:.0}:{:.0}", full_minutes, remainder)
} else { } else {
format!("+{:.3}", time_behind) format!("+{:.3}", time_behind)
}; }
self.time_label.set_text(text)?;
} }
} }
BehindLeader::Laps(laps_behind) => { BehindLeader::Laps(laps_behind) => {
self.time_label.set_text(format!("+{}", laps_behind))?; format!("+{}", laps_behind)
}
BehindLeader::DNF => {
self.time_label.set_text("DNF")?;
}
BehindLeader::DSQ => {
self.time_label.set_text("DSQ")?;
}
BehindLeader::PITS => {
self.time_label.set_text("PIT")?;
} }
BehindLeader::DNF => "DNF".to_string(),
BehindLeader::DSQ => "DSQ".to_string(),
BehindLeader::PITS => "PIT".to_string(),
} }
}
Ok(()) pub fn force_display_behind_leader(&self) -> Result<()> {
self.time_label
.set_text(Self::calculate_behind_leader(self.behind))
} }
pub fn update_time_behind_leader(&mut self, behind: BehindLeader) -> Result<()> { pub fn update_time_behind_leader(&mut self, behind: BehindLeader) -> Result<()> {
@ -210,23 +211,23 @@ impl LeaderBoardEntry {
Ok(()) Ok(())
} }
pub fn force_display_best_lap(&mut self) -> Result<()> { fn calculuate_best_lap(best_lap: f64) -> String {
if self.best_lap <= 0.0 { if best_lap <= 0.0 {
self.time_label.set_text("---")?; "---".to_string()
} else { } else {
let text = if self.best_lap > 60.0 { if best_lap > 60.0 {
let full_minutes = (self.best_lap / 60.0).floor(); let (full_minutes, remainder) = Self::split_minute(best_lap);
let remainder = self.best_lap - (full_minutes * 60.0);
format!("{:.0}:{:.3}", full_minutes, remainder) format!("{:.0}:{:.3}", full_minutes, remainder)
} else { } else {
format!("{:.3}", self.best_lap) format!("{:.3}", best_lap)
}; }
self.time_label.set_text(text)?;
} }
}
Ok(()) pub fn force_display_best_lap(&mut self) -> Result<()> {
self.time_label
.set_text(Self::calculuate_best_lap(self.best_lap))
} }
pub fn update_best_lap(&mut self, time: f64) -> Result<()> { pub fn update_best_lap(&mut self, time: f64) -> Result<()> {
@ -239,19 +240,21 @@ impl LeaderBoardEntry {
Ok(()) Ok(())
} }
pub fn force_display_behind_next(&mut self) -> Result<()> { fn calculate_behind_next(behind_next: f64) -> String {
let text = if self.time_behind_next <= 0.0 { if behind_next <= 0.0 {
"---".to_string() "---".to_string()
} else if self.time_behind_next > 60.0 { } else if behind_next > 60.0 {
let full_minutes = (self.time_behind_next / 60.0).floor(); let (full_minutes, remainder) = Self::split_minute(behind_next);
let remainder = self.time_behind_next - (full_minutes * 60.0);
format!("+{:.0}:{:.3}", full_minutes, remainder) format!("+{:.0}:{:.0}", full_minutes, remainder)
} else { } else {
format!("+{:.3}", self.time_behind_next) format!("+{:.3}", behind_next)
}; }
}
self.time_label.set_text(text) pub fn force_display_behind_next(&mut self) -> Result<()> {
self.time_label
.set_text(Self::calculate_behind_next(self.time_behind_next))
} }
pub fn update_time_behind_next(&mut self, time: f64) -> Result<()> { pub fn update_time_behind_next(&mut self, time: f64) -> Result<()> {
@ -275,6 +278,7 @@ impl LeaderBoardEntry {
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::{BehindLeader, LeaderBoardEntry};
#[test] #[test]
fn test_string_replacement() { fn test_string_replacement() {
@ -296,4 +300,12 @@ mod test {
println!("{}", *b); println!("{}", *b);
} }
} }
#[test]
#[allow(unused)]
fn entry_display_tests() {
let behind_leader = LeaderBoardEntry::calculate_behind_leader(BehindLeader::Time(85.42));
let behind_next = LeaderBoardEntry::calculate_behind_next(150.213423);
let best_lap = LeaderBoardEntry::calculuate_best_lap(97.23436);
}
} }