fix: all commands now work with a lack of tankopedia entries

This commit is contained in:
TresAbhi committed 2023-07-07 01:08:00 +00:00
1 parent f907c79d68
commit ca208e44a1
12 files changed
+59 -26

No files matched your search

+17 -2
View File
@@ -71,12 +71,27 @@ export const eligibleCommand: CommandRegistry = {
(await entries.reduce<PossiblyPromise<number>>( (await entries.reduce<PossiblyPromise<number>>(
async (accumulator, [tankIdString, stats]) => { async (accumulator, [tankIdString, stats]) => {
const tankId = parseInt(tankIdString); const tankId = parseInt(tankIdString);
const tankTier = (await tankopedia)[tankId].tier; const tankopediaEntry = (await tankopedia)[tankId];
if (!tankopediaEntry) return accumulator;
const tankTier = tankopediaEntry.tier;
return (await accumulator) + tankTier * stats.battles; return (await accumulator) + tankTier * stats.battles;
}, },
0, 0,
)) / stats.battles; )) /
(await entries.reduce<PossiblyPromise<number>>(
async (accumulator, [tankIdString, stats]) => {
const tankId = parseInt(tankIdString);
const tankopediaEntry = (await tankopedia)[tankId];
if (!tankopediaEntry) return accumulator;
return stats.battles;
},
0,
));
const WN8 = const WN8 =
(await entries.reduce<PossiblyPromise<number>>( (await entries.reduce<PossiblyPromise<number>>(
async (accumulator, [tankIdString, stats]) => { async (accumulator, [tankIdString, stats]) => {
+15 -9
View File
@@ -7,6 +7,7 @@ import Wrapper from '../components/Wrapper.js';
import { BLITZ_SERVERS } from '../constants/servers.js'; import { BLITZ_SERVERS } from '../constants/servers.js';
import getTankStats from '../core/blitz/getTankStats.js'; import getTankStats from '../core/blitz/getTankStats.js';
import getWargamingResponse from '../core/blitz/getWargamingResponse.js'; import getWargamingResponse from '../core/blitz/getWargamingResponse.js';
import resolveTankName from '../core/blitz/resolveTankName.js';
import { import {
TIER_ROMAN_NUMERALS, TIER_ROMAN_NUMERALS,
Tier, Tier,
@@ -79,7 +80,10 @@ export const ownedTanksCommand: CommandRegistry = {
const tankStats = await getTankStats(server, id); const tankStats = await getTankStats(server, id);
const tanks = ( const tanks = (
await Promise.all( await Promise.all(
tankStats.map(async (tankData) => (await tankopedia)[tankData.tank_id]), tankStats.map(async (tankData) => ({
...(await tankopedia)[tankData.tank_id],
tank_id: tankData.tank_id,
})),
) )
).filter((tank) => tank?.tier === tier); ).filter((tank) => tank?.tier === tier);
const clanData = await getWargamingResponse<PlayerClanData>( const clanData = await getWargamingResponse<PlayerClanData>(
@@ -106,14 +110,16 @@ export const ownedTanksCommand: CommandRegistry = {
{tanks.length > 0 && ( {tanks.length > 0 && (
<Tanks.Root> <Tanks.Root>
{tanks.map((tank) => ( {await Promise.all(
<Tanks.Item tanks.map(async (tank) => (
key={tank.tank_id} <Tanks.Item
name={tank.name} key={tank.tank_id}
type={tank.type} name={await resolveTankName(tank.tank_id)}
icon={tank.images.normal} type={tank.type}
/> icon={tank.images?.normal}
))} />
)),
)}
</Tanks.Root> </Tanks.Root>
)} )}
+3 -1
View File
@@ -69,7 +69,9 @@ export default function AllStatsOverview({
> >
<WN8SurroundingStat <WN8SurroundingStat
label="Tier" label="Tier"
value={supplementaryStats.tier.toFixed(2)} value={
supplementaryStats.tier ? supplementaryStats.tier.toFixed(2) : '--'
}
/> />
<WN8SurroundingStat <WN8SurroundingStat
label="Survival" label="Survival"
+4 -1
View File
@@ -40,7 +40,10 @@ export default function GenericAllStats({
['Wins', stats.wins], ['Wins', stats.wins],
['Losses', stats.losses], ['Losses', stats.losses],
['Average damage', (stats.damage_dealt / stats.battles).toFixed(0)], ['Average damage', (stats.damage_dealt / stats.battles).toFixed(0)],
['Average tier', supplementaryStats?.tier.toFixed(2)], [
'Average tier',
supplementaryStats?.tier ? supplementaryStats?.tier.toFixed(2) : '--',
],
['Average XP', (stats.xp / stats.battles).toFixed(0)], ['Average XP', (stats.xp / stats.battles).toFixed(0)],
['Average shots', (stats.shots / stats.battles).toFixed(2)], ['Average shots', (stats.shots / stats.battles).toFixed(2)],
['Average hits', (stats.hits / stats.battles).toFixed(2)], ['Average hits', (stats.hits / stats.battles).toFixed(2)],
+5 -2
View File
@@ -2,7 +2,7 @@ import { TANK_ICONS, TankType } from '../../../core/blitz/tankopedia.js';
export interface ItemProps { export interface ItemProps {
icon?: string; icon?: string;
type: TankType; type?: TankType;
name: string; name: string;
} }
@@ -23,7 +23,10 @@ export function Item({ icon, type, name }: ItemProps) {
{icon && <img src={icon} style={{ flex: 1, objectFit: 'contain' }} />} {icon && <img src={icon} style={{ flex: 1, objectFit: 'contain' }} />}
<div style={{ display: 'flex', margin: 'auto', gap: 4 }}> <div style={{ display: 'flex', margin: 'auto', gap: 4 }}>
<img src={TANK_ICONS[type]} style={{ width: 14, height: 14 }} /> {type && (
<img src={TANK_ICONS[type]} style={{ width: 14, height: 14 }} />
)}
<span <span
style={{ fontSize: 16, textAlign: 'center', whiteSpace: 'nowrap' }} style={{ fontSize: 16, textAlign: 'center', whiteSpace: 'nowrap' }}
> >
+1 -1
View File
@@ -1,6 +1,6 @@
import { tankopedia } from './tankopedia.js'; import { tankopedia } from './tankopedia.js';
export default async function resolveTankName(tankId: number) { export default async function resolveTankName(tankId: number) {
const tankopediaName = (await tankopedia)[tankId].name; const tankopediaName = (await tankopedia)[tankId]?.name;
return tankopediaName ? tankopediaName : `Unknown Tank ${tankId}`; return tankopediaName ? tankopediaName : `Unknown Tank ${tankId}`;
} }
+1 -1
View File
@@ -13,7 +13,7 @@ export interface TankopediaEntry {
} }
export interface Tankopedia { export interface Tankopedia {
[id: number]: TankopediaEntry; [id: number]: TankopediaEntry | undefined;
} }
console.log('Caching tankopedia...'); console.log('Caching tankopedia...');
+1 -1
View File
@@ -39,7 +39,7 @@ export default async function evolution<Type extends StatType>(
: undefined; : undefined;
} else { } else {
nameDiscriminator = `(${await resolveTankName(tankId!)})`; nameDiscriminator = `(${await resolveTankName(tankId!)})`;
image = (await tankopedia)[tankId!].images.normal; image = (await tankopedia)[tankId!]?.images.normal;
} }
let histories: Histories; let histories: Histories;
+7 -3
View File
@@ -49,7 +49,7 @@ export default async function stats<Type extends StatType>(
: undefined; : undefined;
} else if (type === 'tank') { } else if (type === 'tank') {
nameDiscriminator = `(${await resolveTankName(tankId!)})`; nameDiscriminator = `(${await resolveTankName(tankId!)})`;
image = (await tankopedia)[tankId!].images.normal; image = (await tankopedia)[tankId!]?.images.normal;
} }
const tankStats = await getTankStatsDiffed(server, id, start, end); const tankStats = await getTankStatsDiffed(server, id, start, end);
@@ -109,7 +109,11 @@ export default async function stats<Type extends StatType>(
tierWeights = await entries.reduce<PossiblyPromise<TierWeightsRecord>>( tierWeights = await entries.reduce<PossiblyPromise<TierWeightsRecord>>(
async (accumulator, [tankIdString, stats]) => { async (accumulator, [tankIdString, stats]) => {
const tankId = parseInt(tankIdString); const tankId = parseInt(tankIdString);
const tier = (await tankopedia)[tankId].tier as Tier; const tankopediaEntry = (await tankopedia)[tankId];
if (!tankopediaEntry) return accumulator;
const tier = tankopediaEntry.tier as Tier;
if ((await accumulator)[tier]) { if ((await accumulator)[tier]) {
(await accumulator)[tier]! += stats.battles; (await accumulator)[tier]! += stats.battles;
@@ -128,7 +132,7 @@ export default async function stats<Type extends StatType>(
WN8: stats WN8: stats
? calculateWN8((await tankAverages)[tankId!].all, tankStats[tankId!]) ? calculateWN8((await tankAverages)[tankId!].all, tankStats[tankId!])
: undefined, : undefined,
tier: (await tankopedia)[tankId!].tier, tier: (await tankopedia)[tankId!]?.tier,
}; };
} }
+3 -3
View File
@@ -47,7 +47,7 @@ export default async function statsfull<Type extends StatType>(
: undefined; : undefined;
} else if (type === 'tank') { } else if (type === 'tank') {
nameDiscriminator = `(${await resolveTankName(tankId!)})`; nameDiscriminator = `(${await resolveTankName(tankId!)})`;
image = (await tankopedia)[tankId!].images.normal; image = (await tankopedia)[tankId!]?.images.normal;
} }
const tankStats = await getTankStatsDiffed(server, id, start, end); const tankStats = await getTankStatsDiffed(server, id, start, end);
@@ -111,7 +111,7 @@ export default async function statsfull<Type extends StatType>(
if (!tankopediaEntry) return accumulator; if (!tankopediaEntry) return accumulator;
const tier = (await tankopedia)[tankId].tier as Tier; const tier = tankopediaEntry.tier as Tier;
if ((await accumulator)[tier]) { if ((await accumulator)[tier]) {
(await accumulator)[tier]! += stats.battles; (await accumulator)[tier]! += stats.battles;
@@ -128,7 +128,7 @@ export default async function statsfull<Type extends StatType>(
supplementaryStats = { supplementaryStats = {
WN8: calculateWN8((await tankAverages)[tankId!].all, tankStats[tankId!]), WN8: calculateWN8((await tankAverages)[tankId!].all, tankStats[tankId!]),
tier: (await tankopedia)[tankId!].tier, tier: (await tankopedia)[tankId!]?.tier,
}; };
} }
+1 -1
View File
@@ -132,7 +132,7 @@ export default async function today({ server, id }: ResolvedPlayer) {
icon={ icon={
tankId === 0 tankId === 0
? undefined ? undefined
: (await tankopedia)[tankIdString as unknown as number].images : (await tankopedia)[tankIdString as unknown as number]?.images
.normal .normal
} }
/> />
+1 -1
View File
@@ -21,7 +21,7 @@ export interface AllStats {
export interface SupplementaryStats { export interface SupplementaryStats {
// if undefined, WN8 is not available for tanks without averages // if undefined, WN8 is not available for tanks without averages
WN8?: number; WN8?: number;
tier: number; tier?: number;
} }
export interface ClanStats { export interface ClanStats {