diff --git a/scripts/buildTankopedia.ts b/scripts/buildTankopedia.ts index d62c1cc24..ecbb9dccc 100644 --- a/scripts/buildTankopedia.ts +++ b/scripts/buildTankopedia.ts @@ -5,8 +5,8 @@ import { load } from 'js-yaml'; import { argv, env } from 'process'; import { ElementCompact, xml2js } from 'xml-js'; import { parse } from 'yaml'; -import { TreeTypeString } from '../src/components/Tanks'; import { NATION_IDS } from '../src/constants/nations'; +import { BlitzkriegTankopedia, Tier } from '../src/core/blitzkrieg/tankopedia'; import commitMultipleFiles, { FileChange } from './commitMultipleFiles'; const LANGUAGE = 'en'; @@ -45,21 +45,11 @@ if (!noRewrite) { console.log('Creating new tankopedia folder'); await mkdir('dist/tankopedia'); -interface BlitzkriegTankopedia { - id: number; - nation: string; - name: string; - name_short?: string; - tree_type: TreeTypeString; - tier: number; - type: string; -} - const strings = await readFile( `${TEMP}/${directoriesOfInterest.strings}/${LANGUAGE}.yaml`, 'utf-8', ).then((data) => load(data) as Record); -const tankopedia: Record = {}; +const tankopedia: BlitzkriegTankopedia = {}; const tankIds: number[] = []; const images: Record = {}; const nations = await readdir( @@ -89,7 +79,7 @@ Promise.all( const isCollector = vehicleData.sellPrice ? 'gold' in vehicleData.sellPrice : false; - const tier = parseInt(vehicleData.level._text); + const tier = parseInt(vehicleData.level._text) as Tier; const tags = vehicleData.tags._text.split(' '); const type = tags[0]; @@ -174,7 +164,7 @@ Promise.all( 'blitzkrieg-assets', 'main', new Date().toString(), - iconsChange, + [tankopediaChange, ...iconsChange], true, ); } else { diff --git a/src/app/api/tankopedia/route.ts b/src/app/api/tankopedia/route.ts index 1ee08729c..370ba2f7d 100644 --- a/src/app/api/tankopedia/route.ts +++ b/src/app/api/tankopedia/route.ts @@ -1,5 +1,5 @@ import { NextResponse } from 'next/server'; -import { tankopedia } from '../../../core/blitzstars/tankopedia'; +import { tankopedia } from '../../../core/blitzkrieg/tankopedia'; export async function GET() { return NextResponse.json(await tankopedia); diff --git a/src/app/embeds/session/page.tsx b/src/app/embeds/session/page.tsx index 0b75c1617..0b4cc37f9 100644 --- a/src/app/embeds/session/page.tsx +++ b/src/app/embeds/session/page.tsx @@ -6,11 +6,11 @@ import * as Breakdown from '../../../components/Breakdown'; import { TreeTypeEnum } from '../../../components/Tanks'; import { WARGAMING_APPLICATION_ID } from '../../../constants/wargamingApplicationID'; import fetchBlitz from '../../../core/blitz/fetchBlitz'; -import { tankAverages } from '../../../core/blitzstars/tankAverages'; import { - TankopediaEntry, + BlitzkriegTankopediaEntry, tankopedia, -} from '../../../core/blitzstars/tankopedia'; +} from '../../../core/blitzkrieg/tankopedia'; +import { tankAverages } from '../../../core/blitzstars/tankAverages'; import calculateWN8 from '../../../core/statistics/calculateWN8'; import { deltaTankStats } from '../../../core/statistics/deltaTankStats'; import getWN8Percentile from '../../../core/statistics/getWN8Percentile'; @@ -30,7 +30,7 @@ export default function SessionPage() { list: { stats: IndividualTankStats; - tankopedia?: TankopediaEntry; + tankopedia?: BlitzkriegTankopediaEntry; career: IndividualTankStats; currentWN8?: number; careerWN8?: number; @@ -299,9 +299,10 @@ export default function SessionPage() { type="tank" tankType={tankopedia?.type} treeType={(() => { - if (tankopedia?.is_collectible) + if (tankopedia?.tree_type === 'collector') return TreeTypeEnum.Collector; - if (tankopedia?.is_premium) return TreeTypeEnum.Premium; + if (tankopedia?.tree_type === 'premium') + return TreeTypeEnum.Premium; })()} stats={session.customColumns.map((customColumn) => { const rowStat = rowStats[customColumn.display]; diff --git a/src/commands/breakdown.tsx b/src/commands/breakdown.tsx index bf17273f8..694eb951e 100644 --- a/src/commands/breakdown.tsx +++ b/src/commands/breakdown.tsx @@ -10,10 +10,10 @@ import getTankStats from '../core/blitz/getTankStats'; import getTreeType from '../core/blitz/getTreeType'; import resolveTankName from '../core/blitz/resolveTankName'; import { filtersToDescription } from '../core/blitzkrieg/filtersToDescription'; +import { tankopedia } from '../core/blitzkrieg/tankopedia'; import { getBlitzStarsLinkButton } from '../core/blitzstars/getBlitzStarsLinkButton'; import getStatsInPeriod from '../core/blitzstars/getStatsInPeriod'; import { tankAverages } from '../core/blitzstars/tankAverages'; -import { tankopedia } from '../core/blitzstars/tankopedia'; import addFilterOptions from '../core/discord/addFilterOptions'; import addUsernameChoices from '../core/discord/addUsernameChoices'; import autocompleteTanks from '../core/discord/autocompleteTanks'; diff --git a/src/commands/eligible.ts b/src/commands/eligible.ts index 9dcf864a2..bb141970a 100644 --- a/src/commands/eligible.ts +++ b/src/commands/eligible.ts @@ -2,9 +2,9 @@ import { SlashCommandBuilder } from 'discord.js'; import { getAccountInfo } from '../core/blitz/getAccountInfo'; import getPeriodNow from '../core/blitzkrieg/getPeriodNow'; import getTimeDaysAgo from '../core/blitzkrieg/getTimeDaysAgo'; +import { tankopedia } from '../core/blitzkrieg/tankopedia'; import getStatsInPeriod from '../core/blitzstars/getStatsInPeriod'; import { tankAverages } from '../core/blitzstars/tankAverages'; -import { tankopedia } from '../core/blitzstars/tankopedia'; import addUsernameChoices from '../core/discord/addUsernameChoices'; import embedNegative from '../core/discord/embedNegative'; import embedPositive from '../core/discord/embedPositive'; diff --git a/src/commands/ownedTanks.tsx b/src/commands/ownedTanks.tsx index 242429804..007355a03 100644 --- a/src/commands/ownedTanks.tsx +++ b/src/commands/ownedTanks.tsx @@ -10,12 +10,13 @@ import getTreeType from '../core/blitz/getTreeType'; import resolveTankName from '../core/blitz/resolveTankName'; import { tankopediaInfo } from '../core/blitz/tankopediaInfo'; import { emblemIdToURL } from '../core/blitzkrieg/emblemIdToURL'; +import { tankIcon } from '../core/blitzkrieg/tankIcon'; import { + BlitzkriegTankopediaEntry, TIER_ROMAN_NUMERALS, - TankopediaEntry, Tier, tankopedia, -} from '../core/blitzstars/tankopedia'; +} from '../core/blitzkrieg/tankopedia'; import addTierChoices from '../core/discord/addTierChoices'; import addUsernameChoices from '../core/discord/addUsernameChoices'; import autocompleteUsername from '../core/discord/autocompleteUsername'; @@ -42,12 +43,12 @@ export const ownedTanksCommand: CommandRegistry = { await Promise.all( tankStats.map(async (tankData) => ({ tankopedia: (await tankopedia)[tankData.tank_id]!, - tank_id: tankData.tank_id, + id: tankData.tank_id, })), ) ).filter((tank) => tank.tankopedia?.tier === tier); const clanAccountInfo = await getClanAccountInfo(server, id, ['clan']); - const groupedTanks: Record = {}; + const groupedTanks: Record = {}; const nations: string[] = []; filteredTanks.forEach((tank) => { @@ -94,11 +95,11 @@ export const ownedTanksCommand: CommandRegistry = { {await Promise.all( leftColumn.map(async (tank) => ( )), )} @@ -107,11 +108,11 @@ export const ownedTanksCommand: CommandRegistry = { {await Promise.all( rightColumn.map(async (tank) => ( )), )} diff --git a/src/commands/searchTanks.ts b/src/commands/searchTanks.ts index 0429c5e56..5eff8444f 100644 --- a/src/commands/searchTanks.ts +++ b/src/commands/searchTanks.ts @@ -1,7 +1,7 @@ import { SlashCommandBuilder } from 'discord.js'; import { go } from 'fuzzysort'; import markdownEscape from 'markdown-escape'; -import { TANK_NAMES_DIACRITICS } from '../core/blitzstars/tankopedia'; +import { TANK_NAMES_DIACRITICS } from '../core/blitzkrieg/tankopedia'; import addTankChoices from '../core/discord/addTankChoices'; import embedInfo from '../core/discord/embedInfo'; import { CommandRegistry } from '../events/interactionCreate'; diff --git a/src/components/Tanks/components/Item.tsx b/src/components/Tanks/components/Item.tsx index d9316cbd2..89a60db98 100644 --- a/src/components/Tanks/components/Item.tsx +++ b/src/components/Tanks/components/Item.tsx @@ -2,7 +2,7 @@ import { TANK_ICONS, TANK_ICONS_COLLECTOR, TANK_ICONS_PREMIUM, -} from '../../../core/blitzstars/tankopedia'; +} from '../../../core/blitzkrieg/tankopedia'; import { theme } from '../../../stitches.config'; export enum TreeTypeEnum { diff --git a/src/components/TierWeights.tsx b/src/components/TierWeights.tsx index 61951cb41..355edbce6 100644 --- a/src/components/TierWeights.tsx +++ b/src/components/TierWeights.tsx @@ -1,4 +1,4 @@ -import { Tier } from '../core/blitzstars/tankopedia'; +import { Tier } from '../core/blitzkrieg/tankopedia'; import { theme } from '../stitches.config'; export type TierWeightsRecord = Partial>; diff --git a/src/core/blitz/getTreeType.ts b/src/core/blitz/getTreeType.ts index 01db48e26..6f784129d 100644 --- a/src/core/blitz/getTreeType.ts +++ b/src/core/blitz/getTreeType.ts @@ -1,11 +1,11 @@ import { TreeTypeEnum } from '../../components/Tanks'; -import { tankopedia } from '../blitzstars/tankopedia'; +import { tankopedia } from '../blitzkrieg/tankopedia'; export default async function getTreeType(id: number) { const entry = (await tankopedia)[id]; - if (entry?.is_collectible) return TreeTypeEnum.Collector; - if (entry?.is_premium) return TreeTypeEnum.Premium; + if (entry.tree_type === 'collector') return TreeTypeEnum.Collector; + if (entry.tree_type === 'premium') return TreeTypeEnum.Premium; return TreeTypeEnum.TechTree; } diff --git a/src/core/blitz/resolveTankId.ts b/src/core/blitz/resolveTankId.ts index a3748942f..3d9e9a18b 100644 --- a/src/core/blitz/resolveTankId.ts +++ b/src/core/blitz/resolveTankId.ts @@ -1,6 +1,6 @@ import { go } from 'fuzzysort'; +import { TANKS, tankopedia } from '../blitzkrieg/tankopedia'; import { UserError } from '../blitzkrieg/userError'; -import { TANKS, tankopedia } from '../blitzstars/tankopedia'; export default async function resolveTankId(tank: string | number) { const number = typeof tank === 'string' ? parseInt(tank) : tank; @@ -16,7 +16,7 @@ export default async function resolveTankId(tank: string | number) { cause: `Could not find tank by the name "${tank}".`, }); } else { - return searchResult[0].obj.tank_id; + return searchResult[0].obj.id; } } else { if ((await tankopedia)[number]) { diff --git a/src/core/blitz/resolveTankName.ts b/src/core/blitz/resolveTankName.ts index 7954de496..8dfc2ac28 100644 --- a/src/core/blitz/resolveTankName.ts +++ b/src/core/blitz/resolveTankName.ts @@ -1,4 +1,4 @@ -import { tankopedia } from '../blitzstars/tankopedia'; +import { tankopedia } from '../blitzkrieg/tankopedia'; export default async function resolveTankName(tankId: number) { const tankopediaName = (await tankopedia)[tankId]?.name; diff --git a/src/core/blitzkrieg/asset.ts b/src/core/blitzkrieg/asset.ts new file mode 100644 index 000000000..b659a17e4 --- /dev/null +++ b/src/core/blitzkrieg/asset.ts @@ -0,0 +1,3 @@ +export function asset(path: string) { + return `https://raw.githubusercontent.com/tresabhi/blitzkrieg-assets/main/${path}`; +} diff --git a/src/core/blitzkrieg/filtersToDescription.ts b/src/core/blitzkrieg/filtersToDescription.ts index 8393ac294..32c5f8ec5 100644 --- a/src/core/blitzkrieg/filtersToDescription.ts +++ b/src/core/blitzkrieg/filtersToDescription.ts @@ -1,8 +1,8 @@ import { TREE_TYPE_NAMES } from '../../components/Tanks'; import resolveTankName from '../blitz/resolveTankName'; import { tankopediaInfo } from '../blitz/tankopediaInfo'; -import { TIER_ROMAN_NUMERALS, Tier } from '../blitzstars/tankopedia'; import { StatFilters } from '../statistics/filterStats'; +import { TIER_ROMAN_NUMERALS, Tier } from './tankopedia'; export async function filtersToDescription({ nation, diff --git a/src/core/blitzkrieg/tankIcon.ts b/src/core/blitzkrieg/tankIcon.ts new file mode 100644 index 000000000..8ca97d74f --- /dev/null +++ b/src/core/blitzkrieg/tankIcon.ts @@ -0,0 +1,5 @@ +import { asset } from './asset'; + +export function tankIcon(id: number, size: 'big' | 'small' = 'big') { + return asset(`icons/${size}/${id}.webp`); +} diff --git a/src/core/blitzstars/tankopedia.ts b/src/core/blitzkrieg/tankopedia.ts similarity index 72% rename from src/core/blitzstars/tankopedia.ts rename to src/core/blitzkrieg/tankopedia.ts index faf794f6c..d3c5f52ae 100644 --- a/src/core/blitzstars/tankopedia.ts +++ b/src/core/blitzkrieg/tankopedia.ts @@ -1,5 +1,6 @@ import { deburr } from 'lodash'; -import { context } from '../blitzkrieg/context'; +import { TreeTypeString } from '../../components/Tanks'; +import { asset } from './asset'; export interface TankopediaEntry { name: string; @@ -17,30 +18,36 @@ export interface Tankopedia { [id: number]: TankopediaEntry | undefined; } -export const tankopedia = fetch( - context === 'website' - ? '/api/tankopedia' - : 'https://www.blitzstars.com/bs-tankopedia.json', -) - .then(async (response) => response.json()) - .then((wrapperTankopedia) => - context === 'server' - ? (wrapperTankopedia as { data: Tankopedia }).data - : (wrapperTankopedia as Tankopedia), - ); -const entries = new Promise(async (resolve) => { +export interface BlitzkriegTankopediaEntry { + id: number; + nation: string; + name: string; + name_short?: string; + tree_type: TreeTypeString; + tier: Tier; + type: string; +} + +export type BlitzkriegTankopedia = Record; + +export const tankopedia = fetch(asset('tankopedia.json')).then( + async (response) => response.json() as Promise, +); +const entries = new Promise(async (resolve) => { resolve(Object.entries(await tankopedia).map(([, entry]) => entry)); }); -export const TANKS = new Promise(async (resolve) => { - resolve((await entries).map((entry) => entry)); -}); +export const TANKS = new Promise( + async (resolve) => { + resolve((await entries).map((entry) => entry)); + }, +); export const TANK_NAMES = new Promise(async (resolve) => { resolve((await entries).map(({ name }) => name)); }); export const TANK_NAMES_DIACRITICS = TANK_NAMES.then((tankNames) => Promise.all( tankNames.map(async (tankName, index) => { - const id = (await TANKS)[index].tank_id; + const { id } = (await TANKS)[index]; const name = tankName ?? `Unknown Tank ${id}`; const diacriticless = deburr(name); diff --git a/src/core/blitzstars/tankAverages.ts b/src/core/blitzstars/tankAverages.ts index 6316d7db9..110e1db84 100644 --- a/src/core/blitzstars/tankAverages.ts +++ b/src/core/blitzstars/tankAverages.ts @@ -1,6 +1,6 @@ import { AllStats } from '../blitz/getAccountInfo'; import { context } from '../blitzkrieg/context'; -import { tankopedia } from './tankopedia'; +import { tankopedia } from '../blitzkrieg/tankopedia'; export interface SpecialStats { winrate: number; diff --git a/src/core/discord/autocompleteTanks.ts b/src/core/discord/autocompleteTanks.ts index de74a907f..1b1c584e8 100644 --- a/src/core/discord/autocompleteTanks.ts +++ b/src/core/discord/autocompleteTanks.ts @@ -4,7 +4,7 @@ import { CacheType, } from 'discord.js'; import { go } from 'fuzzysort'; -import { TANK_NAMES_DIACRITICS } from '../blitzstars/tankopedia'; +import { TANK_NAMES_DIACRITICS } from '../blitzkrieg/tankopedia'; import { DISCORD_CHOICES_MAX_NAME_SIZE, OVERFLOW_SUFFIX, diff --git a/src/core/statistics/filterStats.ts b/src/core/statistics/filterStats.ts index a0b3ecc55..172aa6ed9 100644 --- a/src/core/statistics/filterStats.ts +++ b/src/core/statistics/filterStats.ts @@ -1,7 +1,7 @@ import { TreeTypeString } from '../../components/Tanks'; +import { tankopedia } from '../blitzkrieg/tankopedia'; import { DiffedTankStats } from '../blitzstars/getStatsInPeriod'; import { tankAverages } from '../blitzstars/tankAverages'; -import { tankopedia } from '../blitzstars/tankopedia'; import calculateWN8 from './calculateWN8'; import sumStats from './sumStats'; @@ -31,14 +31,11 @@ export async function filterStats( (filters.tier === undefined || entry.tier === filters.tier) && (filters.tankType === undefined || entry.type === filters.tankType) && (filters.treeType === undefined || - (filters.treeType === 'collector' && entry.is_collectible) || - (filters.treeType === 'premium' && - entry.is_premium && - !entry.is_collectible) || + (filters.treeType === 'collector' && entry.tree_type === 'collector') || + (filters.treeType === 'premium' && entry.tree_type === 'premium') || (filters.treeType === 'tech-tree' && - !entry.is_collectible && - !entry.is_premium)) && - (filters.tank === undefined || entry.tank_id === filters.tank) + entry.tree_type === 'tech-tree')) && + (filters.tank === undefined || entry.id === filters.tank) ); }); const stats = sumStats(filteredOrder.map((id) => diff[id])); diff --git a/src/core/statistics/getTierWeights.ts b/src/core/statistics/getTierWeights.ts index a31dde982..020ea7e6b 100644 --- a/src/core/statistics/getTierWeights.ts +++ b/src/core/statistics/getTierWeights.ts @@ -1,6 +1,6 @@ import { TierWeightsRecord } from '../../components/TierWeights'; import { AllStats } from '../blitz/getAccountInfo'; -import { tankopedia } from '../blitzstars/tankopedia'; +import { tankopedia } from '../blitzkrieg/tankopedia'; export async function getTierWeights( stats: Record, @@ -9,7 +9,7 @@ export async function getTierWeights( const awaitedTankopedia = await tankopedia; const tierWeights: TierWeightsRecord = {}; - order.map((id) => { + order.forEach((id) => { const entry = awaitedTankopedia[id]; if (!entry) return;