Files
blitzkit/src/commands/ownedTanks.tsx
T

130 lines
4.8 KiB
TypeScript
Raw Normal View History

2023-05-28 16:56:15 +00:00
import { SlashCommandBuilder } from 'discord.js';
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';
import Wrapper from '../components/Wrapper';
import { getAccountInfo } from '../core/blitz/getAccountInfo';
import { getClanAccountInfo } from '../core/blitz/getClanAccountInfo';
import getTankStats from '../core/blitz/getTankStats';
import getTreeType from '../core/blitz/getTreeType';
2023-10-14 11:25:42 -05:00
import resolveTankName from '../core/blitz/resolveTankName';
2023-10-29 11:54:23 -05:00
import { tankopediaInfo } from '../core/blitz/tankopediaInfo';
2023-10-14 11:06:03 -05:00
import { emblemIdToURL } from '../core/blitzkrieg/emblemIdToURL';
2023-11-29 12:17:21 -06:00
import { tankIcon } from '../core/blitzkrieg/tankIcon';
2023-10-14 11:25:42 -05:00
import {
2023-11-29 12:17:21 -06:00
BlitzkriegTankopediaEntry,
2023-10-14 11:25:42 -05:00
TIER_ROMAN_NUMERALS,
Tier,
tankopedia,
2023-11-29 12:17:21 -06:00
} from '../core/blitzkrieg/tankopedia';
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-08-09 00:19:50 +00:00
tankopedia: (await tankopedia)[tankData.tank_id]!,
2023-11-29 12:17:21 -06:00
id: tankData.tank_id,
})),
)
2023-08-09 00:19:50 +00:00
).filter((tank) => tank.tankopedia?.tier === tier);
2023-10-16 20:16:23 -05:00
const clanAccountInfo = await getClanAccountInfo(server, id, ['clan']);
2023-11-29 12:17:21 -06:00
const groupedTanks: Record<string, BlitzkriegTankopediaEntry[]> = {};
2023-08-09 00:19:50 +00:00
const nations: string[] = [];
filteredTanks.forEach((tank) => {
if (groupedTanks[tank.tankopedia.nation] === undefined) {
groupedTanks[tank.tankopedia.nation] = [tank.tankopedia];
nations.push(tank.tankopedia.nation);
} else {
groupedTanks[tank.tankopedia.nation].push(tank.tankopedia);
}
});
nations.sort();
2023-04-26 00:02:10 +00:00
2023-06-16 02:01:06 +00:00
return (
2023-06-05 18:47:00 +00:00
<Wrapper>
<TitleBar
2023-10-12 13:00:33 -05:00
name={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>
{(await tankopediaInfo).vehicle_nations[nation]}
</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}
name={await resolveTankName(tank.id)}
2023-08-09 00:19:50 +00:00
tankType={tank.type}
2023-11-29 12:17:21 -06:00
image={tankIcon(tank.id)}
treeType={await getTreeType(tank.id)}
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}
name={await resolveTankName(tank.id)}
2023-08-09 00:19:50 +00:00
tankType={tank.type}
2023-11-29 12:17:21 -06:00
image={tankIcon(tank.id)}
treeType={await getTreeType(tank.id)}
2023-08-09 00:19:50 +00:00
/>
)),
)}
</Tanks.Column>
</Tanks.Row>
</Tanks.Root>
);
}),
))}
2023-06-16 02:01:06 +00:00
</Wrapper>
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
};