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

189 lines
6.3 KiB
TypeScript
Raw Normal View History

2024-09-08 10:42:34 -05:00
import { emblemURL, getAccountInfo, getClanAccountInfo } from '@blitzkit/core';
2024-03-06 16:26:36 -06:00
import { Locale, SlashCommandSubcommandGroupBuilder } from 'discord.js';
2024-09-08 10:11:24 -05:00
import { CommandWrapper } from '../components/CommandWrapper';
2023-10-12 11:56:11 -05:00
import * as Graph from '../components/Graph';
2024-03-12 23:36:56 -05:00
import { LineColor } from '../components/Graph/components/Line/constants';
2024-09-08 10:11:24 -05:00
import { NoData } from '../components/NoData';
import { TitleBar } from '../components/TitleBar';
import { resolveTankId } from '../core/blitz/resolveTankId';
2024-09-07 23:00:56 -05:00
import { getBlitzStarsLinkButton } from '../core/blitzstars/getBlitzStarsLinkButton';
2024-09-08 10:11:24 -05:00
import { getPlayerHistories } from '../core/blitzstars/getPlayerHistories';
import { getTankHistories } from '../core/blitzstars/getTankHistories';
2024-03-05 10:51:14 -06:00
import { addPeriodSubCommands } from '../core/discord/addPeriodSubCommands';
2024-09-08 10:11:24 -05:00
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';
2024-09-08 10:11:24 -05:00
import { commandToURL } from '../core/discord/commandToURL';
2024-03-06 15:34:10 -06:00
import { createLocalizedCommand } from '../core/discord/createLocalizedCommand';
2023-10-14 11:32:02 -05:00
import { getCustomPeriodParams } from '../core/discord/getCustomPeriodParams';
2024-09-08 10:11:24 -05:00
import { resolvePeriodFromButton } from '../core/discord/resolvePeriodFromButton';
import {
2023-10-14 11:36:10 -05:00
ResolvedPeriod,
2024-09-08 10:11:24 -05:00
resolvePeriodFromCommand,
2023-10-14 11:36:10 -05:00
} from '../core/discord/resolvePeriodFromCommand';
2024-09-08 10:11:24 -05:00
import { resolvePlayerFromButton } from '../core/discord/resolvePlayerFromButton';
import {
2023-10-14 11:36:10 -05:00
ResolvedPlayer,
2024-09-08 10:11:24 -05:00
resolvePlayerFromCommand,
2023-10-14 11:36:10 -05:00
} from '../core/discord/resolvePlayerFromCommand';
2024-03-06 15:34:10 -06:00
import { CommandRegistry } from '../events/interactionCreate';
2023-10-12 11:56:11 -05:00
type EvolutionStatType = 'player' | 'tank';
2023-10-11 20:18:51 -05:00
async function render(
{ region, id }: ResolvedPlayer,
{ start, end, name }: ResolvedPeriod,
type: 'player' | 'tank',
2023-10-12 11:56:11 -05:00
tankId: number | null,
2024-03-06 16:26:36 -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);
2023-10-16 20:16:23 -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;
2023-10-12 11:56:11 -05:00
const histories = await (type === 'player'
? 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,
] as Graph.PlotItem,
);
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,
suffix: '%',
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({
2024-03-06 15:34:10 -06:00
command: createLocalizedCommand('evolution', [
{
group: 'player',
modify(option: SlashCommandSubcommandGroupBuilder) {
addPeriodSubCommands(option, (option) =>
option.addStringOption(addUsernameChoices),
);
},
},
{
group: 'tank',
modify(option: SlashCommandSubcommandGroupBuilder) {
addPeriodSubCommands(option, (option) =>
option
.addStringOption(addTankChoices)
.addStringOption(addUsernameChoices),
);
},
},
]),
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);
2023-08-04 02:13:21 +00:00
const tankIdRaw = interaction.options.getString('tank')!;
const tankId =
2024-03-09 11:48:35 -06:00
commandGroup === 'tank'
? 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,
interaction.locale,
),
buttonRefresh(interaction, path),
2023-10-12 12:07:45 -05:00
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}`);
2023-10-12 11:56:11 -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,
parseInt(url.searchParams.get('tankId') ?? '0') || null,
2024-03-06 16:26:36 -06:00
interaction.locale,
2023-08-04 02:13:21 +00:00
);
},
});
2023-06-28 18:45:14 +00:00
},
2023-08-04 02:13:21 +00:00
);