feat: filter functionality

This commit is contained in:
TresAbhi committed 2023-08-04 19:49:40 +00:00
1 parent a734a71d2f
commit 909cabd517
7 files changed
+143 -91

No files matched your search

+29 -4
View File
@@ -1,4 +1,5 @@
import { SlashCommandBuilder } from 'discord.js'; import { SlashCommandBuilder } from 'discord.js';
import { TreeTypeString } from '../components/Tanks';
import { CYCLIC_API } from '../constants/cyclic'; import { CYCLIC_API } from '../constants/cyclic';
import getWargamingResponse from '../core/blitz/getWargamingResponse'; import getWargamingResponse from '../core/blitz/getWargamingResponse';
import resolveTankId from '../core/blitz/resolveTankId'; import resolveTankId from '../core/blitz/resolveTankId';
@@ -14,8 +15,7 @@ import resolvePlayerFromButton from '../core/discord/resolvePlayerFromButton';
import resolvePlayerFromCommand from '../core/discord/resolvePlayerFromCommand'; import resolvePlayerFromCommand from '../core/discord/resolvePlayerFromCommand';
import { secrets } from '../core/node/secrets'; import { secrets } from '../core/node/secrets';
import { CommandRegistryRaw } from '../events/interactionCreate'; import { CommandRegistryRaw } from '../events/interactionCreate';
import { StatType } from '../renderers/fullStats'; import stats, { MultiTankFilters, StatType } from '../renderers/stats';
import stats from '../renderers/stats';
import { AccountInfo } from '../types/accountInfo'; import { AccountInfo } from '../types/accountInfo';
export const statsCommand = new Promise<CommandRegistryRaw>(async (resolve) => { export const statsCommand = new Promise<CommandRegistryRaw>(async (resolve) => {
@@ -61,7 +61,23 @@ export const statsCommand = new Promise<CommandRegistryRaw>(async (resolve) => {
)[player.id]; )[player.id];
return [ return [
await stats(commandGroup, resolvedPeriod, player, tankId), await stats(
commandGroup,
resolvedPeriod,
player,
commandGroup === 'tank'
? tankId
: commandGroup === 'multi-tank'
? ({
'tank-type':
interaction.options.getString('tank-type') ?? undefined,
nation: interaction.options.getString('nation') ?? undefined,
tier: interaction.options.getInteger('tier') ?? undefined,
'tree-type': (interaction.options.getString('tree-type') ??
undefined) as TreeTypeString | undefined,
} satisfies Partial<MultiTankFilters>)
: null,
),
primaryButton(path, 'Refresh'), primaryButton(path, 'Refresh'),
linkButton(`${CYCLIC_API}/${path}`, 'Embed'), linkButton(`${CYCLIC_API}/${path}`, 'Embed'),
linkButton( linkButton(
@@ -87,7 +103,16 @@ export const statsCommand = new Promise<CommandRegistryRaw>(async (resolve) => {
commandGroup, commandGroup,
period, period,
player, player,
parseInt(url.searchParams.get('tankId')!), commandGroup === 'tank'
? parseInt(url.searchParams.get('tankId')!)
: commandGroup === 'multi-tank'
? ({
'tank-type': url.searchParams.get('tank-type')!,
nation: url.searchParams.get('nation')!,
tier: parseInt(url.searchParams.get('tier')!),
'tree-type': url.searchParams.get('tree-type') as TreeTypeString,
} satisfies MultiTankFilters)
: null,
); );
}, },
}); });
+10 -8
View File
@@ -5,29 +5,31 @@ import {
} from '../../../core/blitz/tankopedia'; } from '../../../core/blitz/tankopedia';
import { theme } from '../../../stitches.config'; import { theme } from '../../../stitches.config';
export enum TreeType { export enum TreeTypeEnum {
TechTree, TechTree,
Premium, Premium,
Collector, Collector,
} }
export type TreeTypeString = 'techtree' | 'premium' | 'collector';
export interface ItemProps { export interface ItemProps {
image?: string; image?: string;
tankType?: string; tankType?: string;
name: string; name: string;
treeType: TreeType; treeType: TreeTypeEnum;
} }
const TREE_TYPE_COLOR = { const TREE_TYPE_COLOR = {
[TreeType.TechTree]: '', [TreeTypeEnum.TechTree]: '',
[TreeType.Premium]: '_amber', [TreeTypeEnum.Premium]: '_amber',
[TreeType.Collector]: '_blue', [TreeTypeEnum.Collector]: '_blue',
} as const; } as const;
const TREE_TYPE_ICONS = { const TREE_TYPE_ICONS = {
[TreeType.TechTree]: TANK_ICONS, [TreeTypeEnum.TechTree]: TANK_ICONS,
[TreeType.Premium]: TANK_ICONS_PREMIUM, [TreeTypeEnum.Premium]: TANK_ICONS_PREMIUM,
[TreeType.Collector]: TANK_ICONS_COLLECTOR, [TreeTypeEnum.Collector]: TANK_ICONS_COLLECTOR,
}; };
export function Item({ image, tankType, name, treeType }: ItemProps) { export function Item({ image, tankType, name, treeType }: ItemProps) {
+4 -4
View File
@@ -1,11 +1,11 @@
import { TreeType } from '../../components/Tanks'; import { TreeTypeEnum } from '../../components/Tanks';
import { tankopedia } from './tankopedia'; import { tankopedia } from './tankopedia';
export default async function getTreeType(id: number) { export default async function getTreeType(id: number) {
const entry = (await tankopedia)[id]; const entry = (await tankopedia)[id];
if (entry?.is_collectible) return TreeType.Collector; if (entry?.is_collectible) return TreeTypeEnum.Collector;
if (entry?.is_premium) return TreeType.Premium; if (entry?.is_premium) return TreeTypeEnum.Premium;
return TreeType.TechTree; return TreeTypeEnum.TechTree;
} }
@@ -1,4 +1,6 @@
import { SlashCommandBuilder } from 'discord.js'; import { SlashCommandBuilder } from 'discord.js';
import { TreeTypeString } from '../../components/Tanks';
import { StatType } from '../../renderers/stats';
import { tankopediaInfo } from '../blitz/tankopedia'; import { tankopediaInfo } from '../blitz/tankopedia';
import addPeriodSubCommands from './addPeriodSubCommands'; import addPeriodSubCommands from './addPeriodSubCommands';
import addTankChoices from './addTankChoices'; import addTankChoices from './addTankChoices';
@@ -14,7 +16,7 @@ export default async function addStatTypeSubCommandGroups(
addPeriodSubCommands(option, (option) => addPeriodSubCommands(option, (option) =>
option.addStringOption(addUsernameChoices), option.addStringOption(addUsernameChoices),
) )
.setName('player') .setName('player' satisfies StatType)
.setDescription('Player statistics'), .setDescription('Player statistics'),
) )
.addSubcommandGroup((option) => .addSubcommandGroup((option) =>
@@ -23,7 +25,7 @@ export default async function addStatTypeSubCommandGroups(
.addStringOption(addTankChoices) .addStringOption(addTankChoices)
.addStringOption(addUsernameChoices), .addStringOption(addUsernameChoices),
) )
.setName('tank') .setName('tank' satisfies StatType)
.setDescription('Tank statistics'), .setDescription('Tank statistics'),
) )
.addSubcommandGroup((option) => .addSubcommandGroup((option) =>
@@ -69,14 +71,20 @@ export default async function addStatTypeSubCommandGroups(
.setName('tree-type') .setName('tree-type')
.setDescription('Tech tree type') .setDescription('Tech tree type')
.addChoices( .addChoices(
{ name: 'Tech Tree', value: 'techtree' }, {
{ name: 'Premium', value: 'premium' }, name: 'Tech Tree',
{ name: 'Collector', value: 'collector' }, value: 'techtree' satisfies TreeTypeString,
},
{ name: 'Premium', value: 'premium' satisfies TreeTypeString },
{
name: 'Collector',
value: 'collector' satisfies TreeTypeString,
},
) )
.setRequired(false), .setRequired(false),
), ),
) )
.setName('multi-tank') .setName('multi-tank' satisfies StatType)
.setDescription('Multiple tanks'), .setDescription('Multiple tanks'),
); );
} }
+1 -1
View File
@@ -15,7 +15,7 @@ import { secrets } from '../core/node/secrets';
import { AccountInfo } from '../types/accountInfo'; import { AccountInfo } from '../types/accountInfo';
import { Histories } from '../types/histories'; import { Histories } from '../types/histories';
import { PlayerClanData } from '../types/playerClanData'; import { PlayerClanData } from '../types/playerClanData';
import { StatType } from './fullStats'; import { StatType } from './stats';
export default async function evolution<Type extends StatType>( export default async function evolution<Type extends StatType>(
type: Type, type: Type,
+1 -2
View File
@@ -22,8 +22,7 @@ import {
} from '../types/accountInfo'; } from '../types/accountInfo';
import { PlayerClanData } from '../types/playerClanData'; import { PlayerClanData } from '../types/playerClanData';
import { PossiblyPromise } from '../types/possiblyPromise'; import { PossiblyPromise } from '../types/possiblyPromise';
import { StatType } from './stats';
export type StatType = 'player' | 'tank';
export default async function fullStats<Type extends StatType>( export default async function fullStats<Type extends StatType>(
type: Type, type: Type,
+84 -66
View File
@@ -1,6 +1,7 @@
import AllStatsOverview from '../components/AllStatsOverview'; import AllStatsOverview from '../components/AllStatsOverview';
import NoData, { NoDataType } from '../components/NoData'; import NoData, { NoDataType } from '../components/NoData';
import PoweredBy, { PoweredByType } from '../components/PoweredBy'; import PoweredBy, { PoweredByType } from '../components/PoweredBy';
import { TreeTypeString } from '../components/Tanks';
import { TierWeightsRecord } from '../components/TierWeights'; import { TierWeightsRecord } from '../components/TierWeights';
import TitleBar from '../components/TitleBar'; import TitleBar from '../components/TitleBar';
import Wrapper from '../components/Wrapper'; import Wrapper from '../components/Wrapper';
@@ -22,15 +23,25 @@ import {
SupplementaryStats, SupplementaryStats,
} from '../types/accountInfo'; } from '../types/accountInfo';
import { PlayerClanData } from '../types/playerClanData'; import { PlayerClanData } from '../types/playerClanData';
import { PossiblyPromise } from '../types/possiblyPromise';
export type StatType = 'player' | 'tank'; export type StatType = 'player' | 'tank' | 'multi-tank';
export interface MultiTankFilters {
nation: string;
'tank-type': string;
tier: number;
'tree-type': TreeTypeString;
}
export default async function stats<Type extends StatType>( export default async function stats<Type extends StatType>(
type: Type, type: Type,
{ start, end, statsName }: ResolvedPeriod, { start, end, statsName }: ResolvedPeriod,
{ region: server, id }: ResolvedPlayer, { region: server, id }: ResolvedPlayer,
tankId: Type extends 'tank' ? number : null, filters: Type extends 'tank'
? number
: Type extends 'multi-tank'
? Partial<MultiTankFilters>
: null,
naked = false, naked = false,
) { ) {
let nameDiscriminator: string | undefined; let nameDiscriminator: string | undefined;
@@ -48,91 +59,98 @@ export default async function stats<Type extends StatType>(
? `https://wotblitz-gc.gcdn.co/icons/clanEmblems1x/clan-icon-v2-${clan.emblem_set_id}.png` ? `https://wotblitz-gc.gcdn.co/icons/clanEmblems1x/clan-icon-v2-${clan.emblem_set_id}.png`
: undefined; : undefined;
} else if (type === 'tank') { } else if (type === 'tank') {
nameDiscriminator = `(${await resolveTankName(tankId!)})`; nameDiscriminator = `(${await resolveTankName(filters as number)})`;
image = (await tankopedia)[tankId!]?.images.normal; image = (await tankopedia)[filters as number]?.images.normal;
} else {
} }
const { diffed } = await getDiffedTankStats(server, id, start, end); const { diffed, order } = await getDiffedTankStats(server, id, start, end);
const awaitedTankopedia = await tankopedia;
let stats: AllStats | undefined; let stats: AllStats | undefined;
let supplementaryStats: SupplementaryStats; let supplementaryStats: SupplementaryStats;
let tierWeights: TierWeightsRecord; let tierWeights: TierWeightsRecord;
if (type === 'player') { if (type === 'player' || type === 'multi-tank') {
const entries = Object.entries(diffed); const typedFilters = filters as Partial<MultiTankFilters> | null;
stats = sumStats(entries.map(([, stats]) => stats)); const filteredOrder = order.filter(
const battlesOfTanksWithAverages = await entries.reduce< (id) =>
PossiblyPromise<number> typedFilters === null ||
>(async (accumulator, [tankIdString, stats]) => { ((typedFilters.nation === undefined ||
const tankId = parseInt(tankIdString); awaitedTankopedia[id]?.nation === typedFilters.nation) &&
const tankAverage = (await tankAverages)[tankId]; (typedFilters['tank-type'] === undefined ||
awaitedTankopedia[id]?.type === typedFilters['tank-type']) &&
return tankAverage ? (await accumulator) + stats.battles : accumulator; (typedFilters.tier === undefined ||
}, 0); awaitedTankopedia[id]?.tier === typedFilters.tier) &&
const battlesOfTanksWithTankopediaEntry = await entries.reduce< (typedFilters['tree-type'] === undefined ||
PossiblyPromise<number> (typedFilters['tree-type'] === 'collector' &&
>(async (accumulator, [tankIdString, stats]) => { awaitedTankopedia[id]?.is_collectible) ||
const tankId = parseInt(tankIdString); (typedFilters['tree-type'] === 'premium' &&
const tankopediaEntry = (await tankopedia)[tankId]; awaitedTankopedia[id]?.is_premium) ||
(typedFilters['tree-type'] === 'techtree' &&
return tankopediaEntry !awaitedTankopedia[id]?.is_collectible &&
? (await accumulator) + stats.battles !awaitedTankopedia[id]?.is_premium))),
: accumulator; );
}, 0); const awaitedTankAverages = await tankAverages;
stats = sumStats(filteredOrder.map((id) => diffed[id]));
const battlesOfTanksWithAverages = filteredOrder.reduce<number>(
(accumulator, id) =>
awaitedTankAverages[id]
? accumulator + diffed[id].battles
: accumulator,
0,
);
const battlesOfTanksWithTankopediaEntry = filteredOrder.reduce<number>(
(accumulator, id) =>
awaitedTankopedia[id] ? accumulator + diffed[id].battles : accumulator,
0,
);
supplementaryStats = { supplementaryStats = {
WN8: WN8:
(await entries.reduce<PossiblyPromise<number>>( filteredOrder.reduce<number>(
async (accumulator, [tankIdString, stats]) => { (accumulator, id) =>
const tankId = parseInt(tankIdString); awaitedTankAverages[id]
const tankAverage = (await tankAverages)[tankId]; ? accumulator +
calculateWN8(awaitedTankAverages[id].all, diffed[id]) *
return tankAverage diffed[id].battles
? (await accumulator) + : accumulator,
calculateWN8(tankAverage.all, stats) * stats.battles
: accumulator;
},
0, 0,
)) / battlesOfTanksWithAverages, ) / battlesOfTanksWithAverages,
tier: tier:
(await entries.reduce<PossiblyPromise<number>>( filteredOrder.reduce<number>(
async (accumulator, [tankIdString, stats]) => { (accumulator, id) =>
const tankId = parseInt(tankIdString); awaitedTankopedia[id]
const tankopediaEntry = (await tankopedia)[tankId]; ? accumulator + awaitedTankopedia[id]!.tier * diffed[id].battles
: accumulator,
return tankopediaEntry
? (await accumulator) + tankopediaEntry.tier * stats.battles
: accumulator;
},
0, 0,
)) / battlesOfTanksWithTankopediaEntry, ) / battlesOfTanksWithTankopediaEntry,
}; };
tierWeights = await entries.reduce<PossiblyPromise<TierWeightsRecord>>( tierWeights = filteredOrder.reduce<TierWeightsRecord>((accumulator, id) => {
async (accumulator, [tankIdString, stats]) => { const tankopediaEntry = awaitedTankopedia[id];
const tankId = parseInt(tankIdString);
const tankopediaEntry = (await tankopedia)[tankId];
if (!tankopediaEntry) return accumulator; if (!tankopediaEntry) return accumulator;
const tier = tankopediaEntry.tier as Tier; const tier = tankopediaEntry.tier as Tier;
if ((await accumulator)[tier]) { if (accumulator[tier]) {
(await accumulator)[tier]! += stats.battles; accumulator[tier]! += diffed[id].battles;
} else { } else {
(await accumulator)[tier] = stats.battles; accumulator[tier] = diffed[id].battles;
} }
return accumulator; return accumulator;
}, }, {});
{},
);
} else { } else {
stats = diffed[tankId!]; stats = diffed[(filters as number)!];
supplementaryStats = { supplementaryStats = {
WN8: stats WN8: stats
? calculateWN8((await tankAverages)[tankId!].all, diffed[tankId!]) ? calculateWN8(
(await tankAverages)[(filters as number)!].all,
diffed[(filters as number)!],
)
: undefined, : undefined,
tier: (await tankopedia)[tankId!]?.tier, tier: (await tankopedia)[(filters as number)!]?.tier,
}; };
} }