-
Couldn't load subscription status.
- Fork 2
Calculate rank per track #24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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; | ||
|
|
||
|
|
@@ -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) | ||
| } | ||
|
|
||
| #[derive(Clone, PartialEq)] | ||
|
|
@@ -89,6 +91,7 @@ impl Race { | |
| racers, | ||
| categories: categories.into_iter().collect(), | ||
| tracks, | ||
| tracks_rank: HashMap::new(), | ||
| }) | ||
| } | ||
|
|
||
|
|
@@ -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(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we need clone? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| 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()); | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.