mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-11-14 05:06:11 +01:00
55 lines
1.4 KiB
TypeScript
55 lines
1.4 KiB
TypeScript
import {
|
|
TankAttributeChange_AttributeName,
|
|
TankAttributeChange_Modifier,
|
|
type TankAttributeChange,
|
|
} from "@protos/blitz_static_tank_upgrade_single_stage";
|
|
import { times } from "lodash-es";
|
|
|
|
export type AggregateAttributes = Partial<
|
|
Record<TankAttributeChange_AttributeName, number>
|
|
>;
|
|
|
|
function patch(
|
|
changes: TankAttributeChange[],
|
|
attributes: AggregateAttributes
|
|
) {
|
|
for (const { attribute_name, value, modifier } of changes) {
|
|
switch (modifier) {
|
|
case TankAttributeChange_Modifier.MODIFIER_OVERRIDE:
|
|
attributes[attribute_name] = value;
|
|
break;
|
|
|
|
case TankAttributeChange_Modifier.MODIFIER_MULTIPLY: {
|
|
if (!(attribute_name in attributes)) {
|
|
throw new Error(
|
|
`Missing attribute ${TankAttributeChange_AttributeName[attribute_name]} to modify`
|
|
);
|
|
}
|
|
}
|
|
|
|
case TankAttributeChange_Modifier.MODIFIER_MULTIPLY: {
|
|
attributes[attribute_name]! *= value;
|
|
break;
|
|
}
|
|
|
|
default:
|
|
throw new Error(
|
|
`Unhandled modified ${TankAttributeChange_Modifier[modifier]}`
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
export function aggregateAttributes(
|
|
base: TankAttributeChange[],
|
|
stages: TankAttributeChange[][],
|
|
stage: number
|
|
) {
|
|
const attributes: AggregateAttributes = {};
|
|
|
|
patch(base, attributes);
|
|
times(stage, (index) => patch(stages[index], attributes));
|
|
|
|
return attributes;
|
|
}
|