Files
blitzkit/src/commands/evolution.tsx
T

195 lines
6.3 KiB
TypeScript
Raw Normal View History

2023-10-12 11:56:11 -05:00
import {
SlashCommandBuilder,
SlashCommandSubcommandGroupBuilder,
} from 'discord.js';
import * as Graph from '../components/Graph';
import NoData, { NoDataType } from '../components/NoData';
import TitleBar from '../components/TitleBar';
import Wrapper from '../components/Wrapper';
2023-10-14 11:06:03 -05:00
import { getAccountInfo } from '../core/blitz/getAccountInfo';
import { getClanAccountInfo } from '../core/blitz/getClanAccountInfo';
2023-10-14 11:25:42 -05:00
import resolveTankId from '../core/blitz/resolveTankId';
2023-10-14 11:06:03 -05:00
import { emblemIdToURL } from '../core/blitzkrieg/emblemIdToURL';
2023-10-14 11:25:42 -05:00
import getTankHistories from '../core/blitzkrieg/getTankHistories';
import { getBlitzStarsLinkButton } from '../core/blitzstars/getBlitzStarsLinkButton';
import getPlayerHistories from '../core/blitzstars/getPlayerHistories';
2023-10-14 11:32:02 -05:00
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';
2023-10-14 11:36:10 -05:00
import buttonPrimary from '../core/discord/buttonPrimary';
2023-10-14 11:32:02 -05:00
import commandToURL from '../core/discord/commandToURL';
import { getCustomPeriodParams } from '../core/discord/getCustomPeriodParams';
2023-10-14 11:36:10 -05:00
import resolvePeriodFromButton from '../core/discord/resolvePeriodFromButton';
import resolvePeriodFromCommand, {
ResolvedPeriod,
} from '../core/discord/resolvePeriodFromCommand';
import resolvePlayerFromButton from '../core/discord/resolvePlayerFromButton';
import resolvePlayerFromCommand, {
ResolvedPlayer,
} from '../core/discord/resolvePlayerFromCommand';
2023-08-04 02:13:21 +00:00
import { CommandRegistryRaw } 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,
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;
2023-10-14 10:58:48 -05:00
const logo = clan ? emblemIdToURL(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,
})
: getTankHistories(region, id, {
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 (
<Wrapper>
2023-10-12 13:00:33 -05:00
<TitleBar name={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,
}}
xMinLabel={new Date(minTime * 1000).toDateString()}
xMaxLabel={new Date(maxTime * 1000).toDateString()}
>
{Graph.Line({
color: Graph.LineColor.Red,
plot: plot,
minY,
maxY,
})}
</Graph.Root>
)}
{plot.length === 0 && <NoData type={NoDataType.BattlesInPeriod} />}
</Wrapper>
);
}
2023-06-21 01:52:26 +00:00
2023-08-04 02:13:21 +00:00
export const evolutionCommand = new Promise<CommandRegistryRaw>(
async (resolve) => {
2023-10-12 11:56:11 -05:00
let playerGroup: SlashCommandSubcommandGroupBuilder;
let tankGroup: SlashCommandSubcommandGroupBuilder;
const command = new SlashCommandBuilder()
.setName('evolution')
.setDescription('Evolution of statistics')
.addSubcommandGroup((option) => {
playerGroup = option
.setName('player')
.setDescription('Player statistics');
return playerGroup;
})
.addSubcommandGroup((option) => {
tankGroup = option.setName('tank').setDescription('Tank statistics');
return tankGroup;
});
await Promise.all([
addPeriodSubCommands(playerGroup!, (option) =>
option.addStringOption(addUsernameChoices),
),
addPeriodSubCommands(tankGroup!, (option) =>
2023-10-12 12:07:45 -05:00
option
.addStringOption(addTankChoices)
.addStringOption(addUsernameChoices),
2023-10-12 11:56:11 -05:00
),
]);
2023-08-04 02:13:21 +00:00
resolve({
inProduction: true,
inPublic: true,
command,
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 =
commandGroup === 'tank' ? await resolveTankId(tankIdRaw) : 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,
});
2023-10-12 12:07:45 -05:00
getBlitzStarsLinkButton;
2023-08-04 02:13:21 +00:00
return [
2023-10-12 12:07:45 -05:00
await render(player, period, commandGroup, tankId),
2023-10-14 11:36:10 -05:00
buttonPrimary(path, 'Refresh'),
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,
2023-08-04 02:13:21 +00:00
);
},
});
2023-06-28 18:45:14 +00:00
},
2023-08-04 02:13:21 +00:00
);