//! What the recap of « Parties » shows, written once when its Match Recap arrives (or the //! champions' names and portraits do): the page's render only lays it out. use std::collections::HashMap; use std::path::PathBuf; use arena_tracker_core::{ CatalogDisplay, Champion, ChampionSearch, GameCatalog, MatchRecap, RecapPlayer, Statistics, }; use gpui_kit::SharedString; use super::damage::{Death, ShownDamage}; use super::details::Details; use crate::local_time::local_time; use crate::text::{duration, match_time, ordinal}; /// A value absent from the source: never shown as 0. pub(in crate::page) const UNAVAILABLE: &str = "Indisponible"; /// The same in a Provisional Match, whose source gives less until Riot publishes it. pub(in crate::page) const AWAITED: &str = "En attente"; /// A champion of the catalog, as « Parties » needs it. struct Known { champion: Champion, /// The catalog's name, shared with every row that shows it. name: SharedString, portrait: Option, } /// The catalog's name and square portrait of each champion, by id. #[derive(Default)] pub(in crate::page) struct Champions(HashMap); impl Champions { /// From statistics, whose rows hold every champion of the catalog whatever their scope. pub(in crate::page) fn of(stats: Option<&Statistics>) -> Champions { Champions( (stats.iter().flat_map(|s| s.rows.iter())) .map(|row| { let known = Known { champion: row.champion.clone(), name: SharedString::from(row.champion.name.clone()), portrait: row.portrait.clone(), }; (row.champion.id, known) }) .collect(), ) } /// The catalog's name, else the one the Match gives (Riot's key, such as « MonkeyKing »). pub(in crate::page) fn name(&self, id: u32, fallback: &str) -> SharedString { self.0 .get(&id) .map_or_else(|| fallback.to_string().into(), |known| known.name.clone()) } pub(in crate::page) fn portrait(&self, id: u32) -> Option { self.0.get(&id).and_then(|known| known.portrait.clone()) } /// Whether the search finds the champion, by the catalog's names and aliases. Without the /// catalog, the Match's own name is Riot's key, which the aliases are keyed by. pub(in crate::page) fn found_by(&self, search: &ChampionSearch, id: u32, key: &str) -> bool { match self.0.get(&id) { Some(known) => search.matches(&known.champion), None => search.matches(&Champion { id, key: key.into(), name: key.into(), }), } } } /// A value, or the word for its absence, which is drawn muted. #[derive(Clone, Debug, PartialEq)] pub(in crate::page) struct Value { pub(in crate::page) text: SharedString, pub(in crate::page) missing: bool, } impl Value { pub(super) fn of( value: Option, missing: &'static str, show: impl FnOnce(T) -> String, ) -> Value { match value { Some(value) => Value { text: show(value).into(), missing: false, }, None => Value { text: missing.into(), missing: true, }, } } } /// An item or an augment as the catalog shows it: in a slot or a line of the damage seen. #[derive(Clone, Debug, PartialEq)] pub(in crate::page) struct Described { /// « Objet n » or « Amélioration n » when the catalog does not know it. pub(in crate::page) name: SharedString, /// For its bubble; None: the catalog has none. pub(in crate::page) description: Option, /// None shows the neutral icon. pub(in crate::page) icon: Option, } impl From for Described { fn from(shown: CatalogDisplay) -> Described { Described { name: shown.name.into(), description: shown.description.map(Into::into), icon: shown.icon, } } } /// One of the seven item slots. #[derive(Clone, Debug, PartialEq)] pub(in crate::page) enum Slot { /// The source says the slot is empty (item 0). Empty, Item(Described), /// The source does not give this slot: the word for its absence, in its bubble. Missing(&'static str), } #[derive(Clone, Debug, PartialEq)] pub(in crate::page) struct ShownPlayer { pub(in crate::page) is_account: bool, pub(in crate::page) champion: SharedString, pub(in crate::page) portrait: Option, pub(in crate::page) riot_id: Value, pub(in crate::page) level: Value, pub(in crate::page) kda: Value, /// The word for their absence when the source gives no slot at all. pub(in crate::page) items: Result<[Slot; 7], Value>, /// Each with its name (« Amélioration n » when unknown); the word for their absence when /// the source gives no augment field. pub(in crate::page) augments: Result, Value>, /// What unfolds under their line. pub(in crate::page) details: Details, /// The damage seen in the Death Recaps, under their details. pub(in crate::page) damage_seen: ShownDamage, } #[derive(Clone, Debug, PartialEq)] pub(in crate::page) struct ShownTeam { /// « 1ᵉʳ », or the word for an absent Placement. pub(in crate::page) placement: Value, /// The team of the Account the recap is for. pub(in crate::page) account: bool, pub(in crate::page) players: Vec, } #[derive(Clone, Debug, PartialEq)] pub(in crate::page) struct ShownRecap { /// The Account's champion. pub(in crate::page) champion: SharedString, pub(in crate::page) portrait: Option, /// « 15 nov. 2026 · 18:29 · Arena 3x6 · 31 min 14 s ». pub(in crate::page) details: SharedString, pub(in crate::page) provisional: bool, /// By Placement, best first, as the core ranks them. pub(in crate::page) teams: Vec, } impl ShownRecap { /// `champion`: the Account's, from its row, should the recap not find the Account. /// `catalog`: the names and icons of the items and augments. `death`: what is known of the /// Death Recaps of the Match. pub(in crate::page) fn new( recap: &MatchRecap, champion: (u32, &str), champions: &Champions, catalog: &GameCatalog, death: &Death, ) -> ShownRecap { let missing = if recap.provisional { AWAITED } else { UNAVAILABLE }; // Every player of the Match: the best of each measure is its full bar. let all: Vec<&RecapPlayer> = recap.teams.iter().flat_map(|t| &t.players).collect(); let account = all.iter().find(|p| p.is_account).copied(); let (champion_id, fallback) = account.map_or(champion, |p| (p.champion_id, p.champion_name.as_str())); let length = recap .duration_s .map_or_else(|| format!("Durée : {}", missing.to_lowercase()), duration); let details = [ match_time(local_time(recap.started_at_ms)), recap.format.name(), length, ] .join(" · "); ShownRecap { champion: champions.name(champion_id, fallback), portrait: champions.portrait(champion_id), details: details.into(), provisional: recap.provisional, teams: (recap.teams.iter()) .map(|team| ShownTeam { placement: Value::of(team.placement, missing, ordinal), account: team.players.iter().any(|p| p.is_account), players: (team.players.iter()) .map(|p| player(p, &all, recap.provisional, death, champions, catalog)) .collect(), }) .collect(), } } } fn player( p: &RecapPlayer, all: &[&RecapPlayer], provisional: bool, death: &Death, champions: &Champions, catalog: &GameCatalog, ) -> ShownPlayer { let missing = if provisional { AWAITED } else { UNAVAILABLE }; let count = |value: Option| value.map_or_else(|| missing.to_string(), |v| v.to_string()); let kda = [p.kills, p.deaths, p.assists]; let absent = || Value::of(None::<()>, missing, |_| String::new()); let items = (p.items.iter().any(Option::is_some)) .then(|| { p.items.map(|slot| match slot { Some(0) => Slot::Empty, Some(id) => Slot::Item(catalog.item_display(id).into()), None => Slot::Missing(missing), }) }) .ok_or_else(absent); ShownPlayer { is_account: p.is_account, champion: champions.name(p.champion_id, &p.champion_name), portrait: champions.portrait(p.champion_id), riot_id: Value::of(p.riot_id.clone(), missing, |id| id), level: Value::of(p.level, missing, |level| level.to_string()), // Each part says its own absence; all three absent is one word. kda: Value::of( kda.iter().any(Option::is_some).then_some(kda), missing, |kda| kda.map(count).join(" / "), ), items, augments: (p.augments.as_ref()) .map(|augments| { (augments.iter()) .map(|&id| catalog.augment_display(id).into()) .collect() }) .ok_or_else(absent), details: Details::of(p, missing, all), damage_seen: ShownDamage::of(p, provisional, death, catalog), } } #[cfg(test)] mod tests { use super::*; use arena_tracker_core::{DamageByType, Format, RecapTeam}; fn recap(provisional: bool, players: Vec) -> MatchRecap { MatchRecap { match_id: "EUW1_1".into(), provisional, format: Format::Arena3x6, started_at_ms: 1_794_763_752_767, duration_s: None, game_version: None, teams: vec![RecapTeam { placement: None, players, }], } } #[test] fn an_absent_value_is_unavailable_and_a_zero_stays_zero() { let given = RecapPlayer { is_account: true, champion_id: 39, champion_name: "Irelia".into(), kills: Some(0), deaths: Some(0), assists: None, level: Some(0), items: [Some(0), Some(3157), None, None, None, None, None], augments: Some(vec![]), damage_to_champions: DamageByType { total: Some(0), ..DamageByType::default() }, ..RecapPlayer::default() }; let shown = ShownRecap::new( &recap(false, vec![given]), (1, "x"), &Champions::default(), &GameCatalog::default(), &Death::Loading, ); let team = &shown.teams[0]; assert_eq!(team.placement.text, UNAVAILABLE); assert!(team.placement.missing && team.account); let p = &team.players[0]; assert_eq!(p.riot_id.text, UNAVAILABLE); assert!(p.riot_id.missing); assert_eq!((p.level.text.as_ref(), p.level.missing), ("0", false)); assert_eq!(p.kda.text, "0 / 0 / Indisponible"); assert_eq!( p.details.get("Dégâts aux champions", "Total").unwrap().text, "0" ); let slots = p.items.as_ref().unwrap(); assert_eq!(slots[0], Slot::Empty); let unknown = Slot::Item(Described { name: "Objet 3157".into(), description: None, icon: None, }); assert_eq!(slots[1], unknown); assert_eq!(slots[2], Slot::Missing(UNAVAILABLE)); assert_eq!(p.augments, Ok(vec![])); assert!(shown.details.ends_with("Arena 3x6 · Durée : indisponible")); assert_eq!(shown.champion, "Irelia"); } #[test] fn a_player_without_any_field_is_unavailable_everywhere() { let bare = RecapPlayer { champion_id: 39, champion_name: "Irelia".into(), ..RecapPlayer::default() }; let shown = ShownRecap::new( &recap(false, vec![bare]), (39, "x"), &Champions::default(), &GameCatalog::default(), &Death::Loading, ); let p = &shown.teams[0].players[0]; for value in [ &p.riot_id, &p.level, &p.kda, p.details.get("Dégâts aux champions", "Total").unwrap(), ] { assert_eq!((value.text.as_ref(), value.missing), (UNAVAILABLE, true)); } for absent in [p.items.as_ref().err(), p.augments.as_ref().err()] { assert_eq!(absent.map(|v| v.text.as_ref()), Some(UNAVAILABLE)); } } #[test] fn a_provisional_match_waits_for_its_values() { let bare = RecapPlayer::default(); let shown = ShownRecap::new( &recap(true, vec![bare]), (39, "x"), &Champions::default(), &GameCatalog::default(), &Death::Loading, ); assert!(shown.provisional); assert_eq!(shown.teams[0].players[0].level.text, AWAITED); let given = RecapPlayer { items: [Some(3089), None, None, None, None, None, None], ..RecapPlayer::default() }; let shown = ShownRecap::new( &recap(true, vec![given]), (39, "x"), &Champions::default(), &GameCatalog::default(), &Death::Loading, ); let slots = shown.teams[0].players[0].items.as_ref().unwrap(); assert_eq!(slots[1], Slot::Missing(AWAITED)); assert_eq!(shown.teams[0].placement.text, AWAITED); } }