A champion is unique only within a team of an Arena Match, so the proof of a changed PUUID now requires the same champion and Placement in the shared Match. A Friend's Riot ID resolution waits for the follower again before each retry. The page resolves the signed-in Account once per change instead of at each progress message, the arc's circle is kept across counts, and the ring's paint no longer returns a value only a test reads. Tests: a real import of twelve Matches drives the arc, the structural test hovers a card and scrolls the grid, and Death Recaps, blocks and Provisional Matches are read after a rekey. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
273 lines
11 KiB
Rust
273 lines
11 KiB
Rust
use crate::card::Card;
|
|
use crate::page::friends::account_groups;
|
|
use crate::page::{ALL_FORMATS, NextHome, Page, now_ms};
|
|
use crate::worker::{compute_statistics, spawn_statistics};
|
|
use arena_tracker_core::{Scope, SeasonScope, StateFilter};
|
|
use gpui_kit::component::IndexPath;
|
|
use gpui_kit::*;
|
|
use std::time::Instant;
|
|
|
|
impl Page {
|
|
/// A worker's reload: the Accounts now, the statistics on a below-normal worker, so neither a
|
|
/// visible nor a hidden window waits for them (rule #1).
|
|
pub(in crate::page) fn reload_in_background(
|
|
&mut self,
|
|
window: &mut Window,
|
|
cx: &mut Context<Self>,
|
|
) {
|
|
self.reload_accounts(window, cx);
|
|
let start_job = self.stats_job.request();
|
|
self.recompute_in_background(start_job, window, cx);
|
|
}
|
|
|
|
/// Re-reads the Accounts, then the statistics of the one shown.
|
|
pub(in crate::page) fn reload(&mut self, window: &mut Window, cx: &mut Context<Self>) {
|
|
self.reload_accounts(window, cx);
|
|
self.recompute(window, cx);
|
|
}
|
|
|
|
/// Re-reads the Accounts and selects the one shown.
|
|
pub(in crate::page) fn reload_accounts(&mut self, window: &mut Window, cx: &mut Context<Self>) {
|
|
let directory = self.tracker.account_directory();
|
|
self.accounts.clone_from(&directory.accounts);
|
|
self.last_active.clone_from(&directory.last_active);
|
|
self.friends.ids.clone_from(&directory.friends);
|
|
self.follow_replaced_puuids();
|
|
if self.pinned.as_ref() == self.status.updating.as_ref().map(|a| &a.puuid) {
|
|
self.pinned = None;
|
|
}
|
|
let items = self.account_items();
|
|
let changed = items != self.account_items;
|
|
self.account_items = items.clone();
|
|
if changed {
|
|
// The live champ select's Account may now be in the list: resolve it again.
|
|
self.phase_riot_id = None;
|
|
self.import_view.signed_in = None;
|
|
}
|
|
if changed {
|
|
(self.account_select).update(cx, |select, cx| {
|
|
select.set_items(account_groups(&items), window, cx)
|
|
});
|
|
}
|
|
self.select_shown_account(window, cx);
|
|
// The stored Matches may have changed, or the Account.
|
|
self.matches_changed(cx);
|
|
}
|
|
|
|
/// The selector shows the Account of the screen: during a champ select or a Live Match
|
|
/// (disabled) the one signed in, else the one viewed.
|
|
pub(in crate::page) fn select_shown_account(
|
|
&mut self,
|
|
window: &mut Window,
|
|
cx: &mut Context<Self>,
|
|
) {
|
|
let shown = if self.in_phase() {
|
|
self.phase_account()
|
|
} else {
|
|
self.viewed().map(|a| a.puuid.clone())
|
|
};
|
|
self.account_select.update(cx, |select, cx| {
|
|
if let Some(puuid) = &shown
|
|
&& select.selected_value() != Some(puuid)
|
|
{
|
|
select.set_selected_value(puuid, window, cx);
|
|
}
|
|
});
|
|
}
|
|
|
|
/// Settles the scope of the next statistics. Earlier results become stale when they are
|
|
/// computed again (`computing`) or when the viewed Account changed; a request merely queued
|
|
/// behind a running job keeps that job's result for the same scope.
|
|
fn prepare_statistics(
|
|
&mut self,
|
|
computing: bool,
|
|
window: &mut Window,
|
|
cx: &mut Context<Self>,
|
|
) -> Option<String> {
|
|
self.last_reload = Instant::now();
|
|
if matches!(self.season, SeasonScope::AllSeasons)
|
|
&& !matches!(self.filter, StateFilter::All | StateFilter::NeverPlayed)
|
|
{
|
|
self.filter = StateFilter::All;
|
|
}
|
|
if self.in_phase() {
|
|
if computing {
|
|
self.stats_generation += 1;
|
|
}
|
|
// The home is the phase's, on « Défi » too: its Challenge is the current Season's,
|
|
// whatever « Statistiques » analyses.
|
|
self.current_season = self.tracker.current_season(now_ms()).map(|s| s.name);
|
|
self.home_season = self.current_season.clone().map(Into::into);
|
|
let home = self.current_season.clone().map(|season| Scope {
|
|
season: SeasonScope::Season(season),
|
|
format: None,
|
|
});
|
|
self.home_scope = home.filter(|home| *home != self.statistics_scope());
|
|
let phase = self.phase_account();
|
|
if phase != self.shown {
|
|
self.filter = StateFilter::All;
|
|
self.home_filter = StateFilter::All;
|
|
self.shown.clone_from(&phase);
|
|
}
|
|
let puuid = phase?;
|
|
return self.current_season.is_some().then_some(puuid);
|
|
}
|
|
let previous_season = self.home_season.clone();
|
|
self.settle_home_scope();
|
|
let viewed = self.viewed().map(|a| a.puuid.clone());
|
|
if previous_season != self.home_season || viewed != self.shown {
|
|
self.stop_holding();
|
|
self.end_match(cx);
|
|
}
|
|
if computing || viewed != self.shown {
|
|
self.stats_generation += 1;
|
|
}
|
|
if viewed != self.shown {
|
|
// Another Account may not have played the selected Format.
|
|
self.filter = StateFilter::All;
|
|
self.home_filter = StateFilter::All;
|
|
self.shown = viewed.clone();
|
|
if self.format.take().is_some() {
|
|
self.format_select.update(cx, |select, cx| {
|
|
select.set_selected_index(Some(IndexPath::new(0)), window, cx)
|
|
});
|
|
}
|
|
}
|
|
viewed
|
|
}
|
|
|
|
/// Requests the statistics from a worker (`start_job`: none is running), or shows none when no
|
|
/// Account is viewed.
|
|
pub(in crate::page) fn recompute_in_background(
|
|
&mut self,
|
|
start_job: bool,
|
|
window: &mut Window,
|
|
cx: &mut Context<Self>,
|
|
) {
|
|
let viewed = self.prepare_statistics(start_job, window, cx);
|
|
if let Some(puuid) = viewed {
|
|
if start_job {
|
|
let scope = self.statistics_scope();
|
|
// A below-normal worker keeps hidden and visible windows responsive (rule #1).
|
|
spawn_statistics(
|
|
self.tracker.clone(),
|
|
puuid,
|
|
scope,
|
|
self.home_scope.clone(),
|
|
self.stats_generation,
|
|
self.tx.clone(),
|
|
);
|
|
}
|
|
} else {
|
|
if start_job {
|
|
self.stats_job.reset();
|
|
}
|
|
self.stats = None;
|
|
self.stats_error = None;
|
|
self.forget_home();
|
|
self.stored = None;
|
|
self.show_statistics(window, cx);
|
|
}
|
|
}
|
|
|
|
/// Recomputes the statistics for the current scope; never called while rendering. The home's
|
|
/// own statistics are kept when they were computed for the same Account and scope (a change
|
|
/// of Format leaves them as they are).
|
|
pub(in crate::page) fn recompute(&mut self, window: &mut Window, cx: &mut Context<Self>) {
|
|
let viewed = self.prepare_statistics(true, window, cx);
|
|
self.stats = None;
|
|
self.stats_error = None;
|
|
self.stored = None;
|
|
let keep_home = !self.playback.stats_pending && viewed.is_some() && self.home_is_current();
|
|
if !keep_home {
|
|
self.forget_home();
|
|
}
|
|
if let Some(puuid) = &viewed {
|
|
let scope = self.statistics_scope();
|
|
match compute_statistics(&self.tracker, puuid, &scope) {
|
|
Ok(stats) => self.stats = Some(stats),
|
|
Err(error) => self.stats_error = Some(error),
|
|
}
|
|
if !keep_home {
|
|
let home = (self.home_scope.clone())
|
|
.map(|home| Box::new(compute_statistics(&self.tracker, puuid, &home)));
|
|
self.set_home_result(home);
|
|
}
|
|
self.stored = self.tracker.match_count(puuid).ok();
|
|
}
|
|
self.show_statistics(window, cx);
|
|
}
|
|
|
|
/// Shows `stats`: the Format choices, the charts and the table.
|
|
pub(in crate::page) fn show_statistics(&mut self, window: &mut Window, cx: &mut Context<Self>) {
|
|
if self.hold_grid {
|
|
// Row indices are reusable only while the catalog has the same champions.
|
|
let rows = self
|
|
.home_statistics()
|
|
.map_or(&[][..], |s| s.rows.as_slice());
|
|
let cards = self.home_grid.read(cx).cards();
|
|
let same = rows.len() == cards.len()
|
|
&& self.order.len() == rows.len()
|
|
&& self
|
|
.order
|
|
.iter()
|
|
.zip(cards.iter())
|
|
.all(|(&ix, c)| rows.get(ix).is_some_and(|r| r.champion.id == c.id));
|
|
if !same || self.in_champ_select() {
|
|
self.stop_holding();
|
|
}
|
|
}
|
|
if !self.hold_grid {
|
|
self.reorder();
|
|
}
|
|
self.prepare_banner(cx);
|
|
if self.in_champ_select() {
|
|
self.prepare_champ_select(cx);
|
|
}
|
|
self.prepare_home_cards(cx);
|
|
let charts = self.stats.as_ref().map(|s| &s.charts);
|
|
let in_season = self.in_season();
|
|
self.charts
|
|
.update(cx, |row, cx| row.set(charts, in_season, cx));
|
|
let mut names = vec![SharedString::from(ALL_FORMATS)];
|
|
names.extend(
|
|
self.stats
|
|
.iter()
|
|
.flat_map(|s| s.formats.iter().map(|f| SharedString::from(f.name()))),
|
|
);
|
|
if names != self.format_names {
|
|
self.format_names = names.clone();
|
|
self.format_select
|
|
.update(cx, |select, cx| select.set_items(names, window, cx));
|
|
}
|
|
self.refresh_table(cx);
|
|
// The champions' names and portraits of « Parties » come with the statistics.
|
|
self.dress_matches(cx);
|
|
if self.hold_grid {
|
|
// The cards changed in place; their order, which are shown and their count wait for
|
|
// the pointer to leave, prepared now.
|
|
let order = self.sorted_order();
|
|
let rows = self
|
|
.home_statistics()
|
|
.map_or(&[][..], |s| s.rows.as_slice());
|
|
let cards = order
|
|
.iter()
|
|
.filter_map(|&ix| rows.get(ix))
|
|
.map(Card::from_row)
|
|
.collect();
|
|
let shown = self.home_shown(&order);
|
|
self.next_home = Some(NextHome {
|
|
count: shown.len().to_string().into(),
|
|
order,
|
|
cards,
|
|
shown,
|
|
});
|
|
} else {
|
|
self.refresh_home(cx);
|
|
}
|
|
self.playback.stats_pending = false;
|
|
self.play_waiting(window, cx);
|
|
cx.notify();
|
|
}
|
|
}
|