Files
blitzkit/packages/website/src/hooks/useCharacteristics.ts
T

195 lines
5.5 KiB
TypeScript
Raw Normal View History

2026-10-11 00:43:08 -05:00
import { degressiveStat, progressiveStat } from "@blitzkit/core";
2026-10-04 17:08:03 -05:00
import type { BlitzEffectScript } from "@blitzkit/core/src/types/blitzEffectScript";
2026-10-11 00:43:08 -05:00
import { CrewType } from "@blitzkit/protos";
2026-10-01 15:52:14 -05:00
import { api } from "../api/dynamic";
import { characteristics } from "../config/characteristics";
2026-10-04 17:08:03 -05:00
import { defaultEqualizer } from "../config/equalizer";
2026-10-01 15:52:14 -05:00
import { Tankopedia, type TankEnvironment } from "../stores/tankopedia";
import type { TankState } from "../tankopedia/tankState";
import type {
CharacteristicContext,
CharacteristicName,
ComputedCharacteristics,
} from "../types/characteristics";
2026-10-04 17:22:23 -05:00
import { duelSides, type DuelSide } from "./useEquipment";
2026-10-01 15:52:14 -05:00
const tanks = await api.tanks();
2026-10-04 17:08:03 -05:00
const scripts = await api.scripts();
const equipment = await api.equipment();
2026-10-01 15:52:14 -05:00
2026-10-04 17:22:23 -05:00
export function useCharacteristics() {
const protagonist = Tankopedia.use((state) => state.protagonist);
const antagonist = Tankopedia.use((state) => state.antagonist);
2026-10-01 15:52:14 -05:00
const environment = Tankopedia.use((state) => state.environment);
2026-10-04 17:22:23 -05:00
return computeCharacteristics({ protagonist, antagonist }, environment);
2026-10-01 15:52:14 -05:00
}
export function computeCharacteristics(
2026-10-04 17:22:23 -05:00
states: Record<DuelSide, TankState>,
2026-10-01 15:52:14 -05:00
environment: TankEnvironment,
) {
const computed: ComputedCharacteristics = new Map();
2026-10-04 17:22:23 -05:00
const tank = tanks.tanks[states.protagonist.tank];
const engine = tank.engines.find(
(engine) => engine.id === states.protagonist.engine,
)!;
const track = tank.tracks.find(
(track) => track.id === states.protagonist.track,
)!;
const turret = tank.turrets.find(
(turret) => turret.id === states.protagonist.turret,
)!;
const gun = turret.guns.find((gun) => gun.id === states.protagonist.gun)!;
const shell = gun.shells.find(
(shell) => shell.id === states.protagonist.shell,
)!;
2026-10-01 15:52:14 -05:00
2026-10-11 00:43:08 -05:00
const scriptsMap: Record<DuelSide, Map<string, BlitzEffectScript[]>> = {
2026-10-04 17:22:23 -05:00
protagonist: new Map(),
antagonist: new Map(),
};
2026-10-04 17:08:03 -05:00
2026-10-11 00:43:08 -05:00
function addScript(side: DuelSide, name: string, script: BlitzEffectScript) {
if (!scriptsMap[side].has(name)) {
scriptsMap[side].set(name, []);
}
scriptsMap[side].get(name)!.push(script);
}
2026-10-04 17:22:23 -05:00
for (const side of duelSides) {
2026-10-11 00:43:08 -05:00
const sideTank = tanks.tanks[states[side].tank];
const sideEquipmentPreset = equipment.presets[sideTank.equipment_preset];
2026-10-04 17:22:23 -05:00
for (const id of states[side].consumables) {
if (!(id in scripts.consumables)) continue;
2026-10-04 17:08:03 -05:00
2026-10-04 17:22:23 -05:00
const script = scripts.consumables[id];
2026-10-11 00:43:08 -05:00
addScript(side, script["#text"], script);
2026-10-04 17:22:23 -05:00
}
2026-10-04 17:08:03 -05:00
2026-10-04 17:53:57 -05:00
for (const id of states[side].provisions) {
if (!(id in scripts.provisions)) continue;
const script = scripts.provisions[id];
2026-10-11 00:43:08 -05:00
addScript(side, script["#text"], script);
2026-10-04 17:53:57 -05:00
}
2026-10-04 17:22:23 -05:00
for (const index in states[side].equipment) {
const choice = states[side].equipment[index];
2026-10-11 00:43:08 -05:00
const id = sideEquipmentPreset.slots[index].options[choice];
2026-10-04 17:08:03 -05:00
2026-10-04 17:22:23 -05:00
if (!(id in scripts.equipment)) continue;
2026-10-04 17:08:03 -05:00
2026-10-04 17:22:23 -05:00
const script = scripts.equipment[id];
2026-10-11 00:43:08 -05:00
addScript(side, script["#text"], script);
2026-10-04 17:22:23 -05:00
}
2026-10-04 17:08:03 -05:00
}
2026-10-11 00:43:08 -05:00
const protagonist = states.protagonist;
const protagonistEquipmentPreset = equipment.presets[tank.equipment_preset];
const hasImprovedVentilation = Object.entries(protagonist.equipment).some(
([index, choice]) =>
protagonistEquipmentPreset.slots[Number(index)]?.options[choice] === 102,
);
const provisionCrewBonus =
protagonist.provisions.reduce(
(total, id) =>
total +
(scripts.provisions[id]?.bonusValues?.crewLevelIncrease ?? 0) / 100,
0,
) + (hasImprovedVentilation ? 0.08 : 0);
const commanderMastery = 1 + provisionCrewBonus;
const crewMastery = {
commander: commanderMastery,
loader:
commanderMastery *
(tank.crew.some(({ type }) => type === CrewType.CREW_TYPE_LOADER)
? 1.1
: 1.05),
gunner:
commanderMastery *
(tank.crew.some(({ type }) => type === CrewType.CREW_TYPE_GUNNER)
? 1.1
: 1.05),
driver:
commanderMastery *
(tank.crew.some(({ type }) => type === CrewType.CREW_TYPE_DRIVER)
? 1.1
: 1.05),
};
function progressive(role: keyof typeof crewMastery) {
return progressiveStat(crewMastery[role]);
}
function degressive(role: keyof typeof crewMastery) {
return degressiveStat(crewMastery[role]);
}
2026-10-01 15:52:14 -05:00
function characteristic(name: CharacteristicName) {
if (!computed.has(name)) {
throw new Error(`Characteristic ${name} not computed yet`);
}
return computed.get(name)!;
}
2026-10-11 00:37:39 -05:00
// console.log(scriptsMap);
2026-10-04 17:08:03 -05:00
2026-10-04 17:22:23 -05:00
function script(
side: DuelSide,
name: string,
callback: (effect: BlitzEffectScript) => void,
) {
if (!scriptsMap[side].has(name)) return;
2026-10-11 00:43:08 -05:00
const scripts = scriptsMap[side].get(name)!;
for (const script of scripts) {
callback(script);
}
2026-10-04 17:08:03 -05:00
}
2026-10-01 15:52:14 -05:00
const context = {
2026-10-04 17:22:23 -05:00
state: states.protagonist,
2026-10-01 15:52:14 -05:00
environment,
2026-10-04 17:08:03 -05:00
equalizer: tank.equalizer ?? defaultEqualizer,
2026-10-05 17:45:56 -05:00
progressive,
degressive,
2026-10-01 15:52:14 -05:00
tank,
engine,
track,
turret,
gun,
shell,
characteristic,
2026-10-04 17:08:03 -05:00
script,
2026-10-01 15:52:14 -05:00
} satisfies CharacteristicContext;
for (const name in characteristics) {
const characteristic = characteristics[name as CharacteristicName];
let shouldRender = true;
if ("should_render" in characteristic) {
shouldRender = characteristic.should_render(context);
}
if (!shouldRender) continue;
2026-10-11 00:43:08 -05:00
if (!("compute" in characteristic)) continue;
2026-10-01 15:52:14 -05:00
const value = characteristic.compute(context);
computed.set(name as CharacteristicName, value);
}
return computed;
}