mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-10-23 19:49:50 +02:00
feat: null handling
This commit is contained in:
1 parent
dccfe3d652
commit
4ff5f7cdeb
6 files changed
+41
-19
No files matched your search
+1
-2
@@ -1,4 +1,3 @@
|
|||||||
import { Locale } from 'discord.js';
|
|
||||||
import { chunk, times } from 'lodash';
|
import { chunk, times } from 'lodash';
|
||||||
import { compress } from 'lz4js';
|
import { compress } from 'lz4js';
|
||||||
import { argv } from 'process';
|
import { argv } from 'process';
|
||||||
@@ -64,7 +63,7 @@ for (const batch of batches) {
|
|||||||
let playersDone = 0;
|
let playersDone = 0;
|
||||||
const players = await Promise.all(
|
const players = await Promise.all(
|
||||||
chunk.users.map(async (user) => {
|
chunk.users.map(async (user) => {
|
||||||
const tanks = await getTankStats(region, user.blitz, Locale.EnglishUS);
|
const tanks = await getTankStats(region, user.blitz);
|
||||||
|
|
||||||
console.log(`Player ${++playersDone} of ${chunk.users.length}`);
|
console.log(`Player ${++playersDone} of ${chunk.users.length}`);
|
||||||
|
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ import { translator } from '../../core/localization/translator';
|
|||||||
import calculateWN8 from '../../core/statistics/calculateWN8';
|
import calculateWN8 from '../../core/statistics/calculateWN8';
|
||||||
import { StatFilters, filterStats } from '../../core/statistics/filterStats';
|
import { StatFilters, filterStats } from '../../core/statistics/filterStats';
|
||||||
import getWN8Percentile from '../../core/statistics/getWN8Percentile';
|
import getWN8Percentile from '../../core/statistics/getWN8Percentile';
|
||||||
|
import { UserError } from '../../hooks/userError';
|
||||||
|
|
||||||
const ROWS_PER_PAGE = 8;
|
const ROWS_PER_PAGE = 8;
|
||||||
const MAX_PAGES = 9;
|
const MAX_PAGES = 9;
|
||||||
@@ -37,7 +38,7 @@ export async function renderBreakdown(
|
|||||||
const { filteredOrder } = await filterStats(statsInPeriod, filters);
|
const { filteredOrder } = await filterStats(statsInPeriod, filters);
|
||||||
const accountInfo = await getAccountInfo(region, id);
|
const accountInfo = await getAccountInfo(region, id);
|
||||||
const clanData = await getClanAccountInfo(region, id, ['clan']);
|
const clanData = await getClanAccountInfo(region, id, ['clan']);
|
||||||
const tankStats = await getTankStats(region, id, locale);
|
const tankStats = await getTankStats(region, id);
|
||||||
const filterDescriptions = await filtersToDescription(filters, locale);
|
const filterDescriptions = await filtersToDescription(filters, locale);
|
||||||
const orderedCurrentStats: AllStats[] = [];
|
const orderedCurrentStats: AllStats[] = [];
|
||||||
const orderedCareerStats: AllStats[] = [];
|
const orderedCareerStats: AllStats[] = [];
|
||||||
@@ -45,6 +46,10 @@ export async function renderBreakdown(
|
|||||||
const orderedCareerWN8: (number | undefined)[] = [];
|
const orderedCareerWN8: (number | undefined)[] = [];
|
||||||
const orderedCareerWN8Full: (number | undefined)[] = [];
|
const orderedCareerWN8Full: (number | undefined)[] = [];
|
||||||
|
|
||||||
|
if (tankStats === null) {
|
||||||
|
throw new UserError(t`bot.common.errors.no_tank_stats`);
|
||||||
|
}
|
||||||
|
|
||||||
filteredOrder.forEach((id) => {
|
filteredOrder.forEach((id) => {
|
||||||
const tankAveragesEntry = awaitedTankAverages[id];
|
const tankAveragesEntry = awaitedTankAverages[id];
|
||||||
const current = statsInPeriod.diff[id];
|
const current = statsInPeriod.diff[id];
|
||||||
|
|||||||
@@ -31,11 +31,16 @@ export const ownedTanksCommand = new Promise<CommandRegistry>((resolve) => {
|
|||||||
.addStringOption(addUsernameChoices),
|
.addStringOption(addUsernameChoices),
|
||||||
|
|
||||||
async handler(interaction) {
|
async handler(interaction) {
|
||||||
const { translate } = translator(interaction.locale);
|
const { t, translate } = translator(interaction.locale);
|
||||||
const tier = Number(interaction.options.getString('tier'));
|
const tier = Number(interaction.options.getString('tier'));
|
||||||
const { id, region } = await resolvePlayerFromCommand(interaction);
|
const { id, region } = await resolvePlayerFromCommand(interaction);
|
||||||
const accountInfo = await getAccountInfo(region, id);
|
const accountInfo = await getAccountInfo(region, id);
|
||||||
const tankStats = await getTankStats(region, id, interaction.locale);
|
const tankStats = await getTankStats(region, id);
|
||||||
|
|
||||||
|
if (tankStats === null) {
|
||||||
|
return t`bot.common.errors.no_tank_stats`;
|
||||||
|
}
|
||||||
|
|
||||||
const filteredTanks = (
|
const filteredTanks = (
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
tankStats.map(async (tankData) => ({
|
tankStats.map(async (tankData) => ({
|
||||||
|
|||||||
@@ -126,7 +126,11 @@ export const researchCommand = new Promise<CommandRegistry>((resolve) => {
|
|||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
const tankStats = await getTankStats(region, id, interaction.locale);
|
const tankStats = await getTankStats(region, id);
|
||||||
|
|
||||||
|
if (tankStats === null) {
|
||||||
|
return t`bot.common.errors.no_tank_stats`;
|
||||||
|
}
|
||||||
|
|
||||||
if (tankStats.some(({ tank_id }) => tank_id === targetTankId)) {
|
if (tankStats.some(({ tank_id }) => tank_id === targetTankId)) {
|
||||||
return translate('bot.commands.research.errors.already_researched', [
|
return translate('bot.commands.research.errors.already_researched', [
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import { Locale } from 'discord.js';
|
import { Locale } from 'discord.js';
|
||||||
import { Region } from '../../constants/regions';
|
import { Region } from '../../constants/regions';
|
||||||
|
import { UserError } from '../../hooks/userError';
|
||||||
import {
|
import {
|
||||||
GetHistoriesOptions,
|
GetHistoriesOptions,
|
||||||
Histories,
|
Histories,
|
||||||
@@ -8,6 +9,7 @@ import {
|
|||||||
} from '../../types/histories';
|
} from '../../types/histories';
|
||||||
import getTankStats from '../blitz/getTankStats';
|
import getTankStats from '../blitz/getTankStats';
|
||||||
import { emptyAllStats } from '../blitzstars/getStatsInPeriod/constants';
|
import { emptyAllStats } from '../blitzstars/getStatsInPeriod/constants';
|
||||||
|
import { translator } from '../localization/translator';
|
||||||
|
|
||||||
export interface TankHistory extends History {
|
export interface TankHistory extends History {
|
||||||
tank_id: number;
|
tank_id: number;
|
||||||
@@ -38,6 +40,7 @@ export default async function getTankHistories(
|
|||||||
locale: Locale,
|
locale: Locale,
|
||||||
options?: Partial<GetTankHistoriesOptions>,
|
options?: Partial<GetTankHistoriesOptions>,
|
||||||
) {
|
) {
|
||||||
|
const { t } = translator(locale);
|
||||||
const mergedOptions = { ...getTankHistoriesDefaultOptions, ...options };
|
const mergedOptions = { ...getTankHistoriesDefaultOptions, ...options };
|
||||||
const tankHistoriesResponse = await fetch(
|
const tankHistoriesResponse = await fetch(
|
||||||
`https://www.blitzstars.com/api/tankhistories/for/${id}/`,
|
`https://www.blitzstars.com/api/tankhistories/for/${id}/`,
|
||||||
@@ -78,7 +81,13 @@ export default async function getTankHistories(
|
|||||||
const latestNodes: TankHistories = [];
|
const latestNodes: TankHistories = [];
|
||||||
|
|
||||||
if (mergedOptions.includeLatestHistories) {
|
if (mergedOptions.includeLatestHistories) {
|
||||||
(await getTankStats(server, id, locale)).forEach((tankHistory) => {
|
const tankStats = await getTankStats(server, id);
|
||||||
|
|
||||||
|
if (tankStats === null) {
|
||||||
|
throw new UserError(t`bot.common.errors.no_tank_stats`);
|
||||||
|
}
|
||||||
|
|
||||||
|
tankStats.forEach((tankHistory) => {
|
||||||
if (
|
if (
|
||||||
tankHistory.all.battles > (lastBattles[tankHistory.tank_id] ?? 0) &&
|
tankHistory.all.battles > (lastBattles[tankHistory.tank_id] ?? 0) &&
|
||||||
(mergedOptions.tankId === undefined ||
|
(mergedOptions.tankId === undefined ||
|
||||||
|
|||||||
+12
-12
@@ -62,6 +62,18 @@
|
|||||||
|
|
||||||
"bot": {
|
"bot": {
|
||||||
"common": {
|
"common": {
|
||||||
|
"errors": {
|
||||||
|
"clan_not_found": "# Could not find clan\nCouldn't find \"%s\". Try selecting a clan from the search result.",
|
||||||
|
"player_not_found": "# Could not find user\nI couldn't find user \"%s\". Try picking an user from the search result, typing in a valid username, or using the `/link` command.",
|
||||||
|
"player_not_linked": "# I don't know who you are\nUse the `/link` command to get started.",
|
||||||
|
"tank_name_not_found": "# Tank not found\nCould not find tank by the name \"%s\".",
|
||||||
|
"tank_id_not_found": "# Tank not found\nCould not find tank by the ID \"%s\". Try typing the name of the tank instead of a number.",
|
||||||
|
"no_tank_stats": "# No tank stats available\nThis player doesn't have any stats available. This may not be the player you are looking for.",
|
||||||
|
"get_help": "Help Discord server",
|
||||||
|
"uncaught_error": "# Blitzkrieg ran into an error!\nWe're so sorry about this. Feel free to [join the official Discord server](https://discord.gg/nDt7AjGJQH) to get help.",
|
||||||
|
"preview": "# This command is in preview\n\nYou can only access this command in [the official Discord server](https://discord.gg/nDt7AjGJQH). The command will be generally available soon."
|
||||||
|
},
|
||||||
|
|
||||||
"wn8": {
|
"wn8": {
|
||||||
"$": "WN8",
|
"$": "WN8",
|
||||||
"very_bad": "Very bad",
|
"very_bad": "Very bad",
|
||||||
@@ -183,18 +195,6 @@
|
|||||||
"refresh": "Refresh"
|
"refresh": "Refresh"
|
||||||
},
|
},
|
||||||
|
|
||||||
"errors": {
|
|
||||||
"clan_not_found": "# Could not find clan\nCouldn't find \"%s\". Try selecting a clan from the search result.",
|
|
||||||
"player_not_found": "# Could not find user\nI couldn't find user \"%s\". Try picking an user from the search result, typing in a valid username, or using the `/link` command.",
|
|
||||||
"player_not_linked": "# I don't know who you are\nUse the `/link` command to get started.",
|
|
||||||
"tank_name_not_found": "# Tank not found\nCould not find tank by the name \"%s\".",
|
|
||||||
"tank_id_not_found": "# Tank not found\nCould not find tank by the ID \"%s\". Try typing the name of the tank instead of a number.",
|
|
||||||
"no_tank_stats": "# No tank stats available\nThis player doesn't have any stats available. This may not be the player you are looking for.",
|
|
||||||
"get_help": "Help Discord server",
|
|
||||||
"uncaught_error": "# Blitzkrieg ran into an error!\nWe're so sorry about this. Feel free to [join the official Discord server](https://discord.gg/nDt7AjGJQH) to get help.",
|
|
||||||
"preview": "# This command is in preview\n\nYou can only access this command in [the official Discord server](https://discord.gg/nDt7AjGJQH). The command will be generally available soon."
|
|
||||||
},
|
|
||||||
|
|
||||||
"hero_stats": {
|
"hero_stats": {
|
||||||
"battles": "Battles",
|
"battles": "Battles",
|
||||||
"winrate": "Winrate",
|
"winrate": "Winrate",
|
||||||
|
|||||||
Reference in new issue
Block a user