From 58c68968e7741be210df0f3b3256eec900fe9fbb Mon Sep 17 00:00:00 2001 From: tresabhi Date: Mon, 5 Jun 2023 17:46:22 +0000 Subject: [PATCH] feat: encode images on a separate thread --- package.json | 1 + src/commands/debug.ts | 2 -- src/commands/eligible.ts | 4 --- src/commands/help.ts | 2 -- src/commands/inactive.ts | 4 --- src/commands/ownedtanks.ts | 4 --- src/commands/playerachievements.ts | 2 -- src/commands/playerinfo.ts | 2 -- src/commands/searchclans.ts | 2 -- src/commands/searchplayers.ts | 2 -- src/commands/searchtanks.ts | 2 -- src/commands/stats.tsx | 1 - src/commands/tankstats.tsx | 2 -- src/commands/today.tsx | 1 - src/commands/verify.ts | 14 --------- src/core/blitz/tankopedia.ts | 46 +++++++++++++++++++++++------- src/core/ui/URLToBase64.ts | 9 ------ yarn.lock | 5 ++++ 18 files changed, 42 insertions(+), 63 deletions(-) delete mode 100644 src/core/ui/URLToBase64.ts diff --git a/package.json b/package.json index 5b17c3ff9..545aed3fe 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "fuzzysort": "^2.0.4", "lodash.startcase": "^4.4.0", "markdown-escape": "^2.0.0", + "node-worker-threads-pool": "^1.5.1", "react": "^18.2.0", "satori": "^0.9.1" }, diff --git a/src/commands/debug.ts b/src/commands/debug.ts index e7630f90d..278060b45 100644 --- a/src/commands/debug.ts +++ b/src/commands/debug.ts @@ -46,7 +46,5 @@ export default { ), ], }); - - console.log("Displaying bot's debug information"); }, } satisfies CommandRegistry; diff --git a/src/commands/eligible.ts b/src/commands/eligible.ts index 56c16db5f..24060f7c8 100644 --- a/src/commands/eligible.ts +++ b/src/commands/eligible.ts @@ -171,9 +171,5 @@ export default { ? [positiveEmbed(title, body)] : [negativeEmbed(title, body)], }); - - console.log( - `Displaying ${server}/${id}'s eligibility information for ${clan}`, - ); }, } satisfies CommandRegistry; diff --git a/src/commands/help.ts b/src/commands/help.ts index 6e50b7798..0b1511cb8 100644 --- a/src/commands/help.ts +++ b/src/commands/help.ts @@ -39,7 +39,5 @@ export default { ), ], }); - - console.log('Displaying help information'); }, } satisfies CommandRegistry; diff --git a/src/commands/inactive.ts b/src/commands/inactive.ts index 9fc91a20d..1b8b5d257 100644 --- a/src/commands/inactive.ts +++ b/src/commands/inactive.ts @@ -83,10 +83,6 @@ export default { ), ], }); - - console.log( - `Displaying inactive members in ${clanInfo[id].name} for ${threshold} days`, - ); }, autocomplete: clanAutocomplete, diff --git a/src/commands/ownedtanks.ts b/src/commands/ownedtanks.ts index 0e05cd92f..214b37e81 100644 --- a/src/commands/ownedtanks.ts +++ b/src/commands/ownedtanks.ts @@ -93,10 +93,6 @@ export default { ), ], }); - - console.log( - `Displaying ${accountInfo[id].nickname}'s owned tanks in tier ${tier}`, - ); }, autocomplete: usernameAutocomplete, diff --git a/src/commands/playerachievements.ts b/src/commands/playerachievements.ts index a985f216f..536e1bed9 100644 --- a/src/commands/playerachievements.ts +++ b/src/commands/playerachievements.ts @@ -75,8 +75,6 @@ export default { ), ], }); - - console.log(`Displaying player info for ${accounts[id].nickname}`); }, autocomplete: usernameAutocomplete, diff --git a/src/commands/playerinfo.ts b/src/commands/playerinfo.ts index e4de7de85..82c83a05d 100644 --- a/src/commands/playerinfo.ts +++ b/src/commands/playerinfo.ts @@ -59,8 +59,6 @@ export default { ), ], }); - - console.log(`Displaying player info for ${accountInfo.nickname}`); }, autocomplete: usernameAutocomplete, diff --git a/src/commands/searchclans.ts b/src/commands/searchclans.ts index a52e50ddf..db07881eb 100644 --- a/src/commands/searchclans.ts +++ b/src/commands/searchclans.ts @@ -50,7 +50,5 @@ export default { ), ], }); - - console.log(`Clan search results for "${clan}"`); }, } satisfies CommandRegistry; diff --git a/src/commands/searchplayers.ts b/src/commands/searchplayers.ts index b6e16a3fe..d0f731cd8 100644 --- a/src/commands/searchplayers.ts +++ b/src/commands/searchplayers.ts @@ -58,7 +58,5 @@ export default { ), ], }); - - console.log(`Player search results for "${name}"`); }, } satisfies CommandRegistry; diff --git a/src/commands/searchtanks.ts b/src/commands/searchtanks.ts index f14361372..2d525cdc0 100644 --- a/src/commands/searchtanks.ts +++ b/src/commands/searchtanks.ts @@ -41,7 +41,5 @@ export default { ), ], }); - - console.log(`Tank search results for "${tank}"`); }, } satisfies CommandRegistry; diff --git a/src/commands/stats.tsx b/src/commands/stats.tsx index 866c6c8ce..06235a9a9 100644 --- a/src/commands/stats.tsx +++ b/src/commands/stats.tsx @@ -173,7 +173,6 @@ export default { components: [actionRow], }); - console.log(`Showing stats for ${accountInfo[id].nickname}`); }, autocomplete: usernameAutocomplete, diff --git a/src/commands/tankstats.tsx b/src/commands/tankstats.tsx index fd73f439b..c1b62f507 100644 --- a/src/commands/tankstats.tsx +++ b/src/commands/tankstats.tsx @@ -116,8 +116,6 @@ export default { files: [image], components: [actionRow], }); - - console.log(`Showing stats for ${accountInfo[id].nickname}`); }, autocomplete: (interaction) => { diff --git a/src/commands/today.tsx b/src/commands/today.tsx index dfd6bb634..b8212150e 100644 --- a/src/commands/today.tsx +++ b/src/commands/today.tsx @@ -194,7 +194,6 @@ export default { components: [actionRow], }); - console.log(`Showing daily breakdown for ${accountInfo[id].nickname}`); }, autocomplete: usernameAutocomplete, diff --git a/src/commands/verify.ts b/src/commands/verify.ts index 57ab15ab6..7ffa34a8b 100644 --- a/src/commands/verify.ts +++ b/src/commands/verify.ts @@ -48,10 +48,6 @@ export default { ], }); - console.warn( - `${interaction.user.username} failed to verify because of no nickname permission.`, - ); - return; } @@ -83,10 +79,6 @@ export default { ], }); - console.warn( - `${interaction.user.username} failed to verify because of no manage roles permission.`, - ); - return; } @@ -106,10 +98,6 @@ export default { ), ], }); - - console.log( - `${interaction.user.username} verified as ${accountInfo[id].nickname}${clanTag}`, - ); } catch (error) { await interaction.editReply({ embeds: [ @@ -121,8 +109,6 @@ export default { ), ], }); - - console.warn('Failed to verify because of higher permissions.'); } } } diff --git a/src/core/blitz/tankopedia.ts b/src/core/blitz/tankopedia.ts index b37a7a694..526772e74 100644 --- a/src/core/blitz/tankopedia.ts +++ b/src/core/blitz/tankopedia.ts @@ -1,5 +1,5 @@ +import { StaticPool } from 'node-worker-threads-pool'; import { WargamingResponse } from '../../types/wargamingResponse.js'; -import URLToBase64 from '../ui/URLToBase64.js'; export interface TankopediaEntry { name: string; @@ -39,12 +39,38 @@ export const TANK_IDS = Object.keys(tankopedia).map((id) => Number(id)); export const TANKS = TANK_IDS.map((id) => tankopedia[id]); export const TANK_NAMES = TANKS.map((tank) => tank.name); -console.log('Encoding tank images...'); -await Promise.all( - Object.entries(tankopedia).map(async ([, tank]) => { - if (tank.images.normal) { - tank.images.normal = await URLToBase64(tank.images.normal); - } - }), -); -console.log('Encoded tank images'); +const imageEncodingStaticPool = new StaticPool({ + size: 1, + task: async (data: Tankopedia) => { + console.log('Encoding tank images...'); + + const encoded: [number, string][] = []; + + await Promise.all( + Object.entries(data as Tankopedia).map( + async ([, tank]: [string, TankopediaEntry]) => { + 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')}`; + encoded.push([tank.tank_id, base64]); + } + }, + ), + ); + + console.log('Encoded tank images'); + + return encoded; + }, +}); + +imageEncodingStaticPool.exec(tankopedia).then((encoded) => { + console.log('Implementing encoded images...'); + encoded.forEach(([tankId, image]) => { + tankopedia[tankId].images.normal = image; + }); + console.log('Implemented encoded images'); +}); diff --git a/src/core/ui/URLToBase64.ts b/src/core/ui/URLToBase64.ts deleted file mode 100644 index 31b7c9954..000000000 --- a/src/core/ui/URLToBase64.ts +++ /dev/null @@ -1,9 +0,0 @@ -export default async function URLToBase64(URL: string) { - const response = await fetch(URL); - const blob = await response.arrayBuffer(); - const base64 = `data:${response.headers.get( - 'content-type', - )};base64,${Buffer.from(blob).toString('base64')}`; - - return base64; -} diff --git a/yarn.lock b/yarn.lock index 36e6a48e2..171754751 100644 --- a/yarn.lock +++ b/yarn.lock @@ -577,6 +577,11 @@ nanoid@^3.3.6: resolved "https://registry.npmjs.org/nanoid/-/nanoid-3.3.6.tgz" integrity sha512-BGcqMMJuToF7i1rt+2PWSNVnWIkGCU78jBG3RxO/bZlnZPK2Cmi2QaffxGO/2RvWi9sL+FAiRiXMgsyxQ1DIDA== +node-worker-threads-pool@^1.5.1: + version "1.5.1" + resolved "https://registry.yarnpkg.com/node-worker-threads-pool/-/node-worker-threads-pool-1.5.1.tgz#1034303e10bb63a541ec74ae26559e9dff355f27" + integrity sha512-7TXAhpMm+jO4MfESxYLtMGSnJWv+itdNHMdaFmeZuPXxwFGU90mtEB42BciUULXOUAxYBfXILAuvrSG3rQZ7mw== + pako@^0.2.5: version "0.2.9" resolved "https://registry.yarnpkg.com/pako/-/pako-0.2.9.tgz#f3f7522f4ef782348da8161bad9ecfd51bf83a75"