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 <noreply@anthropic.com>
136 lines
4.1 KiB
Rust
136 lines
4.1 KiB
Rust
use std::ops::Range;
|
|
use std::path::Path;
|
|
|
|
use serde::Deserialize;
|
|
|
|
use crate::date::Date;
|
|
use crate::error::Error;
|
|
|
|
const DEFAULT_TABLE: &str = include_str!("default_seasons.toml");
|
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
|
pub struct Season {
|
|
pub name: String,
|
|
pub start: Date,
|
|
/// Name of the game client's progression track holding this Season's Official Progress.
|
|
pub official_track: Option<String>,
|
|
}
|
|
|
|
#[derive(Deserialize)]
|
|
struct Table {
|
|
season: Vec<Entry>,
|
|
}
|
|
|
|
#[derive(Deserialize)]
|
|
struct Entry {
|
|
name: String,
|
|
start: toml::value::Datetime,
|
|
official_track: Option<String>,
|
|
}
|
|
|
|
/// The table shipped with the app, oldest first.
|
|
pub fn default_seasons() -> Vec<Season> {
|
|
parse(DEFAULT_TABLE).expect("the shipped Season table is valid")
|
|
}
|
|
|
|
/// Reads the user's Season table at `path`, first writing the shipped one if the file does
|
|
/// not exist so the user can edit it. Returns the Seasons sorted by start.
|
|
pub fn load_seasons(path: &Path) -> Result<Vec<Season>, Error> {
|
|
let io_error = |e: std::io::Error| Error::Config(format!("{} : {e}", path.display()));
|
|
if !path.exists() {
|
|
if let Some(dir) = path.parent() {
|
|
std::fs::create_dir_all(dir).map_err(io_error)?;
|
|
}
|
|
std::fs::write(path, DEFAULT_TABLE).map_err(io_error)?;
|
|
}
|
|
let text = std::fs::read_to_string(path).map_err(io_error)?;
|
|
parse(&text).map_err(|e| Error::Config(format!("{} : {e}", path.display())))
|
|
}
|
|
|
|
fn parse(text: &str) -> Result<Vec<Season>, String> {
|
|
let table: Table = toml::from_str(text).map_err(|e| e.to_string())?;
|
|
let mut seasons = Vec::new();
|
|
for entry in table.season {
|
|
let start = match entry.start {
|
|
toml::value::Datetime {
|
|
date: Some(d),
|
|
time: None,
|
|
offset: None,
|
|
} => Date {
|
|
year: i32::from(d.year),
|
|
month: d.month,
|
|
day: d.day,
|
|
},
|
|
_ => {
|
|
return Err(format!(
|
|
"« {} » : `start` doit être une date (AAAA-MM-JJ)",
|
|
entry.name
|
|
));
|
|
}
|
|
};
|
|
seasons.push(Season {
|
|
name: entry.name,
|
|
start,
|
|
official_track: entry.official_track,
|
|
});
|
|
}
|
|
seasons.sort_by_key(|s| s.start);
|
|
Ok(seasons)
|
|
}
|
|
|
|
/// The period of a Season in epoch ms: from its start up to the next Season's start
|
|
/// (exclusive); the last one is open-ended. `seasons` must be sorted by start.
|
|
pub(crate) fn period(seasons: &[Season], name: &str) -> Option<Range<i64>> {
|
|
let i = seasons.iter().position(|s| s.name == name)?;
|
|
let end = seasons
|
|
.get(i + 1)
|
|
.map_or(i64::MAX, |next| next.start.start_ms());
|
|
Some(seasons[i].start.start_ms()..end)
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn the_shipped_table_lists_the_known_seasons() {
|
|
let seasons: Vec<_> = default_seasons()
|
|
.into_iter()
|
|
.map(|s| (s.name, s.start.to_string()))
|
|
.collect();
|
|
assert_eq!(
|
|
seasons,
|
|
[
|
|
("Saison 2025".into(), "05/03/2025".into()),
|
|
("Saison 1 2026".into(), "08/01/2026".into()),
|
|
("Saison 2 2026".into(), "29/04/2026".into()),
|
|
]
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn the_shipped_table_carries_the_official_track_names() {
|
|
let tracks: Vec<_> = default_seasons()
|
|
.into_iter()
|
|
.map(|s| s.official_track)
|
|
.collect();
|
|
assert_eq!(
|
|
tracks,
|
|
[
|
|
Some("Cherry 2025 Champs Completed Track".into()),
|
|
Some("2026 Season 1 Champs Completed Track".into()),
|
|
Some("2026 Season 2 Champs Completed Track".into()),
|
|
]
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn periods_run_to_the_next_start_and_the_last_is_open() {
|
|
let seasons = default_seasons();
|
|
let first = period(&seasons, "Saison 2025").unwrap();
|
|
assert_eq!(first.end, seasons[1].start.start_ms());
|
|
assert_eq!(period(&seasons, "Saison 2 2026").unwrap().end, i64::MAX);
|
|
assert!(period(&seasons, "Inconnue").is_none());
|
|
}
|
|
}
|