Files
blitzkit/packages/website-ue/src/core/tankopedia/computeCharacteristics.ts
T

89 lines
2.3 KiB
TypeScript
Raw Normal View History

2025-12-18 16:29:13 -06:00
import {
2025-12-18 15:30:18 -06:00
ShellUpgrageSingleChange_AttributeName,
TankAttributeChange_AttributeName,
2025-12-18 16:29:13 -06:00
type StageParameters,
2025-12-18 15:30:18 -06:00
} from "@protos/blitz_static_tank_upgrade_single_stage";
import {
characteristics,
type CharacteristicName,
type CharacteristicOutput,
} from "./characteristics";
2025-12-18 16:12:01 -06:00
import type { TankState } from "./tankState";
2025-12-18 15:30:18 -06:00
2025-12-18 16:12:01 -06:00
export function computeCharacteristics(
2025-12-18 15:30:18 -06:00
parameters: StageParameters,
2025-12-18 16:12:01 -06:00
state: TankState
2025-12-18 15:30:18 -06:00
) {
const computed: Partial<Record<CharacteristicName, CharacteristicOutput>> =
{};
function characteristic(name: CharacteristicName) {
if (name in computed) return computed[name] as CharacteristicOutput;
throw new Error(`${name} not computed yet`);
}
2025-12-18 21:29:10 -06:00
function attributeSafe(name: TankAttributeChange_AttributeName) {
2025-12-18 15:30:18 -06:00
const attribute = parameters.attributes.find(
(attribute) => attribute.attribute_name === name
);
2025-12-18 21:29:10 -06:00
return attribute ? attribute.value : null;
}
function attribute(name: TankAttributeChange_AttributeName) {
const attribute = attributeSafe(name);
if (attribute === null) {
2025-12-18 16:29:13 -06:00
throw new Error(
`Attribute ${TankAttributeChange_AttributeName[name]} (${name}) not found`
);
}
2025-12-18 15:30:18 -06:00
2025-12-18 21:29:10 -06:00
return attribute;
2025-12-18 15:30:18 -06:00
}
2025-12-18 16:45:02 -06:00
function shellSafe(name: ShellUpgrageSingleChange_AttributeName) {
2025-12-18 15:30:18 -06:00
const shell = parameters.shells_upgrades.find(
2025-12-18 16:12:01 -06:00
(shell) => shell.shell_id === state.shell
2025-12-18 15:30:18 -06:00
);
2025-12-18 16:29:13 -06:00
if (!shell) {
throw new Error(
`Shell ${ShellUpgrageSingleChange_AttributeName[name]} (${name}) not found`
);
}
2025-12-18 15:30:18 -06:00
const attribute = shell.changes.find(
(change) => change.attribute_name === name
);
2025-12-18 16:45:02 -06:00
return attribute ? attribute.value : null;
}
function shell(name: ShellUpgrageSingleChange_AttributeName) {
const attribute = shellSafe(name);
if (attribute === null) {
2025-12-18 16:29:13 -06:00
throw new Error(
`Shell attribute ${ShellUpgrageSingleChange_AttributeName[name]} (${name}) not found`
);
}
2025-12-18 15:30:18 -06:00
2025-12-18 16:45:02 -06:00
return attribute;
2025-12-18 15:30:18 -06:00
}
for (const _name in characteristics) {
const name = _name as CharacteristicName;
2025-12-18 16:45:02 -06:00
const value = characteristics[name]({
characteristic,
attribute,
2025-12-18 21:29:10 -06:00
attributeSafe,
2025-12-18 16:45:02 -06:00
shell,
shellSafe,
2025-12-20 10:15:23 -06:00
state,
2025-12-18 16:45:02 -06:00
});
2025-12-18 16:12:01 -06:00
computed[name] = value;
2025-12-18 15:30:18 -06:00
}
2025-12-18 16:12:01 -06:00
return computed as Record<CharacteristicName, CharacteristicOutput>;
2025-12-18 15:30:18 -06:00
}