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';
|
|
|
|
|
import { getAccountInfo } from '../core/blitz/getAccountInfo';
|
|
|
|
|
import { getClanLogo } from '../core/blitz/getClanLogo';
|
|
|
|
|
import { getPlayerClanInfo } from '../core/blitz/getPlayerClanInfo';
|
2023-07-11 19:54:57 +00:00
|
|
|
import resolveTankId from '../core/blitz/resolveTankId';
|
2023-10-12 12:07:45 -05:00
|
|
|
import { getBlitzStarsLinkButton } from '../core/blitzstars/getBlitzStarsLinkButton';
|
2023-10-11 20:18:51 -05:00
|
|
|
import getPlayerHistories from '../core/blitzstars/getPlayerHistories';
|
|
|
|
|
import getTankHistories from '../core/blitzstars/getTankHistories';
|
2023-10-12 11:56:11 -05:00
|
|
|
import addPeriodSubCommands from '../core/discord/addPeriodSubCommands';
|
2023-10-12 12:07:45 -05:00
|
|
|
import addTankChoices from '../core/discord/addTankChoices';
|
2023-10-12 11:56:11 -05:00
|
|
|
import addUsernameChoices from '../core/discord/addUsernameChoices';
|
2023-07-11 19:54:57 +00:00
|
|
|
import autocompleteTanks from '../core/discord/autocompleteTanks';
|
|
|
|
|
import autocompleteUsername from '../core/discord/autocompleteUsername';
|
2023-10-12 12:07:45 -05:00
|
|
|
import { getCustomPeriodParams } from '../core/discord/getCustomPeriodParams';
|
2023-07-11 19:54:57 +00:00
|
|
|
import interactionToURL from '../core/discord/interactionToURL';
|
|
|
|
|
import primaryButton from '../core/discord/primaryButton';
|
|
|
|
|
import resolvePeriodFromButton from '../core/discord/resolvePeriodFromButton';
|
2023-10-11 20:18:51 -05:00
|
|
|
import resolvePeriodFromCommand, {
|
|
|
|
|
ResolvedPeriod,
|
|
|
|
|
} from '../core/discord/resolvePeriodFromCommand';
|
2023-07-11 19:54:57 +00:00
|
|
|
import resolvePlayerFromButton from '../core/discord/resolvePlayerFromButton';
|
2023-10-11 20:18:51 -05:00
|
|
|
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-12 11:56:11 -05:00
|
|
|
const clan = (await getPlayerClanInfo(region, id))[id]?.clan;
|
|
|
|
|
const logo = clan ? getClanLogo(clan.emblem_set_id) : undefined;
|
|
|
|
|
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>
|
|
|
|
|
<TitleBar
|
|
|
|
|
name={accountInfo[id].nickname}
|
2023-10-12 11:56:11 -05:00
|
|
|
image={logo}
|
2023-10-11 20:18:51 -05:00
|
|
|
description={name}
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
{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-12 12:07:45 -05:00
|
|
|
const { nickname } = (await getAccountInfo(player.region, player.id))[
|
|
|
|
|
player.id
|
|
|
|
|
];
|
2023-08-04 02:13:21 +00:00
|
|
|
const path = interactionToURL(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-08-04 02:13:21 +00:00
|
|
|
primaryButton(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
|
|
|
);
|