feat: aggregateAttributes

This commit is contained in:
TrèsAbhi committed 2025-12-01 13:09:55 -06:00
1 parent ed4b72d85c
commit 122d65840e
2 files changed
+62 -46

No files matched your search

@@ -0,0 +1,51 @@
import {
TankAttributeChange_AttributeName,
TankAttributeChange_Modifier,
type TankAttributeChange,
} from "@protos/blitz_static_tank_upgrade_single_stage";
import { times } from "lodash-es";
function patch(
changes: TankAttributeChange[],
attributes: Partial<Record<TankAttributeChange_AttributeName, number>>
) {
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: Partial<Record<TankAttributeChange_AttributeName, number>> =
{};
patch(base, attributes);
times(stage, (index) => patch(stages[index], attributes));
return attributes;
}
@@ -1,8 +1,4 @@
import {
TankAttributeChange,
TankAttributeChange_AttributeName,
TankAttributeChange_Modifier,
} from "@protos/blitz_static_tank_upgrade_single_stage";
import { TankAttributeChange_AttributeName } from "@protos/blitz_static_tank_upgrade_single_stage";
import { times } from "lodash-es";
import { Suspense, useMemo } from "react";
import {
@@ -10,6 +6,7 @@ import {
characteristicsOrder,
} from "../../../constants/characteristicsOrder";
import { api } from "../../../core/api/dynamic";
import { aggregateAttributes } from "../../../core/blitz/aggregateAttributes";
import { useAwait } from "../../../hooks/useAwait";
import { Tankopedia } from "../../../stores/tankopedia";
@@ -26,47 +23,15 @@ function Content({ id }: { id: string }) {
const stage = Tankopedia.use((state) => state.stage);
const attributes = useMemo(() => {
const attributes: Partial<
Record<TankAttributeChange_AttributeName, number>
> = {};
function patch(changes: TankAttributeChange[]) {
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]}`
);
}
}
}
patch(tank.base_stats!.attributes);
times(stage, (index) => {
patch(tank.upgrade_stages[index].attributes);
});
return attributes;
}, [stage]);
const attributes = useMemo(
() =>
aggregateAttributes(
tank.base_stats!.attributes,
tank.upgrade_stages.map((stage) => stage.attributes),
stage
),
[stage]
);
return (
<>