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

182 lines
6.1 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 { 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 { createLocalizedCommand } from "../core/discord/createLocalizedCommand";
import { resolvePeriodFromButton } from "../core/discord/resolvePeriodFromButton";
2026-02-21 22:26:29 -06:00
import { type ResolvedPeriod } from "../core/discord/resolvePeriodFromCommand";
2025-10-13 23:20:26 -05:00
import { resolvePlayerFromButton } from "../core/discord/resolvePlayerFromButton";
2026-02-21 22:26:29 -06:00
import { type ResolvedPlayer } from "../core/discord/resolvePlayerFromCommand";
import { translator } from "../core/localization/translator";
2025-10-13 23:20:26 -05:00
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,
2026-02-21 22:26:29 -06: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,
2026-02-21 22:26:29 -06: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) =>
2026-02-21 22:26:29 -06: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)
2026-02-21 22:26:29 -06:00
.addStringOption(addUsernameChoices),
2024-03-06 15:34:10 -06:00
);
},
},
]),
2023-08-04 02:13:21 +00:00
async handler(interaction) {
2026-02-21 22:26:29 -06:00
const { strings } = translator(interaction.locale);
return strings.bot.commands.dead;
2023-08-04 02:13:21 +00:00
2026-02-21 22:26:29 -06:00
// const commandGroup =
// interaction.options.getSubcommandGroup() as EvolutionStatType;
// const player = await resolvePlayerFromCommand(interaction);
// const period = resolvePeriodFromCommand(player.region, interaction);
// const tankIdRaw = interaction.options.getString("tank")!;
// const tankId =
// commandGroup === "tank"
// ? await resolveTankId(tankIdRaw, interaction.locale)
// : null;
// const path = commandToURL(interaction, {
// ...player,
// ...getCustomPeriodParams(interaction),
// tankId,
// });
// return [
// await render(
// player,
// period,
// commandGroup,
// tankId,
// interaction.locale
// ),
// buttonRefresh(interaction, path),
// await getBlitzStarsLinkButton(
// player.region,
// player.id,
// 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,
2026-02-21 22:26:29 -06:00
interaction.locale,
2023-08-04 02:13:21 +00:00
);
},
});
2026-02-21 22:26:29 -06:00
},
2023-08-04 02:13:21 +00:00
);