feat: code split a lot

This commit is contained in:
TrèsAbhi committed 2023-10-11 18:42:36 -05:00
1 parent 78e4c82f20
commit d2668974f7
18 files changed
+155 -133

No files matched your search

+2 -2
View File
@@ -12,7 +12,7 @@ import embedNegative from '../core/discord/embedNegative';
import { CommandRegistry } from '../events/interactionCreate';
import { theme } from '../stitches.config';
import { AccountInfo } from '../types/accountInfo';
import { PlayerClanData } from '../types/playerClanData';
import { PlayerClanInfo } from '../types/playerClanData';
const serverAndIdPattern = /(com|eu|asia)\/[0-9]+/;
@@ -99,7 +99,7 @@ export const verifyCommand: CommandRegistry = {
getWargamingResponse<AccountInfo>(
`https://api.wotblitz.${region}/wotb/account/info/?application_id=${WARGAMING_APPLICATION_ID}&account_id=${id}`,
),
getWargamingResponse<PlayerClanData>(
getWargamingResponse<PlayerClanInfo>(
`https://api.wotblitz.${region}/wotb/clans/accountinfo/?application_id=${WARGAMING_APPLICATION_ID}&account_id=${id}&extra=clan`,
),
]);
+2 -2
View File
@@ -21,7 +21,7 @@ import autocompleteUsername from '../core/discord/autocompleteUsername';
import resolvePlayerFromCommand from '../core/discord/resolvePlayerFromCommand';
import { CommandRegistry } from '../events/interactionCreate';
import { AccountInfo } from '../types/accountInfo';
import { PlayerClanData } from '../types/playerClanData';
import { PlayerClanInfo } from '../types/playerClanData';
const COMP_TANKS = [
// light tanks
@@ -70,7 +70,7 @@ export const ownedTanksCommand: CommandRegistry = {
})),
)
).filter((tank) => tank.tankopedia?.tier === tier);
const clanData = await getWargamingResponse<PlayerClanData>(
const clanData = await getWargamingResponse<PlayerClanInfo>(
`https://api.wotblitz.${server}/wotb/clans/accountinfo/?application_id=${WARGAMING_APPLICATION_ID}&account_id=${id}&extra=clan`,
);
const groupedTanks: Record<string, TankopediaEntry[]> = {};
+4 -4
View File
@@ -26,7 +26,7 @@ import { octokit } from '../core/github/octokit';
import throwError from '../core/node/throwError';
import { CommandRegistryRaw } from '../events/interactionCreate';
import { AccountInfo } from '../types/accountInfo';
import { PlayerClanData } from '../types/playerClanData';
import { PlayerClanInfo } from '../types/playerClanData';
export interface RatingsPlayer {
spa_id: number;
@@ -313,7 +313,7 @@ export const ratingsCommand = new Promise<CommandRegistryRaw>(
lastPlayerIndex + 1,
);
const ids = trimmed.map((player) => player.id).join(',');
const clanData = await getWargamingResponse<PlayerClanData>(
const clanData = await getWargamingResponse<PlayerClanInfo>(
`https://api.wotblitz.${region}/wotb/clans/accountinfo/?application_id=${WARGAMING_APPLICATION_ID}&account_id=${ids}&extra=clan`,
);
@@ -377,7 +377,7 @@ export const ratingsCommand = new Promise<CommandRegistryRaw>(
`https://api.wotblitz.${region}/wotb/account/info/?application_id=${WARGAMING_APPLICATION_ID}&account_id=${id}`,
);
const clan = (
await getWargamingResponse<PlayerClanData>(
await getWargamingResponse<PlayerClanInfo>(
`https://api.wotblitz.${region}/wotb/clans/accountinfo/?application_id=${WARGAMING_APPLICATION_ID}&account_id=${id}&extra=clan`,
)
)[id]?.clan;
@@ -437,7 +437,7 @@ export const ratingsCommand = new Promise<CommandRegistryRaw>(
playerIndex + halfRange + 1,
);
const ids = trimmed.map((player) => player.id).join(',');
const clanData = await getWargamingResponse<PlayerClanData>(
const clanData = await getWargamingResponse<PlayerClanInfo>(
`https://api.wotblitz.${region}/wotb/clans/accountinfo/?application_id=${WARGAMING_APPLICATION_ID}&account_id=${ids}&extra=clan`,
);
+19 -53
View File
@@ -1,20 +1,22 @@
import { SlashCommandBuilder } from 'discord.js';
import AllStatsOverview from '../components/AllStatsOverview';
import NoData, { NoDataType } from '../components/NoData';
import { TreeTypeString } from '../components/Tanks';
import TitleBar from '../components/TitleBar';
import Wrapper from '../components/Wrapper';
import { WARGAMING_APPLICATION_ID } from '../constants/wargamingApplicationID';
import { StatFilters, filterStats } from '../core/blitz/filterStats';
import getWargamingResponse from '../core/blitz/getWargamingResponse';
import { getAccountInfo } from '../core/blitz/getAccountInfo';
import { getClanLogo } from '../core/blitz/getClanLogo';
import { playerClanInfo } from '../core/blitz/playerClanInfo';
import { filtersToDescription } from '../core/blitzkrieg/filtersToDescription';
import { getBlitzStarsLinkButton } from '../core/blitzstars/getBlitzStarsLinkButton';
import getDiffedTankStats from '../core/blitzstars/getDiffedTankStats';
import { PeriodType } from '../core/discord/addPeriodSubCommands';
import addStatTypeSubCommandGroups from '../core/discord/addStatTypeSubCommandGroups';
import addUsernameChoices from '../core/discord/addUsernameChoices';
import autocompleteTanks from '../core/discord/autocompleteTanks';
import autocompleteUsername from '../core/discord/autocompleteUsername';
import { getCustomPeriodParams } from '../core/discord/getCustomPeriodParams';
import { getFiltersFromButton } from '../core/discord/getFiltersFromButton';
import { getFiltersFromCommand } from '../core/discord/getFiltersFromCommand';
import interactionToURL from '../core/discord/interactionToURL';
import { refreshButton } from '../core/discord/refreshButton';
import resolvePeriodFromButton from '../core/discord/resolvePeriodFromButton';
@@ -26,27 +28,15 @@ import resolvePlayerFromCommand, {
ResolvedPlayer,
} from '../core/discord/resolvePlayerFromCommand';
import { CommandRegistryRaw } from '../events/interactionCreate';
import { AccountInfo } from '../types/accountInfo';
import { PlayerClanData } from '../types/playerClanData';
export default async function render(
{ region, id }: ResolvedPlayer,
{ start, end, name: statsName }: ResolvedPeriod,
{ start, end, name }: ResolvedPeriod,
filters: StatFilters,
) {
const nickname = (
await getWargamingResponse<AccountInfo>(
`https://api.wotblitz.${region}/wotb/account/info/?application_id=${WARGAMING_APPLICATION_ID}&account_id=${id}`,
)
)[id].nickname;
const clan = (
await getWargamingResponse<PlayerClanData>(
`https://api.wotblitz.${region}/wotb/clans/accountinfo/?application_id=${WARGAMING_APPLICATION_ID}&account_id=${id}&extra=clan`,
)
)[id]?.clan;
const clanImage = clan
? `https://wotblitz-gc.gcdn.co/icons/clanEmblems1x/clan-icon-v2-${clan.emblem_set_id}.png`
: undefined;
const nickname = (await getAccountInfo(region, id))[id].nickname;
const clan = (await playerClanInfo(region, id))[id]?.clan;
const clanImage = clan ? getClanLogo(clan.emblem_set_id) : undefined;
const diffedTankStats = await getDiffedTankStats(region, id, start, end);
const { stats, supplementary } = await filterStats(diffedTankStats, filters);
@@ -55,7 +45,7 @@ export default async function render(
<TitleBar
name={nickname}
image={clanImage}
description={`${statsName} • ${await filtersToDescription(filters)}`}
description={`${name} • ${await filtersToDescription(filters)}`}
/>
{!stats?.battles && <NoData type={NoDataType.BattlesInPeriod} />}
@@ -86,34 +76,18 @@ export const statsCommand = new Promise<CommandRegistryRaw>(async (resolve) => {
resolvedPlayer.region,
interaction,
);
const filters = {
nation: interaction.options.getString('nation') ?? undefined,
tier:
parseInt(interaction.options.getString('tier') ?? '0') || undefined,
tankType: interaction.options.getString('tank-type') ?? undefined,
treeType:
(interaction.options.getString('tree-type') as
| TreeTypeString
| undefined) ?? undefined,
tank:
parseInt(interaction.options.getString('tank') ?? '0') || undefined,
} satisfies StatFilters;
const filters = getFiltersFromCommand(interaction);
const path = interactionToURL(interaction, {
...resolvedPlayer,
...((interaction.options.getSubcommand() as PeriodType) === 'custom'
? {
start: interaction.options.getInteger('start', true),
end: interaction.options.getInteger('end', true),
}
: undefined),
...getCustomPeriodParams(interaction),
...filters,
});
return [
await render(resolvedPlayer, resolvedPeriod, filters),
return Promise.all([
render(resolvedPlayer, resolvedPeriod, filters),
refreshButton(interaction, path),
await getBlitzStarsLinkButton(resolvedPlayer.region, resolvedPlayer.id),
];
getBlitzStarsLinkButton(resolvedPlayer.region, resolvedPlayer.id),
]);
},
autocomplete: (interaction) => {
@@ -122,19 +96,11 @@ export const statsCommand = new Promise<CommandRegistryRaw>(async (resolve) => {
},
async button(interaction) {
const url = new URL(`https://example.com${interaction.customId}`);
const player = await resolvePlayerFromButton(interaction);
const period = resolvePeriodFromButton(player.region, interaction);
const filters = getFiltersFromButton(interaction);
return await render(player, period, {
nation: url.searchParams.get('nation') ?? undefined,
tank: parseInt(url.searchParams.get('tank') ?? '0') || undefined,
tankType: url.searchParams.get('tank-type') ?? undefined,
treeType:
(url.searchParams.get('tree-type') as TreeTypeString | undefined) ??
undefined,
tier: parseInt(url.searchParams.get('tier') ?? '0') || undefined,
});
return await render(player, period, filters);
},
});
});
+11
View File
@@ -0,0 +1,11 @@
import { Region } from '../../constants/regions';
import { WARGAMING_APPLICATION_ID } from '../../constants/wargamingApplicationID';
import { AccountInfo } from '../../types/accountInfo';
import getWargamingResponse from './getWargamingResponse';
// BIG TODO: generalize all get wargaming calls
export function getAccountInfo(region: Region, id: number) {
return getWargamingResponse<AccountInfo>(
`https://api.wotblitz.${region}/wotb/account/info/?application_id=${WARGAMING_APPLICATION_ID}&account_id=${id}`,
);
}
+3
View File
@@ -0,0 +1,3 @@
export function getClanLogo(emblemId: number) {
return `https://wotblitz-gc.gcdn.co/icons/clanEmblems1x/clan-icon-v2-${emblemId}.png`
}
+10
View File
@@ -0,0 +1,10 @@
import { Region } from '../../constants/regions';
import { WARGAMING_APPLICATION_ID } from '../../constants/wargamingApplicationID';
import { PlayerClanInfo } from '../../types/playerClanData';
import getWargamingResponse from './getWargamingResponse';
export function playerClanInfo(region: Region, id: number) {
return getWargamingResponse<PlayerClanInfo>(
`https://api.wotblitz.${region}/wotb/clans/accountinfo/?application_id=${WARGAMING_APPLICATION_ID}&account_id=${id}&extra=clan`,
);
}
+14
View File
@@ -0,0 +1,14 @@
import { ChatInputCommandInteraction } from 'discord.js';
import { PeriodType } from './addPeriodSubCommands';
export function getCustomPeriodParams(
interaction: ChatInputCommandInteraction,
) {
if ((interaction.options.getSubcommand() as PeriodType) === 'custom')
return {
start: interaction.options.getInteger('start', true),
end: interaction.options.getInteger('end', true),
};
return undefined;
}
+17
View File
@@ -0,0 +1,17 @@
import { ButtonInteraction } from 'discord.js';
import { TreeTypeString } from '../../components/Tanks';
import { StatFilters } from '../blitz/filterStats';
export function getFiltersFromButton(interaction: ButtonInteraction) {
const url = new URL(`https://example.com/${interaction.customId}`);
return {
nation: url.searchParams.get('nation') ?? undefined,
tank: parseInt(url.searchParams.get('tank') ?? '0') || undefined,
tankType: url.searchParams.get('tank-type') ?? undefined,
treeType:
(url.searchParams.get('tree-type') as TreeTypeString | undefined) ??
undefined,
tier: parseInt(url.searchParams.get('tier') ?? '0') || undefined,
} satisfies StatFilters;
}
+18
View File
@@ -0,0 +1,18 @@
import { ChatInputCommandInteraction } from 'discord.js';
import { TreeTypeString } from '../../components/Tanks';
import { StatFilters } from '../blitz/filterStats';
export function getFiltersFromCommand(
interaction: ChatInputCommandInteraction,
) {
return {
nation: interaction.options.getString('nation') ?? undefined,
tier: parseInt(interaction.options.getString('tier') ?? '0') || undefined,
tankType: interaction.options.getString('tank-type') ?? undefined,
treeType:
(interaction.options.getString('tree-type') as
| TreeTypeString
| undefined) ?? undefined,
tank: parseInt(interaction.options.getString('tank') ?? '0') || undefined,
} satisfies StatFilters;
}
+38 -8
View File
@@ -1,13 +1,43 @@
import { ButtonInteraction, CacheType } from 'discord.js';
import { ButtonInteraction } from 'discord.js';
import { Region } from '../../constants/regions';
import resolvePeriodFromURL from './resolvePeriodFromURL';
import getPeriodNow from '../blitzstars/getPeriodNow';
import getPeriodStart from '../blitzstars/getPeriodStart';
import getTimeDaysAgo from '../blitzstars/getTimeDaysAgo';
import { PeriodType } from './addPeriodSubCommands';
import {
ResolvedPeriod,
getPeriodOptionName,
} from './resolvePeriodFromCommand';
export default function resolvePeriodFromButton(
server: Region,
interaction: ButtonInteraction<CacheType>,
region: Region,
interaction: ButtonInteraction,
) {
return resolvePeriodFromURL(
server,
`https://example.com/${interaction.customId}`,
);
let name: string;
let start: number;
let end: number;
const url = new URL(`https://example.com/${interaction.customId}`);
const path = url.pathname.split('/');
const periodSubcommand = path[2] as PeriodType;
if (periodSubcommand === 'custom') {
const startRaw = parseInt(url!.searchParams.get('start')!);
const endRaw = parseInt(url!.searchParams.get('end')!);
const startDaysAgoMin = Math.min(startRaw, endRaw);
const endDaysAgoMax = Math.max(startRaw, endRaw);
name = `${startDaysAgoMin} - ${endDaysAgoMax} days`;
start = getTimeDaysAgo(region, endDaysAgoMax);
end = getTimeDaysAgo(region, startDaysAgoMin);
} else {
name = getPeriodOptionName(periodSubcommand);
start = getPeriodStart(region, periodSubcommand);
end = getPeriodNow();
}
return {
name,
start,
end,
} satisfies ResolvedPeriod;
}
-42
View File
@@ -1,42 +0,0 @@
import { Region } from '../../constants/regions';
import getPeriodNow from '../blitzstars/getPeriodNow';
import getPeriodStart from '../blitzstars/getPeriodStart';
import getTimeDaysAgo from '../blitzstars/getTimeDaysAgo';
import { PeriodType } from './addPeriodSubCommands';
import {
ResolvedPeriod,
getPeriodOptionName,
} from './resolvePeriodFromCommand';
export default function resolvePeriodFromURL(
region: Region,
urlString: string,
) {
let name: string;
let start: number;
let end: number;
const url = new URL(urlString);
const path = url.pathname.split('/');
const periodSubcommand = path[2] as PeriodType;
if (periodSubcommand === 'custom') {
const startRaw = parseInt(url!.searchParams.get('start')!);
const endRaw = parseInt(url!.searchParams.get('end')!);
const startDaysAgoMin = Math.min(startRaw, endRaw);
const endDaysAgoMax = Math.max(startRaw, endRaw);
name = `${startDaysAgoMin} - ${endDaysAgoMax} days`;
start = getTimeDaysAgo(region, endDaysAgoMax);
end = getTimeDaysAgo(region, startDaysAgoMin);
} else {
name = getPeriodOptionName(periodSubcommand);
start = getPeriodStart(region, periodSubcommand);
end = getPeriodNow();
}
return {
name,
start,
end,
} satisfies ResolvedPeriod;
}
+10 -4
View File
@@ -1,8 +1,14 @@
import { ButtonInteraction, CacheType } from 'discord.js';
import resolvePlayerFromURL from './resolvePlayerFromURL';
import { ButtonInteraction } from 'discord.js';
import { Region } from '../../constants/regions';
import { ResolvedPlayer } from './resolvePlayerFromCommand';
export default async function resolvePlayerFromButton(
interaction: ButtonInteraction<CacheType>,
interaction: ButtonInteraction,
) {
return resolvePlayerFromURL(`https://exmaple.com/${interaction.customId}`);
const url = new URL(`https://exmaple.com/${interaction.customId}`);
return {
id: parseInt(url.searchParams.get('id')!),
region: url.searchParams.get('region') as Region,
} satisfies ResolvedPlayer;
}
-11
View File
@@ -1,11 +0,0 @@
import { Region } from '../../constants/regions';
import { ResolvedPlayer } from '../discord/resolvePlayerFromCommand';
export default function resolvePlayerFromURL(urlString: string) {
const url = new URL(urlString);
return {
id: parseInt(url.searchParams.get('id')!),
region: url.searchParams.get('region') as Region,
} satisfies ResolvedPlayer;
}
+2 -2
View File
@@ -12,7 +12,7 @@ import { ResolvedPeriod } from '../core/discord/resolvePeriodFromCommand';
import { ResolvedPlayer } from '../core/discord/resolvePlayerFromCommand';
import { AccountInfo } from '../types/accountInfo';
import { Histories } from '../types/histories';
import { PlayerClanData } from '../types/playerClanData';
import { PlayerClanInfo } from '../types/playerClanData';
import { StatType } from './stats';
export default async function evolution<Type extends StatType>(
@@ -26,7 +26,7 @@ export default async function evolution<Type extends StatType>(
if (type === 'player') {
const clan = (
await getWargamingResponse<PlayerClanData>(
await getWargamingResponse<PlayerClanInfo>(
`https://api.wotblitz.${server}/wotb/clans/accountinfo/?application_id=${WARGAMING_APPLICATION_ID}&account_id=${id}&extra=clan`,
)
)[id]?.clan;
+2 -2
View File
@@ -18,7 +18,7 @@ import {
AllStats,
SupplementaryStats,
} from '../types/accountInfo';
import { PlayerClanData } from '../types/playerClanData';
import { PlayerClanInfo } from '../types/playerClanData';
import { MultiTankFilters, StatType } from './stats';
export default async function fullStats<Type extends StatType>(
@@ -36,7 +36,7 @@ export default async function fullStats<Type extends StatType>(
if (type === 'player' || type === 'multi-tank') {
const clan = (
await getWargamingResponse<PlayerClanData>(
await getWargamingResponse<PlayerClanInfo>(
`https://api.wotblitz.${server}/wotb/clans/accountinfo/?application_id=${WARGAMING_APPLICATION_ID}&account_id=${id}&extra=clan`,
)
)[id]?.clan;
+2 -2
View File
@@ -17,7 +17,7 @@ import getTimeDaysAgo from '../core/blitzstars/getTimeDaysAgo';
import { tankAverages } from '../core/blitzstars/tankAverages';
import { ResolvedPlayer } from '../core/discord/resolvePlayerFromCommand';
import { AccountInfo, AllStats } from '../types/accountInfo';
import { PlayerClanData } from '../types/playerClanData';
import { PlayerClanInfo } from '../types/playerClanData';
import { PossiblyPromise } from '../types/possiblyPromise';
export default async function today(
@@ -36,7 +36,7 @@ export default async function today(
const accountInfo = await getWargamingResponse<AccountInfo>(
`https://api.wotblitz.${server}/wotb/account/info/?application_id=${WARGAMING_APPLICATION_ID}&account_id=${id}`,
);
const clanData = await getWargamingResponse<PlayerClanData>(
const clanData = await getWargamingResponse<PlayerClanInfo>(
`https://api.wotblitz.${server}/wotb/clans/accountinfo/?application_id=${WARGAMING_APPLICATION_ID}&account_id=${id}&extra=clan`,
);
const careerTankStatsRaw = await getTankStats(server, id);
+1 -1
View File
@@ -1,4 +1,4 @@
export type PlayerClanData = {
export type PlayerClanInfo = {
[accountId: number]: {
account_id: number;
joined_at: number;