From fcf7d99012183e35b32346675302d9c116ab7fcc Mon Sep 17 00:00:00 2001 From: tresabhi Date: Mon, 29 May 2023 18:15:03 +0000 Subject: [PATCH] feat: `/stats` to `getTankStatsOverTime` --- src/commands/stats.tsx | 127 ++++++++------------ src/core/blitzstars/getBlitzStarsAccount.ts | 25 ---- 2 files changed, 48 insertions(+), 104 deletions(-) delete mode 100644 src/core/blitzstars/getBlitzStarsAccount.ts diff --git a/src/commands/stats.tsx b/src/commands/stats.tsx index 277bf3d91..b517fc53d 100644 --- a/src/commands/stats.tsx +++ b/src/commands/stats.tsx @@ -14,7 +14,8 @@ import { supportBlitzStars } from '../core/actions/supportBlitzStars.js'; import usernameAutocomplete from '../core/autocomplete/username.js'; import getBlitzAccount from '../core/blitz/getBlitzAccount.js'; import getWargamingResponse from '../core/blitz/getWargamingResponse.js'; -import getBlitzStarsAccount from '../core/blitzstars/getBlitzStarsAccount.js'; +import sumStats from '../core/blitz/sumStats.js'; +import getTankStatsOverTime from '../core/blitzstars/getTankStatsOverTime.js'; import cmdName from '../core/interaction/cmdName.js'; import addUsernameOption from '../core/options/addUsernameOption.js'; import { args } from '../core/process/args.js'; @@ -22,19 +23,19 @@ import render from '../core/ui/render.js'; import { CommandRegistry } from '../events/interactionCreate.js'; import { AccountInfo, AllStats } from '../types/accountInfo.js'; import { PlayerClanData } from '../types/playerClanData.js'; -import { BlitzStartsComputedPeriodicStatistics } from '../types/statistics.js'; -export type StatPeriod = 'today' | '30' | '90' | 'career'; +export type StatPeriod = 'today' | '30' | '60' | '90' | 'career'; export const statPeriodNames: Record = { today: "Today's statistics", 30: '30-day statistics', + 60: '60-day statistics', 90: '90-day statistics', career: 'Career statistics', }; export default { inProduction: true, - inDevelopment: false, + inDevelopment: true, inPublic: true, command: new SlashCommandBuilder() @@ -47,6 +48,7 @@ export default { .setChoices( { name: 'Today', value: 'today' satisfies StatPeriod }, { name: '30 Days', value: '30' satisfies StatPeriod }, + { name: '60 Days', value: '60' satisfies StatPeriod }, { name: '90 Days', value: '90' satisfies StatPeriod }, { name: 'Career', value: 'career' satisfies StatPeriod }, ) @@ -59,59 +61,39 @@ export default { const username = interaction.options.getString('username')!; const blitzAccount = await getBlitzAccount(interaction, username); const { id, server } = blitzAccount; - let stats: BlitzStartsComputedPeriodicStatistics; const accountInfo = await getWargamingResponse( `https://api.wotblitz.${server}/wotb/account/info/?application_id=${args['wargaming-application-id']}&account_id=${id}`, ); const clanData = await getWargamingResponse( `https://api.wotblitz.${server}/wotb/clans/accountinfo/?application_id=${args['wargaming-application-id']}&account_id=${id}&extra=clan`, ); - const blitzStarsAccount = await getBlitzStarsAccount(interaction, id); + let stats: AllStats; - if (period === 'today') { - const a1 = blitzStarsAccount.statistics; - const a2 = accountInfo[id].statistics; - const battles = a2.all.battles - a1.all.battles; - - function diff(value: (allStats: AllStats) => number) { - return value(a2.all) - value(a1.all); - } - - stats = { - all: { - battles, - capture_points: diff((a) => a.capture_points), - damage_dealt: diff((a) => a.damage_dealt), - damage_received: diff((a) => a.damage_received), - dropped_capture_points: diff((a) => a.dropped_capture_points), - frags: diff((a) => a.frags), - frags8p: diff((a) => a.frags8p), - hits: diff((a) => a.hits), - losses: diff((a) => a.losses), - max_frags: a2.all.max_frags, - max_xp: a2.all.max_xp, - shots: diff((a) => a.shots), - spotted: diff((a) => a.spotted), - survived_battles: diff((a) => a.survived_battles), - win_and_survived: diff((a) => a.win_and_survived), - wins: diff((a) => a.wins), - xp: diff((a) => a.xp), - }, - avg_tier: -Infinity, - wn7: -Infinity, - wn8: -Infinity, - }; + if (period === 'career') { + stats = accountInfo[id].statistics.all; } else { - stats = - period === 'career' - ? blitzStarsAccount.statistics - : blitzStarsAccount[`period${period}d`]; + const daysAgo = period === 'today' ? 0 : Number(period) - 1; + const periodStart = new Date(); + + periodStart.setDate(periodStart.getDate() - daysAgo); + periodStart.setHours(5, 0, 0, 0); + + stats = sumStats( + Object.entries( + await getTankStatsOverTime( + server, + id, + periodStart.getTime() / 1000, + new Date().getTime() / 1000, + ), + ).map(([, stats]) => stats), + ); } const image = await render( - {stats.all.battles === 0 && } - {stats.all.battles > 0 && ( + {stats.battles === 0 && } + {stats.battles > 0 && ( , - accountId: number, -) { - try { - const response = await fetch( - `https://www.blitzstars.com/api/top/player/${accountId}`, - ); - const parsed = (await response.json()) as PlayerPeriodicStatsCollection; - - if (parsed.statistics) { - return parsed; - } else throw new Error('No data to display'); - } catch (error) { - await interaction.editReply({ - embeds: [notTrackedByBlitzStars], - }); - - throw new Error(`${accountId} not tracked by BlitzStars.`); - } -}