Prepare the held grid on arrival and keep the followed game apart from older Matches (#89)
CI / core (pull_request) Successful in 1m33s
CI / core (push) Successful in 1m36s

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit was merged in pull request #99.
This commit is contained in:
nessarandClaude Opus 5.5 committed 2026-11-22 16:06:03 +01:00
1 parent 44c2d10b3b
commit ee2ab5cc33
9 files changed
+572 -182

No files matched your search

+39 -6
View File
@@ -21,6 +21,10 @@ pub const RETRY_EVERY_MS: i64 = 20_000;
const ADOPT_RETRY_MS: i64 = 60_000;
const WAIT_WHILE_PENDING: Duration = Duration::from_millis(RETRY_EVERY_MS as u64);
const WAIT_OTHERWISE: Duration = Duration::from_secs(60);
/// How far Riot's clock may be behind the computer's: a game's Match starts no earlier than the
/// last time a round saw the client out of game before it, less this. A Match played before then
/// ended before that time, and started longer ago than this: an Arena game lasts longer.
const CLOCK_SLACK_MS: i64 = 5 * 60_000;
/// Why the last update did not (fully) happen.
#[derive(Debug, Clone, PartialEq)]
@@ -106,6 +110,9 @@ pub struct Status {
struct Pending {
/// The client's id of that game, when it gave one.
game_id: Option<u64>,
/// No Match started before this is that game's (`CLOCK_SLACK_MS`); `None` when the app
/// started during that game.
started_after_ms: Option<i64>,
tries: usize,
next_try_ms: i64,
}
@@ -119,6 +126,8 @@ pub(crate) struct Follow {
/// Set when a round's adoption failed: rounds do not try again before.
adopt_retry_after_ms: Option<i64>,
gameflow: Gameflow,
/// The latest round that saw the client out of game.
out_of_game_ms: Option<i64>,
pending: Vec<Pending>,
/// The Riot ID the app last gave to fall back on when there is nothing else.
fallback: Option<String>,
@@ -272,12 +281,17 @@ impl Tracker {
let known = previous.game_id.is_some()
&& follow.pending.iter().any(|p| p.game_id == previous.game_id);
if ended && !known {
let started_after_ms = follow.out_of_game_ms.map(|ms| ms - CLOCK_SLACK_MS);
follow.pending.push(Pending {
game_id: previous.game_id,
started_after_ms,
tries: 0,
next_try_ms: now_ms,
});
}
if follow.gameflow.phase != GamePhase::InGame {
follow.out_of_game_ms = Some(now_ms);
}
}
let account = self.follow().updating.clone();
let mut due: Vec<Option<u64>> = (self.follow().pending.iter())
@@ -417,7 +431,8 @@ impl Tracker {
}
/// A sync whose failure is recorded as the current problem (a success clears it). One that
/// adds a Match ends the waits for the games the client gave no id for.
/// adds a Match ends the waits for the games the client gave no id for whose Match is now
/// stored: one that started after the game's bound, or any without a bound.
fn update(
&self,
account_id: &str,
@@ -426,7 +441,12 @@ impl Tracker {
match self.sync(account_id, on_progress) {
Ok(report) => {
if report.added > 0 {
self.follow().pending.retain(|p| p.game_id.is_some());
let newest = self.latest_start_ms(&report.account.puuid).ok().flatten();
self.follow().pending.retain(|p| {
p.game_id.is_some()
|| p.started_after_ms
.is_some_and(|after| newest.is_none_or(|n| n < after))
});
}
// Riot has just answered for this Account: one more request for its points.
self.refresh_mastery(&report.account.puuid);
@@ -477,12 +497,19 @@ impl Tracker {
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)
&& let Ok(Some(id)) =
self.followed_in_sync(puuid, game_id, newest_before, started_after)
{
self.report(puuid, &id, on_match);
}
@@ -506,13 +533,15 @@ 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.
/// 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<u64>,
newest_before: Option<i64>,
started_after: Option<i64>,
) -> Result<Option<String>, Error> {
let db = self.db();
let newest = match game_id {
@@ -531,8 +560,12 @@ impl Tracker {
),
}
.optional()?;
let followed = |started: i64| match started_after {
Some(after) => started >= after && newest_before.is_none_or(|b| started > b),
None => newest_before.is_some_and(|b| started > b),
};
Ok(newest
.filter(|(_, started)| game_id.is_some() || newest_before.is_some_and(|b| *started > b))
.filter(|(_, started)| game_id.is_some() || followed(*started))
.map(|(id, _)| id))
}