fix: tank stats being null for players without battles

This commit is contained in:
TresAbhi committed 2023-06-03 01:19:52 +00:00
1 parent 99135f25c7
commit 96ae6b715f
6 files changed
+48 -22

No files matched your search

+31
View File
@@ -0,0 +1,31 @@
import { CacheType, ChatInputCommandInteraction } from 'discord.js';
import { BLITZ_SERVERS, BlitzServer } from '../../constants/servers.js';
import { TanksStats } from '../../types/tanksStats.js';
import errorEmbed from '../interaction/errorEmbed.js';
import { args } from '../process/args.js';
import getWargamingResponse from './getWargamingResponse.js';
export default async function getTankStats(
interaction: ChatInputCommandInteraction<CacheType>,
server: BlitzServer,
id: number,
) {
const tankStats = await getWargamingResponse<TanksStats>(
`https://api.wotblitz.${server}/wotb/tanks/stats/?application_id=${args['wargaming-application-id']}&account_id=${id}`,
);
if (tankStats[id] === null) {
interaction.editReply({
embeds: [
errorEmbed(
'No tank stats available',
`Wargaming says there is no tank stats for this account. This account may not have any battles/tanks or exist in the ${BLITZ_SERVERS[server]} server.`,
),
],
});
throw new Error(`No tank stats available for ${server}/${id}`);
}
return tankStats[id];
}
+5 -7
View File
@@ -1,8 +1,7 @@
import { CacheType, ChatInputCommandInteraction } from 'discord.js';
import { BlitzServer } from '../../constants/servers.js';
import { AllStats } from '../../types/accountInfo.js';
import { TanksStats } from '../../types/tanksStats.js';
import getWargamingResponse from '../blitz/getWargamingResponse.js';
import { args } from '../process/args.js';
import getTankStats from '../blitz/getTankStats.js';
export interface TankHistoryNode {
all: AllStats;
@@ -45,19 +44,18 @@ export const emptyTankHistoryNode: TankHistoryNode = {
export type TankHistory = TankHistoryNode[];
export default async function getTankStatsOverTime(
interaction: ChatInputCommandInteraction<CacheType>,
server: BlitzServer,
id: number,
start: number,
end: number,
) {
const latestTankStatsRaw = await getWargamingResponse<TanksStats>(
`https://api.wotblitz.${server}/wotb/tanks/stats/?application_id=${args['wargaming-application-id']}&account_id=${id}`,
);
const latestTankStatsRaw = await getTankStats(interaction, server, id);
const tankHistoriesResponse = await fetch(
`https://www.blitzstars.com/api/tankhistories/for/${id}/`,
);
const tankHistories = (await tankHistoriesResponse.json()) as TankHistory;
const history = [...tankHistories, ...latestTankStatsRaw[id]];
const history = [...tankHistories, ...latestTankStatsRaw];
// sort them even though most won't be used
const tankSortedHistory: Record<number, TankHistory> = {};