Play the strongest Match on its own champion and report every followed game (#89)
CI / core (pull_request) Successful in 1m40s
CI / core (pull_request) Successful in 1m40s
From the review of PR #99: - Of several followed Matches waiting, the strongest plays on its own champion (tick, card and splash), the most recent among equals. The comet no longer flies to one champion's tick with another's splash. - A game the client gave no id for, or seen before any Match gave the platform, is reported when the sync stores it: the client's id when it has one, else the newest Match newer than every one stored. A first import without that id stays history. - What a Match brought comes from the Challenge's own Marks, with and without it, instead of a second set of rules in SQL; `fetch_and_store` is back to fetching and storing. `MatchEnd::Completed` takes the glossary's word, and its variants are documented again. - Only the count of the banner's title rolls in, from 10 px below, as in the prototype; the « Dernière Partie » line still fades in. - The Windows setting is checked where a Match is queued, and the tests now go through the follower round and the statistics; a test checks that the end state is what a reload shows. - The Match's state is one `MatchPlayback`, ended in one place; the durations are named; `MarkFill`, `Changing` and `RevealFrame` replace bare tuples; a splash that cannot be read ends its reveal. - `follow_client_with` takes the report itself, without a forwarding function. docs/performance.md describes the Match animation. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
126912fd09
commit
44c2d10b3b
14 files changed
+711
-325
No files matched your search
+62
-21
@@ -3,11 +3,11 @@ use std::sync::{MutexGuard, PoisonError};
|
||||
use std::thread;
|
||||
use std::time::Duration;
|
||||
|
||||
use rusqlite::OptionalExtension;
|
||||
use rusqlite::{OptionalExtension, params};
|
||||
|
||||
use crate::client::{ClientEvent, ClientSnapshot, GameClientSource, GamePhase, Gameflow};
|
||||
use crate::error::{Error, SourceError};
|
||||
use crate::model::{Account, SyncProgress, SyncReport};
|
||||
use crate::model::{Account, FollowedMatch, SyncProgress, SyncReport};
|
||||
use crate::seasons::Season;
|
||||
use crate::statistics::{Scope, SeasonScope};
|
||||
use crate::tracker::{Fetched, Tracker, account_from_row};
|
||||
@@ -204,6 +204,7 @@ impl Tracker {
|
||||
fallback_riot_id,
|
||||
&self.client_snapshot(),
|
||||
on_progress,
|
||||
&mut |_| {},
|
||||
)
|
||||
}
|
||||
|
||||
@@ -216,25 +217,16 @@ impl Tracker {
|
||||
}
|
||||
}
|
||||
|
||||
/// A follower round on a client read already made for this wake.
|
||||
/// A follower round on a client read already made for this wake. `on_match` hears of each
|
||||
/// Match of a game the client saw end, once, when the round stores it; never of a Match
|
||||
/// stored by a sync of the history.
|
||||
pub fn follow_client_with(
|
||||
&self,
|
||||
now_ms: i64,
|
||||
fallback_riot_id: Option<&str>,
|
||||
snapshot: &ClientSnapshot,
|
||||
on_progress: &mut dyn FnMut(SyncProgress),
|
||||
) -> bool {
|
||||
self.follow_client_reporting(now_ms, fallback_riot_id, snapshot, on_progress, &mut |_| {})
|
||||
}
|
||||
|
||||
/// Reports only newly inserted Matches identified by the live gameflow.
|
||||
pub fn follow_client_reporting(
|
||||
&self,
|
||||
now_ms: i64,
|
||||
fallback_riot_id: Option<&str>,
|
||||
snapshot: &ClientSnapshot,
|
||||
on_progress: &mut dyn FnMut(SyncProgress),
|
||||
on_match: &mut dyn FnMut(crate::FollowedMatch),
|
||||
on_match: &mut dyn FnMut(FollowedMatch),
|
||||
) -> bool {
|
||||
let before = self.status();
|
||||
let mut added = 0;
|
||||
@@ -479,20 +471,30 @@ impl Tracker {
|
||||
account: &Account,
|
||||
game_id: Option<u64>,
|
||||
on_progress: &mut dyn FnMut(SyncProgress),
|
||||
on_match: &mut dyn FnMut(crate::FollowedMatch),
|
||||
on_match: &mut dyn FnMut(FollowedMatch),
|
||||
) -> (usize, bool) {
|
||||
let platform = self.platform_of(&account.puuid).ok().flatten();
|
||||
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();
|
||||
// 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);
|
||||
return (added.map_or(0, |report| report.added), false);
|
||||
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)
|
||||
{
|
||||
self.report(puuid, &id, on_match);
|
||||
}
|
||||
return (added, false);
|
||||
};
|
||||
match self.fetch_and_store(&account.puuid, &match_id, Some(on_match)) {
|
||||
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(&account.puuid);
|
||||
self.refresh_mastery(puuid);
|
||||
self.report(puuid, &match_id, on_match);
|
||||
}
|
||||
(added, fetched != Fetched::NotFound)
|
||||
}
|
||||
@@ -503,6 +505,45 @@ impl Tracker {
|
||||
}
|
||||
}
|
||||
|
||||
/// 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. Without
|
||||
/// that id, a first import is only history.
|
||||
fn followed_in_sync(
|
||||
&self,
|
||||
puuid: &str,
|
||||
game_id: Option<u64>,
|
||||
newest_before: Option<i64>,
|
||||
) -> Result<Option<String>, Error> {
|
||||
let db = self.db();
|
||||
let newest = match game_id {
|
||||
// Only reached without a platform, so without any Match stored before the sync.
|
||||
Some(id) => db.query_row(
|
||||
"SELECT match_id, started_at_ms FROM matches
|
||||
WHERE puuid = ?1 AND match_id LIKE '%\\_' || ?2 ESCAPE '\\'",
|
||||
params![puuid, id.to_string()],
|
||||
|row| Ok((row.get::<_, String>(0)?, i64::MIN)),
|
||||
),
|
||||
None => db.query_row(
|
||||
"SELECT match_id, started_at_ms FROM matches WHERE puuid = ?1
|
||||
ORDER BY started_at_ms DESC LIMIT 1",
|
||||
[puuid],
|
||||
|row| Ok((row.get(0)?, row.get(1)?)),
|
||||
),
|
||||
}
|
||||
.optional()?;
|
||||
Ok(newest
|
||||
.filter(|(_, started)| game_id.is_some() || newest_before.is_some_and(|b| *started > b))
|
||||
.map(|(id, _)| id))
|
||||
}
|
||||
|
||||
/// Tells `on_match` what the stored Match `match_id` brought; a read error only loses the
|
||||
/// animation.
|
||||
fn report(&self, puuid: &str, match_id: &str, on_match: &mut dyn FnMut(FollowedMatch)) {
|
||||
if let Ok(Some(event)) = self.followed_match(puuid, match_id) {
|
||||
on_match(event);
|
||||
}
|
||||
}
|
||||
|
||||
fn remember_active(&self, account: &Account) -> Result<(), Error> {
|
||||
self.db().execute(
|
||||
"INSERT OR REPLACE INTO meta (name, value) VALUES ('last_active_puuid', ?1)",
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
use crate::statistics::Marks;
|
||||
use serde::Deserialize;
|
||||
|
||||
/// One Arena variant, derived from the game queue.
|
||||
@@ -51,14 +52,33 @@ pub struct Match {
|
||||
pub started_at_ms: i64,
|
||||
}
|
||||
|
||||
/// The strongest milestone gained when a followed Match is first stored.
|
||||
/// What a followed Match brought its champion's Challenge, from the weakest to the strongest:
|
||||
/// the animation it plays on « Défi » is in proportion.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
|
||||
pub enum MatchEnd {
|
||||
/// Nothing new: the tick blinks.
|
||||
Nothing,
|
||||
/// A new Mark, « Joué » or « Victoire »: the tick steps up.
|
||||
Mark,
|
||||
Completion,
|
||||
/// The champion's first top 1, it is Completed: the comet.
|
||||
Completed,
|
||||
}
|
||||
|
||||
impl MatchEnd {
|
||||
/// The strongest change from the champion's Marks without the Match to those with it.
|
||||
pub fn between(without: Marks, with: Marks) -> MatchEnd {
|
||||
if with.completed && !without.completed {
|
||||
MatchEnd::Completed
|
||||
} else if with != without {
|
||||
MatchEnd::Mark
|
||||
} else {
|
||||
MatchEnd::Nothing
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A Match the live follow saw end and stored, with what it brought its champion's Challenge
|
||||
/// in its Season. A Match stored by a sync of the history is never one.
|
||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||
pub struct FollowedMatch {
|
||||
pub puuid: String,
|
||||
|
||||
@@ -8,7 +8,7 @@ use std::path::PathBuf;
|
||||
use crate::champions::{Champion, card_portrait_path, cards_dir, file_names};
|
||||
use crate::date::Date;
|
||||
use crate::error::Error;
|
||||
use crate::model::{Format, Match};
|
||||
use crate::model::{FollowedMatch, Format, Match, MatchEnd};
|
||||
use crate::seasons::{Season, period};
|
||||
use crate::splash::{Splash, cached_splashes, splashes_dir};
|
||||
use crate::tracker::Tracker;
|
||||
@@ -135,6 +135,16 @@ pub struct Marks {
|
||||
}
|
||||
|
||||
impl ChampionChallenge {
|
||||
/// The figures of a champion's progress; zeros for a champion never played.
|
||||
fn of(progress: Option<&Progress>) -> ChampionChallenge {
|
||||
progress.map_or_else(ChampionChallenge::default, |p| ChampionChallenge {
|
||||
matches: p.matches,
|
||||
wins: p.wins,
|
||||
best_placement: p.best_placement,
|
||||
completed_on: p.completed_at_ms.map(Date::from_ms),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn marks(&self) -> Marks {
|
||||
let completed = self.completed_on.is_some();
|
||||
Marks {
|
||||
@@ -303,6 +313,20 @@ fn challenge_pass(matches: &[&Match]) -> ChallengePass {
|
||||
pass
|
||||
}
|
||||
|
||||
/// What the Match `match_id` brought its champion's Challenge. `matches` are that champion's
|
||||
/// Matches of one Season, oldest first, the Match included: its Marks with them all are compared
|
||||
/// with its Marks without it.
|
||||
pub(crate) fn match_end(matches: &[Match], match_id: &str) -> MatchEnd {
|
||||
let marks = |matches: Vec<&Match>| {
|
||||
let pass = challenge_pass(&matches);
|
||||
ChampionChallenge::of(pass.progress.values().next()).marks()
|
||||
};
|
||||
MatchEnd::between(
|
||||
marks(matches.iter().filter(|m| m.match_id != match_id).collect()),
|
||||
marks(matches.iter().collect()),
|
||||
)
|
||||
}
|
||||
|
||||
/// `matches` are all the Account's stored Matches, oldest first. `period` is the Season's
|
||||
/// period (`None` = all Seasons). The Challenge only ever looks at the Season, never at `format`.
|
||||
pub(crate) fn compute(
|
||||
@@ -356,13 +380,7 @@ pub(crate) fn compute(
|
||||
Some(_) => ChampionState::InProgress,
|
||||
None => ChampionState::NeverPlayed,
|
||||
};
|
||||
let challenge =
|
||||
p.map_or_else(ChampionChallenge::default, |p| ChampionChallenge {
|
||||
matches: p.matches,
|
||||
wins: p.wins,
|
||||
best_placement: p.best_placement,
|
||||
completed_on: p.completed_at_ms.map(Date::from_ms),
|
||||
});
|
||||
let challenge = ChampionChallenge::of(p);
|
||||
(
|
||||
Some(p.map_or(0, |p| p.attempts)),
|
||||
Some(state),
|
||||
@@ -475,6 +493,30 @@ impl Tracker {
|
||||
.cloned()
|
||||
}
|
||||
|
||||
/// The stored Match `match_id` as the live follow reports it: what it brought its champion's
|
||||
/// Challenge in its Season. `None` when it is not stored or no Season holds it.
|
||||
pub(crate) fn followed_match(
|
||||
&self,
|
||||
puuid: &str,
|
||||
match_id: &str,
|
||||
) -> Result<Option<FollowedMatch>, Error> {
|
||||
let Some(m) = self.stored_match(puuid, match_id)? else {
|
||||
return Ok(None);
|
||||
};
|
||||
let Some(season) = self.current_season(m.started_at_ms) else {
|
||||
return Ok(None);
|
||||
};
|
||||
let range = period(&self.seasons, &season.name).expect("a Season of the table");
|
||||
let matches = self.champion_matches(puuid, m.champion_id, range)?;
|
||||
Ok(Some(FollowedMatch {
|
||||
puuid: puuid.into(),
|
||||
season: season.name,
|
||||
champion_id: m.champion_id,
|
||||
started_at_ms: m.started_at_ms,
|
||||
end: match_end(&matches, match_id),
|
||||
}))
|
||||
}
|
||||
|
||||
pub fn statistics(&self, puuid: &str, scope: &Scope) -> Result<Statistics, Error> {
|
||||
let season_period = match &scope.season {
|
||||
SeasonScope::AllSeasons => None,
|
||||
|
||||
+63
-61
@@ -1,9 +1,9 @@
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::ops::Range;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::{Arc, Mutex, MutexGuard, PoisonError};
|
||||
use std::thread;
|
||||
|
||||
use crate::{FollowedMatch, MatchEnd};
|
||||
use rusqlite::{Connection, OptionalExtension, Row, params};
|
||||
use serde::Deserialize;
|
||||
|
||||
@@ -180,7 +180,7 @@ impl Tracker {
|
||||
let mut added = 0;
|
||||
for (i, match_id) in to_fetch.iter().enumerate() {
|
||||
// Stored at once, so an interrupted import loses nothing.
|
||||
if self.fetch_and_store(&account.puuid, match_id, None)? == Fetched::Added {
|
||||
if self.fetch_and_store(&account.puuid, match_id)? == Fetched::Added {
|
||||
added += 1;
|
||||
}
|
||||
on_progress(SyncProgress {
|
||||
@@ -206,12 +206,7 @@ impl Tracker {
|
||||
|
||||
/// Fetches one Match by id and stores it. A Match Riot does not have or that cannot be
|
||||
/// read is skipped rather than failing the caller: one bad id must not block the rest.
|
||||
pub(crate) fn fetch_and_store(
|
||||
&self,
|
||||
puuid: &str,
|
||||
match_id: &str,
|
||||
report: Option<&mut dyn FnMut(FollowedMatch)>,
|
||||
) -> Result<Fetched, Error> {
|
||||
pub(crate) fn fetch_and_store(&self, puuid: &str, match_id: &str) -> Result<Fetched, Error> {
|
||||
let raw = match with_retry(|| self.history.fetch_match(match_id)) {
|
||||
Ok(raw) => raw,
|
||||
Err(SourceError::NotFound) => return Ok(Fetched::NotFound),
|
||||
@@ -220,43 +215,12 @@ impl Tracker {
|
||||
let Ok(Some((m, queue_id))) = decode_match(&raw, puuid) else {
|
||||
return Ok(Fetched::Ignored);
|
||||
};
|
||||
let db = self.db();
|
||||
let event = if report.is_some()
|
||||
&& let Some(season) = self.current_season(m.started_at_ms)
|
||||
{
|
||||
let range = crate::seasons::period(&self.seasons, &season.name).expect("known Season");
|
||||
let (played, won, completed): (bool, bool, bool) = db.query_row(
|
||||
"SELECT count(*) > 0, coalesce(max(win), 0), coalesce(min(placement) = 1, 0)
|
||||
FROM matches WHERE puuid = ?1 AND champion_id = ?2
|
||||
AND started_at_ms >= ?3 AND started_at_ms < ?4",
|
||||
params![puuid, m.champion_id, range.start, range.end],
|
||||
|row| Ok((row.get(0)?, row.get(1)?, row.get(2)?)),
|
||||
)?;
|
||||
let end = if !completed && m.placement == 1 {
|
||||
MatchEnd::Completion
|
||||
} else if !played || (!completed && !won && m.win) {
|
||||
MatchEnd::Mark
|
||||
} else {
|
||||
MatchEnd::Nothing
|
||||
};
|
||||
Some(FollowedMatch {
|
||||
puuid: puuid.into(),
|
||||
season: season.name,
|
||||
champion_id: m.champion_id,
|
||||
started_at_ms: m.started_at_ms,
|
||||
end,
|
||||
})
|
||||
let inserted = insert_match(&self.db(), puuid, &m, queue_id, &raw)?;
|
||||
Ok(if inserted {
|
||||
Fetched::Added
|
||||
} else {
|
||||
None
|
||||
};
|
||||
if !insert_match(&db, puuid, &m, queue_id, &raw)? {
|
||||
return Ok(Fetched::Known);
|
||||
}
|
||||
drop(db);
|
||||
if let (Some(report), Some(event)) = (report, event) {
|
||||
report(event);
|
||||
}
|
||||
Ok(Fetched::Added)
|
||||
Fetched::Known
|
||||
})
|
||||
}
|
||||
|
||||
pub fn accounts(&self) -> Result<Vec<Account>, Error> {
|
||||
@@ -273,23 +237,44 @@ impl Tracker {
|
||||
/// Stored Matches of the Account, oldest first.
|
||||
pub fn matches(&self, puuid: &str) -> Result<Vec<Match>, Error> {
|
||||
let db = self.db();
|
||||
let mut stmt = db.prepare(
|
||||
"SELECT match_id, queue_id, champion_id, champion_name, placement, win, started_at_ms
|
||||
FROM matches WHERE puuid = ?1 ORDER BY started_at_ms, match_id",
|
||||
)?;
|
||||
let mut stmt = db.prepare(&format!(
|
||||
"SELECT {MATCH_COLUMNS} FROM matches WHERE puuid = ?1 ORDER BY started_at_ms, match_id"
|
||||
))?;
|
||||
let matches = stmt
|
||||
.query_map([puuid], |row| {
|
||||
let queue_id: u32 = row.get(1)?;
|
||||
Ok(Match {
|
||||
match_id: row.get(0)?,
|
||||
format: Format::from_queue(queue_id).unwrap_or(Format::Other(queue_id)),
|
||||
champion_id: row.get(2)?,
|
||||
champion_name: row.get(3)?,
|
||||
placement: row.get(4)?,
|
||||
win: row.get(5)?,
|
||||
started_at_ms: row.get(6)?,
|
||||
})
|
||||
})?
|
||||
.query_map([puuid], read_match)?
|
||||
.collect::<Result<_, _>>()?;
|
||||
Ok(matches)
|
||||
}
|
||||
|
||||
/// The stored Match with this id, if any.
|
||||
pub(crate) fn stored_match(&self, puuid: &str, match_id: &str) -> Result<Option<Match>, Error> {
|
||||
Ok(self
|
||||
.db()
|
||||
.query_row(
|
||||
&format!("SELECT {MATCH_COLUMNS} FROM matches WHERE puuid = ?1 AND match_id = ?2"),
|
||||
params![puuid, match_id],
|
||||
read_match,
|
||||
)
|
||||
.optional()?)
|
||||
}
|
||||
|
||||
/// The Account's stored Matches with one champion started within `period`, oldest first.
|
||||
pub(crate) fn champion_matches(
|
||||
&self,
|
||||
puuid: &str,
|
||||
champion_id: u32,
|
||||
period: Range<i64>,
|
||||
) -> Result<Vec<Match>, Error> {
|
||||
let db = self.db();
|
||||
let mut stmt = db.prepare(&format!(
|
||||
"SELECT {MATCH_COLUMNS} FROM matches WHERE puuid = ?1 AND champion_id = ?2
|
||||
AND started_at_ms >= ?3 AND started_at_ms < ?4 ORDER BY started_at_ms, match_id"
|
||||
))?;
|
||||
let matches = stmt
|
||||
.query_map(
|
||||
params![puuid, champion_id, period.start, period.end],
|
||||
read_match,
|
||||
)?
|
||||
.collect::<Result<_, _>>()?;
|
||||
Ok(matches)
|
||||
}
|
||||
@@ -391,7 +376,7 @@ impl Tracker {
|
||||
Ok(id.and_then(|id| id.split_once('_').map(|(platform, _)| platform.into())))
|
||||
}
|
||||
|
||||
fn latest_start_ms(&self, puuid: &str) -> Result<Option<i64>, Error> {
|
||||
pub(crate) fn latest_start_ms(&self, puuid: &str) -> Result<Option<i64>, Error> {
|
||||
Ok(self.db().query_row(
|
||||
"SELECT MAX(started_at_ms) FROM matches WHERE puuid = ?1",
|
||||
[puuid],
|
||||
@@ -437,6 +422,23 @@ pub(crate) fn account_from_row(row: &Row) -> rusqlite::Result<Account> {
|
||||
})
|
||||
}
|
||||
|
||||
/// The columns `read_match` reads, in its order.
|
||||
const MATCH_COLUMNS: &str =
|
||||
"match_id, queue_id, champion_id, champion_name, placement, win, started_at_ms";
|
||||
|
||||
fn read_match(row: &Row) -> rusqlite::Result<Match> {
|
||||
let queue_id: u32 = row.get(1)?;
|
||||
Ok(Match {
|
||||
match_id: row.get(0)?,
|
||||
format: Format::from_queue(queue_id).unwrap_or(Format::Other(queue_id)),
|
||||
champion_id: row.get(2)?,
|
||||
champion_name: row.get(3)?,
|
||||
placement: row.get(4)?,
|
||||
win: row.get(5)?,
|
||||
started_at_ms: row.get(6)?,
|
||||
})
|
||||
}
|
||||
|
||||
fn synced_until_key(puuid: &str) -> String {
|
||||
format!("synced_until_ms:{puuid}")
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user