From d62708c21e540ccd1fd8a1ecf8f62fd4c73920f3 Mon Sep 17 00:00:00 2001 From: tresabhi Date: Sun, 28 May 2023 17:19:14 +0000 Subject: [PATCH] feat: cache images --- package.json | 2 +- src/commands/ownedtanks.ts | 2 +- src/commands/tankstats.ts | 2 +- src/commands/today.tsx | 6 +++--- src/core/blitzstars/tankopedia.ts | 34 ++++++++++++++++++++++++------- 5 files changed, 33 insertions(+), 13 deletions(-) diff --git a/package.json b/package.json index 15d164770..951580865 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "blitzkrieg", - "version": "1.3.1", + "version": "1.3.2", "description": "", "main": "index.js", "type": "module", diff --git a/src/commands/ownedtanks.ts b/src/commands/ownedtanks.ts index a3b306429..a7f628d85 100644 --- a/src/commands/ownedtanks.ts +++ b/src/commands/ownedtanks.ts @@ -64,7 +64,7 @@ export default { `https://api.wotblitz.${server}/wotb/tanks/stats/?application_id=${args['wargaming-application-id']}&account_id=${id}`, ); const tanks = tankStats[id] - .map((tankData) => tankopedia.data[tankData.tank_id]) + .map((tankData) => tankopedia[tankData.tank_id]) .filter((tank) => tank?.tier === tier); await interaction.editReply({ diff --git a/src/commands/tankstats.ts b/src/commands/tankstats.ts index 1566b29eb..776c3969a 100644 --- a/src/commands/tankstats.ts +++ b/src/commands/tankstats.ts @@ -52,7 +52,7 @@ export default { const accountInfo = await getWargamingResponse( `https://api.wotblitz.${server}/wotb/account/info/?application_id=${args['wargaming-application-id']}&account_id=${id}`, ); - const tank = tankopedia.data[tankId as unknown as number]; + const tank = tankopedia[tankId as unknown as number]; if (tank) { const periodicStats = (await getPeriodicStats(id, Number(period))) diff --git a/src/commands/today.tsx b/src/commands/today.tsx index 5838f4a5d..0003eee57 100644 --- a/src/commands/today.tsx +++ b/src/commands/today.tsx @@ -29,7 +29,7 @@ import resolveTankName from '../utilities/resolveTankName.js'; export default { inProduction: true, - inDevelopment: false, + inDevelopment: true, inPublic: true, command: new SlashCommandBuilder() @@ -71,7 +71,7 @@ export default { key={tankId} name={resolveTankName({ tank_id: tankId as unknown as number, - name: tankopedia.data[tankId as unknown as number].name, + name: tankopedia[tankId as unknown as number].name, })} winrate={tankStats.wins / tankStats.battles} careerWinrate={career.wins / career.battles} @@ -83,7 +83,7 @@ export default { careerSurvival={career.survived_battles / career.battles} battles={tankStats.battles} careerBattles={career.battles} - icon={tankopedia.data[tankId as unknown as number].images.normal} + icon={tankopedia[tankId as unknown as number].images.normal} /> ); }, diff --git a/src/core/blitzstars/tankopedia.ts b/src/core/blitzstars/tankopedia.ts index f6a977ed4..0c4eaee43 100644 --- a/src/core/blitzstars/tankopedia.ts +++ b/src/core/blitzstars/tankopedia.ts @@ -17,7 +17,7 @@ export interface Tankopedia { }; images: { preview: string; - normal: string; + normal?: string; }; tank_id: number; type: TankType; @@ -26,15 +26,19 @@ export interface Tankopedia { }; } -export const tankopedia = (await ( - await fetch('https://www.blitzstars.com/bs-tankopedia.json') -).json()) as Tankopedia; +console.log('Caching tankopedia...'); +export const tankopedia = ( + (await ( + await fetch('https://www.blitzstars.com/bs-tankopedia.json') + ).json()) as Tankopedia +).data; +console.log('Cached tankopedia'); -export const tankIds = Object.keys(tankopedia.data) as unknown as number[]; +export const tankIds = Object.keys(tankopedia) as unknown as number[]; -export const tankNames = tankIds.map((id) => tankopedia.data[id].name); +export const tankNames = tankIds.map((id) => tankopedia[id].name); -export const tanks = tankIds.map((id) => tankopedia.data[id]); +export const tanks = tankIds.map((id) => tankopedia[id]); export const TANK_TYPE_NAMES: Record = { 'AT-SPG': 'Tank Destroyer', @@ -49,3 +53,19 @@ export const TANK_TYPE_EMOJIS: Record = { mediumTank: '🇲', lightTank: '🇱', }; + +console.log('Encoding tank images...'); +await Promise.all( + Object.entries(tankopedia).map(async ([, tank]) => { + if (tank.images.normal) { + const response = await fetch(tank.images.normal); + const blob = await response.arrayBuffer(); + const base64 = `data:${response.headers.get( + 'content-type', + )};base64,${Buffer.from(blob).toString('base64')}`; + + tank.images.normal = base64; + } + }), +); +console.log('Encoded tank images');