Probable fix for time display
This commit is contained in:
parent
3a463f2bec
commit
ee75183765
1 changed files with 52 additions and 40 deletions
|
@ -164,40 +164,41 @@ impl LeaderBoardEntry {
|
|||
self.time_label.set_text("---")
|
||||
}
|
||||
|
||||
pub fn force_display_behind_leader(&mut self) -> Result<()> {
|
||||
match self.behind {
|
||||
fn split_minute(time: f64) -> (f64, f64) {
|
||||
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) => {
|
||||
// check if we are leader
|
||||
if time_behind <= 0.0 {
|
||||
self.time_label.set_text("---")?;
|
||||
"---".to_string()
|
||||
} 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);
|
||||
if time_behind > 60.0 {
|
||||
let (full_minutes, remainder) = Self::split_minute(time_behind);
|
||||
|
||||
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))?;
|
||||
}
|
||||
BehindLeader::DNF => {
|
||||
self.time_label.set_text("DNF")?;
|
||||
}
|
||||
BehindLeader::DSQ => {
|
||||
self.time_label.set_text("DSQ")?;
|
||||
}
|
||||
BehindLeader::PITS => {
|
||||
self.time_label.set_text("PIT")?;
|
||||
format!("+{}", laps_behind)
|
||||
}
|
||||
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<()> {
|
||||
|
@ -210,23 +211,23 @@ impl LeaderBoardEntry {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
pub fn force_display_best_lap(&mut self) -> Result<()> {
|
||||
if self.best_lap <= 0.0 {
|
||||
self.time_label.set_text("---")?;
|
||||
fn calculuate_best_lap(best_lap: f64) -> String {
|
||||
if best_lap <= 0.0 {
|
||||
"---".to_string()
|
||||
} 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);
|
||||
if best_lap > 60.0 {
|
||||
let (full_minutes, remainder) = Self::split_minute(best_lap);
|
||||
|
||||
format!("{:.0}:{:.3}", full_minutes, remainder)
|
||||
} else {
|
||||
format!("{:.3}", self.best_lap)
|
||||
};
|
||||
|
||||
self.time_label.set_text(text)?;
|
||||
format!("{:.3}", best_lap)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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<()> {
|
||||
|
@ -239,19 +240,21 @@ impl LeaderBoardEntry {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
pub fn force_display_behind_next(&mut self) -> Result<()> {
|
||||
let text = if self.time_behind_next <= 0.0 {
|
||||
fn calculate_behind_next(behind_next: f64) -> String {
|
||||
if behind_next <= 0.0 {
|
||||
"---".to_string()
|
||||
} else 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);
|
||||
} else if behind_next > 60.0 {
|
||||
let (full_minutes, remainder) = Self::split_minute(behind_next);
|
||||
|
||||
format!("+{:.0}:{:.3}", full_minutes, remainder)
|
||||
format!("+{:.0}:{:.0}", full_minutes, remainder)
|
||||
} 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<()> {
|
||||
|
@ -275,6 +278,7 @@ impl LeaderBoardEntry {
|
|||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::{BehindLeader, LeaderBoardEntry};
|
||||
|
||||
#[test]
|
||||
fn test_string_replacement() {
|
||||
|
@ -296,4 +300,12 @@ mod test {
|
|||
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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue