Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/components/racers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub fn Racers(race: Race) -> Element {
Th { sorter, field: RacerField::Track, "Track" }
th { "Start" }
th { "Finish" }
th { "Track rank" }
th {
CategoriesList {
categories: race.categories,
Expand All @@ -46,6 +47,7 @@ pub fn Racers(race: Race) -> Element {
}
}
tbody {

for racer in sorted.iter() {
if (selected_category_id.read().clone())
.is_none_or(|cat_id| racer.categories.contains(&cat_id))
Expand All @@ -57,6 +59,12 @@ pub fn Racers(race: Race) -> Element {
td { "{racer.track}" }
td { "{format_time(racer.start)}" }
td { "{format_time(racer.finish)}" }
td {
"{race.tracks_rank.get(&racer.track)
.and_then(|m| m.get(&racer.start_number))
.map(|rank| rank.to_string())
.unwrap_or_default() }"
}
td {
for category in racer.categories.clone() {
"{category} "
Expand Down
46 changes: 44 additions & 2 deletions src/race.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use chrono::{DateTime, Utc};
use std::cmp::Ordering;
use std::collections::HashMap;
use std::collections::HashSet;
use tracing::error;

Expand All @@ -11,6 +12,7 @@ pub struct Race {
pub racers: Vec<Racer>,
pub categories: Vec<String>,
pub tracks: Vec<String>,
pub tracks_rank: HashMap<String, HashMap<u32, u32>>, // track -> (start_number -> rank)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of comments we should do proper datatypes to ensure type-safety

struct Track(String);

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, but in that case also tracks attribute shall be changed to tracks: Vec and that type shall be defined.

}

#[derive(Clone, PartialEq)]
Expand Down Expand Up @@ -89,6 +91,7 @@ impl Race {
racers,
categories: categories.into_iter().collect(),
tracks,
tracks_rank: HashMap::new(),
})
}

Expand All @@ -104,22 +107,61 @@ impl Race {
let racer = self
.racers
.iter_mut()
.find(|r| r.start_number == start_number);
.find(|r| r.start_number == start_number && r.start.is_some() && r.finish.is_none());

if let Some(racer) = racer {
racer.finish = Some(Utc::now());
self.map_start_number_to_track_rank();
} else {
error!("Racer with starting number {start_number} not found.");
}
}

pub fn tag_finished(&mut self, tag: &str) {
let racer = self.racers.iter_mut().find(|r| r.tag == tag);
let racer = self
.racers
.iter_mut()
.find(|r| r.tag == tag && r.start.is_some() && r.finish.is_none());

if let Some(racer) = racer {
racer.finish = Some(Utc::now());
self.map_start_number_to_track_rank();
} else {
error!("Racer with tag {tag} not found.");
}
}

pub fn map_start_number_to_track_rank(&mut self) {
let tracks = self.tracks.clone();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need clone?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unfortunatelly yes - otherwise I need to somehow specify the self.tracks is immutable in calculate_track_rank function

for track in tracks {
self.calculate_track_rank(&track);
}
}

fn calculate_track_rank(&mut self, track: &str) {
let mut finished: Vec<&Racer> = self
.racers
.iter()
.filter(|r| r.track == track)
.filter(|r| r.finish.is_some())
.collect();

finished.sort_by(|a, b| {
let ord = a.finish.cmp(&b.finish);
if ord != std::cmp::Ordering::Equal {
return ord;
}

// in case the finish times are equal, sort by start number
a.start_number.cmp(&b.start_number)
});

let current_track_rank = self.tracks_rank.entry(track.to_string()).or_default();

current_track_rank.clear(); // Clear previous rankings

for (rank, r) in finished.into_iter().enumerate() {
current_track_rank.insert(r.start_number, (rank + 1).try_into().unwrap());
}
}
}
Loading