Files
blitzkit/packages/website-ue/src/components/TankopediaLoadout/index.tsx
T

439 lines
12 KiB
TypeScript
Raw Normal View History

2026-05-11 16:49:14 -05:00
import { romanize } from "@blitzkit/core";
2026-05-17 13:29:03 -05:00
import type { BlitzStaticEquipmentPresetComponent_EquipmentSlot } from "@protos/blitz_static_equipment_preset_component";
2026-05-15 22:46:36 -05:00
import { Grade } from "@protos/blitz_static_standard_grades_enum";
2026-05-19 13:20:54 -05:00
import { StandardPrice } from "@protos/blitz_static_standard_price";
2026-05-15 22:46:36 -05:00
import type { UpgradeLine } from "@protos/blitz_static_tank_upgrade_line";
import type { StageParameters } from "@protos/blitz_static_tank_upgrade_single_stage";
2026-05-19 13:20:54 -05:00
import {
CornerBottomLeftIcon,
DoubleArrowLeftIcon,
DoubleArrowRightIcon,
} from "@radix-ui/react-icons";
2026-05-17 13:19:28 -05:00
import { chunk } from "lodash-es";
2026-05-15 10:54:36 -05:00
import { useMemo } from "react";
2026-05-17 13:19:28 -05:00
import { equipmentColumns } from "../../config/equipment";
import { catImages } from "../../config/images";
2026-05-15 18:43:08 -05:00
import {
alternativeLines,
isAlternativeLine,
2026-05-19 13:20:54 -05:00
maxModulesPerRow,
2026-05-15 21:18:01 -05:00
originalLineName,
2026-05-17 13:19:28 -05:00
} from "../../config/modules";
2026-05-20 13:48:20 -05:00
import { useCompatibility } from "../../hooks/useCompatibility";
2026-05-20 13:09:19 -05:00
import { useConsumables } from "../../hooks/useConsumables";
2026-05-17 12:56:23 -05:00
import { useEquipment } from "../../hooks/useEquipment";
2026-05-15 10:54:36 -05:00
import { useProtagonist } from "../../hooks/useProtagonist";
2026-04-29 16:27:13 -05:00
import { useStrings } from "../../hooks/useStrings";
2026-05-20 13:09:19 -05:00
import { useTierPrices } from "../../hooks/useTierPrices";
2026-05-15 10:54:36 -05:00
import { useUpgradePreset } from "../../hooks/useUpgradePreset";
2026-04-29 16:27:13 -05:00
import { Tankopedia } from "../../stores/tankopedia";
2026-05-15 18:16:06 -05:00
import { Button } from "../Button";
2026-04-29 16:27:13 -05:00
import { Heading } from "../Heading";
2026-05-19 13:20:54 -05:00
import { Link } from "../Link";
2026-05-15 10:54:36 -05:00
import { Price } from "../Price";
2026-04-29 16:27:13 -05:00
import { Section } from "../Section";
2026-05-11 16:49:14 -05:00
import { Text } from "../Text";
2026-04-29 16:27:13 -05:00
import styles from "./index.module.css";
export function TankopediaLoadout() {
2026-05-18 16:57:21 -05:00
const tank = useProtagonist();
const showModules = tank.tank!.upgrade_lines.some(
(line) => line.stages.length > 1 || line.name in alternativeLines,
);
2026-05-15 21:41:40 -05:00
return (
<Section>
2026-05-18 16:57:21 -05:00
{showModules && <Modules />}
2026-05-15 21:58:30 -05:00
<Equipment />
2026-05-20 13:09:19 -05:00
<Consumables />
2026-05-15 21:41:40 -05:00
</Section>
);
}
2026-04-29 16:27:13 -05:00
2026-05-20 13:09:19 -05:00
function Consumables() {
const consumables = useConsumables();
const tierPrices = useTierPrices();
2026-05-20 13:48:20 -05:00
const tank = useProtagonist();
const isCompatible = useCompatibility();
2026-05-20 13:09:19 -05:00
return Object.values(consumables.consumables).map(
({ compatibility, consumable }) => {
2026-05-20 13:48:20 -05:00
if (!isCompatible(compatibility!, tank)) return null;
2026-05-20 13:09:19 -05:00
return <span>{consumable!.name_key}</span>;
},
);
}
2026-05-15 21:58:30 -05:00
function Equipment() {
const strings = useStrings();
const tank = useProtagonist();
2026-05-17 13:46:53 -05:00
const equipment = useEquipment(tank.tank!);
2026-05-15 21:58:30 -05:00
return (
<div className={styles.section}>
<div className={styles.header}>
<Heading size="4">{strings.tanks.loadout.equipment}</Heading>
</div>
2026-05-17 13:19:28 -05:00
<div className={styles.equipment}>
2026-05-17 13:29:03 -05:00
{chunk(equipment.preset.slots, equipmentColumns).map((chunk, index) => (
<EquipmentRow key={index} chunk={chunk} rowIndex={index} />
))}
2026-05-17 13:19:28 -05:00
</div>
2026-05-15 21:58:30 -05:00
</div>
);
}
2026-05-17 13:29:03 -05:00
interface EquipmentRowProps {
chunk: BlitzStaticEquipmentPresetComponent_EquipmentSlot[];
rowIndex: number;
}
function EquipmentRow({ chunk, rowIndex }: EquipmentRowProps) {
return (
<div className={styles.row}>
{chunk.map((slot, index) => (
<EquipmentSlot
key={index}
slot={slot}
rowIndex={rowIndex}
columnIndex={index}
/>
))}
</div>
);
}
interface EquipmentSlotProps {
slot: BlitzStaticEquipmentPresetComponent_EquipmentSlot;
rowIndex: number;
columnIndex: number;
}
function EquipmentSlot({ slot, rowIndex, columnIndex }: EquipmentSlotProps) {
2026-05-17 13:46:53 -05:00
const equipmentIndex = rowIndex * equipmentColumns + columnIndex;
const tank = useProtagonist();
const equipment = useEquipment(tank.tank!);
const { price } = equipment.price.unlock_slot_prices[equipmentIndex];
2026-05-17 13:29:03 -05:00
return (
<div className={styles.slots}>
2026-05-17 13:46:53 -05:00
<div className={styles.options}>
{slot.options_catalog_i_ds.map((id, index) => (
<EquipmentOption
key={id}
optionIndex={index}
rowIndex={rowIndex}
columnIndex={columnIndex}
/>
))}
</div>
<Price price={price!} />
2026-05-17 13:29:03 -05:00
</div>
);
}
interface EquipmentOptionProps {
optionIndex: number;
rowIndex: number;
columnIndex: number;
}
function EquipmentOption({
optionIndex,
rowIndex,
columnIndex,
}: EquipmentOptionProps) {
const equipmentIndex = rowIndex * equipmentColumns + columnIndex;
2026-05-17 13:46:53 -05:00
2026-05-17 13:29:03 -05:00
const isSelected = Tankopedia.use(
(state) =>
equipmentIndex in state.protagonist.equipment &&
state.protagonist.equipment[equipmentIndex] === optionIndex,
);
return (
<Button
color={isSelected ? undefined : "gray"}
variant={isSelected ? "surface" : "soft"}
data-selected={isSelected}
radius="1"
className={styles.slot}
onClick={() => {
Tankopedia.mutate((draft) => {
if (isSelected) {
delete draft.protagonist.equipment[equipmentIndex];
} else {
draft.protagonist.equipment[equipmentIndex] = optionIndex;
}
});
}}
>
<img src={catImages.squareTransparent} />
</Button>
);
}
2026-05-15 21:41:40 -05:00
function Modules() {
const strings = useStrings();
2026-05-15 10:54:36 -05:00
const tank = useProtagonist();
2026-04-29 16:27:13 -05:00
return (
2026-05-15 21:41:40 -05:00
<div className={styles.section}>
<div className={styles.header}>
<Heading size="4">{strings.tanks.loadout.modules}</Heading>
2026-05-19 13:20:54 -05:00
<Link
2026-05-15 21:41:40 -05:00
onClick={() => {
Tankopedia.mutate((draft) => {
for (const line of tank.tank!.upgrade_lines) {
if (isAlternativeLine(line.name)) continue;
draft.protagonist.upgrades[line.name] = 0;
draft.protagonist.alternates[line.name] = false;
}
});
}}
2026-05-19 13:20:54 -05:00
color="gray"
2026-05-15 21:41:40 -05:00
>
2026-05-19 13:20:54 -05:00
<DoubleArrowLeftIcon />
</Link>
2026-05-15 21:41:40 -05:00
2026-05-19 13:20:54 -05:00
<Link
2026-05-15 21:41:40 -05:00
onClick={() => {
Tankopedia.mutate((draft) => {
for (const line of tank.tank!.upgrade_lines) {
draft.protagonist.upgrades[line.name] = line.stages.length - 1;
if (isAlternativeLine(line.name)) {
const original = originalLineName(line.name)!;
draft.protagonist.alternates[original] = true;
}
}
});
}}
2026-05-19 13:20:54 -05:00
color="gray"
2026-05-15 21:41:40 -05:00
>
2026-05-19 13:20:54 -05:00
<DoubleArrowRightIcon />
</Link>
2026-05-15 21:41:40 -05:00
</div>
2026-04-29 16:27:13 -05:00
<div className={styles.lines}>
2026-05-18 16:57:21 -05:00
{/* TODO: hide lines with 1 module except for alts */}
2026-04-29 16:27:13 -05:00
{tank.tank!.upgrade_lines.map((line) => (
2026-05-11 17:00:30 -05:00
<Line
key={line.name}
2026-05-15 10:54:36 -05:00
name={line.name}
2026-05-11 17:00:30 -05:00
lines={tank.tank!.upgrade_lines}
/>
2026-04-29 16:27:13 -05:00
))}
</div>
2026-05-15 21:41:40 -05:00
</div>
2026-04-29 16:27:13 -05:00
);
}
interface LineProps {
2026-05-15 10:54:36 -05:00
name: string;
2026-05-11 17:00:30 -05:00
lines: UpgradeLine[];
2026-04-29 16:27:13 -05:00
}
2026-05-15 10:54:36 -05:00
function Line({ name, lines }: LineProps) {
2026-05-11 17:00:30 -05:00
const combinedLines = useMemo(() => {
2026-05-15 10:54:36 -05:00
const names: string[] = [name];
2026-05-11 17:00:30 -05:00
2026-05-15 18:43:08 -05:00
for (const originalLine in alternativeLines) {
const alternativeLine = alternativeLines[originalLine];
if (
originalLine === name &&
lines.some((line) => line.name === alternativeLine)
) {
names.push(alternativeLine);
2026-05-11 17:00:30 -05:00
}
}
2026-05-15 10:54:36 -05:00
return names;
}, [name]);
2026-04-29 16:27:13 -05:00
return (
<div className={styles.line}>
2026-05-15 18:43:08 -05:00
{!isAlternativeLine(name) &&
2026-05-15 10:54:36 -05:00
combinedLines.map((name) => (
2026-05-19 13:43:43 -05:00
<div className={styles["chunks-wrapper"]}>
<LineInner key={name} name={name} lines={lines} />
</div>
2026-05-15 10:54:36 -05:00
))}
2026-04-29 16:27:13 -05:00
</div>
);
}
2026-05-11 17:00:30 -05:00
2026-05-15 10:54:36 -05:00
function LineInner({ name, lines }: LineProps) {
const line = lines.find((line) => line.name === name)!;
2026-05-11 17:00:30 -05:00
2026-05-19 13:20:54 -05:00
return chunk(line.stages, maxModulesPerRow).map((chunk, wrapIndex) => (
<div className={styles["line-wrap"]}>
{wrapIndex > 0 && (
<Text size="minor" className={styles["wrap-marker"]} lowContrast>
<CornerBottomLeftIcon />
</Text>
)}
{chunk.map((stage, elementIndex) => (
<LineElement
key={elementIndex}
index={wrapIndex * maxModulesPerRow + elementIndex}
lineName={name}
stage={stage}
/>
))}
</div>
2026-05-15 10:54:36 -05:00
));
}
interface LineElementProps {
index: number;
lineName: string;
stage: StageParameters;
}
function LineElement({ index, lineName, stage }: LineElementProps) {
const upgrades = Tankopedia.use((state) => state.protagonist.upgrades);
2026-05-15 21:18:01 -05:00
const alternates = Tankopedia.use((state) => state.protagonist.alternates);
2026-05-15 10:54:36 -05:00
const tank = useProtagonist();
2026-05-11 17:00:30 -05:00
const strings = useStrings();
2026-05-15 10:54:36 -05:00
const upgradePreset = useUpgradePreset(tank.tank!.tank_upgrade_preset);
let price: StandardPrice | undefined;
switch (stage.grade) {
case Grade.GRADE_COMMON:
price = upgradePreset.common_grade_price;
break;
case Grade.GRADE_RARE:
price = upgradePreset.rare_grade_price;
break;
case Grade.GRADE_EPIC:
price = upgradePreset.unique_grade_price;
break;
case Grade.GRADE_LEGENDARY:
price = upgradePreset.legendary_grade_price;
break;
}
for (const override of upgradePreset.prices_overrides) {
if (override.stage_tech_name !== stage.tech_name) continue;
price = override.price;
}
2026-05-19 13:20:54 -05:00
price ??= StandardPrice.create();
2026-05-15 21:18:01 -05:00
let isSelected: boolean;
if (isAlternativeLine(lineName)) {
const originalLine = originalLineName(lineName);
isSelected = alternates[originalLine!];
} else {
if (alternates[lineName]) {
isSelected = false;
} else {
isSelected = upgrades[lineName] === index;
}
}
2026-05-15 10:54:36 -05:00
return (
2026-05-17 13:46:53 -05:00
<div className={styles.wrapper}>
<Button
className={styles.stage}
color={isSelected ? undefined : "gray"}
variant={isSelected ? "surface" : "soft"}
radius="1"
onClick={() => {
2026-05-19 13:43:43 -05:00
// TODO: make minimum selectable upgrade the last free module for tanks like the destiny
2026-05-17 13:46:53 -05:00
Tankopedia.mutate((draft) => {
const tankData = tank.tank!;
2026-05-15 21:18:01 -05:00
2026-05-17 13:46:53 -05:00
if (isAlternativeLine(lineName)) {
const originalLine = originalLineName(lineName)!;
2026-05-15 21:18:01 -05:00
2026-05-17 13:46:53 -05:00
draft.protagonist.alternates[originalLine] = true;
draft.protagonist.upgrades[lineName] = index;
} else {
draft.protagonist.alternates[lineName] = false;
draft.protagonist.upgrades[lineName] = index;
}
2026-05-15 10:54:36 -05:00
2026-05-17 13:46:53 -05:00
for (const required of stage.required_upgrades) {
for (const line of tankData.upgrade_lines) {
let i = 0;
2026-05-11 17:00:30 -05:00
2026-05-17 13:46:53 -05:00
for (const candidateStage of line.stages) {
if (candidateStage.tech_name === required) {
draft.protagonist.upgrades[line.name] = Math.max(
draft.protagonist.upgrades[line.name],
i,
);
}
i++;
2026-05-11 17:00:30 -05:00
}
2026-05-15 10:54:36 -05:00
}
}
2026-05-17 13:46:53 -05:00
for (const line of tankData.upgrade_lines) {
if (draft.protagonist.alternates[line.name]) {
continue;
}
let i = draft.protagonist.upgrades[line.name];
while (i > 0) {
const currentStage = line.stages[i];
const valid = currentStage.required_upgrades.every(
(required) => {
for (const otherLine of tankData.upgrade_lines) {
const idx = draft.protagonist.upgrades[otherLine.name];
if (otherLine.stages[idx]?.tech_name === required) {
return true;
}
}
return false;
},
);
if (valid) break;
i--;
}
draft.protagonist.upgrades[line.name] = i;
2026-05-15 21:18:01 -05:00
}
2026-05-17 13:46:53 -05:00
});
}}
>
<img
className={styles.icon}
src={`/api/tanks/modules/${stage.stage_type}.webp`}
/>
{/* {stage.display_name} */}
2026-05-15 21:18:01 -05:00
2026-05-17 13:46:53 -05:00
<Text weight="light" lowContrast size="minor" className={styles.tier}>
{isAlternativeLine(lineName)
? strings.tanks.loadout.alternative
: romanize(stage.number)}
</Text>
</Button>
2026-05-11 17:00:30 -05:00
2026-05-19 13:20:54 -05:00
{<Price className={styles.price} price={price} />}
2026-05-17 13:46:53 -05:00
</div>
2026-05-15 10:54:36 -05:00
);
2026-05-11 17:00:30 -05:00
}