Files
blitzkit/packages/website/src/components/Embeds/breakdown.tsx
T

167 lines
4.6 KiB
TypeScript
Raw Normal View History

2024-10-28 10:30:53 -05:00
import {
2024-11-08 10:32:44 -06:00
calculateCompositeStats,
compositeStats,
2024-10-28 10:30:53 -05:00
compositeStatsKeys,
deltaTankStats,
getTankStats,
idToRegion,
2024-11-08 10:32:44 -06:00
sumCompositeStats,
2024-10-28 10:30:53 -05:00
type IndividualTankStats,
2025-09-01 23:00:44 -05:00
} from "@blitzkit/core";
import { STRINGS } from "@blitzkit/i18n";
import { ReloadIcon } from "@radix-ui/react-icons";
import { ContextMenu } from "@radix-ui/themes";
import { useEffect, useMemo, useState } from "react";
import { breakdownConfig } from "../../constants/embeds";
import { awaitableAverageDefinitions } from "../../core/awaitables/averageDefinitions";
import { awaitableTankDefinitions } from "../../core/awaitables/tankDefinitions";
import { EmbedBreakdown } from "../../stores/embedBreakdown";
import { useEmbedStateCurry } from "../../stores/embedState/utilities";
import { BreakdownEmbedCard, BreakdownEmbedWrapper } from "../TanksEmbed";
2024-08-08 14:18:22 -05:00
2024-09-08 21:52:12 -05:00
export const compositeStatsKeysOptions = compositeStatsKeys.map((value) => ({
2024-08-10 13:29:10 -05:00
value,
2025-01-16 14:10:28 -06:00
locales: Object.entries(STRINGS).reduce((accumulator, [locale, strings]) => {
return {
...accumulator,
[locale]: strings.common.composite_stats[value],
};
}, {}),
2024-08-10 13:29:10 -05:00
}));
2024-11-29 13:20:27 -06:00
const [tankDefinitions, averageDefinitions] = await Promise.all([
awaitableTankDefinitions,
awaitableAverageDefinitions,
]);
2024-11-08 10:32:44 -06:00
export const fakeCompositeStats = compositeStats(
{
battle_life_time: 5 * 50 * 60,
battles: 50,
capture_points: 5,
damage_dealt: 3201 * 50,
damage_received: 1093 * 50,
dropped_capture_points: 40,
frags: 99,
frags8p: 0,
hits: 50 * 13,
losses: 20,
max_frags: 7,
max_xp: 1203,
shots: 50 * 14,
spotted: 50 * 1.5,
survived_battles: 32,
win_and_survived: 28,
wins: 30,
xp: 1230 * 50,
},
2025-09-01 23:00:44 -05:00
averageDefinitions.averages[7297].mu
2024-11-08 10:32:44 -06:00
);
2024-10-05 23:20:04 -05:00
2024-08-08 14:18:22 -05:00
export function BreakdownPreview() {
2024-10-28 10:31:49 -05:00
const tanks = Object.values(tankDefinitions.tanks);
2024-10-28 10:30:53 -05:00
const { useEmbedState } = useEmbedStateCurry<typeof breakdownConfig>();
2024-08-08 14:18:22 -05:00
return (
<BreakdownEmbedWrapper>
2025-09-01 23:00:44 -05:00
{useEmbedState("showTotal") && (
2024-11-08 10:32:44 -06:00
<BreakdownEmbedCard composite={fakeCompositeStats} tank={null} />
)}
2024-08-08 14:18:22 -05:00
2025-09-01 23:00:44 -05:00
{tanks.slice(0, useEmbedState("listMaxTanks")).map((tank) => (
2024-11-08 10:32:44 -06:00
<BreakdownEmbedCard
composite={fakeCompositeStats}
key={tank.id}
tank={tank}
/>
2024-08-08 14:18:22 -05:00
))}
</BreakdownEmbedWrapper>
);
}
2024-10-27 22:29:28 -05:00
export function BreakdownRenderer() {
2024-10-28 10:30:53 -05:00
const { useEmbedState } = useEmbedStateCurry<typeof breakdownConfig>();
const params = new URLSearchParams(window.location.search);
2025-09-01 23:00:44 -05:00
const id = Number(params.get("id"));
const tankStatsA = EmbedBreakdown.use((state) => state[id] ?? []);
2024-10-28 10:30:53 -05:00
const [tankStatsB, setTankStatsB] = useState<IndividualTankStats[]>([]);
const diff = useMemo(
() =>
deltaTankStats(tankStatsA, tankStatsB).sort(
2025-09-01 23:00:44 -05:00
(a, b) => b.last_battle_time - a.last_battle_time
2024-10-28 10:30:53 -05:00
),
2025-09-01 23:00:44 -05:00
[tankStatsB]
2024-10-28 10:30:53 -05:00
);
2024-11-08 10:32:44 -06:00
const compositeStats = diff
.map((diff) => {
const average = averageDefinitions.averages[diff.tank_id];
if (average === undefined) return null;
const compositeStats = calculateCompositeStats(
{ battle_life_time: diff.battle_life_time, ...diff.all },
2025-09-01 23:00:44 -05:00
average.mu
2024-11-08 10:32:44 -06:00
);
return { id: diff.tank_id, compositeStats };
})
.filter((stats) => stats !== null);
const sum = sumCompositeStats(
2025-09-01 23:00:44 -05:00
compositeStats.map(({ compositeStats }) => compositeStats)
2024-11-08 10:32:44 -06:00
);
2024-10-28 10:30:53 -05:00
useEffect(() => {
const interval = setInterval(async () => {
const newB = await getTankStats(idToRegion(id), id);
if (newB === null) return;
setTankStatsB(newB);
}, 1000);
return () => clearInterval(interval);
}, []);
2024-10-27 22:29:28 -05:00
2024-10-27 22:38:14 -05:00
return (
2024-11-04 10:36:43 -06:00
<ContextMenu.Root>
<ContextMenu.Trigger>
<BreakdownEmbedWrapper>
2025-09-01 23:00:44 -05:00
{useEmbedState("showTotal") && (
2024-11-08 10:32:44 -06:00
<BreakdownEmbedCard composite={sum} tank={null} />
)}
2024-10-27 22:38:14 -05:00
2024-11-08 10:32:44 -06:00
{compositeStats
2025-09-01 23:00:44 -05:00
.slice(0, useEmbedState("listMaxTanks"))
2024-11-08 10:32:44 -06:00
.map(({ id, compositeStats }) => {
const tank = tankDefinitions.tanks[id];
2024-10-28 10:30:53 -05:00
2024-11-08 10:32:44 -06:00
return (
<BreakdownEmbedCard
key={tank.id}
tank={tank}
composite={compositeStats}
/>
);
})}
2024-11-04 10:36:43 -06:00
</BreakdownEmbedWrapper>
</ContextMenu.Trigger>
<ContextMenu.Content>
<ContextMenu.Item
color="red"
onClick={async () => {
const newA = await getTankStats(idToRegion(id), id);
if (newA === null) return;
2025-09-01 23:00:44 -05:00
EmbedBreakdown.mutate((draft) => {
2024-11-04 10:36:43 -06:00
draft[id] = newA;
});
}}
>
<ReloadIcon /> Reset
</ContextMenu.Item>
</ContextMenu.Content>
</ContextMenu.Root>
2024-10-27 22:38:14 -05:00
);
2024-10-27 22:29:28 -05:00
}