//! Match history: the timelines on disk, newest first, filtered by queue. //! //! The card reads like `GameHistory.vue`: result banner, champion, queue, KDA, per-minute //! stats, items are not shown because the app has no Data Dragon image cache yet. use rune::tok::{radius, space, type_}; use rune::{Color, El, col, el, row, text}; use crate::app::App; use crate::format; use crate::history::{Outcome, QueueCategory, Timeline}; use crate::screens::review; use crate::ui::{self, Paint, body, heading}; const FILTERS: [Option; 6] = [ None, Some(QueueCategory::RankedSolo), Some(QueueCategory::RankedFlex), Some(QueueCategory::Normals), Some(QueueCategory::Aram), Some(QueueCategory::Custom), ]; fn filter_label(filter: Option) -> &'static str { filter.map_or("All", QueueCategory::label) } fn outcome_color(p: Paint, outcome: Outcome) -> Color { match outcome { Outcome::Victory => p.success(), Outcome::Defeat => p.danger(), Outcome::Terminated => p.muted(), } } pub fn view(app: &App, p: Paint) -> El { let filtered: Vec<(usize, &Timeline)> = app .games .iter() .enumerate() .filter(|(_, g)| app.filter.is_none_or(|f| g.category() == f)) .collect(); let mut list = col().key("games").w_full().gap(space::S2); if !app.games_loaded { list = list.child( text("Loading match history…") .key("loading") .font(body(13.0)) .color(p.muted()) .padding(space::S6), ); } else if app.games.is_empty() { list = list.child(empty( p, "No games recorded", "Start playing: the daemon records each game and it shows up here.", )); } else if filtered.is_empty() { list = list.child(empty( p, "No games match this filter", "Pick another queue above.", )); } else { for (index, game) in filtered { list = list.child(game_card(p, game, index)); } } let mut filters = row().key("filters").gap(4.0).center_y(); for filter in FILTERS { let selected = app.filter == filter; filters = filters.child(ui::tab( p, filter_label(filter), filter_label(filter), selected, move |app: &mut App| app.filter = filter, )); } col() .key("history") .w_full() .h_full() .padding(space::S6) .gap(space::S3) .child( row() .key("history-header") .w_full() .justify_between() .center_y() .child( col() .key("history-title") .gap(2.0) .child( text("Match history") .key("title") .font(heading(20.0)) .color(p.t.text), ) .child( text(format!("{} recordings", app.games.len())) .key("count") .font(body(11.5)) .color(p.muted()), ), ) .child(filters), ) .child( app.scroll.follow( col() .key("list-scroller") .w_full() .grow(1.0) .min_h(0.0) .scroll(rune::ScrollSpec { stick_bottom: false, }) .child(list), ), ) } fn empty(p: Paint, title: &str, detail: &str) -> El { col() .key("empty") .w_full() .py(64.0) .gap(6.0) .center() .child( text(title) .key("empty-title") .font(heading(15.0)) .color(p.t.text), ) .child( text(detail) .key("empty-detail") .font(body(12.0)) .color(p.muted()), ) } fn game_card(p: Paint, game: &Timeline, index: usize) -> El { let outcome = game.outcome(); let ink = outcome_color(p, outcome); let champion = game.champion().unwrap_or_else(|| "Unknown champion".into()); let duration = game.duration_secs.max(0) as f64; let lp = game.lp_change(); let stats = game.stats(); let mut banner = row() .key("banner") .w_full() .h(26.0) .px(space::S3) .center_y() .justify_between() .fill(ink.alpha(0.16)) .child( row() .key("banner-left") .gap(8.0) .center_y() .child( text(outcome.label()) .key("outcome") .font(type_::LABEL) .color(ink), ) .child( text(game.summoner().unwrap_or_default()) .key("summoner") .font(body(11.0)) .color(p.muted()), ), ); if let Some(lp) = &lp { let lp_ink = if lp.delta >= 0 { p.success() } else { p.danger() }; banner = banner.child( text(format!( "{} LP · {}", format::lp_delta(lp.delta), format::rank(&lp.tier, &lp.division) )) .key("lp") .font(type_::MONO_CAPTION) .color(lp_ink), ); } let kda = match &stats { Some(s) => format!("{}/{}/{}", s.kills, s.deaths, s.assists), None => "0/0/0".into(), }; let ratio = stats.as_ref().map_or("0.0".into(), |s| { format::kda_ratio(s.kills, s.deaths, s.assists) }); let mut stats_row = row().key("stats").gap(18.0).center_y(); if let Some(s) = &stats { stats_row = stats_row .child(stat(p, &format::cs_per_min(s), "CS/m")) .child(stat(p, &format::gold_per_min(s), "Gold/m")) .child(stat(p, &s.vision_score.to_string(), "Vision")); } el().key(format!("game-{index}")) .role("button") .label(format!("{champion}, {}", outcome.label())) .focusable(true) .w_full() .radius(radius::LG) .fill(p.t.card) .border(p.hair, p.t.card_border) .shadow(1.0, 2.0, p.t.shadow) .shadow(8.0, 24.0, p.t.shadow_soft) .hover(p.t.hover) .press(p.t.press) .on_press(move |app: &mut App| review::open(app, index)) .child( col().key("card").w_full().child(banner).child( row() .key("content") .w_full() .px(space::S3) .py(space::S3) .gap(space::S3) .center_y() .justify_between() .child( row() .key("identity") .gap(space::S3) .center_y() .child(portrait(p, game.champion().as_deref())) .child( col() .key("identity-labels") .gap(2.0) .child( text(champion) .key("champion") .font(heading(16.0)) .color(p.t.text), ) .child( text(format!( "{} · {}", game.queue_label(), format::duration(duration) )) .key("queue") .font(body(11.5)) .color(p.muted()), ) .child( text(format::relative_time(game.start_time)) .key("when") .font(body(11.0)) .color(p.muted()), ), ), ) .child( col() .key("kda") .gap(1.0) .items_end() .child( text(kda) .key("kda-values") .font(heading(15.0)) .color(p.t.text), ) .child( text(format!("{ratio} KDA")) .key("kda-ratio") .font(body(11.0)) .color(p.muted()), ), ) .child(stats_row) .child( text("Review →") .key("open") .font(type_::LABEL) .color(p.accent), ), ), ) } fn stat(p: Paint, value: &str, label: &str) -> El { col() .key(format!("stat-{label}")) .gap(0.0) .items_end() .child( text(value) .key("value") .font(type_::MONO_CAPTION) .color(p.t.text), ) .child(text(label).key("label").font(body(10.5)).color(p.muted())) } /// A square champion tile. There is no Data Dragon image cache yet, so the tile carries the /// champion's initials on a colour derived from the name: the same champion always gets the /// same tile. fn portrait(p: Paint, champion: Option<&str>) -> El { let name = champion.unwrap_or_default(); let hue = name .bytes() .fold(2166136261u32, |hash, byte| { hash.wrapping_mul(16777619) ^ byte as u32 }) .rem_euclid(360) as f32; let (fill, ink) = if name.is_empty() { (p.t.control_off, p.muted()) } else { (rune::oklch(0.36, 0.075, hue), rune::oklch(0.94, 0.02, hue)) }; el().key("portrait") .size(52.0, 52.0) .radius(radius::MD) .fill(fill) .border(p.hair, ink.alpha(0.18)) .a11y_hidden() .child(text(initials(name)).font(heading(17.0)).color(ink).center()) } /// Up to two letters from the champion's name: `Kai'Sa` is KS, `Jinx` is J. fn initials(name: &str) -> String { let letters: Vec = name .split(|c: char| !c.is_alphanumeric()) .filter(|word| !word.is_empty()) .filter_map(|word| word.chars().next()) .take(2) .collect(); if letters.is_empty() { "?".into() } else { letters.into_iter().collect() } }