Files
arena-tracker/crates/app/src/page/matches.rs
T
nessar 8b6062aebc
CI / core (pull_request) Successful in 1m54s
CI / core (push) Successful in 1m58s
Center player stats and scroll to the viewed account in a chosen Match
2026-11-25 22:50:55 +01:00

498 lines
19 KiB
Rust

//! « Parties »: the stored Matches of the Account chosen in the top bar, newest first, and beside
//! them the Match Recap of the one chosen. The list is read on a worker when the screen shows (or
//! the stored Matches change while it does), and only the Match chosen is decoded, on a worker
//! too: nothing runs while the screen is hidden but marking the list stale (rule #1).
mod auto_open;
mod bubble;
mod damage;
mod damage_view;
mod death;
mod details;
mod details_view;
mod filters;
#[cfg(test)]
mod inspect;
mod list;
mod recap;
mod screen;
mod shown;
mod unfolded;
pub(in crate::page) use damage::Death;
#[cfg(test)]
pub(in crate::page) use damage::{NONE_SEEN, OTHERS, ShownDamage};
use filters::Filters;
use list::MatchChosen;
pub(in crate::page) use list::MatchList;
#[cfg(test)]
pub(in crate::page) use list::MatchRow as Row;
pub(in crate::page) use list::Outcome;
pub use list::init;
#[cfg(test)]
pub(in crate::page) use recap::{augment_selector, item_selector};
#[cfg(test)]
pub(in crate::page) use shown::{AWAITED, Described, ShownPlayer, Slot, UNAVAILABLE};
use shown::{Champions, ShownRecap};
use super::{Page, Tab};
use crate::worker::{MatchEntry, RefreshGate, spawn_match_list, spawn_recap};
use arena_tracker_core::{DeathRecapState, GameCatalog, MatchRecap, Season};
use gpui_kit::component::input::InputEvent;
use gpui_kit::*;
use std::sync::Arc;
/// The recap beside the list.
pub(in crate::page) enum Recap {
/// No Match chosen.
None,
/// The Match chosen is being decoded.
Loading {
center_player: bool,
},
/// The Match is no longer stored.
Missing,
Failed(SharedString),
Ready {
recap: Box<MatchRecap>,
pane: Entity<recap::RecapPane>,
/// The Death Recaps of the Match (#128): what the details show of them.
death: Death,
},
}
/// The state of « Parties ».
pub(in crate::page) struct Matches {
/// Its own view: the selection, the scrolling and the hover.
pub(in crate::page) list: Entity<MatchList>,
/// The Account the list is of.
puuid: Option<String>,
/// Its Matches, newest first; `None` until the first list of that Account arrives.
entries: Option<Vec<MatchEntry>>,
/// The positions in `entries` of the Matches the filters keep, as the list shows them.
visible: Vec<usize>,
/// The filters of the screen that « Statistiques » does not share.
filters: Filters,
/// Why the last list failed: the Matches already known stay.
error: Option<SharedString>,
/// The stored Matches may have changed since the list was read.
stale: bool,
job: RefreshGate,
generation: u64,
/// The Match chosen. Not kept from one opening of the screen to the next.
pub(in crate::page) selected: Option<String>,
/// The Match whose end opened the screen, chosen once the list holds it (#129).
pub(in crate::page) arriving: Option<String>,
pub(in crate::page) recap: Recap,
recap_job: RefreshGate,
recap_generation: u64,
/// The Match whose Death Recap is being asked of Riot: asked once, however many times its
/// recap is decoded meanwhile.
death_fetching: Option<String>,
/// A Death Recap read is running: at most one at a time.
death_running: bool,
/// The read that waits for it (`true`: asks Riot first); only the latest is kept.
death_next: Option<(MatchRecap, bool)>,
/// The names and icons of the items and augments, from the cache on disk.
catalog: Arc<GameCatalog>,
/// In a narrow window, the list is open over the recap.
pub(in crate::page) list_open: bool,
/// The list was in the last frame: once it is not, its portraits are given back.
list_drawn: std::cell::Cell<bool>,
_chosen: Subscription,
_searched: Subscription,
}
impl Matches {
/// `seasons`: the Season table, which says what Season each Match is of.
pub(in crate::page) fn new(
seasons: &[Season],
window: &mut Window,
cx: &mut Context<Page>,
) -> Matches {
let list = cx.new(MatchList::new);
let chosen = cx.subscribe(&list, |page, _, event: &MatchChosen, cx| {
page.choose_match(event.match_id.clone(), event.close, cx)
});
let filters = Filters::new(seasons, window, cx);
let searched = cx.subscribe(&filters.input, |page, _, event: &InputEvent, cx| {
if matches!(event, InputEvent::Change) {
page.matches_searched(cx);
}
});
Matches {
list,
puuid: None,
entries: None,
visible: Vec::new(),
filters,
error: None,
stale: true,
job: RefreshGate::default(),
generation: 0,
selected: None,
arriving: None,
recap: Recap::None,
recap_job: RefreshGate::default(),
recap_generation: 0,
death_fetching: None,
death_running: false,
death_next: None,
catalog: Arc::default(),
list_open: false,
list_drawn: std::cell::Cell::new(false),
_chosen: chosen,
_searched: searched,
}
}
}
impl Page {
/// The stored Matches or the Account may have changed. The list follows the
/// Account chosen at once, never mixing two Accounts' Matches, and is read again while it
/// shows; hidden, it is only marked stale.
pub(in crate::page) fn matches_changed(&mut self, cx: &mut Context<Self>) {
let puuid = self.viewed().map(|a| a.puuid.clone());
let matches = &mut self.matches;
if puuid != matches.puuid {
matches.puuid = puuid;
matches.entries = None;
matches.visible.clear();
matches.error = None;
matches.selected = None;
matches.arriving = None;
matches.recap = Recap::None;
matches.recap_generation += 1;
matches.list_open = false;
matches
.list
.update(cx, |list, cx| list.set_rows(Vec::new(), None, cx));
}
matches.stale = true;
if self.shown_tab == Tab::Matches {
self.request_matches();
}
self.in_game.matches_changed();
}
/// Reads the list on a worker, or queues the reading behind the one running: an earlier
/// result is stale.
fn request_matches(&mut self) {
let matches = &mut self.matches;
matches.stale = false;
matches.generation += 1;
if matches.puuid.is_some() && matches.job.request() {
self.spawn_match_list();
}
}
/// Starts the reading the gate let through; none while the screen is hidden.
fn spawn_match_list(&mut self) {
match self.matches.puuid.clone() {
Some(puuid) if self.shown_tab == Tab::Matches => spawn_match_list(
self.tracker.clone(),
puuid,
self.matches.generation,
self.tx.clone(),
),
_ => self.matches.job.reset(),
}
}
/// The screen arrives: the list is read again if the stored Matches changed, and the most
/// recent Match the filters keep is chosen, the previous choice forgotten.
pub(in crate::page) fn open_matches(&mut self, cx: &mut Context<Self>) {
let matches = &mut self.matches;
matches.selected = None;
matches.recap = Recap::None;
matches.recap_generation += 1;
matches.list_open = false;
matches.list.update(cx, |list, _| list.scroll_to_top());
if matches.stale || matches.entries.is_none() {
// The most recent is chosen when the list arrives.
self.request_matches();
}
// Else the most recent is chosen with the list written.
self.dress_matches(cx);
}
/// The screen leaves: the recap goes, and the portraits of both sides. A list or a recap
/// still being read is dropped, and nothing queued starts.
pub(in crate::page) fn close_matches(&mut self, window: &mut Window, cx: &mut Context<Self>) {
self.release_matches_images(window, cx);
let matches = &mut self.matches;
// The list is read again at the next opening only if it may be out of date: a read cut
// short here, or one that failed. Stored data that changes meanwhile marks it too.
matches.stale |= matches.job.running() || matches.error.is_some();
matches.generation += 1;
matches.selected = None;
matches.arriving = None;
matches.recap = Recap::None;
matches.recap_generation += 1;
// The read running ends by itself; none starts after it.
matches.death_next = None;
}
pub(in crate::page) fn release_matches_images(
&mut self,
window: &mut Window,
cx: &mut Context<Self>,
) {
self.matches
.list
.update(cx, |list, cx| list.release_images(window, cx));
if let Recap::Ready { pane, .. } = &self.matches.recap {
pane.read(cx)
.images
.clone()
.update(cx, |images, cx| images.release(window, cx));
}
}
/// A list read by the worker.
pub(in crate::page) fn apply_matches(
&mut self,
generation: u64,
result: Result<Vec<MatchEntry>, String>,
window: &Window,
cx: &mut Context<Self>,
) {
if generation == self.matches.generation {
match result {
Ok(entries) => {
self.show_matches(entries, cx);
// The Match the end of a game opened is chosen: its animation may play.
self.play_waiting(window, cx);
}
// The Matches already known stay.
Err(error) => self.matches.error = Some(error.into()),
}
}
if self.matches.job.finished() {
self.spawn_match_list();
}
}
fn show_matches(&mut self, entries: Vec<MatchEntry>, cx: &mut Context<Self>) {
let matches = &mut self.matches;
matches.error = None;
let previous = matches.entries.replace(entries).unwrap_or_default();
let entries = matches.entries.as_deref().unwrap_or_default();
auto_open::forget_unlisted(&mut matches.arriving, entries);
// The Match chosen stays chosen while it is listed and kept by the filters, as a sync
// lists them again; the filters choose another when it is not.
let corrected = (matches.selected.as_ref())
.and_then(|id| entries.iter().find(|entry| entry.match_id == *id))
// Published or corrected meanwhile: decoded again.
.is_some_and(|entry| !previous.contains(entry));
// A decoding that failed is tried again as the list is read again.
let failed = matches!(matches.recap, Recap::Failed(_))
&& (matches.selected.as_ref())
.is_some_and(|id| entries.iter().any(|entry| entry.match_id == *id));
if corrected || failed {
let center = matches!(
matches.recap,
Recap::Loading {
center_player: true
}
);
self.request_recap(center);
}
self.dress_matches(cx);
}
/// The list chose a Match: its recap is decoded.
fn choose_match(&mut self, match_id: String, close: bool, cx: &mut Context<Self>) {
if close {
self.matches.list_open = false;
}
// The player's choice comes first.
self.matches.arriving = None;
if self.matches.selected.as_deref() != Some(match_id.as_str()) {
self.matches.selected = Some(match_id);
self.request_recap(true);
} else {
match &mut self.matches.recap {
Recap::Loading { center_player } => *center_player = true,
Recap::Ready { pane, .. } => pane.update(cx, |pane, cx| {
pane.center_player = true;
cx.notify();
}),
_ => {}
}
}
self.dress_banner(cx);
cx.notify();
}
/// Decodes the Match chosen on a worker, one at a time: a choice made meanwhile is decoded
/// once it reports, and its result dropped.
fn request_recap(&mut self, center_player: bool) {
let matches = &mut self.matches;
matches.recap_generation += 1;
let Some(selected) = &matches.selected else {
matches.recap = Recap::None;
return;
};
// The same Match decoded again keeps its recap until the new one arrives.
let same =
matches!(&matches.recap, Recap::Ready { recap, .. } if recap.match_id == *selected);
if !same {
matches.recap = Recap::Loading { center_player };
}
if matches.recap_job.request() {
self.spawn_recap();
}
}
/// Starts the decoding the gate let through; none while the screen is hidden.
fn spawn_recap(&mut self) {
let matches = &self.matches;
match (&matches.puuid, &matches.selected) {
(Some(puuid), Some(selected)) if self.shown_tab == Tab::Matches => spawn_recap(
self.tracker.clone(),
puuid.clone(),
selected.clone(),
matches.catalog.clone(),
matches.recap_generation,
self.tx.clone(),
),
_ => self.matches.recap_job.reset(),
}
}
/// A Match Recap decoded by the worker.
pub(in crate::page) fn apply_recap(
&mut self,
generation: u64,
result: Result<Option<MatchRecap>, String>,
death: Result<DeathRecapState, String>,
catalog: &Arc<GameCatalog>,
cx: &mut Context<Self>,
) {
if generation == self.matches.recap_generation {
self.matches.recap = match result {
Ok(Some(recap)) => {
// What the same Match showed before, when it is decoded again.
let (existing, open, previous) = match &self.matches.recap {
Recap::Ready {
recap: old,
pane,
death,
..
} if old.match_id == recap.match_id => (
Some(pane.clone()),
// A corrected Placement may move the player: followed, else folded.
pane.read(cx)
.open
.and_then(|open| unfolded::same_player(old, open, &recap)),
Some(death.clone()),
),
_ => (None, None, None),
};
let death = self.death_on_arrival(&recap, death, previous.as_ref());
let champions = self.champions();
let catalog = self.matches.catalog.clone();
let shown = ShownRecap::new(
&recap,
self.chosen_champion(),
&champions,
&catalog,
&death,
);
let pane = if let Some(pane) = existing {
pane.update(cx, |pane, cx| {
pane.open = open;
pane.shown = shown;
cx.notify();
});
pane
} else {
let center = matches!(
self.matches.recap,
Recap::Loading {
center_player: true
}
);
recap::RecapPane::new(shown, center, cx)
};
Recap::Ready {
recap: Box::new(recap),
pane,
death,
}
}
Ok(None) => Recap::Missing,
Err(error) => Recap::Failed(format!("Récapitulatif indisponible : {error}").into()),
};
// Matched to a catalog that the game data replaced meanwhile: read again.
if !Arc::ptr_eq(catalog, &self.matches.catalog) {
self.death_recap_matched_again();
}
}
if self.matches.recap_job.finished() {
self.spawn_recap();
}
cx.notify();
}
/// The champions' names and portraits, as the statistics have them.
fn champions(&self) -> Champions {
Champions::of(self.home_statistics().or(self.stats.as_ref()))
}
/// The champion of the Match chosen, from its row.
pub(in crate::page) fn chosen_champion(&self) -> (u32, &str) {
let matches = &self.matches;
(matches.entries.iter().flatten())
.find(|entry| Some(&entry.match_id) == matches.selected.as_ref())
.map_or((0, ""), |e| (e.champion_id, e.champion_name.as_str()))
}
/// Keeps the Matches the filters leave, writes the rows of the list and the recap with the
/// champions' names and portraits, which the statistics may bring after the Matches, and
/// chooses the first Match kept when the one chosen is not.
pub(in crate::page) fn dress_matches(&mut self, cx: &mut Context<Self>) {
if self.shown_tab != Tab::Matches && self.tab != Tab::Matches {
return;
}
let champions = self.champions();
self.refilter(&champions, cx);
self.rewrite_recap(&champions, cx);
}
/// New names and icons for the items and augments: the recap shown is written again.
pub(in crate::page) fn apply_game_data(
&mut self,
catalog: Arc<GameCatalog>,
cx: &mut Context<Self>,
) {
self.matches.catalog = catalog;
// A hidden screen holds no recap: the next one decoded has them.
if matches!(self.matches.recap, Recap::Ready { .. }) {
let champions = self.champions();
self.rewrite_recap(&champions, cx);
// The Death Recap is matched to the new names.
self.death_recap_matched_again();
cx.notify();
}
}
/// Writes the recap shown again, with the champions and the catalog as they are now.
fn rewrite_recap(&mut self, champions: &Champions, cx: &mut Context<Self>) {
let champion = self.chosen_champion();
let champion = (champion.0, champion.1.to_string());
let catalog = self.matches.catalog.clone();
if let Recap::Ready {
recap, pane, death, ..
} = &mut self.matches.recap
{
pane.update(cx, |pane, cx| {
pane.shown =
ShownRecap::new(recap, (champion.0, &champion.1), champions, &catalog, death);
cx.notify();
});
}
}
}