Files
arena-tracker/crates/app/src/card_order.rs
T
nessarandClaude Opus 5.5 a457a19fe5 Sort the cards by Mastery Points or A to Z and remember the choice (#76)
The statistics rows carry the Mastery Points: None for an Account whose
points were never fetched, Some(0) for a champion missing from the last
list. One card order, computed only when the statistics or the preference
change, feeds the home grid, the Anvil row and « Autres champions ». The
sort selector stays visible during a champ select, the choice is saved
with the other preferences, and the sheet shows the Mastery Points
(unknown ones after the known, and not as zero).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-21 02:26:14 +01:00

92 lines
3.2 KiB
Rust

//! The one order every section of cards follows: the chosen sort applied to the statistics' rows.
use arena_tracker_core::{ChampionRow, ChampionSort};
/// The positions of `rows` in display order. `rows` come A to Z (French collation), and every
/// sort is stable, so that order breaks every tie.
///
/// - `Name`: the rows' own order.
/// - `Mastery`: the champions with known points by points descending, then the ones whose points
/// are unknown (an unknown value is not a zero). When no row has a known value, the rows' own
/// order: the sort falls back to A to Z, and the player's preference stays as it is.
pub fn card_order(rows: &[ChampionRow], sort: ChampionSort) -> Vec<usize> {
let mut order: Vec<usize> = (0..rows.len()).collect();
if sort == ChampionSort::Mastery && rows.iter().any(|row| row.mastery_points.is_some()) {
// `None` sorts before every `Some`: reversed, the known values come first, highest first.
order.sort_by_key(|&ix| std::cmp::Reverse(rows[ix].mastery_points));
}
order
}
#[cfg(test)]
mod tests {
use arena_tracker_core::{Champion, ChampionRow, ChampionSort};
use super::card_order;
use crate::champion_table::tests::row;
/// Rows A to Z, as the statistics give them, each with its points.
fn rows(points: &[(&str, Option<u64>)]) -> Vec<ChampionRow> {
points
.iter()
.enumerate()
.map(|(id, (name, points))| ChampionRow {
champion: Champion {
id: id as u32 + 1,
..row(name).champion
},
mastery_points: *points,
..row(name)
})
.collect()
}
fn names(rows: &[ChampionRow], order: &[usize]) -> Vec<String> {
order
.iter()
.map(|&ix| rows[ix].champion.name.clone())
.collect()
}
#[test]
fn mastery_sorts_by_points_descending_then_by_name() {
let rows = rows(&[
("Ahri", Some(500)),
("Brand", Some(9_000)),
("Garen", Some(500)),
("Jinx", Some(0)),
("Zed", Some(500)),
]);
let order = card_order(&rows, ChampionSort::Mastery);
assert_eq!(
names(&rows, &order),
["Brand", "Ahri", "Garen", "Zed", "Jinx"]
);
}
#[test]
fn an_unknown_value_comes_after_the_known_ones_even_zero() {
let rows = rows(&[
("Ahri", None),
("Brand", Some(0)),
("Garen", None),
("Jinx", Some(12)),
]);
let order = card_order(&rows, ChampionSort::Mastery);
assert_eq!(names(&rows, &order), ["Jinx", "Brand", "Ahri", "Garen"]);
}
#[test]
fn without_any_known_value_the_cards_are_sorted_a_to_z() {
let rows = rows(&[("Ahri", None), ("Brand", None), ("Zed", None)]);
assert_eq!(card_order(&rows, ChampionSort::Mastery), [0, 1, 2]);
}
#[test]
fn the_name_sort_keeps_the_rows_order_whatever_the_points() {
let rows = rows(&[("Ahri", Some(1)), ("Brand", None), ("Zed", Some(900))]);
assert_eq!(card_order(&rows, ChampionSort::Name), [0, 1, 2]);
assert!(card_order(&[], ChampionSort::Mastery).is_empty());
}
}