Files
blitzkit/src/commands/ownedTanks.tsx
T

169 lines
5.8 KiB
TypeScript
Raw Normal View History

2023-05-28 16:56:15 +00:00
import { SlashCommandBuilder } from 'discord.js';
2023-07-11 19:54:57 +00:00
import NoData, { NoDataType } from '../components/NoData';
import * as Tanks from '../components/Tanks';
import TitleBar from '../components/TitleBar';
import Wrapper from '../components/Wrapper';
2023-09-23 21:51:00 +00:00
import { WARGAMING_APPLICATION_ID } from '../constants/wargamingApplicationID';
2023-07-11 19:54:57 +00:00
import getTankStats from '../core/blitz/getTankStats';
import getTreeType from '../core/blitz/getTreeType';
import getWargamingResponse from '../core/blitz/getWargamingResponse';
import resolveTankName from '../core/blitz/resolveTankName';
2023-06-21 01:52:26 +00:00
import {
TIER_ROMAN_NUMERALS,
2023-08-09 00:19:50 +00:00
TankopediaEntry,
2023-06-21 01:52:26 +00:00
Tier,
tankopedia,
2023-08-09 00:19:50 +00:00
tankopediaInfo,
2023-07-11 19:54:57 +00:00
} from '../core/blitz/tankopedia';
2023-08-23 00:14:41 +00:00
import addTierChoices from '../core/discord/addTierChoices';
2023-07-11 19:54:57 +00:00
import addUsernameChoices from '../core/discord/addUsernameChoices';
import autocompleteUsername from '../core/discord/autocompleteUsername';
import resolvePlayerFromCommand from '../core/discord/resolvePlayerFromCommand';
import { CommandRegistry } from '../events/interactionCreate';
import { AccountInfo } from '../types/accountInfo';
2023-10-11 18:42:36 -05:00
import { PlayerClanInfo } from '../types/playerClanData';
2023-04-27 19:55:42 +00:00
2023-04-28 13:07:51 +00:00
const COMP_TANKS = [
2023-04-27 19:55:42 +00:00
// light tanks
2023-04-28 13:07:51 +00:00
24321, // t-100 lt
20257, // sheridan
3649, // bat chat
19537, // vickers
2023-04-27 19:55:42 +00:00
// heavy tanks
2023-04-28 13:07:51 +00:00
6145, // is-4
7169, // is-7
10785, // t110e5
12161, // strvk
4481, // kranvagn
22817, // m6 yoh
6225, // fv215b
5425, // wz 113
7297, // 60tp
58641, // vk 72
6753, // type 71
];
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-05-16 02:02:50 +00:00
const accountInfo = await getWargamingResponse<AccountInfo>(
2023-09-23 21:51:00 +00:00
`https://api.wotblitz.${server}/wotb/account/info/?application_id=${WARGAMING_APPLICATION_ID}&account_id=${id}`,
2023-05-16 02:02:50 +00:00
);
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]!,
tank_id: tankData.tank_id,
})),
)
2023-08-09 00:19:50 +00:00
).filter((tank) => tank.tankopedia?.tier === tier);
2023-10-11 18:42:36 -05:00
const clanData = await getWargamingResponse<PlayerClanInfo>(
2023-09-23 21:51:00 +00:00
`https://api.wotblitz.${server}/wotb/clans/accountinfo/?application_id=${WARGAMING_APPLICATION_ID}&account_id=${id}&extra=clan`,
2023-06-05 18:47:00 +00:00
);
2023-08-09 00:19:50 +00:00
const groupedTanks: Record<string, TankopediaEntry[]> = {};
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
name={accountInfo[id].nickname}
image={
clanData[id]?.clan
? `https://wotblitz-gc.gcdn.co/icons/clanEmblems1x/clan-icon-v2-${clanData[id]?.clan?.emblem_set_id}.png`
: 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
key={tank.tank_id}
name={await resolveTankName(tank.tank_id)}
tankType={tank.type}
image={tank.images?.normal}
treeType={await getTreeType(tank.tank_id)}
/>
)),
)}
</Tanks.Column>
<Tanks.Column>
{await Promise.all(
rightColumn.map(async (tank) => (
<Tanks.Item
key={tank.tank_id}
name={await resolveTankName(tank.tank_id)}
tankType={tank.type}
image={tank.images?.normal}
treeType={await getTreeType(tank.tank_id)}
/>
)),
)}
</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
};
2023-08-09 00:35:33 +00:00
// TODO: make this work in the future
// console.log('Caching tank images...');
// TANKS.then((tanks) =>
// jsxToSvg(
// <div style={{ display: 'flex' }}>
// {tanks.map(({ images, tank_id }) => {
// return (
// images.normal && <img key={tank_id} src={images.normal} width={0} />
// );
// })}
// </div>,
// ),
// ).then(() => console.log('Cached tank images'));