import { tankIcon } from "@blitzkit/core"; import { TankType, type AverageDefinitionsAllStats, type TankDefinition, } from "@blitzkit/protos"; import { memo, useEffect, useMemo, useRef, useState, } from "react"; import { api } from "../../api/dynamic"; import { performanceStatNames, performanceStats, type PerformanceEntry, type PerformanceStatName, } from "../../config/performanceStats"; import { awaitableAverageDefinitions } from "../../core/awaitables/averageDefinitions"; import { awaitableDiscoveredIdsDefinitions } from "../../core/awaitables/discoveredIdsDefinitions"; import { filterTanks } from "../../core/blitzkit/filterTanks"; import { useLocale } from "../../hooks/useLocale"; import { useStrings } from "../../hooks/useStrings"; import { useUnwrapper } from "../../hooks/useUnwrapper"; import { App } from "../../stores/app"; import { Performance } from "../../stores/performance"; import { TankFilters } from "../../stores/tankFilters"; import { SortDirection } from "../../stores/tankopediaPersistent"; import type { MaybeSkeletonComponentProps } from "../../types/maybeSkeletonComponentProps"; import { ClassIcon } from "../ClassIcon"; import { EmptyState } from "../EmptyState"; import { ExperimentIcon } from "../ExperimentIcon"; import { IncrementalLoader } from "../IncrementalLoader"; import { InlineSkeleton } from "../InlineSkeleton"; import { LazyImage } from "../LazyImage"; import { LinkI18n } from "../LinkI18n"; import { Skeleton } from "../Skeleton"; import { Table } from "../Table"; import { Text } from "../Text"; import styles from "./index.module.css"; const [tankDefinitions, averageDefinitions, discoveredIdsDefinitions] = await Promise.all([ api.tanks(), awaitableAverageDefinitions, awaitableDiscoveredIdsDefinitions, ]); // Only tanks with statistics can be listed const tanksWithAverages = Object.values(tankDefinitions.tanks).filter( ({ id }) => id in averageDefinitions.averages, ); // Samples only cover part of the discovered players, so player counts are scaled up to all of them const exclusionRatio = discoveredIdsDefinitions.count / averageDefinitions.samples!.total; interface PerformanceRow { tank: TankDefinition; entry: PerformanceEntry; values: Record; } function computeValues(entry: PerformanceEntry) { const values = {} as Record; for (const name of performanceStatNames) { values[name] = performanceStats[name].value(entry); } return values; } export function PerformanceTable() { const strings = useStrings(); const filters = TankFilters.use(); const wargaming = App.use((state) => state.logins.wargaming); const [filtered, setFiltered] = useState(null); // Filtering can wait on the player's garage, so the last results stay up until it settles useEffect(() => { let cancelled = false; filterTanks(filters, tanksWithAverages, wargaming?.id).then((tanks) => { if (!cancelled) setFiltered(tanks); }); return () => { cancelled = true; }; }, [filters, wargaming?.id]); if (filtered !== null && filtered.length === 0) { return ( ); } return ( ); } function Head() { return ( {performanceStatNames.map((name) => ( ))} ); } interface BodyProps { filtered: TankDefinition[] | null; } // Owns sorting and period, so changing them never re-renders the head function Body({ filtered }: BodyProps) { const sorting = Performance.use((state) => state.sorting); const period = Performance.use((state) => state.player_count_period); const rows = useMemo(() => { if (filtered === null) return null; return filtered.map((tank) => { const average = averageDefinitions.averages[tank.id]; const entry = { stats: average.mu!, players: exclusionRatio * average.samples![period], } satisfies PerformanceEntry; return { tank, entry, values: computeValues(entry), } satisfies PerformanceRow; }); }, [filtered, period]); // Every tank's stats weighted by how many players it had in the period const total = useMemo(() => { if (rows === null) return null; const stats = {} as AverageDefinitionsAllStats; let samples = 0; for (const { tank } of rows) { const average = averageDefinitions.averages[tank.id]; const weight = average.samples![period]; samples += weight; for (const key in average.mu!) { const stat = key as keyof AverageDefinitionsAllStats; stats[stat] = (stats[stat] ?? 0) + weight * average.mu![stat]; } } return computeValues({ stats, players: exclusionRatio * samples }); }, [rows, period]); const sorted = useMemo(() => { if (rows === null) return null; const direction = sorting.direction === SortDirection.Ascending ? 1 : -1; return [...rows].sort( (a, b) => direction * (a.values[sorting.by] - b.values[sorting.by]), ); }, [rows, sorting]); const data = useMemo( () => (sorted ?? []).map((row) => ({ key: `${row.tank.id}`, row, total: total!, })), [sorted, total], ); return ( ); } interface TotalRowProps { total: Record | null; } function TotalRow({ total }: TotalRowProps) { const strings = useStrings(); return ( {strings.performance.total} {performanceStatNames.map((name) => ( {total === null ? ( ) : ( )} ))} ); } interface ColumnHeaderProps { name: PerformanceStatName; } function ColumnHeader({ name }: ColumnHeaderProps) { const strings = useStrings(); const sorting = Performance.use((state) => state.sorting); return ( { Performance.mutate((draft) => { const ascending = draft.sorting.by === name && draft.sorting.direction === SortDirection.Descending; draft.sorting = { by: name, direction: ascending ? SortDirection.Ascending : SortDirection.Descending, }; }); }} > {strings.common.stats[name]} ); } interface ValueProps { name: PerformanceStatName; value: number; } function Value({ name, value }: ValueProps) { const locale = useLocale(); return ( {Number.isFinite(value) ? performanceStats[name].format(value, locale) : "-"} ); } interface RowProps { row: PerformanceRow; total: Record; } // Memoized so loading more rows leaves the loaded ones be const Row = memo((props: MaybeSkeletonComponentProps) => { if (props.skeleton) { return ; } const { row, total } = props; return ( {performanceStatNames.map((name) => ( ))} ); }); interface TankLinkProps { tank: TankDefinition; } function TankLink({ tank }: TankLinkProps) { const locale = useLocale(); const unwrap = useUnwrapper(); const name = unwrap(tank.name!); return ( {tank.testing && } {name} ); } interface SkeletonRowProps { onIntersection?: () => void; } function SkeletonRow({ onIntersection }: SkeletonRowProps) { const row = useRef(null); useEffect(() => { if (!onIntersection) return; const observer = new IntersectionObserver(([entry]) => { if (entry.isIntersecting) onIntersection(); }); observer.observe(row.current!); return () => observer.disconnect(); }, []); return ( {performanceStatNames.map((name) => ( ))} ); }