diff --git a/crates/core/src/lib.rs b/crates/core/src/lib.rs index 50f171c..411b01f 100644 --- a/crates/core/src/lib.rs +++ b/crates/core/src/lib.rs @@ -32,8 +32,8 @@ pub use riot::RiotApi; pub use seasons::{Season, default_seasons, load_seasons}; pub use splash::{BannerFrame, Splash, SplashFocus}; pub use statistics::{ - ChallengeSummary, ChampionChallenge, ChampionRow, ChampionState, Charts, Column, Marks, Scope, - SeasonScope, StateFilter, Statistics, sort_rows, + ChallengeSummary, ChampionChallenge, ChampionRow, ChampionState, Charts, Column, LastMatch, + Marks, MatchOutcome, Scope, SeasonScope, StateFilter, Statistics, sort_rows, }; pub use tracker::{MatchHistorySource, Tracker}; pub use update_package::{download_update, restart_to_update, update_at_launch, verify_release}; diff --git a/crates/core/src/statistics.rs b/crates/core/src/statistics.rs index c70ca27..636302b 100644 --- a/crates/core/src/statistics.rs +++ b/crates/core/src/statistics.rs @@ -47,6 +47,29 @@ pub struct ChallengeSummary { /// Over Completed champions only. pub average_attempts: Option, pub first_tries: usize, + /// The Completed champions' ids in the order of their first top 1, the earliest first. + pub completion_order: Vec, + /// The Season's latest Match, all Formats; `None` without any Match in the Season. + pub last_match: Option, +} + +/// A Match as the Challenge saw it. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct LastMatch { + pub champion_id: u32, + pub placement: u8, + pub outcome: MatchOutcome, +} + +/// What a Match did to its champion's Challenge. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum MatchOutcome { + /// The champion's Attempt of that number (1 for the first), which did not complete it. + Attempt(usize), + /// The champion's first top 1, after that many Attempts (1 for a First Try). + Completed(usize), + /// Played once the champion was already Completed: not an Attempt. + AfterCompletion, } #[derive(Clone, Copy, Debug, PartialEq, Eq)] @@ -242,23 +265,42 @@ struct Progress { best_placement: Option, } +/// What one chronological pass over the Season's Matches gives the Challenge. +#[derive(Default)] +struct ChallengePass { + progress: HashMap, + completion_order: Vec, + last_match: Option, +} + /// Progress per champion from Matches in chronological order: a champion's Attempts count /// its Matches up to and including its first top 1; the other figures count them all. -fn progress_by_champion(matches: &[&Match]) -> HashMap { - let mut progress: HashMap = HashMap::new(); +fn challenge_pass(matches: &[&Match]) -> ChallengePass { + let mut pass = ChallengePass::default(); for m in matches { - let p = progress.entry(m.champion_id).or_default(); + let p = pass.progress.entry(m.champion_id).or_default(); p.matches += 1; p.wins += usize::from(m.win); p.best_placement = Some(p.best_placement.map_or(m.placement, |b| b.min(m.placement))); - if p.completed_at_ms.is_none() { + let outcome = if p.completed_at_ms.is_some() { + MatchOutcome::AfterCompletion + } else { p.attempts += 1; if m.placement == 1 { p.completed_at_ms = Some(m.started_at_ms); + pass.completion_order.push(m.champion_id); + MatchOutcome::Completed(p.attempts) + } else { + MatchOutcome::Attempt(p.attempts) } - } + }; + pass.last_match = Some(LastMatch { + champion_id: m.champion_id, + placement: m.placement, + outcome, + }); } - progress + pass } /// `matches` are all the Account's stored Matches, oldest first. `period` is the Season's @@ -296,16 +338,16 @@ pub(crate) fn compute( for m in &in_scope { by_champion.entry(m.champion_id).or_default().push(m); } - let progress = period.is_some().then(|| progress_by_champion(&in_period)); + let mut pass = period.is_some().then(|| challenge_pass(&in_period)); let ever_played: HashSet = matches.iter().map(|m| m.champion_id).collect(); let rows: Vec = champions .into_iter() .map(|champion| { let mine = by_champion.get(&champion.id).map_or(&[][..], Vec::as_slice); - let (attempts, state, challenge) = match &progress { - Some(progress) => { - let p = progress.get(&champion.id); + let (attempts, state, challenge) = match &pass { + Some(pass) => { + let p = pass.progress.get(&champion.id); let state = match p { Some(Progress { completed_at_ms: Some(_), @@ -362,7 +404,8 @@ pub(crate) fn compute( let mut challenge = None; let mut completed_over_time = Vec::new(); let mut attempts_to_complete = [0; 5]; - if let Some(progress) = &progress { + if let Some(pass) = &mut pass { + let progress = &pass.progress; let completed: Vec<(usize, i64)> = progress .values() .filter_map(|p| Some((p.attempts, p.completed_at_ms?))) @@ -375,6 +418,8 @@ pub(crate) fn compute( completed.iter().map(|(n, _)| n).sum::() as f64 / completed.len() as f64 }), first_tries: completed.iter().filter(|(n, _)| *n == 1).count(), + completion_order: std::mem::take(&mut pass.completion_order), + last_match: pass.last_match, }); let mut per_day: BTreeMap = BTreeMap::new(); for &(attempts, ms) in &completed { diff --git a/crates/core/tests/statistics.rs b/crates/core/tests/statistics.rs index 082ace2..938fa84 100644 --- a/crates/core/tests/statistics.rs +++ b/crates/core/tests/statistics.rs @@ -1,13 +1,14 @@ mod common; use arena_tracker_core::{ - Account, Champion, ChampionChallenge, ChampionRow, ChampionState, Column, Error, Format, Marks, - Scope, Season, SeasonScope, StateFilter, Statistics, Tracker, sort_rows, + Account, Champion, ChampionChallenge, ChampionRow, ChampionState, Column, Error, Format, + LastMatch, Marks, MatchOutcome, Scope, Season, SeasonScope, StateFilter, Statistics, Tracker, + sort_rows, }; use common::jan_2026 as jan; use common::{ - CATALOG, FakeChampions, FakeHistory, RIOT_ID, S1, S2, SELF_PUUID, at, date, imported_tracker, - payload, payloads, played_by, played_match, recorded_account, run, seasons, + CATALOG, FakeChampions, FakeHistory, PlayedMatch, RIOT_ID, S1, S2, SELF_PUUID, at, date, + imported_tracker, payload, payloads, played_by, played_match, recorded_account, run, seasons, }; use std::sync::atomic::Ordering; @@ -862,3 +863,92 @@ fn the_first_top_1_day_matches_the_progress_curve() { assert_eq!(days, curve); assert_eq!(challenge(&stats, 11).completed_on, Some(date(2026, 1, 6))); } + +#[test] +fn the_completions_keep_the_order_of_their_first_top_1() { + let dir = tempfile::tempdir().unwrap(); + let matches = [ + run(222, &[3, 1], jan(2, 0)), // Completed second, on its 2nd Attempt + run(11, &[1, 1], jan(2, 0)), // Completed first: the later top 1 changes nothing + run(103, &[1], jan(4, 0)), + run(1, &[4], jan(5, 0)), // played, never Completed + ] + .concat(); + let tracker = imported_tracker(dir.path(), &matches); + + let challenge = stats(&tracker, S1, None).challenge.unwrap(); + + assert_eq!(challenge.completion_order, [11, 222, 103]); +} + +#[test] +fn the_last_match_says_what_it_did_to_its_champion() { + let last = |matches: &[PlayedMatch], format: Option| { + let dir = tempfile::tempdir().unwrap(); + let tracker = imported_tracker(dir.path(), matches); + stats(&tracker, S1, format).challenge.unwrap().last_match + }; + let outcome = |champion_id, placement, outcome| { + Some(LastMatch { + champion_id, + placement, + outcome, + }) + }; + // Not yet Completed: its 3rd Attempt. + assert_eq!( + last(&run(11, &[5, 4, 4], jan(2, 0)), None), + outcome(11, 4, MatchOutcome::Attempt(3)) + ); + // The Match that completes it, here on the 3rd Attempt, or on the first. + assert_eq!( + last(&run(11, &[5, 4, 1], jan(2, 0)), None), + outcome(11, 1, MatchOutcome::Completed(3)) + ); + assert_eq!( + last(&run(222, &[1], jan(2, 0)), None), + outcome(222, 1, MatchOutcome::Completed(1)) + ); + // Already Completed: not an Attempt. + assert_eq!( + last(&run(11, &[1, 4], jan(2, 0)), None), + outcome(11, 4, MatchOutcome::AfterCompletion) + ); + // The latest by start time, whatever the champion, and all Formats whatever the filter. + let matches = [ + played_match(103, 2, jan(3, 0)), + played_match(11, 6, jan(4, 0)).in_format(1740), + played_match(222, 3, jan(2, 0)), + ]; + assert_eq!( + last(&matches, Some(Format::Arena3x6)), + outcome(11, 6, MatchOutcome::Attempt(1)) + ); + // Only the Season's Matches: one of the next Season is not the last of this one. + assert_eq!( + last( + &[ + played_match(103, 2, jan(3, 0)), + played_match(11, 1, april(2)) + ], + None + ), + outcome(103, 2, MatchOutcome::Attempt(1)) + ); +} + +#[test] +fn a_season_without_any_match_has_no_last_match_nor_completion() { + let dir = tempfile::tempdir().unwrap(); + let tracker = imported_tracker(dir.path(), &run(11, &[1], jan(2, 0))); + + let challenge = stats(&tracker, S2, None).challenge.unwrap(); + + assert_eq!(challenge.last_match, None); + assert!(challenge.completion_order.is_empty()); +} + +/// A day of April 2026, in the second Season. +fn april(day: u8) -> i64 { + at(date(2026, 4, day), 0) +}