Files
blitzkit/src/commands/ownedTanks.tsx
T

128 lines
4.7 KiB
TypeScript
Raw Normal View History

2023-05-28 16:56:15 +00:00
import { SlashCommandBuilder } from 'discord.js';
2024-03-06 09:52:09 -06:00
import CommandWrapper from '../components/CommandWrapper';
2023-10-14 11:06:03 -05:00
import NoData, { NoDataType } from '../components/NoData';
import * as Tanks from '../components/Tanks';
import TitleBar from '../components/TitleBar';
2023-12-05 17:36:18 -06:00
import { encyclopediaInfo } from '../core/blitz/encyclopediaInfo';
2023-10-14 11:06:03 -05:00
import { getAccountInfo } from '../core/blitz/getAccountInfo';
import { getClanAccountInfo } from '../core/blitz/getClanAccountInfo';
import getTankStats from '../core/blitz/getTankStats';
2023-12-14 22:19:01 -06:00
import { emblemIdToURL } from '../core/blitzkrieg/emblemIdToURL';
2023-12-05 17:36:18 -06:00
import {
TIER_ROMAN_NUMERALS,
2023-12-14 22:19:01 -06:00
TankDefinition,
2023-12-05 17:36:18 -06:00
Tier,
tankDefinitions,
2023-12-14 22:19:01 -06:00
} from '../core/blitzkrieg/tankDefinitions';
2023-11-29 12:17:21 -06:00
import { tankIcon } from '../core/blitzkrieg/tankIcon';
2023-10-14 11:32:02 -05:00
import addTierChoices from '../core/discord/addTierChoices';
import addUsernameChoices from '../core/discord/addUsernameChoices';
import autocompleteUsername from '../core/discord/autocompleteUsername';
2023-10-14 11:36:10 -05:00
import resolvePlayerFromCommand from '../core/discord/resolvePlayerFromCommand';
2023-07-11 19:54:57 +00:00
import { CommandRegistry } from '../events/interactionCreate';
2023-04-25 01:39:43 +00:00
2023-07-05 01:15:18 +00:00
export const ownedTanksCommand: CommandRegistry = {
2023-04-26 00:02:10 +00:00
inProduction: true,
inPublic: true,
2023-04-25 01:39:43 +00:00
2023-04-26 00:02:10 +00:00
command: new SlashCommandBuilder()
2023-08-04 02:24:14 +00:00
.setName('owned-tanks')
2023-04-26 00:02:10 +00:00
.setDescription("Shows a player's owned tanks")
2023-08-23 00:14:41 +00:00
.addStringOption(addTierChoices)
2023-06-27 02:22:28 +00:00
.addStringOption(addUsernameChoices),
2023-04-25 01:39:43 +00:00
2023-06-27 01:43:30 +00:00
async handler(interaction) {
2023-06-16 02:07:01 +00:00
const tier = Number(interaction.options.getString('tier'));
2023-06-27 02:07:14 +00:00
const account = await resolvePlayerFromCommand(interaction);
const { id, region: server } = account;
2023-10-12 13:00:33 -05:00
const accountInfo = await getAccountInfo(server, id);
const tankStats = await getTankStats(server, id);
2023-08-09 00:19:50 +00:00
const filteredTanks = (
await Promise.all(
tankStats.map(async (tankData) => ({
2023-12-05 17:36:18 -06:00
tankDefinitions: (await tankDefinitions)[tankData.tank_id]!,
2023-11-29 12:17:21 -06:00
id: tankData.tank_id,
})),
)
2023-12-05 17:36:18 -06:00
).filter((tank) => tank.tankDefinitions?.tier === tier);
2023-10-16 20:16:23 -05:00
const clanAccountInfo = await getClanAccountInfo(server, id, ['clan']);
2023-12-14 22:19:01 -06:00
const groupedTanks: Record<string, TankDefinition[]> = {};
2023-08-09 00:19:50 +00:00
const nations: string[] = [];
filteredTanks.forEach((tank) => {
2023-12-05 17:36:18 -06:00
if (groupedTanks[tank.tankDefinitions.nation] === undefined) {
groupedTanks[tank.tankDefinitions.nation] = [tank.tankDefinitions];
nations.push(tank.tankDefinitions.nation);
2023-08-09 00:19:50 +00:00
} else {
2023-12-05 17:36:18 -06:00
groupedTanks[tank.tankDefinitions.nation].push(tank.tankDefinitions);
2023-08-09 00:19:50 +00:00
}
});
nations.sort();
2023-04-26 00:02:10 +00:00
2023-06-16 02:01:06 +00:00
return (
2024-03-06 09:52:09 -06:00
<CommandWrapper>
2023-06-05 18:47:00 +00:00
<TitleBar
2024-03-06 09:52:09 -06:00
title={accountInfo.nickname}
2023-06-05 18:47:00 +00:00
image={
2023-10-12 13:00:33 -05:00
clanAccountInfo?.clan
2023-10-14 10:58:48 -05:00
? emblemIdToURL(clanAccountInfo.clan.emblem_set_id)
2023-06-05 18:47:00 +00:00
: undefined
}
2023-10-11 19:04:58 -05:00
description={`Tier ${TIER_ROMAN_NUMERALS[tier as Tier]} tanks`}
2023-06-05 18:47:00 +00:00
/>
2023-08-09 00:19:50 +00:00
{filteredTanks.length === 0 && <NoData type={NoDataType.TanksFound} />}
2023-06-06 16:53:13 +00:00
2023-08-09 00:19:50 +00:00
{filteredTanks.length > 0 &&
(await Promise.all(
nations.map(async (nation) => {
const tanks = groupedTanks[nation];
const leftColumnSize = Math.ceil(tanks.length / 2);
const leftColumn = tanks.slice(0, leftColumnSize);
const rightColumn = tanks.slice(leftColumnSize);
return (
<Tanks.Root>
<Tanks.Title>
2023-12-05 17:36:18 -06:00
{(await encyclopediaInfo).vehicle_nations[nation]}
2023-08-09 00:19:50 +00:00
</Tanks.Title>
<Tanks.Row>
<Tanks.Column>
{await Promise.all(
leftColumn.map(async (tank) => (
<Tanks.Item
2023-11-29 12:17:21 -06:00
key={tank.id}
2023-12-12 18:11:55 -06:00
name={tank.name}
2023-08-09 00:19:50 +00:00
tankType={tank.type}
2023-11-29 12:17:21 -06:00
image={tankIcon(tank.id)}
2024-02-03 15:54:21 -06:00
treeType={tank.treeType}
2023-08-09 00:19:50 +00:00
/>
)),
)}
</Tanks.Column>
<Tanks.Column>
{await Promise.all(
rightColumn.map(async (tank) => (
<Tanks.Item
2023-11-29 12:17:21 -06:00
key={tank.id}
2023-12-12 18:11:55 -06:00
name={tank.name}
2023-08-09 00:19:50 +00:00
tankType={tank.type}
2023-11-29 12:17:21 -06:00
image={tankIcon(tank.id)}
2024-02-03 15:54:21 -06:00
treeType={tank.treeType}
2023-08-09 00:19:50 +00:00
/>
)),
)}
</Tanks.Column>
</Tanks.Row>
</Tanks.Root>
);
}),
))}
2024-03-06 09:52:09 -06:00
</CommandWrapper>
2023-06-05 18:47:00 +00:00
);
2023-04-26 00:02:10 +00:00
},
2023-05-14 17:13:42 +00:00
2023-06-27 02:22:28 +00:00
autocomplete: autocompleteUsername,
2023-07-05 01:15:18 +00:00
};