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

378 lines
10 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";
import type { StandardPrice } from "@protos/blitz_static_standard_price";
import type { UpgradeLine } from "@protos/blitz_static_tank_upgrade_line";
import type { StageParameters } from "@protos/blitz_static_tank_upgrade_single_stage";
2026-05-15 21:41:40 -05:00
import { DoubleArrowDownIcon, DoubleArrowUpIcon } 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-15 21:18:01 -05:00
originalLineName,
2026-05-17 13:19:28 -05:00
} from "../../config/modules";
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-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-15 21:41:40 -05:00
import { IconButton } from "../IconButton";
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-15 21:41:40 -05:00
return (
<Section>
<Modules />
2026-05-15 21:58:30 -05:00
<Equipment />
2026-05-15 21:41:40 -05:00
</Section>
);
}
2026-04-29 16:27:13 -05:00
2026-05-15 21:58:30 -05:00
function Equipment() {
const strings = useStrings();
const tank = useProtagonist();
2026-05-17 12:56:23 -05:00
const equipment = useEquipment(
tank.tank!.equipment_preset_catalog_id,
tank.tank!.equipment_price_preset_catalog_id,
);
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) {
return (
<div className={styles.slots}>
{slot.options_catalog_i_ds.map((id, index) => (
<EquipmentOption
key={id}
optionIndex={index}
rowIndex={rowIndex}
columnIndex={columnIndex}
/>
))}
</div>
);
}
interface EquipmentOptionProps {
optionIndex: number;
rowIndex: number;
columnIndex: number;
}
function EquipmentOption({
optionIndex,
rowIndex,
columnIndex,
}: EquipmentOptionProps) {
const equipmentIndex = rowIndex * equipmentColumns + columnIndex;
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>
<IconButton
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;
}
});
}}
color="tomato"
variant="ghost"
>
<DoubleArrowDownIcon />
</IconButton>
<IconButton
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;
}
}
});
}}
color="jade"
variant="ghost"
>
<DoubleArrowUpIcon />
</IconButton>
</div>
2026-04-29 16:27:13 -05:00
<div className={styles.lines}>
{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) => (
<LineInner key={name} name={name} lines={lines} />
))}
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-15 10:54:36 -05:00
return line.stages.map((stage, index) => (
<LineElement key={index} index={index} lineName={name} stage={stage} />
));
}
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-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-15 18:16:06 -05:00
<Button
2026-05-15 10:54:36 -05:00
color={isSelected ? undefined : "gray"}
variant={isSelected ? "surface" : "soft"}
radius="1"
className={styles.stage}
onClick={() => {
Tankopedia.mutate((draft) => {
2026-05-15 21:18:01 -05:00
const tankData = tank.tank!;
if (isAlternativeLine(lineName)) {
const originalLine = originalLineName(lineName)!;
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
for (const required of stage.required_upgrades) {
2026-05-15 21:18:01 -05:00
for (const line of tankData.upgrade_lines) {
2026-05-15 10:54:36 -05:00
let i = 0;
2026-05-11 17:00:30 -05:00
2026-05-15 21:18:01 -05:00
for (const candidateStage of line.stages) {
if (candidateStage.tech_name === required) {
2026-05-15 10:54:36 -05:00
draft.protagonist.upgrades[line.name] = Math.max(
draft.protagonist.upgrades[line.name],
i,
);
2026-05-11 17:00:30 -05:00
}
2026-05-15 10:54:36 -05:00
i++;
}
}
}
2026-05-15 21:18:01 -05:00
for (const line of tankData.upgrade_lines) {
if (draft.protagonist.alternates[line.name]) {
continue;
}
2026-05-15 10:54:36 -05:00
let i = draft.protagonist.upgrades[line.name];
2026-05-11 17:00:30 -05:00
2026-05-15 10:54:36 -05:00
while (i > 0) {
const currentStage = line.stages[i];
2026-05-15 21:18:01 -05:00
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) {
2026-05-15 10:54:36 -05:00
return true;
}
}
2026-05-11 17:00:30 -05:00
2026-05-15 10:54:36 -05:00
return false;
});
2026-05-11 17:00:30 -05:00
2026-05-15 10:54:36 -05:00
if (valid) break;
2026-05-11 17:00:30 -05:00
2026-05-15 10:54:36 -05:00
i--;
}
2026-05-11 17:00:30 -05:00
2026-05-15 10:54:36 -05:00
draft.protagonist.upgrades[line.name] = i;
}
});
}}
>
<img
className={styles.icon}
src={`/api/tanks/modules/${stage.stage_type}.webp`}
/>
{/* {stage.display_name} */}
2026-05-11 17:00:30 -05:00
2026-05-15 21:18:01 -05:00
<Text weight="light" lowContrast size="minor" className={styles.tier}>
2026-05-15 18:43:08 -05:00
{isAlternativeLine(lineName)
2026-05-15 10:54:36 -05:00
? strings.tanks.loadout.alternative
: romanize(stage.number)}
</Text>
2026-05-11 17:00:30 -05:00
2026-05-15 10:54:36 -05:00
{price && <Price className={styles.price} price={price} />}
2026-05-15 18:16:06 -05:00
</Button>
2026-05-15 10:54:36 -05:00
);
2026-05-11 17:00:30 -05:00
}