2026-11-14 01:17:38 -06:00
|
|
|
import { alias, formatCompact, romanize, tankIcon } from "@blitzkit/core";
|
|
|
|
|
import {
|
|
|
|
|
ModuleType,
|
|
|
|
|
type EngineDefinition,
|
|
|
|
|
type GunDefinition,
|
|
|
|
|
type TankDefinition,
|
|
|
|
|
type TrackDefinition,
|
|
|
|
|
type TurretDefinition,
|
|
|
|
|
type Unlock,
|
|
|
|
|
} from "@blitzkit/protos";
|
|
|
|
|
import { DoubleArrowDownIcon, DoubleArrowUpIcon } from "@radix-ui/react-icons";
|
|
|
|
|
import { useCallback, useMemo } from "react";
|
|
|
|
|
import { api } from "../../api/dynamic";
|
|
|
|
|
import { useLocale } from "../../hooks/useLocale";
|
|
|
|
|
import { useStrings } from "../../hooks/useStrings";
|
|
|
|
|
import { useUnwrapper } from "../../hooks/useUnwrapper";
|
|
|
|
|
import { layoutModuleTree } from "../../tankopedia/moduleTreeLayout";
|
|
|
|
|
import type { TankState } from "../../tankopedia/tankState";
|
2026-11-15 17:28:43 -06:00
|
|
|
import { classNames } from "../../ui/classNames";
|
2026-11-14 01:17:38 -06:00
|
|
|
import { Discriminator } from "../Discriminator";
|
|
|
|
|
import { IconButton } from "../IconButton";
|
|
|
|
|
import { LinkI18n } from "../LinkI18n";
|
|
|
|
|
import { LoadoutSection } from "../LoadoutSection";
|
2026-11-15 17:28:43 -06:00
|
|
|
import shared from "../LoadoutTree/index.module.css";
|
2026-11-14 01:17:38 -06:00
|
|
|
import { Text } from "../Text";
|
|
|
|
|
import { Tooltip } from "../Tooltip";
|
|
|
|
|
import styles from "./index.module.css";
|
|
|
|
|
|
|
|
|
|
const tanks = await api.tanks();
|
|
|
|
|
|
|
|
|
|
interface ContentProps {
|
|
|
|
|
tank: TankDefinition;
|
|
|
|
|
selected: Pick<TankState, "turret" | "gun" | "engine" | "track">;
|
|
|
|
|
onSelect: (unlock: Unlock) => void;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const moduleIconNames: Record<ModuleType, string> = {
|
|
|
|
|
[ModuleType.MODULE_TYPE_VEHICLE]: "",
|
|
|
|
|
[ModuleType.MODULE_TYPE_ENGINE]: "engine",
|
|
|
|
|
[ModuleType.MODULE_TYPE_TRACKS]: "chassis",
|
|
|
|
|
[ModuleType.MODULE_TYPE_TURRET]: "turret",
|
|
|
|
|
[ModuleType.MODULE_TYPE_GUN]: "gun",
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
type AnyModule =
|
|
|
|
|
| TurretDefinition
|
|
|
|
|
| GunDefinition
|
|
|
|
|
| TrackDefinition
|
|
|
|
|
| EngineDefinition;
|
|
|
|
|
|
|
|
|
|
function stock(type: ModuleType, id: number) {
|
|
|
|
|
return { id, type, cost: { type: "xp", value: 0 } } satisfies Unlock;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function Content({ tank, selected, onSelect }: ContentProps) {
|
|
|
|
|
const unwrap = useUnwrapper();
|
|
|
|
|
|
|
|
|
|
// The same gun can sit on several turrets; reversing keeps the first one,
|
|
|
|
|
// which is what a linear find would return
|
|
|
|
|
const modules = useMemo(
|
|
|
|
|
() =>
|
|
|
|
|
new Map<ModuleType, Map<number, AnyModule>>([
|
|
|
|
|
[
|
|
|
|
|
ModuleType.MODULE_TYPE_TRACKS,
|
|
|
|
|
new Map(tank.tracks.map((track) => [track.id, track])),
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
ModuleType.MODULE_TYPE_ENGINE,
|
|
|
|
|
new Map(tank.engines.map((engine) => [engine.id, engine])),
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
ModuleType.MODULE_TYPE_TURRET,
|
|
|
|
|
new Map(tank.turrets.map((turret) => [turret.id, turret])),
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
ModuleType.MODULE_TYPE_GUN,
|
|
|
|
|
new Map(
|
|
|
|
|
tank.turrets
|
|
|
|
|
.flatMap((turret) => turret.guns)
|
|
|
|
|
.toReversed()
|
2026-11-15 17:28:43 -06:00
|
|
|
.map((gun) => [gun.id, gun]),
|
2026-11-14 01:17:38 -06:00
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
]),
|
2026-11-15 17:28:43 -06:00
|
|
|
[tank],
|
2026-11-14 01:17:38 -06:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const resolve = useCallback(
|
|
|
|
|
(unlock: Unlock) =>
|
|
|
|
|
unlock.type === ModuleType.MODULE_TYPE_VEHICLE
|
|
|
|
|
? (tanks.tanks[unlock.id] as TankDefinition | undefined)
|
|
|
|
|
: modules.get(unlock.type)?.get(unlock.id),
|
2026-11-15 17:28:43 -06:00
|
|
|
[modules],
|
2026-11-14 01:17:38 -06:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const { cells, columns, rows } = useMemo(
|
|
|
|
|
() =>
|
|
|
|
|
layoutModuleTree(
|
|
|
|
|
[
|
|
|
|
|
stock(ModuleType.MODULE_TYPE_GUN, tank.turrets[0].guns[0].id),
|
|
|
|
|
stock(ModuleType.MODULE_TYPE_TURRET, tank.turrets[0].id),
|
|
|
|
|
stock(ModuleType.MODULE_TYPE_ENGINE, tank.engines[0].id),
|
|
|
|
|
stock(ModuleType.MODULE_TYPE_TRACKS, tank.tracks[0].id),
|
|
|
|
|
].filter((unlock) => resolve(unlock) !== undefined),
|
|
|
|
|
(unlock) => {
|
|
|
|
|
const module = resolve(unlock);
|
|
|
|
|
const children =
|
|
|
|
|
module !== undefined && "unlocks" in module ? module.unlocks : [];
|
|
|
|
|
|
|
|
|
|
return children.filter((child) => resolve(child) !== undefined);
|
|
|
|
|
},
|
2026-11-15 17:28:43 -06:00
|
|
|
tank.module_positions,
|
2026-11-14 01:17:38 -06:00
|
|
|
),
|
2026-11-15 17:28:43 -06:00
|
|
|
[tank, resolve],
|
2026-11-14 01:17:38 -06:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Prices hang below the modules, so room is only reserved if the bottom row has one
|
|
|
|
|
const bottomRow = Math.max(...cells.map((cell) => cell.position[0]));
|
|
|
|
|
const hasBottomCost = cells.some(
|
|
|
|
|
(cell) =>
|
|
|
|
|
cell.position[0] === bottomRow &&
|
|
|
|
|
cell.unlock !== undefined &&
|
2026-11-15 17:28:43 -06:00
|
|
|
cell.unlock.cost!.value > 0,
|
2026-11-14 01:17:38 -06:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div
|
2026-11-15 17:28:43 -06:00
|
|
|
className={classNames(shared.tree, styles.tree)}
|
2026-11-14 01:17:38 -06:00
|
|
|
data-has-bottom-cost={hasBottomCost}
|
|
|
|
|
style={{
|
|
|
|
|
// Transposed: the layout's columns (depth) become rows
|
|
|
|
|
gridTemplateColumns: `repeat(${rows}, var(--module-size))`,
|
|
|
|
|
gridTemplateRows: `repeat(${columns}, var(--module-size))`,
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{cells.map((cell) => {
|
|
|
|
|
const module = cell.unlock && resolve(cell.unlock);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div
|
|
|
|
|
key={cell.position.join(",")}
|
2026-11-15 17:28:43 -06:00
|
|
|
className={classNames(shared.cell, styles.cell)}
|
2026-11-14 01:17:38 -06:00
|
|
|
style={{ gridColumn: cell.position[1], gridRow: cell.position[0] }}
|
|
|
|
|
>
|
|
|
|
|
{cell.trunkUp && <span className={styles["trunk-left"]} />}
|
|
|
|
|
{cell.trunkDown && <span className={styles["trunk-right"]} />}
|
|
|
|
|
{cell.snakeUp && <span className={styles["snake-left"]} />}
|
|
|
|
|
{cell.snakeDown && <span className={styles["snake-right"]} />}
|
|
|
|
|
{cell.right && <span className={styles.bottom} />}
|
|
|
|
|
{cell.left && <span className={styles.top} />}
|
|
|
|
|
|
|
|
|
|
{cell.unlock && module && (
|
|
|
|
|
<ModuleNode
|
|
|
|
|
unlock={cell.unlock}
|
|
|
|
|
module={module}
|
|
|
|
|
tank={tank}
|
|
|
|
|
selected={selected}
|
|
|
|
|
tooltip={unwrap(module.name!)}
|
|
|
|
|
onSelect={onSelect}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
})}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface ModuleNodeProps {
|
|
|
|
|
unlock: Unlock;
|
|
|
|
|
module: AnyModule | TankDefinition;
|
|
|
|
|
tank: TankDefinition;
|
|
|
|
|
selected: ContentProps["selected"];
|
|
|
|
|
tooltip: string;
|
|
|
|
|
onSelect: ContentProps["onSelect"];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function ModuleNode({
|
|
|
|
|
unlock,
|
|
|
|
|
module,
|
|
|
|
|
tank,
|
|
|
|
|
selected,
|
|
|
|
|
tooltip,
|
|
|
|
|
onSelect,
|
|
|
|
|
}: ModuleNodeProps) {
|
|
|
|
|
const locale = useLocale();
|
|
|
|
|
|
|
|
|
|
const isTank = unlock.type === ModuleType.MODULE_TYPE_VEHICLE;
|
|
|
|
|
const topTurret = tank.turrets.at(-1)!;
|
|
|
|
|
|
|
|
|
|
const isSelected =
|
|
|
|
|
(unlock.type === ModuleType.MODULE_TYPE_TURRET &&
|
|
|
|
|
selected.turret === unlock.id) ||
|
|
|
|
|
(unlock.type === ModuleType.MODULE_TYPE_GUN &&
|
|
|
|
|
selected.gun === unlock.id) ||
|
|
|
|
|
(unlock.type === ModuleType.MODULE_TYPE_ENGINE &&
|
|
|
|
|
selected.engine === unlock.id) ||
|
|
|
|
|
(unlock.type === ModuleType.MODULE_TYPE_TRACKS &&
|
|
|
|
|
selected.track === unlock.id);
|
|
|
|
|
|
|
|
|
|
const isTop =
|
|
|
|
|
(unlock.type === ModuleType.MODULE_TYPE_TURRET &&
|
|
|
|
|
unlock.id === topTurret.id) ||
|
|
|
|
|
(unlock.type === ModuleType.MODULE_TYPE_GUN &&
|
|
|
|
|
unlock.id === topTurret.guns.at(-1)!.id) ||
|
|
|
|
|
(unlock.type === ModuleType.MODULE_TYPE_ENGINE &&
|
|
|
|
|
unlock.id === tank.engines.at(-1)!.id) ||
|
|
|
|
|
(unlock.type === ModuleType.MODULE_TYPE_TRACKS &&
|
|
|
|
|
unlock.id === tank.tracks.at(-1)!.id);
|
|
|
|
|
|
|
|
|
|
const button = (
|
|
|
|
|
<IconButton
|
|
|
|
|
radius="1"
|
2026-11-15 17:28:43 -06:00
|
|
|
size="major"
|
2026-11-14 01:17:38 -06:00
|
|
|
variant={isSelected ? "surface" : "soft"}
|
|
|
|
|
color={isSelected ? (isTop ? "amber" : undefined) : "gray"}
|
2026-11-15 17:28:43 -06:00
|
|
|
className={shared.node}
|
2026-11-14 01:17:38 -06:00
|
|
|
data-major={unlock.type === ModuleType.MODULE_TYPE_VEHICLE}
|
|
|
|
|
onClick={() => {
|
|
|
|
|
if (!isTank) onSelect(unlock);
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<img
|
|
|
|
|
alt={tooltip}
|
|
|
|
|
src={
|
|
|
|
|
isTank
|
|
|
|
|
? tankIcon(unlock.id)
|
|
|
|
|
: alias(
|
|
|
|
|
"api",
|
2026-11-15 17:28:43 -06:00
|
|
|
`/icons/modules/${moduleIconNames[unlock.type]}.webp`,
|
2026-11-14 01:17:38 -06:00
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
<Discriminator corner="top-left">{romanize(module.tier)}</Discriminator>
|
|
|
|
|
|
|
|
|
|
{unlock.cost!.value > 0 && (
|
2026-11-15 17:28:43 -06:00
|
|
|
<div className={shared.price}>
|
2026-11-14 01:17:38 -06:00
|
|
|
<Text size="minor">{formatCompact(locale, unlock.cost!.value)}</Text>
|
|
|
|
|
<img
|
|
|
|
|
alt={unlock.cost!.type}
|
|
|
|
|
src={alias("api", `/icons/currencies/${unlock.cost!.type}.webp`)}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
</IconButton>
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Tooltip tooltip={tooltip}>
|
|
|
|
|
{isTank ? (
|
|
|
|
|
<LinkI18n
|
|
|
|
|
locale={locale}
|
|
|
|
|
href={`/tanks/${tanks.tanks[unlock.id].slug}`}
|
|
|
|
|
>
|
|
|
|
|
{button}
|
|
|
|
|
</LinkI18n>
|
|
|
|
|
) : (
|
|
|
|
|
button
|
|
|
|
|
)}
|
|
|
|
|
</Tooltip>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface LoadoutModulesProps {
|
|
|
|
|
tank: TankDefinition;
|
|
|
|
|
selected: Pick<TankState, "turret" | "gun" | "engine" | "track">;
|
|
|
|
|
onSelect: (unlock: Unlock) => void;
|
|
|
|
|
onStock: () => void;
|
|
|
|
|
onMaximize: () => void;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function LoadoutModules({
|
|
|
|
|
tank,
|
|
|
|
|
selected,
|
|
|
|
|
onSelect,
|
|
|
|
|
onStock,
|
|
|
|
|
onMaximize,
|
|
|
|
|
}: LoadoutModulesProps) {
|
|
|
|
|
const strings = useStrings();
|
|
|
|
|
const hasUpgrades =
|
|
|
|
|
tank.turrets.length > 1 ||
|
|
|
|
|
tank.turrets[0].guns.length > 1 ||
|
|
|
|
|
tank.engines.length > 1 ||
|
|
|
|
|
tank.tracks.length > 1;
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<LoadoutSection
|
|
|
|
|
title={strings.tanks.loadout.modules}
|
|
|
|
|
actions={
|
|
|
|
|
hasUpgrades && (
|
|
|
|
|
<>
|
|
|
|
|
<IconButton
|
|
|
|
|
highContrast
|
|
|
|
|
color="gray"
|
|
|
|
|
size="minor"
|
|
|
|
|
variant="ghost"
|
|
|
|
|
onClick={onStock}
|
|
|
|
|
>
|
|
|
|
|
<DoubleArrowUpIcon />
|
|
|
|
|
</IconButton>
|
|
|
|
|
|
|
|
|
|
<IconButton
|
|
|
|
|
highContrast
|
|
|
|
|
color="gray"
|
|
|
|
|
size="minor"
|
|
|
|
|
variant="ghost"
|
|
|
|
|
onClick={onMaximize}
|
|
|
|
|
>
|
|
|
|
|
<DoubleArrowDownIcon />
|
|
|
|
|
</IconButton>
|
|
|
|
|
</>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
>
|
|
|
|
|
<Content tank={tank} selected={selected} onSelect={onSelect} />
|
|
|
|
|
</LoadoutSection>
|
|
|
|
|
);
|
|
|
|
|
}
|