Files
arena-tracker/crates/core/tests/common/game_data.rs
T
nessarandClaude Opus 5.5 dcb7ac303c
CI / core (pull_request) Successful in 1m57s
CI / core (push) Successful in 1m49s
Show augments as item-like slots with the game's coloured icons (#135)
- The recap draws six augment slots per player, like the seven item slots:
  the augments taken, then empty slots, so the lines read in columns; the
  name moves to the bubble.
- The catalog takes CommunityDragon's large augment icon (coloured by rarity)
  instead of the small white glyph, shrunk to 64 px at download.
- catalog.json gets a format number: a cache from before is read again from
  the sources at the same patch and its augment icons downloaded again.
- A first download tells the page every 100 icons, so icons show as they come
  instead of after the whole pass.
- The icon download tests move to tests/game_data/icons.rs (Rule #2).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0192He3BEKJ9v922udzup4Qn
2026-11-25 13:45:00 +01:00

137 lines
4.5 KiB
Rust

use arena_tracker_core::{CatalogSource, SourceError};
use super::*;
pub const VERSIONS_URL: &str = "https://ddragon.leagueoflegends.com/api/versions.json";
pub const RECORDED_VERSION: &str = "16.23.1";
/// The recorded Data Dragon items (trimmed to six), and CommunityDragon's augments (nine) and
/// descriptions (four of them).
pub const RECORDED_ITEMS: &str = include_str!("../fixtures/ddragon/item.json");
const AUGMENTS: &str = include_str!("../fixtures/communitydragon/cherry-augments.json");
const ARENA: &str = include_str!("../fixtures/communitydragon/arena.json");
pub fn items_url(version: &str) -> String {
format!("https://ddragon.leagueoflegends.com/cdn/{version}/data/fr_FR/item.json")
}
pub fn augments_url(patch: &str) -> String {
format!(
"https://raw.communitydragon.org/{patch}/plugins/rcp-be-lol-game-data/global/fr_fr/v1/cherry-augments.json"
)
}
pub fn arena_url(patch: &str) -> String {
format!("https://raw.communitydragon.org/{patch}/cdragon/arena/fr_fr.json")
}
pub fn item_icon_url(version: &str, file: &str) -> String {
format!("https://ddragon.leagueoflegends.com/cdn/{version}/img/item/{file}")
}
/// A stand-in PNG for an icon URL (its bytes say which URL it came from).
pub fn icon_png(url: &str) -> Vec<u8> {
[b"\x89PNG\r\n\x1a\n".as_slice(), url.as_bytes()].concat()
}
#[derive(Default)]
struct CatalogState {
pages: HashMap<String, Vec<u8>>,
failing: HashMap<String, SourceError>,
/// Requests the network may still answer before it goes down; `None` is no limit.
budget: Option<usize>,
asked: Vec<String>,
}
/// Data Dragon and CommunityDragon without the network: pages by URL, and any `.png` URL is an
/// icon. Remembers the URLs asked.
#[derive(Default)]
pub struct FakeCatalog {
state: Mutex<CatalogState>,
}
impl FakeCatalog {
/// Both sources at the recorded patch.
pub fn recorded() -> FakeCatalog {
let catalog = FakeCatalog::default();
catalog.publish_data_dragon(RECORDED_VERSION, RECORDED_ITEMS);
catalog.publish_community_dragon("16.23");
catalog
}
/// Data Dragon's newest version is this one, with these items.
pub fn publish_data_dragon(&self, version: &str, items: &str) {
let mut state = self.state.lock().unwrap();
let versions = format!(r#"["{version}", "16.22.1"]"#);
state
.pages
.insert(VERSIONS_URL.into(), versions.into_bytes());
state
.pages
.insert(items_url(version), items.as_bytes().to_vec());
}
/// CommunityDragon has this patch ("16.23"), with the recorded augments.
pub fn publish_community_dragon(&self, patch: &str) {
let mut state = self.state.lock().unwrap();
state
.pages
.insert(augments_url(patch), AUGMENTS.as_bytes().to_vec());
state
.pages
.insert(arena_url(patch), ARENA.as_bytes().to_vec());
}
pub fn serve(&self, url: &str, body: impl AsRef<[u8]>) {
(self.state.lock().unwrap().pages).insert(url.into(), body.as_ref().to_vec());
}
pub fn fail(&self, url: &str, error: SourceError) {
self.state.lock().unwrap().failing.insert(url.into(), error);
}
/// The network answers this many more requests, then fails until `reconnect`.
pub fn lose_network_after(&self, requests: usize) {
self.state.lock().unwrap().budget = Some(requests);
}
pub fn reconnect(&self) {
self.state.lock().unwrap().budget = None;
}
/// The URLs asked for so far, in order.
pub fn asked(&self) -> Vec<String> {
self.state.lock().unwrap().asked.clone()
}
pub fn icons_asked(&self) -> Vec<String> {
(self.asked().into_iter())
.filter(|url| url.ends_with(".png"))
.collect()
}
pub fn forget_asked(&self) {
self.state.lock().unwrap().asked.clear();
}
}
impl CatalogSource for FakeCatalog {
fn get(&self, url: &str) -> Result<Vec<u8>, SourceError> {
let mut state = self.state.lock().unwrap();
if let Some(budget) = state.budget.as_mut() {
if *budget == 0 {
return Err(SourceError::Network("hors ligne".into()));
}
*budget -= 1;
}
state.asked.push(url.into());
if let Some(error) = state.failing.get(url) {
return Err(error.clone());
}
match state.pages.get(url) {
Some(body) => Ok(body.clone()),
None if url.ends_with(".png") => Ok(icon_png(url)),
None => Err(SourceError::NotFound),
}
}
}