From 4548b6ac64cdb1bfd444ad01858cd49ea370f29f Mon Sep 17 00:00:00 2001
From: TO
Date: Sun, 22 Nov 2026 16:49:32 +0100
Subject: [PATCH] Split large core modules into focused child modules (#102)
---
crates/core/src/champ_select.rs | 218 +-------------------------
crates/core/src/champ_select/tests.rs | 215 +++++++++++++++++++++++++
crates/core/src/follow.rs | 185 +---------------------
crates/core/src/follow/pending.rs | 102 ++++++++++++
crates/core/src/follow/progress.rs | 34 ++++
crates/core/src/follow/tests.rs | 52 ++++++
crates/core/src/statistics.rs | 105 +------------
crates/core/src/statistics/rows.rs | 103 ++++++++++++
crates/core/src/tracker.rs | 152 +-----------------
crates/core/src/tracker/payload.rs | 65 ++++++++
crates/core/src/tracker/storage.rs | 30 ++++
crates/core/src/tracker/tests.rs | 57 +++++++
12 files changed, 674 insertions(+), 644 deletions(-)
create mode 100644 crates/core/src/champ_select/tests.rs
create mode 100644 crates/core/src/follow/pending.rs
create mode 100644 crates/core/src/follow/progress.rs
create mode 100644 crates/core/src/follow/tests.rs
create mode 100644 crates/core/src/statistics/rows.rs
create mode 100644 crates/core/src/tracker/payload.rs
create mode 100644 crates/core/src/tracker/storage.rs
create mode 100644 crates/core/src/tracker/tests.rs
diff --git a/crates/core/src/champ_select.rs b/crates/core/src/champ_select.rs
index fddcca7..75ebcd2 100644
--- a/crates/core/src/champ_select.rs
+++ b/crates/core/src/champ_select.rs
@@ -321,220 +321,4 @@ impl ChampSelectLists {
}
#[cfg(test)]
-mod tests {
- use super::*;
- use serde_json::json;
-
- #[test]
- fn the_lists_change_only_for_the_events_that_concern_them() {
- let mut lists = ChampSelectLists::from_bodies("[1,2,3]", "[3]", Some("[9,8]")).unwrap();
- assert_eq!(&*lists.pickable(), &HashSet::from([1, 2]));
- assert_eq!(lists.anvil(), Some(&[9, 8][..]));
-
- // A Delete carries no data: it changes nothing.
- lists.set_pickable(&Value::Null);
- lists.set_anvil(&Value::Null);
- assert_eq!(&*lists.pickable(), &HashSet::from([1, 2]));
- assert_eq!(lists.anvil(), Some(&[9, 8][..]));
-
- lists.set_pickable(&json!([1, 4]));
- assert_eq!(&*lists.pickable(), &HashSet::from([1, 4]));
- assert_eq!(lists.anvil(), Some(&[9, 8][..]));
-
- lists.set_disabled(&json!([1]));
- assert_eq!(&*lists.pickable(), &HashSet::from([4]));
-
- lists.set_anvil(&json!([5, 6]));
- assert_eq!(lists.anvil(), Some(&[5, 6][..]));
- }
-
- #[test]
- fn the_anvil_champions_are_read_once_when_the_champ_select_starts() {
- let anvil = |body| {
- ChampSelectLists::from_bodies("[]", "[]", body)
- .unwrap()
- .anvil()
- .map(<[u32]>::to_vec)
- };
- // No answer, or one that is not a list: the client gave no list.
- assert_eq!(anvil(None), None);
- assert_eq!(anvil(Some("not json")), None);
- assert_eq!(anvil(Some(r#"{"error":"x"}"#)), None);
- // An empty list is a list: the client says there are none.
- assert_eq!(anvil(Some("[]")), Some(vec![]));
- assert_eq!(anvil(Some("[1,2]")), Some(vec![1, 2]));
- // The client's order is kept.
- assert_eq!(anvil(Some("[9,1,5]")), Some(vec![9, 1, 5]));
- // A malformed anvil answer only loses the anvil list, not the pickable one.
- let lists = ChampSelectLists::from_bodies("[7]", "[]", Some("not json")).unwrap();
- assert_eq!(&*lists.pickable(), &HashSet::from([7]));
- assert_eq!(lists.anvil(), None);
- }
-
- #[test]
- fn an_anvil_event_gives_a_list_from_unknown_and_a_delete_keeps_the_value() {
- let mut lists = ChampSelectLists::from_bodies("[]", "[]", None).unwrap();
- lists.set_anvil(&Value::Null);
- assert_eq!(lists.anvil(), None);
- lists.set_anvil(&json!([4, 2]));
- assert_eq!(lists.anvil(), Some(&[4, 2][..]));
- lists.set_anvil(&Value::Null);
- assert_eq!(lists.anvil(), Some(&[4, 2][..]));
- // An empty list from an event is a known empty list.
- lists.set_anvil(&json!([]));
- assert_eq!(lists.anvil(), Some(&[][..]));
- }
-
- fn session(
- local_intent: u32,
- ally_id: u32,
- ally_intent: u32,
- actions: Value,
- bans: Value,
- ) -> Value {
- json!({
- "localPlayerCellId": 16,
- "myTeam": [
- {"cellId": 16, "team": 1, "championId": 0, "championPickIntent": local_intent},
- {"cellId": 17, "team": 1, "championId": ally_id, "championPickIntent": ally_intent},
- {"cellId": 18, "team": 2, "championId": 77, "championPickIntent": 0},
- ],
- "actions": actions,
- "bans": bans,
- })
- }
-
- fn champ_select(current_session: Value) -> ChampSelect {
- ChampSelect::from_session(
- ¤t_session,
- Arc::new(HashSet::from([1, 2, 3, 4, 5, 6])),
- Some(vec![1, 2]),
- )
- .unwrap()
- }
-
- fn reasons(banned: bool, taken_by_ally: bool, not_offered: bool) -> Unpickable {
- Unpickable {
- banned,
- taken_by_ally,
- not_offered,
- }
- }
-
- #[test]
- fn every_reason_that_applies_is_given() {
- let pick = champ_select(session(
- 0,
- 2,
- 0,
- json!([[
- {"type": "ban", "championId": 1, "completed": true},
- {"type": "ban", "championId": 3, "completed": false},
- {"type": "pick", "championId": 4, "completed": true},
- ]]),
- json!({"myTeamBans": [5], "theirTeamBans": [2]}),
- ));
- // A completed ban, either team's ban list.
- assert_eq!(pick.unpickable(1), reasons(true, false, false));
- assert_eq!(pick.unpickable(5), reasons(true, false, false));
- // Banned and taken by an ally add up; a ban in progress or a pick action count for
- // nothing; a player of the other team is not an ally.
- assert_eq!(pick.unpickable(2), reasons(true, true, false));
- assert_eq!(pick.unpickable(3), Unpickable::default());
- assert_eq!(pick.unpickable(4), Unpickable::default());
- assert_eq!(pick.unpickable(77), reasons(false, false, true));
- // Not in the client's pickable list.
- assert_eq!(pick.unpickable(99), reasons(false, false, true));
- assert_eq!(pick.unpickable(6), Unpickable::default());
- // `can_pick` agrees with the reasons.
- for id in [1, 2, 3, 4, 5, 6, 77, 99] {
- assert_eq!(
- pick.can_pick(id),
- pick.unpickable(id) == Unpickable::default(),
- "champion {id}"
- );
- }
- }
-
- #[test]
- fn an_ally_pre_pick_takes_nothing_but_the_confirmed_pick_does() {
- let hover = champ_select(session(0, 0, 3, json!([]), json!({})));
- assert_eq!(hover.unpickable(3), Unpickable::default());
- let locked = champ_select(session(0, 3, 3, json!([]), json!({})));
- assert_eq!(locked.unpickable(3), reasons(false, true, false));
- }
-
- #[test]
- fn a_disabled_champion_is_not_offered() {
- let lists = ChampSelectLists::from_bodies("[1,2,3]", "[3]", None).unwrap();
- let pick = ChampSelect::from_session(
- &session(0, 0, 0, json!([]), json!({})),
- lists.pickable(),
- None,
- )
- .unwrap();
- assert_eq!(pick.unpickable(3), reasons(false, false, true));
- assert_eq!(pick.unpickable(1), Unpickable::default());
- }
-
- #[test]
- fn the_players_own_pick_is_never_greyed() {
- // Hovered then locked, though the data also bans it, takes it and does not offer it.
- let banned = json!([[{"type": "ban", "championId": 99, "completed": true}]]);
- let intent = champ_select(session(99, 99, 0, banned.clone(), json!({})));
- assert_eq!(intent.current, Some(99));
- assert_eq!(intent.unpickable(99), Unpickable::default());
- let mut locked = session(0, 99, 0, banned, json!({"myTeamBans": [99]}));
- locked["myTeam"][0]["championId"] = json!(99);
- let locked = champ_select(locked);
- assert_eq!(locked.unpickable(99), Unpickable::default());
- assert!(locked.can_pick(99));
- }
-
- #[test]
- fn two_champ_selects_differ_by_their_bans_and_ally_picks() {
- let plain = champ_select(session(0, 0, 0, json!([]), json!({})));
- let banned = champ_select(session(0, 0, 0, json!([]), json!({"myTeamBans": [3]})));
- let taken = champ_select(session(0, 3, 0, json!([]), json!({})));
- assert!(!plain.same_pickable(&banned));
- assert!(!plain.same_pickable(&taken));
- assert!(!banned.same_pickable(&taken));
- assert_ne!(plain, banned);
- }
-
- #[test]
- fn a_read_uses_the_session_of_an_event_and_reads_the_lists_once() {
- let reader = ChampSelectReader::default();
- let session = || {
- Ok(
- r#"{"localPlayerCellId":16,"myTeam":[{"cellId":16,"championId":0,
- "championPickIntent":0}],"actions":[],
- "bans":{"myTeamBans":[],"theirTeamBans":[]}}"#
- .to_string(),
- )
- };
- let lists = || {
- Ok((
- "[1,2]".to_string(),
- "[]".to_string(),
- Some("[9]".to_string()),
- ))
- };
- reader.read(session, lists).unwrap();
-
- // A session event replaces the request; the lists stay cached.
- reader.set_session(json!({
- "localPlayerCellId": 16,
- "myTeam": [{"cellId": 16, "championId": 2, "championPickIntent": 0}],
- "actions": [],
- "bans": {"myTeamBans": [], "theirTeamBans": []}
- }));
- let pick = reader
- .read(
- || panic!("the event session must be used"),
- || panic!("the lists must stay cached"),
- )
- .unwrap();
- assert_eq!(pick.current, Some(2));
- }
-}
+mod tests;
diff --git a/crates/core/src/champ_select/tests.rs b/crates/core/src/champ_select/tests.rs
new file mode 100644
index 0000000..51a150a
--- /dev/null
+++ b/crates/core/src/champ_select/tests.rs
@@ -0,0 +1,215 @@
+use super::*;
+use serde_json::json;
+
+#[test]
+fn the_lists_change_only_for_the_events_that_concern_them() {
+ let mut lists = ChampSelectLists::from_bodies("[1,2,3]", "[3]", Some("[9,8]")).unwrap();
+ assert_eq!(&*lists.pickable(), &HashSet::from([1, 2]));
+ assert_eq!(lists.anvil(), Some(&[9, 8][..]));
+
+ // A Delete carries no data: it changes nothing.
+ lists.set_pickable(&Value::Null);
+ lists.set_anvil(&Value::Null);
+ assert_eq!(&*lists.pickable(), &HashSet::from([1, 2]));
+ assert_eq!(lists.anvil(), Some(&[9, 8][..]));
+
+ lists.set_pickable(&json!([1, 4]));
+ assert_eq!(&*lists.pickable(), &HashSet::from([1, 4]));
+ assert_eq!(lists.anvil(), Some(&[9, 8][..]));
+
+ lists.set_disabled(&json!([1]));
+ assert_eq!(&*lists.pickable(), &HashSet::from([4]));
+
+ lists.set_anvil(&json!([5, 6]));
+ assert_eq!(lists.anvil(), Some(&[5, 6][..]));
+}
+
+#[test]
+fn the_anvil_champions_are_read_once_when_the_champ_select_starts() {
+ let anvil = |body| {
+ ChampSelectLists::from_bodies("[]", "[]", body)
+ .unwrap()
+ .anvil()
+ .map(<[u32]>::to_vec)
+ };
+ // No answer, or one that is not a list: the client gave no list.
+ assert_eq!(anvil(None), None);
+ assert_eq!(anvil(Some("not json")), None);
+ assert_eq!(anvil(Some(r#"{"error":"x"}"#)), None);
+ // An empty list is a list: the client says there are none.
+ assert_eq!(anvil(Some("[]")), Some(vec![]));
+ assert_eq!(anvil(Some("[1,2]")), Some(vec![1, 2]));
+ // The client's order is kept.
+ assert_eq!(anvil(Some("[9,1,5]")), Some(vec![9, 1, 5]));
+ // A malformed anvil answer only loses the anvil list, not the pickable one.
+ let lists = ChampSelectLists::from_bodies("[7]", "[]", Some("not json")).unwrap();
+ assert_eq!(&*lists.pickable(), &HashSet::from([7]));
+ assert_eq!(lists.anvil(), None);
+}
+
+#[test]
+fn an_anvil_event_gives_a_list_from_unknown_and_a_delete_keeps_the_value() {
+ let mut lists = ChampSelectLists::from_bodies("[]", "[]", None).unwrap();
+ lists.set_anvil(&Value::Null);
+ assert_eq!(lists.anvil(), None);
+ lists.set_anvil(&json!([4, 2]));
+ assert_eq!(lists.anvil(), Some(&[4, 2][..]));
+ lists.set_anvil(&Value::Null);
+ assert_eq!(lists.anvil(), Some(&[4, 2][..]));
+ // An empty list from an event is a known empty list.
+ lists.set_anvil(&json!([]));
+ assert_eq!(lists.anvil(), Some(&[][..]));
+}
+
+fn session(
+ local_intent: u32,
+ ally_id: u32,
+ ally_intent: u32,
+ actions: Value,
+ bans: Value,
+) -> Value {
+ json!({
+ "localPlayerCellId": 16,
+ "myTeam": [
+ {"cellId": 16, "team": 1, "championId": 0, "championPickIntent": local_intent},
+ {"cellId": 17, "team": 1, "championId": ally_id, "championPickIntent": ally_intent},
+ {"cellId": 18, "team": 2, "championId": 77, "championPickIntent": 0},
+ ],
+ "actions": actions,
+ "bans": bans,
+ })
+}
+
+fn champ_select(current_session: Value) -> ChampSelect {
+ ChampSelect::from_session(
+ ¤t_session,
+ Arc::new(HashSet::from([1, 2, 3, 4, 5, 6])),
+ Some(vec![1, 2]),
+ )
+ .unwrap()
+}
+
+fn reasons(banned: bool, taken_by_ally: bool, not_offered: bool) -> Unpickable {
+ Unpickable {
+ banned,
+ taken_by_ally,
+ not_offered,
+ }
+}
+
+#[test]
+fn every_reason_that_applies_is_given() {
+ let pick = champ_select(session(
+ 0,
+ 2,
+ 0,
+ json!([[
+ {"type": "ban", "championId": 1, "completed": true},
+ {"type": "ban", "championId": 3, "completed": false},
+ {"type": "pick", "championId": 4, "completed": true},
+ ]]),
+ json!({"myTeamBans": [5], "theirTeamBans": [2]}),
+ ));
+ // A completed ban, either team's ban list.
+ assert_eq!(pick.unpickable(1), reasons(true, false, false));
+ assert_eq!(pick.unpickable(5), reasons(true, false, false));
+ // Banned and taken by an ally add up; a ban in progress or a pick action count for
+ // nothing; a player of the other team is not an ally.
+ assert_eq!(pick.unpickable(2), reasons(true, true, false));
+ assert_eq!(pick.unpickable(3), Unpickable::default());
+ assert_eq!(pick.unpickable(4), Unpickable::default());
+ assert_eq!(pick.unpickable(77), reasons(false, false, true));
+ // Not in the client's pickable list.
+ assert_eq!(pick.unpickable(99), reasons(false, false, true));
+ assert_eq!(pick.unpickable(6), Unpickable::default());
+ // `can_pick` agrees with the reasons.
+ for id in [1, 2, 3, 4, 5, 6, 77, 99] {
+ assert_eq!(
+ pick.can_pick(id),
+ pick.unpickable(id) == Unpickable::default(),
+ "champion {id}"
+ );
+ }
+}
+
+#[test]
+fn an_ally_pre_pick_takes_nothing_but_the_confirmed_pick_does() {
+ let hover = champ_select(session(0, 0, 3, json!([]), json!({})));
+ assert_eq!(hover.unpickable(3), Unpickable::default());
+ let locked = champ_select(session(0, 3, 3, json!([]), json!({})));
+ assert_eq!(locked.unpickable(3), reasons(false, true, false));
+}
+
+#[test]
+fn a_disabled_champion_is_not_offered() {
+ let lists = ChampSelectLists::from_bodies("[1,2,3]", "[3]", None).unwrap();
+ let pick = ChampSelect::from_session(
+ &session(0, 0, 0, json!([]), json!({})),
+ lists.pickable(),
+ None,
+ )
+ .unwrap();
+ assert_eq!(pick.unpickable(3), reasons(false, false, true));
+ assert_eq!(pick.unpickable(1), Unpickable::default());
+}
+
+#[test]
+fn the_players_own_pick_is_never_greyed() {
+ // Hovered then locked, though the data also bans it, takes it and does not offer it.
+ let banned = json!([[{"type": "ban", "championId": 99, "completed": true}]]);
+ let intent = champ_select(session(99, 99, 0, banned.clone(), json!({})));
+ assert_eq!(intent.current, Some(99));
+ assert_eq!(intent.unpickable(99), Unpickable::default());
+ let mut locked = session(0, 99, 0, banned, json!({"myTeamBans": [99]}));
+ locked["myTeam"][0]["championId"] = json!(99);
+ let locked = champ_select(locked);
+ assert_eq!(locked.unpickable(99), Unpickable::default());
+ assert!(locked.can_pick(99));
+}
+
+#[test]
+fn two_champ_selects_differ_by_their_bans_and_ally_picks() {
+ let plain = champ_select(session(0, 0, 0, json!([]), json!({})));
+ let banned = champ_select(session(0, 0, 0, json!([]), json!({"myTeamBans": [3]})));
+ let taken = champ_select(session(0, 3, 0, json!([]), json!({})));
+ assert!(!plain.same_pickable(&banned));
+ assert!(!plain.same_pickable(&taken));
+ assert!(!banned.same_pickable(&taken));
+ assert_ne!(plain, banned);
+}
+
+#[test]
+fn a_read_uses_the_session_of_an_event_and_reads_the_lists_once() {
+ let reader = ChampSelectReader::default();
+ let session = || {
+ Ok(
+ r#"{"localPlayerCellId":16,"myTeam":[{"cellId":16,"championId":0,
+ "championPickIntent":0}],"actions":[],
+ "bans":{"myTeamBans":[],"theirTeamBans":[]}}"#
+ .to_string(),
+ )
+ };
+ let lists = || {
+ Ok((
+ "[1,2]".to_string(),
+ "[]".to_string(),
+ Some("[9]".to_string()),
+ ))
+ };
+ reader.read(session, lists).unwrap();
+
+ // A session event replaces the request; the lists stay cached.
+ reader.set_session(json!({
+ "localPlayerCellId": 16,
+ "myTeam": [{"cellId": 16, "championId": 2, "championPickIntent": 0}],
+ "actions": [],
+ "bans": {"myTeamBans": [], "theirTeamBans": []}
+ }));
+ let pick = reader
+ .read(
+ || panic!("the event session must be used"),
+ || panic!("the lists must stay cached"),
+ )
+ .unwrap();
+ assert_eq!(pick.current, Some(2));
+}
diff --git a/crates/core/src/follow.rs b/crates/core/src/follow.rs
index ab3bdf6..0ccdb51 100644
--- a/crates/core/src/follow.rs
+++ b/crates/core/src/follow.rs
@@ -1,16 +1,17 @@
//! Keeping the active Account up to date by following the game client.
+mod pending;
+mod progress;
use std::sync::{MutexGuard, PoisonError};
use std::thread;
use std::time::Duration;
-use rusqlite::{OptionalExtension, params};
+use rusqlite::OptionalExtension;
use crate::client::{ClientEvent, ClientSnapshot, GameClientSource, GamePhase, Gameflow};
use crate::error::{Error, SourceError};
use crate::model::{Account, FollowedMatch, SyncProgress, SyncReport};
use crate::seasons::Season;
-use crate::statistics::{Scope, SeasonScope};
-use crate::tracker::{Fetched, Tracker, account_from_row};
+use crate::tracker::{Tracker, account_from_row};
/// Tries at the Match of an Arena game that just ended, one per `RETRY_EVERY_MS` (about 30
/// minutes: Riot publishes it only when the whole game is over, which a player eliminated early
@@ -483,100 +484,6 @@ impl Tracker {
result
}
- /// One try at the Match of a finished Arena game: fetched by its id when the client gave it
- /// and a stored Match gives the platform, else looked for by a normal sync. Returns the
- /// number of Matches added and whether the wait for this one is over.
- fn look_for_match(
- &self,
- account: &Account,
- game_id: Option,
- on_progress: &mut dyn FnMut(SyncProgress),
- on_match: &mut dyn FnMut(FollowedMatch),
- ) -> (usize, bool) {
- let puuid = &account.puuid;
- let platform = self.platform_of(puuid).ok().flatten();
- let Some(match_id) = game_id.zip(platform).map(|(id, p)| format!("{p}_{id}")) else {
- let newest_before = self.latest_start_ms(puuid).ok().flatten();
- // The earliest bound of the games waiting without an id, if they all have one.
- let started_after = (self.follow().pending.iter())
- .filter(|p| p.game_id.is_none())
- .map(|p| p.started_after_ms)
- .min()
- .flatten();
- // Not over: `update` ends the waits without an id, a fetch by id ends the others.
- let added = self
- .update(&account_riot_id(account), on_progress)
- .map_or(0, |report| report.added);
- if added > 0
- && let Ok(Some(id)) =
- self.followed_in_sync(puuid, game_id, newest_before, started_after)
- {
- self.report(puuid, &id, on_match);
- }
- return (added, false);
- };
- match self.fetch_and_store(puuid, &match_id) {
- Ok(fetched) => {
- let added = usize::from(fetched == Fetched::Added);
- self.clear_problem(added);
- if added > 0 {
- self.refresh_mastery(puuid);
- self.report(puuid, &match_id, on_match);
- }
- (added, fetched != Fetched::NotFound)
- }
- Err(e) => {
- self.record(UpdateProblem::from(&e));
- (0, false)
- }
- }
- }
-
- /// The followed game among the Matches a sync has just stored: the one with the client's id
- /// for it, else the newest Match when it is newer than every Match stored before and started
- /// after the game's bound (`Pending::started_after_ms`): an older Match the sync also brings
- /// is history. Without that id nor a bound, a first import is only history.
- fn followed_in_sync(
- &self,
- puuid: &str,
- game_id: Option,
- newest_before: Option,
- started_after: Option,
- ) -> Result