From c44cf52a6a80388419ef66c6adf45110a285c887 Mon Sep 17 00:00:00 2001
From: Nessar
Date: Mon, 16 Nov 2026 19:06:52 +0100
Subject: [PATCH] Add core: Match import, statistics and game client following
Core half of tickets #4 to #12, behind one public interface tested with
fake sources and a real SQLite database:
- import of Arena Match history from the Riot API, idempotent and
resumable, with a sync mark written only when a sync completes
- Season table, champion catalog, Challenge summary, champion rows,
chart series, Season and Format scopes
- game client port and adapter: signed-in Account, end of an Arena
game fetched by its id, Official Progress cross-check
- update problems reported for banners while stored data stays readable
The app crate is adapted in the next commit.
Co-Authored-By: Claude Fable 5.2
---
Cargo.lock | 173 +++++
crates/core/Cargo.toml | 11 +
crates/core/src/champions.rs | 246 +++++++
crates/core/src/client.rs | 78 ++
crates/core/src/config.rs | 166 +++++
crates/core/src/date.rs | 97 +++
crates/core/src/default_seasons.toml | 18 +
crates/core/src/error.rs | 71 ++
crates/core/src/follow.rs | 509 +++++++++++++
crates/core/src/league_client.rs | 558 ++++++++++++++
crates/core/src/lib.rs | 40 +-
crates/core/src/model.rs | 90 +++
crates/core/src/riot.rs | 258 +++++++
crates/core/src/seasons.rs | 135 ++++
crates/core/src/statistics.rs | 380 ++++++++++
crates/core/src/tracker.rs | 492 +++++++++++++
crates/core/tests/champions.rs | 248 +++++++
crates/core/tests/common/mod.rs | 671 +++++++++++++++++
crates/core/tests/follow.rs | 1025 ++++++++++++++++++++++++++
crates/core/tests/import.rs | 624 ++++++++++++++++
crates/core/tests/statistics.rs | 595 +++++++++++++++
21 files changed, 6472 insertions(+), 13 deletions(-)
create mode 100644 crates/core/src/champions.rs
create mode 100644 crates/core/src/client.rs
create mode 100644 crates/core/src/config.rs
create mode 100644 crates/core/src/date.rs
create mode 100644 crates/core/src/default_seasons.toml
create mode 100644 crates/core/src/error.rs
create mode 100644 crates/core/src/follow.rs
create mode 100644 crates/core/src/league_client.rs
create mode 100644 crates/core/src/model.rs
create mode 100644 crates/core/src/riot.rs
create mode 100644 crates/core/src/seasons.rs
create mode 100644 crates/core/src/statistics.rs
create mode 100644 crates/core/src/tracker.rs
create mode 100644 crates/core/tests/champions.rs
create mode 100644 crates/core/tests/common/mod.rs
create mode 100644 crates/core/tests/follow.rs
create mode 100644 crates/core/tests/import.rs
create mode 100644 crates/core/tests/statistics.rs
diff --git a/Cargo.lock b/Cargo.lock
index 5c01f86..d2a94c7 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -213,6 +213,17 @@ dependencies = [
[[package]]
name = "arena-tracker-core"
version = "0.1.0"
+dependencies = [
+ "base64 0.23.1",
+ "rusqlite",
+ "rustls",
+ "serde",
+ "serde_json",
+ "tempfile",
+ "toml 1.2.1+spec-1.1.0",
+ "tungstenite",
+ "ureq",
+]
[[package]]
name = "arg_enum_proc_macro"
@@ -1167,6 +1178,12 @@ dependencies = [
"linktime-proc-macro",
]
+[[package]]
+name = "data-encoding"
+version = "2.11.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "4583a4551df46e2792f82ceeac45e850d2e2d5debba0b91f102385cda5b11f06"
+
[[package]]
name = "data-url"
version = "0.3.2"
@@ -1533,6 +1550,18 @@ dependencies = [
"zune-inflate",
]
+[[package]]
+name = "fallible-iterator"
+version = "0.3.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "2acce4a10f12dc2fb14a218589d4f1f62ef011b2d0cc4b3cb1bba8e94da14649"
+
+[[package]]
+name = "fallible-streaming-iterator"
+version = "0.1.9"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7360491ce676a36bf9bb3c56c1aa791658183a54d2744120f27285738d90465a"
+
[[package]]
name = "fastrand"
version = "2.5.0"
@@ -2709,6 +2738,15 @@ dependencies = [
"foldhash",
]
+[[package]]
+name = "hashlink"
+version = "0.12.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a596f1b20ed2cc5ecac41a164aaebc7258057060f06c0cf7a2ba3991ee7990fb"
+dependencies = [
+ "hashbrown 0.17.1",
+]
+
[[package]]
name = "heapless"
version = "0.9.3"
@@ -3416,6 +3454,17 @@ dependencies = [
"libc",
]
+[[package]]
+name = "libsqlite3-sys"
+version = "0.40.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b06e69070b7e12262b0cd90832f0a7396f142a2b330181255562decf9f0c6215"
+dependencies = [
+ "cc",
+ "pkg-config",
+ "vcpkg",
+]
+
[[package]]
name = "linebender_resource_handle"
version = "0.1.1"
@@ -5292,6 +5341,30 @@ dependencies = [
"memchr",
]
+[[package]]
+name = "rsqlite-vfs"
+version = "0.2.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "353b74f9810d5b085b6c42c9d86a5f0fdb795553235cdd48753b32e3809d18a7"
+dependencies = [
+ "thiserror 2.0.21",
+]
+
+[[package]]
+name = "rusqlite"
+version = "0.43.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f172c4812a19b26d1c23cdb0070e69db335503dfa378b910eb857386e5b8beba"
+dependencies = [
+ "bitflags 2.13.2",
+ "fallible-iterator",
+ "fallible-streaming-iterator",
+ "hashlink",
+ "libsqlite3-sys",
+ "smallvec",
+ "sqlite-wasm-rs",
+]
+
[[package]]
name = "rust-embed"
version = "8.14.0"
@@ -5423,6 +5496,7 @@ version = "0.23.46"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "48e13bd8c0e9365c43cfa5c9e8f9ad49d3c8444926c9aac819e0e4dc503c8fdf"
dependencies = [
+ "log",
"once_cell",
"ring",
"rustls-pki-types",
@@ -5751,6 +5825,17 @@ dependencies = [
"serde",
]
+[[package]]
+name = "sha1"
+version = "0.11.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "aacc4cc499359472b4abe1bf11d0b12e688af9a805fa5e3016f9a386dc2d0214"
+dependencies = [
+ "cfg-if",
+ "cpufeatures 0.3.1",
+ "digest 0.11.3",
+]
+
[[package]]
name = "sha1_smol"
version = "1.0.1"
@@ -5954,6 +6039,16 @@ dependencies = [
"bitflags 2.13.2",
]
+[[package]]
+name = "sqlite-wasm-rs"
+version = "0.6.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a6cd504cecbfe7eef649988840f9a6cb768d11085efec138f8e99f160632d867"
+dependencies = [
+ "cc",
+ "rsqlite-vfs",
+]
+
[[package]]
name = "stable_deref_trait"
version = "1.2.1"
@@ -6581,6 +6676,25 @@ dependencies = [
"core_maths",
]
+[[package]]
+name = "tungstenite"
+version = "0.30.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e48ac77174b19c110a50ab2128b24215ac9cb40e0e12e093fb602d175c569d22"
+dependencies = [
+ "bytes",
+ "data-encoding",
+ "http",
+ "httparse",
+ "log",
+ "rand 0.10.4",
+ "rustls",
+ "rustls-pki-types",
+ "sha1",
+ "thiserror 2.0.21",
+ "webpki-roots 0.26.11",
+]
+
[[package]]
name = "typenum"
version = "1.20.1"
@@ -6676,6 +6790,35 @@ version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1"
+[[package]]
+name = "ureq"
+version = "3.5.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "3ac5cecf619ca81f66a96482db09687c9f7465af65f4300a8c30cca05bd5a59d"
+dependencies = [
+ "base64 0.23.1",
+ "flate2",
+ "log",
+ "percent-encoding",
+ "rustls",
+ "rustls-pki-types",
+ "ureq-proto",
+ "utf8-zero",
+ "webpki-roots 1.0.12",
+]
+
+[[package]]
+name = "ureq-proto"
+version = "0.7.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "622dce33c252115466d83f63e9744a18f12b4406aae51a01c15ad8fcf1185eb3"
+dependencies = [
+ "base64 0.23.1",
+ "http",
+ "httparse",
+ "log",
+]
+
[[package]]
name = "url"
version = "2.5.8"
@@ -6748,6 +6891,12 @@ version = "0.7.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9"
+[[package]]
+name = "utf8-zero"
+version = "0.8.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b8c0a043c9540bae7c578c88f91dda8bd82e59ae27c21baca69c8b191aaf5a6e"
+
[[package]]
name = "utf8_iter"
version = "1.1.0"
@@ -6787,6 +6936,12 @@ dependencies = [
"wasm-bindgen",
]
+[[package]]
+name = "vcpkg"
+version = "0.2.15"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426"
+
[[package]]
name = "version_check"
version = "0.9.5"
@@ -7052,6 +7207,24 @@ dependencies = [
"wasm-bindgen",
]
+[[package]]
+name = "webpki-roots"
+version = "0.26.11"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "521bc38abb08001b01866da9f51eb7c5d647a19260e00054a8c7fd5f9e57f7a9"
+dependencies = [
+ "webpki-roots 1.0.12",
+]
+
+[[package]]
+name = "webpki-roots"
+version = "1.0.12"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7265c9301c16a8711c992c2d8da90ee77ef54b1ed51482d6dfeb00dc756b1297"
+dependencies = [
+ "rustls-pki-types",
+]
+
[[package]]
name = "weezl"
version = "0.1.12"
diff --git a/crates/core/Cargo.toml b/crates/core/Cargo.toml
index 92e5eb9..e4d53e4 100644
--- a/crates/core/Cargo.toml
+++ b/crates/core/Cargo.toml
@@ -5,3 +5,14 @@ rust-version.workspace = true
version.workspace = true
[dependencies]
+base64 = { version = "0.23.1", default-features = false, features = ["std"] }
+rusqlite = { version = "0.43.0", features = ["bundled"] }
+rustls = { version = "0.23.46", default-features = false, features = ["ring", "std", "tls12"] }
+serde = { version = "1.0.229", features = ["derive"] }
+serde_json = "1.0.154"
+toml = "1.2.1"
+tungstenite = { version = "0.30.0", default-features = false, features = ["handshake", "rustls-tls-webpki-roots"] }
+ureq = "3.5.2"
+
+[dev-dependencies]
+tempfile = "3.27.0"
diff --git a/crates/core/src/champions.rs b/crates/core/src/champions.rs
new file mode 100644
index 0000000..c42e99d
--- /dev/null
+++ b/crates/core/src/champions.rs
@@ -0,0 +1,246 @@
+use std::collections::BTreeMap;
+use std::path::PathBuf;
+
+use rusqlite::params;
+use serde::Deserialize;
+use ureq::Agent;
+
+use crate::error::{Error, SourceError};
+use crate::riot::{agent, encode, get};
+use crate::tracker::Tracker;
+
+#[derive(Clone, Debug, PartialEq, Eq)]
+pub struct Champion {
+ pub id: u32,
+ /// Data Dragon id, e.g. "MasterYi"; also the portrait's file name.
+ pub key: String,
+ /// French display name, e.g. "Maître Yi".
+ pub name: String,
+}
+
+#[derive(Clone, Debug, PartialEq, Eq)]
+pub struct ChampionCatalog {
+ pub version: String,
+ pub champions: Vec,
+}
+
+/// Port: champion reference data (Riot's public game data). Real adapter: `DataDragon`.
+pub trait ChampionSource: Send + Sync {
+ fn latest_version(&self) -> Result;
+ fn catalog(&self, version: &str) -> Result;
+ /// PNG bytes.
+ fn portrait(&self, version: &str, key: &str) -> Result, SourceError>;
+}
+
+const HOST: &str = "https://ddragon.leagueoflegends.com";
+
+/// Data Dragon, Riot's public static game data (no API key, no rate limit).
+pub struct DataDragon {
+ agent: Agent,
+}
+
+impl DataDragon {
+ pub fn new() -> DataDragon {
+ DataDragon { agent: agent() }
+ }
+}
+
+impl Default for DataDragon {
+ fn default() -> Self {
+ DataDragon::new()
+ }
+}
+
+impl ChampionSource for DataDragon {
+ fn latest_version(&self) -> Result {
+ let url = format!("{HOST}/api/versions.json");
+ decode_latest_version(&get(&self.agent, &url, None, |b| b.read_to_string())?)
+ }
+
+ fn catalog(&self, version: &str) -> Result {
+ let url = format!("{HOST}/cdn/{}/data/fr_FR/champion.json", encode(version));
+ decode_catalog(
+ version,
+ &get(&self.agent, &url, None, |b| b.read_to_string())?,
+ )
+ }
+
+ fn portrait(&self, version: &str, key: &str) -> Result, SourceError> {
+ let url = format!(
+ "{HOST}/cdn/{}/img/champion/{}.png",
+ encode(version),
+ encode(key)
+ );
+ get(&self.agent, &url, None, |b| b.read_to_vec())
+ }
+}
+
+fn decode_latest_version(body: &str) -> Result {
+ let versions: Vec = serde_json::from_str(body)
+ .map_err(|e| SourceError::Other(format!("Versions illisibles : {e}")))?;
+ versions
+ .into_iter()
+ .next()
+ .ok_or_else(|| SourceError::Other("Aucune version de jeu publiée.".into()))
+}
+
+#[derive(Deserialize)]
+struct CatalogFile {
+ data: BTreeMap,
+}
+
+#[derive(Deserialize)]
+struct CatalogEntry {
+ id: String,
+ /// The numeric champion id, as a string.
+ key: String,
+ name: String,
+}
+
+fn decode_catalog(version: &str, body: &str) -> Result {
+ let file: CatalogFile = serde_json::from_str(body)
+ .map_err(|e| SourceError::Other(format!("Champions illisibles : {e}")))?;
+ let mut champions = file
+ .data
+ .into_values()
+ .map(|entry| {
+ let id = entry.key.parse().map_err(|_| {
+ SourceError::Other(format!("Identifiant de champion invalide : {}", entry.key))
+ })?;
+ Ok(Champion {
+ id,
+ key: entry.id,
+ name: entry.name,
+ })
+ })
+ .collect::, SourceError>>()?;
+ champions.sort_by_key(|c| c.id);
+ Ok(ChampionCatalog {
+ version: version.into(),
+ champions,
+ })
+}
+
+impl Tracker {
+ /// Champion reference data comes from `source`; portraits are kept in `portraits_dir`.
+ pub fn with_champions(
+ mut self,
+ source: Box,
+ portraits_dir: PathBuf,
+ ) -> Tracker {
+ self.champions = Some((source, portraits_dir));
+ self
+ }
+
+ /// Refreshes the cached champion catalog when the game version changed, then downloads
+ /// the portraits missing from the portraits dir. A failed portrait is skipped; a failed
+ /// version or catalog call is returned and leaves the cached catalog untouched.
+ pub fn refresh_champions(&self) -> Result<(), Error> {
+ let Some((source, portraits_dir)) = &self.champions else {
+ return Ok(());
+ };
+ let version = source.latest_version()?;
+ if self.cached_version()?.as_deref() != Some(version.as_str()) {
+ let catalog = source.catalog(&version)?;
+ self.store_catalog(&version, &catalog.champions)?;
+ }
+
+ std::fs::create_dir_all(portraits_dir)
+ .map_err(|e| Error::Storage(format!("{} : {e}", portraits_dir.display())))?;
+ for champion in self.cached_champions()? {
+ let path = portraits_dir.join(format!("{}.png", champion.key));
+ // The key becomes a file name: only trust plain names.
+ if path.exists() || !champion.key.chars().all(|c| c.is_ascii_alphanumeric()) {
+ continue;
+ }
+ if let Ok(png) = source.portrait(&version, &champion.key) {
+ // Written under another name first so a cut download is never taken for a portrait.
+ let partial = path.with_extension("part");
+ if std::fs::write(&partial, png).is_ok() {
+ let _ = std::fs::rename(&partial, &path);
+ }
+ }
+ }
+ Ok(())
+ }
+
+ pub(crate) fn cached_champions(&self) -> Result, Error> {
+ let db = self.db();
+ let mut stmt = db.prepare("SELECT id, key, name FROM champions")?;
+ let champions = stmt
+ .query_map([], |row| {
+ Ok(Champion {
+ id: row.get(0)?,
+ key: row.get(1)?,
+ name: row.get(2)?,
+ })
+ })?
+ .collect::>()?;
+ Ok(champions)
+ }
+
+ fn cached_version(&self) -> Result