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
9 changes: 9 additions & 0 deletions src/components/racers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ fn matches_filters(
return false;
}
}
_ => {
tracing::error!("Unimplemented filter for field {:?}", field);
}
}
}

Expand Down Expand Up @@ -168,6 +171,7 @@ pub fn Racers(race: Race) -> Element {
selected_category_id,
}
}
Th { sorter, field: RacerField::CategoriesRank, "Categories rank" }
}
}
tbody {
Expand All @@ -189,6 +193,11 @@ pub fn Racers(race: Race) -> Element {
"{category} "
}
}
td {
for category_rank in &racer.categories_rank {
span { class: "me-2", "{category_rank.0}: {category_rank.1}" }
}
}
}
}
}
Expand Down
65 changes: 65 additions & 0 deletions src/race.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use chrono::{Duration, TimeDelta};
use serde::{Deserialize, Serialize};
use std::cell::RefCell;
use std::cmp::Ordering;
use std::collections::HashMap;
use std::collections::HashSet;
use std::fmt;
use std::num::ParseIntError;
Expand Down Expand Up @@ -67,6 +68,7 @@ pub struct Racer {
pub track: Track,
pub track_rank: Option<u32>,
pub categories: Vec<Category>,
pub categories_rank: HashMap<Category, u32>,
pub start: Option<DateTime<Utc>>,
pub finish: Option<DateTime<Utc>>,
pub time: Option<Duration>,
Expand All @@ -80,6 +82,7 @@ pub enum RacerField {
TagId,
Track,
TrackRank,
CategoriesRank,
Start,
Finish,
Time,
Expand All @@ -99,6 +102,34 @@ impl Racer {
(None, Some(_)) => std::cmp::Ordering::Greater,
(None, None) => std::cmp::Ordering::Equal,
},
RacerField::CategoriesRank => {
// Collect union of category keys, sort them for deterministic ordering
let a_map = &self.categories_rank;
let b_map = &other.categories_rank;

let mut keys: Vec<_> = a_map.keys().chain(b_map.keys()).collect();
keys.sort();
keys.dedup();

for key in keys {
let a_rank = a_map.get(key);
let b_rank = b_map.get(key);

match (a_rank, b_rank) {
(Some(&a), Some(&b)) => {
let ord = a.cmp(&b);
if ord != std::cmp::Ordering::Equal {
return ord;
}
}
(Some(_), None) => return std::cmp::Ordering::Less,
(None, Some(_)) => return std::cmp::Ordering::Greater,
(None, None) => continue,
}
}

std::cmp::Ordering::Equal
}
RacerField::Start => self.start.cmp(&other.start),
RacerField::Finish => self.finish.cmp(&other.finish),
RacerField::Time => self.time.cmp(&other.time),
Expand Down Expand Up @@ -179,6 +210,7 @@ impl Race {
.into_iter()
.map(|category| Category(category.name))
.collect(),
categories_rank: HashMap::new(),
start,
finish,
time: calculate_time(start, finish),
Expand All @@ -194,6 +226,7 @@ impl Race {
log: RefCell::new(racelog).into(),
};
race.map_start_number_to_track_rank();
race.map_start_number_to_categories_rank();
Ok(race)
}

Expand All @@ -218,6 +251,7 @@ impl Race {
.borrow_mut()
.log_finish(racer.start_number.clone(), finish_time);
self.map_start_number_to_track_rank();
self.map_start_number_to_categories_rank();
Ok(())
}

Expand All @@ -239,6 +273,37 @@ impl Race {
}
}

pub fn map_start_number_to_categories_rank(&mut self) {
let categories = self.categories.clone();
for category in categories {
self.calculate_categories_rank(&category);
}
}

fn calculate_categories_rank(&mut self, category: &Category) {
let mut finished: Vec<&mut Racer> = self
.racers
.iter_mut()
.filter(|r| r.categories.contains(category))
.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.0.cmp(&b.start_number.0)
});

for (index, r) in finished.into_iter().enumerate() {
let rank: u32 = (index + 1) as u32;
r.categories_rank.insert(category.clone(), rank);
}
}

pub fn map_start_number_to_track_rank(&mut self) {
let tracks = self.tracks.clone();
for track in tracks {
Expand Down
Loading