feat: custom periods for /stats and /tankstats

This commit is contained in:
TresAbhi committed 2023-06-18 01:56:21 +00:00
1 parent 78424c1535
commit e0c1db2a0b
14 files changed
+125 -49

No files matched your search

-1
View File
@@ -60,7 +60,6 @@ export default {
if (clan === 'SKLLD') {
const tankStatsOverTime = await getTankStatsOverTime(
interaction,
server,
id,
getPeriodicStart('30'),
+1 -1
View File
@@ -73,7 +73,7 @@ export default {
const accountInfo = await getWargamingResponse<AccountInfo>(
`https://api.wotblitz.${server}/wotb/account/info/?application_id=${WARGAMING_APPLICATION_ID}&account_id=${id}`,
);
const tankStats = await getTankStats(interaction, server, id);
const tankStats = await getTankStats(server, id);
const tanks = tankStats
.map((tankData) => tankopedia[tankData.tank_id])
.filter((tank) => tank?.tier === tier);
+39 -12
View File
@@ -14,15 +14,17 @@ import sumStats from '../core/blitz/sumStats.js';
import { tankopedia } from '../core/blitz/tankopedia.js';
import getPeriodNow from '../core/blitzstars/getPeriodNow.js';
import getPeriodicStart from '../core/blitzstars/getPeriodStart.js';
import getPeriodStartFromDaysAgo from '../core/blitzstars/getPeriodStartFromDaysAgo.js';
import getTankStatsOverTime from '../core/blitzstars/getTankStatsOverTime.js';
import { tankAverages } from '../core/blitzstars/tankAverages.js';
import cmdName from '../core/interaction/cmdName.js';
import fullBlitzStarsStats from '../core/interaction/fullBlitzStarsStats.js';
import { supportBlitzStars } from '../core/interaction/supportBlitzStars.js';
import addStatPeriodChoices, {
import addCustomPeriodOption from '../core/options/addCustomPeriodOption.js';
import addPeriodChoices, {
StatPeriod,
statPeriodNames,
} from '../core/options/addStatPeriodChoices.js';
} from '../core/options/addPeriodChoices.js';
import addUsernameOption from '../core/options/addUsernameOption.js';
import { WARGAMING_APPLICATION_ID } from '../core/process/args.js';
import { CommandRegistry } from '../events/interactionCreate.js';
@@ -35,14 +37,25 @@ import { PlayerClanData } from '../types/playerClanData.js';
export default {
inProduction: true,
inDevelopment: false,
inDevelopment: true,
inPublic: true,
command: new SlashCommandBuilder()
.setName(cmdName('stats'))
.setDescription("Gets the user's in-game statistics")
.addStringOption(addStatPeriodChoices)
.addStringOption(addUsernameOption),
.addSubcommand((option) =>
option
.setName('period')
.setDescription('Pick from a predetermined periods')
.addStringOption(addPeriodChoices)
.addStringOption(addUsernameOption),
)
.addSubcommand((option) =>
addCustomPeriodOption(option)
.setName('customperiod')
.setDescription('Custom periods')
.addStringOption(addUsernameOption),
),
async execute(interaction) {
const period = interaction.options.getString('period') as StatPeriod;
@@ -53,12 +66,25 @@ export default {
const clanData = await getWargamingResponse<PlayerClanData>(
`https://api.wotblitz.${server}/wotb/clans/accountinfo/?application_id=${WARGAMING_APPLICATION_ID}&account_id=${id}&extra=clan`,
);
const tankStats = await getTankStats(interaction, server, id);
let stats: AllStats;
let supplementaryStats: SupplementaryStats | undefined;
let hasWN8AccumulatedAtAll = false;
const isCustomPeriod =
interaction.options.getSubcommand() === 'customperiod';
let periodName: string;
const startRaw = interaction.options.getInteger('start')!;
const endRaw = interaction.options.getInteger('end')!;
const start = Math.max(startRaw, endRaw);
const end = Math.min(startRaw, endRaw);
if (isCustomPeriod) {
periodName = `${start} to ${end} days ago`;
} else {
periodName = statPeriodNames[period];
}
if (period === 'career') {
const tankStats = await getTankStats(server, id);
stats = accountInfo[id].statistics.all;
const totalBattles = tankStats.reduce(
(accumulator, { all }) => accumulator + all.battles,
@@ -87,11 +113,12 @@ export default {
};
} else {
const tankStatsOverTime = await getTankStatsOverTime(
interaction,
server,
id,
getPeriodicStart(period),
getPeriodNow(),
isCustomPeriod
? getPeriodStartFromDaysAgo(start)
: getPeriodicStart(period),
isCustomPeriod ? getPeriodStartFromDaysAgo(end) : getPeriodNow(),
);
const entries = Object.entries(tankStatsOverTime);
const totalBattles = entries.reduce(
@@ -141,9 +168,9 @@ export default {
? `https://wotblitz-gc.gcdn.co/icons/clanEmblems1x/clan-icon-v2-${clanData[id]?.clan?.emblem_set_id}.png`
: undefined
}
description={`${
statPeriodNames[period]
} • ${new Date().toDateString()} • ${BLITZ_SERVERS[server]}`}
description={`${periodName} • ${new Date().toDateString()} • ${
BLITZ_SERVERS[server]
}`}
/>
{stats.battles === 0 && <NoData type={NoDataType.BattlesInPeriod} />}
+41 -13
View File
@@ -16,6 +16,7 @@ import resolveTankName from '../core/blitz/resolveTankName.js';
import { tankopedia } from '../core/blitz/tankopedia.js';
import getPeriodNow from '../core/blitzstars/getPeriodNow.js';
import getPeriodicStart from '../core/blitzstars/getPeriodStart.js';
import getPeriodStartFromDaysAgo from '../core/blitzstars/getPeriodStartFromDaysAgo.js';
import getTankStatsOverTime, {
emptyAllStats,
} from '../core/blitzstars/getTankStatsOverTime.js';
@@ -23,10 +24,11 @@ import { tankAverages } from '../core/blitzstars/tankAverages.js';
import cmdName from '../core/interaction/cmdName.js';
import fullBlitzStarsStats from '../core/interaction/fullBlitzStarsStats.js';
import { supportBlitzStars } from '../core/interaction/supportBlitzStars.js';
import addStatPeriodChoices, {
import addCustomPeriodOption from '../core/options/addCustomPeriodOption.js';
import addPeriodChoices, {
StatPeriod,
statPeriodNames,
} from '../core/options/addStatPeriodChoices.js';
} from '../core/options/addPeriodChoices.js';
import addTankChoices from '../core/options/addTankChoices.js';
import addUsernameOption from '../core/options/addUsernameOption.js';
import { WARGAMING_APPLICATION_ID } from '../core/process/args.js';
@@ -35,15 +37,27 @@ import { AccountInfo, AllStats } from '../types/accountInfo.js';
export default {
inProduction: true,
inDevelopment: false,
inDevelopment: true,
inPublic: true,
command: new SlashCommandBuilder()
.setName(cmdName('tankstats'))
.setDescription('Stats for a tank over a period')
.addStringOption(addTankChoices)
.addStringOption(addStatPeriodChoices)
.addStringOption(addUsernameOption),
.addSubcommand((option) =>
option
.setName('period')
.setDescription('Pick from a predetermined periods')
.addStringOption(addTankChoices)
.addStringOption(addPeriodChoices)
.addStringOption(addUsernameOption),
)
.addSubcommand((option) =>
addCustomPeriodOption(option)
.setName('customperiod')
.addStringOption(addTankChoices)
.setDescription('Custom periods')
.addStringOption(addUsernameOption),
),
async execute(interaction) {
const tank = await resolveTankId(
@@ -56,9 +70,22 @@ export default {
const accountInfo = await getWargamingResponse<AccountInfo>(
`https://api.wotblitz.${server}/wotb/account/info/?application_id=${WARGAMING_APPLICATION_ID}&account_id=${id}`,
);
const tankStats = await getTankStats(interaction, server, id);
const tankStats = await getTankStats(server, id);
let stats: AllStats;
const tankId = Number(tank);
const isCustomPeriod =
interaction.options.getSubcommand() === 'customperiod';
let periodName: string;
const startRaw = interaction.options.getInteger('start')!;
const endRaw = interaction.options.getInteger('end')!;
const start = Math.max(startRaw, endRaw);
const end = Math.min(startRaw, endRaw);
if (isCustomPeriod) {
periodName = `${start} to ${end} days ago`;
} else {
periodName = statPeriodNames[period];
}
if (period === 'career') {
stats =
@@ -66,11 +93,12 @@ export default {
emptyAllStats;
} else {
const tankStatsOverTime = await getTankStatsOverTime(
interaction,
server,
id,
getPeriodicStart(period),
getPeriodNow(),
isCustomPeriod
? getPeriodStartFromDaysAgo(start)
: getPeriodicStart(period),
isCustomPeriod ? getPeriodStartFromDaysAgo(end) : getPeriodNow(),
);
stats = tankStatsOverTime[tankId] ?? emptyAllStats;
@@ -82,9 +110,9 @@ export default {
name={accountInfo[id].nickname}
nameDiscriminator={`(${resolveTankName(tankId)})`}
image={tankopedia[tankId].images.normal}
description={`${
statPeriodNames[period]
} • ${new Date().toDateString()} • ${BLITZ_SERVERS[server]}`}
description={`${periodName} • ${new Date().toDateString()} • ${
BLITZ_SERVERS[server]
}`}
/>
{stats.battles === 0 && <NoData type={NoDataType.BattlesInPeriod} />}
+1 -2
View File
@@ -38,7 +38,6 @@ export default {
async execute(interaction) {
const { id, server } = await getBlitzAccount(interaction);
const tankStatsOverTime = await getTankStatsOverTime(
interaction,
server,
id,
last5AM().getTime() / 1000,
@@ -50,7 +49,7 @@ export default {
const clanData = await getWargamingResponse<PlayerClanData>(
`https://api.wotblitz.${server}/wotb/clans/accountinfo/?application_id=${WARGAMING_APPLICATION_ID}&account_id=${id}&extra=clan`,
);
const careerTankStatsRaw = await getTankStats(interaction, server, id);
const careerTankStatsRaw = await getTankStats(server, id);
const careerStats: Record<number, AllStats> = {
0: accountInfo[id].statistics.all,
};