Read the portraits folders once and stop the card pass offline (#77)

The statistics list the portraits folder and its cards subfolder once
instead of checking about 340 files. The tall-portrait pass is skipped
when the catalog refresh failed and stops at the first network error;
the core tells after each attempt whether a portrait was saved, so the
worker's throttle can tell the page after any attempt, not only saves.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
nessarandClaude Opus 5.5 committed 2026-11-21 02:26:14 +01:00
1 parent 485d7b9065
commit 18b06f8ea6
5 files changed
+183 -53

No files matched your search

+20 -8
View File
@@ -244,9 +244,11 @@ impl Tracker {
/// Downloads the tall card portraits missing from the portraits dir's `cards` folder,
/// cropped and shrunk to the card's size (see `process_card_portrait`). A failed
/// download or an unreadable image is skipped and retried by the next pass. `on_saved`
/// is called after each portrait is saved. Meant to run after `refresh_champions`.
pub fn refresh_card_portraits(&self, on_saved: &mut dyn FnMut()) -> Result<(), Error> {
/// download of one champion (not found, an unreadable image) is skipped and retried by the
/// next pass; a network error stops the pass, as every other request would fail as well.
/// `on_attempt` is called after each download attempt, with whether a portrait was saved.
/// Meant to run after `refresh_champions`.
pub fn refresh_card_portraits(&self, on_attempt: &mut dyn FnMut(bool)) -> Result<(), Error> {
let Some((source, portraits_dir)) = &self.champions else {
return Ok(());
};
@@ -259,19 +261,29 @@ impl Tracker {
if path.exists() || !champion.key.chars().all(|c| c.is_ascii_alphanumeric()) {
continue;
}
let Ok(art) = source.card_portrait(&champion.key) else {
continue;
let art = match source.card_portrait(&champion.key) {
Ok(art) => art,
Err(SourceError::Network(_)) => {
on_attempt(false);
break;
}
Err(_) => {
on_attempt(false);
continue;
}
};
let Ok(card) = process_card_portrait(&art) else {
on_attempt(false);
continue;
};
// Written under another name first so a cut write is never taken for a portrait.
let partial = path.with_extension("part");
if std::fs::write(&partial, card).is_ok() && std::fs::rename(&partial, &path).is_ok() {
on_saved();
} else {
let saved =
std::fs::write(&partial, card).is_ok() && std::fs::rename(&partial, &path).is_ok();
if !saved {
let _ = std::fs::remove_file(&partial);
}
on_attempt(saved);
}
Ok(())
}
+23 -4
View File
@@ -1,8 +1,9 @@
//! Challenge figures and champion statistics, computed on demand from stored Matches.
use std::cmp::Ordering;
use std::collections::{BTreeMap, HashMap, HashSet};
use std::ffi::{OsStr, OsString};
use std::ops::Range;
use std::path::PathBuf;
use std::path::{Path, PathBuf};
use crate::champions::{Champion, card_portrait_path};
use crate::date::Date;
@@ -403,6 +404,18 @@ pub(crate) fn compute(
}
}
/// The names of the files in `dir`; none when it is missing or unreadable.
fn file_names(dir: &Path) -> HashSet<OsString> {
let Ok(entries) = std::fs::read_dir(dir) else {
return HashSet::new();
};
entries
.filter_map(Result::ok)
.filter(|entry| entry.file_type().is_ok_and(|kind| kind.is_file()))
.map(|entry| entry.file_name())
.collect()
}
impl Tracker {
/// Replaces the Season table; Matches are assigned to Seasons on demand, so already
/// stored Matches move accordingly.
@@ -446,11 +459,17 @@ impl Tracker {
}
}
if let Some((_, dir)) = &self.champions {
// One read of each folder rather than a file check per champion and per portrait.
let squares = file_names(dir);
let cards = file_names(&dir.join("cards"));
for row in &mut stats.rows {
let path = dir.join(format!("{}.png", row.champion.key));
row.portrait = path.is_file().then_some(path);
let name = format!("{}.png", row.champion.key);
row.portrait = squares.contains(OsStr::new(&name)).then(|| dir.join(name));
let card = card_portrait_path(dir, &row.champion.key);
row.card_portrait = card.is_file().then_some(card);
row.card_portrait = card
.file_name()
.is_some_and(|name| cards.contains(name))
.then_some(card);
}
}
Ok(stats)