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

106 lines
2.6 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,
} from "@protos/blitz_static_tank_upgrade_single_stage";
2025-12-26 20:07:22 -06:00
import type { Tank } from "../../protos/tank";
2025-12-20 10:26:25 -06:00
import { aggregateStageParameters } from "./aggregateStageParameters";
2025-12-18 15:30:18 -06:00
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-26 14:59:09 -06:00
export type ComputedCharacteristics = Record<
CharacteristicName,
CharacteristicOutput
>;
2025-12-18 16:12:01 -06:00
export function computeCharacteristics(
2025-12-26 14:14:29 -06:00
id: string,
2025-12-26 20:07:22 -06:00
tank: Tank,
2025-12-18 16:12:01 -06:00
state: TankState
2025-12-18 15:30:18 -06:00
) {
2025-12-20 10:26:25 -06:00
const parameters = aggregateStageParameters(
2025-12-26 20:07:22 -06:00
tank.tank!.base_stats!,
tank.tank!.upgrade_stages.slice(0, state.stage)
2025-12-20 10:26:25 -06:00
);
2025-12-26 14:59:09 -06:00
const computed: Partial<ComputedCharacteristics> = {};
2025-12-18 15:30:18 -06:00
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-20 10:26:25 -06:00
tank,
parameters,
2025-12-26 14:14:29 -06:00
id,
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
2025-12-20 10:26:25 -06:00
return {
2025-12-26 14:59:09 -06:00
characteristics: computed as ComputedCharacteristics,
2025-12-20 10:26:25 -06:00
parameters,
};
2025-12-18 15:30:18 -06:00
}