Download champion splashes and cache their banner focus (#83)
CI / core (pull_request) Successful in 1m31s

This commit is contained in:
nessar committed 2026-11-21 14:16:58 +01:00
1 parent 3de8fd527c
commit e440d155fc
11 files changed
+488 -99

No files matched your search

+1
View File
@@ -396,6 +396,7 @@ mod tests {
},
portrait: None,
card_portrait: None,
splash: None,
matches: 0,
best_placement: None,
top1: 0,
+1
View File
@@ -298,6 +298,7 @@ pub(crate) mod tests {
},
portrait: None,
card_portrait: None,
splash: None,
matches: 0,
best_placement: None,
top1: 0,
+4
View File
@@ -2690,6 +2690,10 @@ mod tests {
fn card_portrait(&self, _: &str) -> Result<Vec<u8>, SourceError> {
Err(SourceError::NotFound)
}
fn splash(&self, _: &str) -> Result<Vec<u8>, SourceError> {
Err(SourceError::NotFound)
}
}
/// The tracker of Joueur (and of Autre, who has played nothing), on the catalog.
+33 -56
View File
@@ -77,46 +77,31 @@ impl RefreshGate {
}
}
/// While card portraits keep arriving, how often the page is told: each time, it reloads its
/// While champion art keeps arriving, how often the page is told: each time, it reloads its
/// statistics to show them.
const PORTRAITS_TOLD_EVERY: Duration = Duration::from_secs(1);
/// Spaces out what saved portraits cause: the first save is told at once, the next ones when
/// `every` has passed since the last message, and one still untold when the pass ends is told
/// then. No timer: each attempt of the pass, saved or not, and its end are what tell.
/// Shares the catalog notification's deadline across the card and splash passes.
struct Throttle {
every: Duration,
last_told: Option<Instant>,
last_told: Instant,
untold: bool,
}
impl Throttle {
fn new(every: Duration) -> Throttle {
Throttle {
every,
last_told: None,
untold: false,
}
}
/// An attempt ended at `now`, with a portrait saved or not: true when the page must be told
/// now, as a save is untold and `every` has passed since the last message.
fn attempted(&mut self, saved: bool, now: Instant) -> bool {
self.untold |= saved;
let due = self
.last_told
.is_none_or(|last| now.duration_since(last) >= self.every);
if self.untold && due {
self.last_told = Some(now);
if self.untold && now.duration_since(self.last_told) >= PORTRAITS_TOLD_EVERY {
self.last_told = now;
self.untold = false;
return true;
}
false
}
/// The pass ended: true when a save is still untold.
fn finished(self) -> bool {
/// A final batch needs one wake at its deadline, even when the pass ends early.
fn final_delay(self, now: Instant) -> Option<Duration> {
self.untold
.then(|| PORTRAITS_TOLD_EVERY.saturating_sub(now.duration_since(self.last_told)))
}
}
@@ -177,15 +162,17 @@ pub fn spawn_follower(tracker: Arc<Tracker>, fallback_riot_id: String, tx: Sende
if refreshed.is_err() {
return;
}
// The tall card portraits after the square ones, which the Statistiques table uses. They
// show as they arrive, without a restart, the page reloading at most once a second.
let mut throttle = Throttle::new(PORTRAITS_TOLD_EVERY);
let _ = champions.refresh_card_portraits(&mut |saved| {
let mut throttle = Throttle {
last_told: Instant::now(),
untold: false,
};
let _ = champions.refresh_champion_art(&mut |saved| {
if throttle.attempted(saved, Instant::now()) {
let _ = tx.try_send(Msg::Champions);
}
});
if throttle.finished() {
if let Some(delay) = throttle.final_delay(Instant::now()) {
thread::sleep(delay);
let _ = tx.try_send(Msg::Champions);
}
});
@@ -448,39 +435,29 @@ mod tests {
}
#[test]
fn saved_portraits_are_told_once_a_second_after_any_attempt_and_the_last_ones_at_the_end() {
fn saved_art_is_told_at_most_once_a_second_including_the_final_batch() {
let start = Instant::now();
let at = |ms: u64| start + Duration::from_millis(ms);
let mut throttle = Throttle::new(Duration::from_secs(1));
assert!(
throttle.attempted(true, at(0)),
"the first save is told at once"
);
assert!(!throttle.attempted(true, at(300)));
let mut throttle = Throttle {
last_told: start,
untold: false,
};
assert!(!throttle.attempted(true, at(0)));
assert!(!throttle.attempted(true, at(999)));
assert!(throttle.attempted(true, at(1000)));
assert!(!throttle.attempted(true, at(1500)));
// A failed attempt tells nothing before the second is over, then the save at 1.5 s is.
assert!(!throttle.attempted(false, at(1900)));
assert!(throttle.attempted(false, at(2000)));
assert!(
!throttle.attempted(false, at(5000)),
"nothing is untold any more"
assert!(throttle.attempted(false, at(1000)));
assert!(!throttle.attempted(false, at(2000)));
assert!(throttle.attempted(true, at(2100)));
assert!(!throttle.attempted(true, at(2110)));
assert_eq!(
throttle.final_delay(at(2300)),
Some(Duration::from_millis(800))
);
assert!(!throttle.finished());
// The last save was told late or never: the end of the pass tells it.
let mut throttle = Throttle::new(Duration::from_secs(1));
assert!(throttle.attempted(true, at(0)));
assert!(!throttle.attempted(true, at(10)));
assert!(!throttle.attempted(false, at(20)));
assert!(throttle.finished());
// Nothing saved, nothing told.
let mut throttle = Throttle::new(Duration::from_secs(1));
assert!(!throttle.attempted(false, at(0)));
assert!(!throttle.attempted(false, at(5000)));
assert!(!throttle.finished());
let empty = Throttle {
last_told: start,
untold: false,
};
assert_eq!(empty.final_delay(at(100)), None);
}
#[test]