2026-11-14 01:17:38 -06:00
|
|
|
import { alias } from "@blitzkit/core";
|
|
|
|
|
import { literals } from "@blitzkit/i18n/src/literals";
|
|
|
|
|
import {
|
|
|
|
|
CaretLeftIcon,
|
|
|
|
|
CaretRightIcon,
|
|
|
|
|
CaretSortIcon,
|
|
|
|
|
} from "@radix-ui/react-icons";
|
|
|
|
|
import { useMemo } from "react";
|
|
|
|
|
import { characteristicsGroups } from "../../config/characteristicsGroups";
|
2026-11-27 14:57:39 -06:00
|
|
|
import { compareInsertionMarker } from "../../config/compare";
|
2026-11-14 01:17:38 -06:00
|
|
|
import { computeCharacteristics } from "../../hooks/useCharacteristics";
|
2026-11-27 14:57:39 -06:00
|
|
|
import { withLocale } from "../../hocs/withLocale";
|
2026-11-14 01:17:38 -06:00
|
|
|
import { useStrings } from "../../hooks/useStrings";
|
|
|
|
|
import { useUnwrapper } from "../../hooks/useUnwrapper";
|
|
|
|
|
import {
|
|
|
|
|
CompareEphemeral,
|
|
|
|
|
syncShellSlot,
|
|
|
|
|
type CompareMember,
|
|
|
|
|
} from "../../stores/compareEphemeral";
|
|
|
|
|
import { ComparePersistent } from "../../stores/comparePersistent";
|
2026-11-17 22:01:46 -06:00
|
|
|
import { SortDirection } from "../../stores/tankopediaPersistent";
|
2026-11-14 01:17:38 -06:00
|
|
|
import { rankingValue } from "../../tankopedia/calculateRankings";
|
|
|
|
|
import { resolveTankModules } from "../../tankopedia/resolveTankModules";
|
|
|
|
|
import {
|
|
|
|
|
type CharacteristicName,
|
|
|
|
|
type CharacteristicRenderConfig,
|
|
|
|
|
type ComputedCharacteristics,
|
|
|
|
|
} from "../../types/characteristics";
|
|
|
|
|
import { CharacteristicValue } from "../CharacteristicValue";
|
|
|
|
|
import { CompareEnvironment } from "../CompareEnvironment";
|
|
|
|
|
import { CompareLoadout } from "../CompareLoadout";
|
|
|
|
|
import { CompareTankCard } from "../CompareTankCard";
|
2026-11-27 14:57:39 -06:00
|
|
|
import { EmptyState } from "../EmptyState";
|
2026-11-14 01:17:38 -06:00
|
|
|
import { IconButton } from "../IconButton";
|
2026-11-27 14:57:39 -06:00
|
|
|
import { InsertionMarker } from "../InsertionMarker";
|
2026-11-20 23:23:40 -06:00
|
|
|
import { LazyImage } from "../LazyImage";
|
2026-11-14 01:17:38 -06:00
|
|
|
import { Table } from "../Table";
|
|
|
|
|
import { Text } from "../Text";
|
|
|
|
|
import { Tooltip } from "../Tooltip";
|
|
|
|
|
import styles from "./index.module.css";
|
|
|
|
|
|
|
|
|
|
// Removes one occurrence of each excluded id, keeping other tanks' duplicates
|
|
|
|
|
function without(ids: number[], excluded: number[]) {
|
|
|
|
|
const remaining = [...ids];
|
|
|
|
|
|
|
|
|
|
for (const id of excluded) {
|
|
|
|
|
remaining.splice(remaining.indexOf(id), 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return remaining;
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-27 14:57:39 -06:00
|
|
|
// The table of members, or an empty state when there are none
|
|
|
|
|
export const CompareTable = withLocale(Content);
|
|
|
|
|
|
|
|
|
|
function Content() {
|
|
|
|
|
const strings = useStrings();
|
|
|
|
|
const members = CompareEphemeral.useDeferred(
|
|
|
|
|
(state) => state.members,
|
|
|
|
|
CompareEphemeral.initial.members,
|
|
|
|
|
);
|
2026-11-14 01:17:38 -06:00
|
|
|
const environment = CompareEphemeral.use((state) => state.environment);
|
|
|
|
|
|
|
|
|
|
// Other tanks' consumables and provisions can reduce a tank's damage, so each
|
|
|
|
|
// tank is computed against a synthetic opponent carrying all of theirs
|
|
|
|
|
const characteristics = useMemo(() => {
|
|
|
|
|
const allConsumables = members.flatMap(({ state }) => state.consumables);
|
|
|
|
|
const allProvisions = members.flatMap(({ state }) => state.provisions);
|
|
|
|
|
|
|
|
|
|
return members.map((member) => {
|
|
|
|
|
const antagonist = {
|
|
|
|
|
...member.state,
|
|
|
|
|
consumables: without(allConsumables, member.state.consumables),
|
|
|
|
|
provisions: without(allProvisions, member.state.provisions),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return computeCharacteristics(
|
|
|
|
|
{
|
|
|
|
|
protagonist: member.state,
|
|
|
|
|
antagonist: members.length > 1 ? antagonist : member.state,
|
|
|
|
|
},
|
|
|
|
|
environment,
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
}, [members, environment]);
|
|
|
|
|
|
2026-11-27 14:57:39 -06:00
|
|
|
if (members.length === 0) {
|
|
|
|
|
return (
|
|
|
|
|
<EmptyState
|
|
|
|
|
title={strings.compare.no_tanks.title}
|
|
|
|
|
body={strings.compare.no_tanks.body}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-14 01:17:38 -06:00
|
|
|
return (
|
2026-11-27 14:57:39 -06:00
|
|
|
<>
|
|
|
|
|
<Table.Root>
|
|
|
|
|
<Table.Head>
|
|
|
|
|
<Table.Row>
|
|
|
|
|
<Table.IconCorner />
|
2026-11-14 01:17:38 -06:00
|
|
|
|
2026-11-27 14:57:39 -06:00
|
|
|
{members.map(({ key, state }, index) => (
|
|
|
|
|
<CompareTankCard
|
|
|
|
|
key={key}
|
|
|
|
|
index={index}
|
|
|
|
|
tank={resolveTankModules(state).tank}
|
|
|
|
|
/>
|
|
|
|
|
))}
|
|
|
|
|
</Table.Row>
|
|
|
|
|
</Table.Head>
|
2026-11-14 01:17:38 -06:00
|
|
|
|
2026-11-27 14:57:39 -06:00
|
|
|
<Table.Body>
|
|
|
|
|
<Table.Row>
|
|
|
|
|
<Table.RowHeader>
|
|
|
|
|
<CompareEnvironment />
|
|
|
|
|
</Table.RowHeader>
|
2026-11-14 01:17:38 -06:00
|
|
|
|
2026-11-27 14:57:39 -06:00
|
|
|
{members.map((member, index) => (
|
|
|
|
|
<LoadoutCell key={member.key} index={index} />
|
|
|
|
|
))}
|
|
|
|
|
</Table.Row>
|
|
|
|
|
</Table.Body>
|
2026-11-14 01:17:38 -06:00
|
|
|
|
2026-11-27 14:57:39 -06:00
|
|
|
{characteristicsGroups.map((group) => (
|
|
|
|
|
<Group
|
|
|
|
|
key={group.name}
|
|
|
|
|
group={group}
|
|
|
|
|
characteristics={characteristics}
|
|
|
|
|
/>
|
|
|
|
|
))}
|
|
|
|
|
</Table.Root>
|
|
|
|
|
|
|
|
|
|
<InsertionMarker ref={compareInsertionMarker} />
|
|
|
|
|
</>
|
2026-11-14 01:17:38 -06:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface LoadoutCellProps {
|
|
|
|
|
index: number;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function LoadoutCell({ index }: LoadoutCellProps) {
|
|
|
|
|
return (
|
|
|
|
|
<Table.Cell className={styles.loadout}>
|
|
|
|
|
<CompareLoadout index={index} />
|
|
|
|
|
</Table.Cell>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface ShellSwitcherProps {
|
|
|
|
|
member: CompareMember;
|
|
|
|
|
index: number;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function ShellSwitcher({ member, index }: ShellSwitcherProps) {
|
|
|
|
|
const unwrap = useUnwrapper();
|
|
|
|
|
const { gun, shell } = resolveTankModules(member.state);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className={styles.shells}>
|
|
|
|
|
{gun.shells.map((candidate, shellIndex) => {
|
|
|
|
|
const isSelected = candidate.id === shell.id;
|
|
|
|
|
const name = unwrap(candidate.name!);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Tooltip key={candidate.id} tooltip={name}>
|
|
|
|
|
<IconButton
|
|
|
|
|
size="minor"
|
|
|
|
|
color={isSelected ? undefined : "gray"}
|
|
|
|
|
variant={isSelected ? "surface" : "soft"}
|
|
|
|
|
parentListChild="horizontal"
|
|
|
|
|
onClick={() => {
|
|
|
|
|
CompareEphemeral.mutate((draft) => {
|
|
|
|
|
if (draft.syncShells) {
|
|
|
|
|
syncShellSlot(draft.members, shellIndex);
|
|
|
|
|
} else {
|
|
|
|
|
draft.members[index].state.shell = candidate.id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
draft.sorting.by = null;
|
|
|
|
|
});
|
|
|
|
|
}}
|
|
|
|
|
>
|
2026-11-20 23:23:40 -06:00
|
|
|
<LazyImage
|
2026-11-14 01:17:38 -06:00
|
|
|
alt={name}
|
2026-11-28 17:26:21 -06:00
|
|
|
src={alias("api", `icons/shells/${candidate.icon}.webp`)}
|
2026-11-14 01:17:38 -06:00
|
|
|
/>
|
|
|
|
|
</IconButton>
|
|
|
|
|
</Tooltip>
|
|
|
|
|
);
|
|
|
|
|
})}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface GroupProps {
|
|
|
|
|
group: (typeof characteristicsGroups)[number];
|
|
|
|
|
characteristics: ComputedCharacteristics[];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function Group({ group, characteristics }: GroupProps) {
|
|
|
|
|
const strings = useStrings();
|
|
|
|
|
const members = CompareEphemeral.use((state) => state.members);
|
2026-11-20 23:44:14 -06:00
|
|
|
// A lone tank still shows its values; otherwise rows need two tanks to compare
|
|
|
|
|
const minimumPresent = Math.min(2, characteristics.length);
|
2026-11-14 01:17:38 -06:00
|
|
|
const items = group.order.filter(
|
|
|
|
|
(item): item is CharacteristicRenderConfig =>
|
|
|
|
|
"name" in item &&
|
2026-11-20 23:44:14 -06:00
|
|
|
characteristics.filter((computed) => computed.has(item.name)).length >=
|
|
|
|
|
minimumPresent,
|
2026-11-14 01:17:38 -06:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (items.length === 0) return null;
|
|
|
|
|
|
|
|
|
|
const isShellGroup = group.name === "shell";
|
|
|
|
|
const title = (
|
|
|
|
|
strings.tanks.characteristics.titles as Record<string, string>
|
|
|
|
|
)[group.name];
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Table.Body>
|
|
|
|
|
<Table.Row>
|
|
|
|
|
<Table.GroupTitle
|
|
|
|
|
colSpan={isShellGroup ? undefined : characteristics.length + 1}
|
|
|
|
|
>
|
|
|
|
|
{title}
|
|
|
|
|
</Table.GroupTitle>
|
|
|
|
|
|
|
|
|
|
{isShellGroup &&
|
|
|
|
|
members.map((member, index) => (
|
|
|
|
|
<Table.GroupCell key={member.key} className={styles["shell-cell"]}>
|
|
|
|
|
<ShellSwitcher member={member} index={index} />
|
|
|
|
|
</Table.GroupCell>
|
|
|
|
|
))}
|
|
|
|
|
</Table.Row>
|
|
|
|
|
|
|
|
|
|
{items.map((config) => (
|
|
|
|
|
<Row
|
|
|
|
|
key={config.name}
|
|
|
|
|
config={config}
|
|
|
|
|
characteristics={characteristics}
|
|
|
|
|
/>
|
|
|
|
|
))}
|
|
|
|
|
</Table.Body>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface RowProps {
|
|
|
|
|
config: CharacteristicRenderConfig;
|
|
|
|
|
characteristics: ComputedCharacteristics[];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function Row({ config, characteristics }: RowProps) {
|
|
|
|
|
const strings = useStrings();
|
|
|
|
|
const sorting = CompareEphemeral.use((state) => state.sorting);
|
|
|
|
|
const deltaMode = ComparePersistent.use((state) => state.deltaMode);
|
|
|
|
|
const members = CompareEphemeral.use((state) => state.members);
|
|
|
|
|
|
|
|
|
|
const values = characteristics.map((computed) =>
|
|
|
|
|
rankingValue(config.name, computed.get(config.name)),
|
|
|
|
|
);
|
|
|
|
|
const truth = values[0];
|
|
|
|
|
const isSorted = sorting.by === config.name;
|
|
|
|
|
const label = (strings.tanks.characteristics.items as Record<string, string>)[
|
|
|
|
|
config.name
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Table.Row>
|
|
|
|
|
<Table.RowHeader>
|
|
|
|
|
<div className={styles.label} data-indent={config.indent}>
|
|
|
|
|
<IconButton
|
|
|
|
|
variant="ghost"
|
|
|
|
|
size="minor"
|
|
|
|
|
color={isSorted ? undefined : "gray"}
|
|
|
|
|
onClick={() => sort(config.name, values, members)}
|
|
|
|
|
>
|
|
|
|
|
{!isSorted && <CaretSortIcon className={styles.sort} />}
|
2026-11-17 22:01:46 -06:00
|
|
|
{isSorted && sorting.direction === SortDirection.Ascending && (
|
|
|
|
|
<CaretRightIcon />
|
|
|
|
|
)}
|
|
|
|
|
{isSorted && sorting.direction === SortDirection.Descending && (
|
|
|
|
|
<CaretLeftIcon />
|
|
|
|
|
)}
|
2026-11-14 01:17:38 -06:00
|
|
|
</IconButton>
|
|
|
|
|
|
|
|
|
|
<Text>{label}</Text>
|
|
|
|
|
|
|
|
|
|
{config.units !== undefined && (
|
|
|
|
|
<Text lowContrast size="minor">
|
|
|
|
|
{literals(strings.units[config.units], { value: "" })}
|
|
|
|
|
</Text>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
</Table.RowHeader>
|
|
|
|
|
|
|
|
|
|
{characteristics.map((computed, index) => {
|
|
|
|
|
const value = values[index];
|
|
|
|
|
const output = computed.get(config.name);
|
|
|
|
|
const delta =
|
|
|
|
|
value !== undefined && truth !== undefined ? value - truth : 0;
|
|
|
|
|
|
|
|
|
|
return (
|
2026-11-17 22:01:46 -06:00
|
|
|
<Table.CellRanked
|
2026-11-14 01:17:38 -06:00
|
|
|
key={members[index].key}
|
|
|
|
|
align="end"
|
2026-11-17 22:01:46 -06:00
|
|
|
ranking={config.ranking}
|
|
|
|
|
value={value}
|
|
|
|
|
truth={truth}
|
2026-11-14 01:17:38 -06:00
|
|
|
>
|
|
|
|
|
<div className={styles.value}>
|
|
|
|
|
{output && (
|
|
|
|
|
<CharacteristicValue
|
|
|
|
|
config={config}
|
|
|
|
|
value={output}
|
|
|
|
|
units={false}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
{delta !== 0 && Number.isFinite(delta) && (
|
|
|
|
|
<>
|
|
|
|
|
{deltaMode === "absolute" && (
|
|
|
|
|
<Text lowContrast size="minor">
|
|
|
|
|
({delta > 0 ? "+" : ""}
|
|
|
|
|
{Number(delta.toFixed(config.decimals ?? 2))})
|
|
|
|
|
</Text>
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
{deltaMode === "percentage" && truth !== 0 && (
|
|
|
|
|
<Text lowContrast size="minor">
|
|
|
|
|
({delta > 0 ? "+" : ""}
|
|
|
|
|
{Math.round((delta / Math.abs(truth!)) * 100)}%)
|
|
|
|
|
</Text>
|
|
|
|
|
)}
|
|
|
|
|
</>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
2026-11-17 22:01:46 -06:00
|
|
|
</Table.CellRanked>
|
2026-11-14 01:17:38 -06:00
|
|
|
);
|
|
|
|
|
})}
|
|
|
|
|
</Table.Row>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function sort(
|
|
|
|
|
name: CharacteristicName,
|
|
|
|
|
values: (number | undefined)[],
|
|
|
|
|
members: CompareMember[],
|
|
|
|
|
) {
|
|
|
|
|
const byKey = new Map(members.map(({ key }, index) => [key, values[index]]));
|
|
|
|
|
|
|
|
|
|
CompareEphemeral.mutate((draft) => {
|
|
|
|
|
const ascending =
|
|
|
|
|
draft.sorting?.by === name &&
|
2026-11-17 22:01:46 -06:00
|
|
|
draft.sorting.direction === SortDirection.Descending;
|
2026-11-14 01:17:38 -06:00
|
|
|
|
|
|
|
|
// Missing values always sink to the end
|
|
|
|
|
draft.members.sort((a, b) => {
|
|
|
|
|
const valueA = byKey.get(a.key);
|
|
|
|
|
const valueB = byKey.get(b.key);
|
|
|
|
|
|
|
|
|
|
if (valueA === undefined && valueB === undefined) return 0;
|
|
|
|
|
if (valueA === undefined) return 1;
|
|
|
|
|
if (valueB === undefined) return -1;
|
|
|
|
|
|
|
|
|
|
return ascending ? valueA - valueB : valueB - valueA;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
draft.sorting = {
|
|
|
|
|
by: name,
|
2026-11-17 22:01:46 -06:00
|
|
|
direction: ascending ? SortDirection.Ascending : SortDirection.Descending,
|
2026-11-14 01:17:38 -06:00
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
}
|