feat: updated full-stats to new subcommands

This commit is contained in:
TresAbhi committed 2023-08-07 22:51:04 +00:00
1 parent b455e3380f
commit 338cec4f18
2 files changed
+104 -70

No files matched your search

+25 -3
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';
@@ -15,7 +16,7 @@ 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 fullStats from '../renderers/fullStats'; import fullStats from '../renderers/fullStats';
import { StatType } from '../renderers/stats'; import { MultiTankFilters, StatType } from '../renderers/stats';
import { AccountInfo } from '../types/accountInfo'; import { AccountInfo } from '../types/accountInfo';
export const fullStatsCommand = new Promise<CommandRegistryRaw>( export const fullStatsCommand = new Promise<CommandRegistryRaw>(
@@ -38,7 +39,11 @@ export const fullStatsCommand = new Promise<CommandRegistryRaw>(
true, true,
) as StatType; ) as StatType;
const player = await resolvePlayerFromCommand(interaction); const player = await resolvePlayerFromCommand(interaction);
const period = resolvePeriodFromCommand(player.region, interaction); const resolvedPeriod = resolvePeriodFromCommand(
player.region,
interaction,
);
const period = interaction.options.getString('period');
const tankIdRaw = interaction.options.getString('tank')!; const tankIdRaw = interaction.options.getString('tank')!;
const tankId = const tankId =
commandGroup === 'tank' ? await resolveTankId(tankIdRaw) : null; commandGroup === 'tank' ? await resolveTankId(tankIdRaw) : null;
@@ -49,6 +54,7 @@ export const fullStatsCommand = new Promise<CommandRegistryRaw>(
tankId, tankId,
start, start,
end, end,
period,
}); });
const { nickname } = ( const { nickname } = (
await getWargamingResponse<AccountInfo>( await getWargamingResponse<AccountInfo>(
@@ -57,7 +63,23 @@ export const fullStatsCommand = new Promise<CommandRegistryRaw>(
)[player.id]; )[player.id];
return [ return [
await fullStats(commandGroup, period, player, tankId), await fullStats(
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(
+79 -67
View File
@@ -20,14 +20,17 @@ import {
SupplementaryStats, SupplementaryStats,
} from '../types/accountInfo'; } from '../types/accountInfo';
import { PlayerClanData } from '../types/playerClanData'; import { PlayerClanData } from '../types/playerClanData';
import { PossiblyPromise } from '../types/possiblyPromise'; import { MultiTankFilters, StatType } from './stats';
import { StatType } from './stats';
export default async function fullStats<Type extends StatType>( export default async function fullStats<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,
) { ) {
let nameDiscriminator: string | undefined; let nameDiscriminator: string | undefined;
let image: string | undefined; let image: string | undefined;
@@ -44,89 +47,98 @@ export default async function fullStats<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);
let stats: AllStats; const awaitedTankopedia = await tankopedia;
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: calculateWN8((await tankAverages)[tankId!].all, diffed[tankId!]), WN8: stats
tier: (await tankopedia)[tankId!]?.tier, ? calculateWN8(
(await tankAverages)[(filters as number)!].all,
diffed[(filters as number)!],
)
: undefined,
tier: (await tankopedia)[(filters as number)!]?.tier,
}; };
} }