Files
blitzkit/packages/bot/src/commands/evolution.tsx
T

189 lines
6.3 KiB
TypeScript
Raw Normal View History

2025-10-13 23:20:26 -05:00
import { emblemURL, getAccountInfo, getClanAccountInfo } from "@blitzkit/core";
import { Locale, SlashCommandSubcommandGroupBuilder } from "discord.js";
import { CommandWrapper } from "../components/CommandWrapper";
import * as Graph from "../components/Graph";
import { LineColor } from "../components/Graph/components/Line/constants";
import { NoData } from "../components/NoData";
import { TitleBar } from "../components/TitleBar";
import { resolveTankId } from "../core/blitz/resolveTankId";
import { getBlitzStarsLinkButton } from "../core/blitzstars/getBlitzStarsLinkButton";
import { getPlayerHistories } from "../core/blitzstars/getPlayerHistories";
import { getTankHistories } from "../core/blitzstars/getTankHistories";
import { addPeriodSubCommands } from "../core/discord/addPeriodSubCommands";
import { addTankChoices } from "../core/discord/addTankChoices";
import { addUsernameChoices } from "../core/discord/addUsernameChoices";
import { autocompleteTanks } from "../core/discord/autocompleteTanks";
import { autocompleteUsername } from "../core/discord/autocompleteUsername";
import { buttonRefresh } from "../core/discord/buttonRefresh";
import { commandToURL } from "../core/discord/commandToURL";
import { createLocalizedCommand } from "../core/discord/createLocalizedCommand";
import { getCustomPeriodParams } from "../core/discord/getCustomPeriodParams";
import { resolvePeriodFromButton } from "../core/discord/resolvePeriodFromButton";
2024-09-08 10:11:24 -05:00
import {
2025-10-13 23:20:26 -05:00
type ResolvedPeriod,
2024-09-08 10:11:24 -05:00
resolvePeriodFromCommand,
2025-10-13 23:20:26 -05:00
} from "../core/discord/resolvePeriodFromCommand";
import { resolvePlayerFromButton } from "../core/discord/resolvePlayerFromButton";
2024-09-08 10:11:24 -05:00
import {
2025-10-13 23:20:26 -05:00
type ResolvedPlayer,
2024-09-08 10:11:24 -05:00
resolvePlayerFromCommand,
2025-10-13 23:20:26 -05:00
} from "../core/discord/resolvePlayerFromCommand";
import type { CommandRegistry } from "../events/interactionCreate";
2023-10-12 11:56:11 -05:00
2025-10-13 23:20:26 -05:00
type EvolutionStatType = "player" | "tank";
2023-10-11 20:18:51 -05:00
async function render(
{ region, id }: ResolvedPlayer,
{ start, end, name }: ResolvedPeriod,
2025-10-13 23:20:26 -05:00
type: "player" | "tank",
2023-10-12 11:56:11 -05:00
tankId: number | null,
2025-10-13 23:20:26 -05:00
locale: Locale
2023-10-11 20:18:51 -05:00
) {
2023-10-12 12:07:45 -05:00
const accountInfo = await getAccountInfo(region, id);
2025-10-13 23:20:26 -05:00
const clan = (await getClanAccountInfo(region, id, ["clan"]))?.clan;
2024-03-24 22:06:48 -05:00
const logo = clan ? emblemURL(clan.emblem_set_id) : undefined;
2025-10-13 23:20:26 -05:00
const histories = await (type === "player"
2023-10-12 11:56:11 -05:00
? getPlayerHistories(region, id, {
start,
end,
includeLatestHistories: true,
includePreviousHistories: true,
})
2024-03-09 11:48:35 -06:00
: getTankHistories(region, id, locale, {
2023-10-12 11:56:11 -05:00
tankId: tankId!,
start,
end,
includeLatestHistories: true,
includePreviousHistories: true,
}));
2023-10-11 20:18:51 -05:00
const plot = histories.map(
(history) =>
[
history.all.battles,
(history.all.wins / history.all.battles) * 100,
2025-10-13 23:20:26 -05:00
] as Graph.PlotItem
2023-10-11 20:18:51 -05:00
);
const times = histories.map((history) => history.last_battle_time);
const minTime = Math.min(...times);
const maxTime = Math.max(...times);
const ys = plot.map(([, y]) => y);
const xs = plot.map(([x]) => x);
const maxY = Math.max(...ys);
const minY = Math.min(...ys);
const maxX = Math.max(...xs);
const minX = Math.min(...xs);
return (
2024-03-06 09:52:09 -06:00
<CommandWrapper>
<TitleBar title={accountInfo.nickname} image={logo} description={name} />
2023-10-11 20:18:51 -05:00
{plot.length > 0 && (
<Graph.Root
verticalMargin={{
min: minY,
max: maxY,
2025-10-13 23:20:26 -05:00
suffix: "%",
2023-10-11 20:18:51 -05:00
precision: 2,
}}
horizontalMargin={{
min: minX,
max: maxX,
precision: 0,
}}
2024-03-07 18:27:05 -06:00
xMinLabel={new Date(minTime * 1000).toLocaleDateString(locale)}
xMaxLabel={new Date(maxTime * 1000).toLocaleDateString(locale)}
2023-10-11 20:18:51 -05:00
>
{Graph.Line({
2024-03-12 23:36:56 -05:00
color: LineColor.Red,
2023-10-11 20:18:51 -05:00
plot: plot,
minY,
maxY,
})}
</Graph.Root>
)}
2024-03-06 16:26:36 -06:00
{plot.length === 0 && <NoData type="battles_in_period" locale={locale} />}
2024-03-06 09:52:09 -06:00
</CommandWrapper>
2023-10-11 20:18:51 -05:00
);
}
2023-06-21 01:52:26 +00:00
2024-03-06 15:34:10 -06:00
export const evolutionCommand = new Promise<CommandRegistry>(
2023-08-04 02:13:21 +00:00
async (resolve) => {
resolve({
2025-10-13 23:20:26 -05:00
command: createLocalizedCommand("evolution", [
2024-03-06 15:34:10 -06:00
{
2025-10-13 23:20:26 -05:00
group: "player",
2024-03-06 15:34:10 -06:00
modify(option: SlashCommandSubcommandGroupBuilder) {
addPeriodSubCommands(option, (option) =>
2025-10-13 23:20:26 -05:00
option.addStringOption(addUsernameChoices)
2024-03-06 15:34:10 -06:00
);
},
},
{
2025-10-13 23:20:26 -05:00
group: "tank",
2024-03-06 15:34:10 -06:00
modify(option: SlashCommandSubcommandGroupBuilder) {
addPeriodSubCommands(option, (option) =>
option
.addStringOption(addTankChoices)
2025-10-13 23:20:26 -05:00
.addStringOption(addUsernameChoices)
2024-03-06 15:34:10 -06:00
);
},
},
]),
2023-08-04 02:13:21 +00:00
async handler(interaction) {
2023-10-12 11:56:11 -05:00
const commandGroup =
interaction.options.getSubcommandGroup() as EvolutionStatType;
2023-10-12 12:07:45 -05:00
const player = await resolvePlayerFromCommand(interaction);
const period = resolvePeriodFromCommand(player.region, interaction);
2025-10-13 23:20:26 -05:00
const tankIdRaw = interaction.options.getString("tank")!;
2023-08-04 02:13:21 +00:00
const tankId =
2025-10-13 23:20:26 -05:00
commandGroup === "tank"
2024-03-09 11:48:35 -06:00
? await resolveTankId(tankIdRaw, interaction.locale)
: null;
2023-10-14 11:32:02 -05:00
const path = commandToURL(interaction, {
2023-10-12 12:07:45 -05:00
...player,
...getCustomPeriodParams(interaction),
2023-08-04 02:13:21 +00:00
tankId,
});
return [
2024-03-06 16:26:36 -06:00
await render(
player,
period,
commandGroup,
tankId,
2025-10-13 23:20:26 -05:00
interaction.locale
2024-03-06 16:26:36 -06:00
),
buttonRefresh(interaction, path),
2023-10-12 12:07:45 -05:00
await getBlitzStarsLinkButton(
player.region,
player.id,
2025-10-13 23:20:26 -05:00
tankId ?? undefined
2023-08-04 02:13:21 +00:00
),
];
},
autocomplete: (interaction) => {
autocompleteUsername(interaction);
autocompleteTanks(interaction);
},
async button(interaction) {
2023-10-11 18:26:35 -05:00
const url = new URL(`https://example.com/${interaction.customId}`);
2025-10-13 23:20:26 -05:00
const path = url.pathname.split("/");
2023-10-12 12:07:45 -05:00
const commandGroup = path[2] as EvolutionStatType;
2023-08-04 02:13:21 +00:00
const player = await resolvePlayerFromButton(interaction);
const period = resolvePeriodFromButton(player.region, interaction);
2023-10-11 20:18:51 -05:00
return await render(
2023-08-04 02:13:21 +00:00
player,
2023-10-12 11:56:11 -05:00
period,
commandGroup,
2025-10-13 23:20:26 -05:00
parseInt(url.searchParams.get("tankId") ?? "0") || null,
interaction.locale
2023-08-04 02:13:21 +00:00
);
},
});
2025-10-13 23:20:26 -05:00
}
2023-08-04 02:13:21 +00:00
);