import type { MetadataAccessor } from "@blitzkit/closed"; import type { Strings } from "@blitzkit/i18n"; import locales from "@blitzkit/i18n/locales.json"; import type { Avatar } from "@protos/avatar"; import type { AvatarList } from "@protos/avatar_list"; import type { Avatars } from "@protos/avatars"; import type { Background } from "@protos/background"; import type { Backgrounds } from "@protos/backgrounds"; import type { TankUpgradePricePresetComponent } from "@protos/blitz_static_tank_upgrade_price_preset_component"; import type { Equipment } from "@protos/equipment"; import type { PopularTanks } from "@protos/popular_tanks"; import type { Tank } from "@protos/tank"; import type { TankList } from "@protos/tank_list"; import type { Tanks } from "@protos/tanks"; import type { Tiers } from "@protos/tiers"; export abstract class AbstractAPI { abstract metadata: MetadataAccessor; abstract mediaPrefix(path: string): string; abstract tankList(): Promise; abstract tank(id: string): Promise; abstract tanks(): Promise; abstract popularTanks(): Promise; abstract tankUpgradePreset( name: string, ): Promise; abstract tiers(): Promise; abstract equipment(): Promise; abstract strings(locale: string): Promise; abstract gameStrings(locale: string): Promise>; protected abstract _groupedGameStrings( locale: string, group: string, ): Promise>; abstract gameStringGroups(): Promise; abstract avatar(id: string): Promise; abstract avatars(): Promise; abstract avatarList(): Promise; abstract background(id: string): Promise; abstract backgrounds(): Promise; abstract currencyIcon(id: string): Promise; private assertLocale(locale: string) { const supported = locales.supported.find( (supported) => supported.locale === locale, ); if (supported === undefined) { throw new Error(`Unsupported locale: ${locale}`); } return supported; } @Cache() async groupedGameStrings(locale: string, group: string, prefix: boolean) { this.assertLocale(locale); const gameStrings = await this._groupedGameStrings(locale, group); if (!prefix) return gameStrings; const strings: Record = {}; for (const key in gameStrings) { strings[`${group}__${key}`] = gameStrings[key]; } return strings; } } export function Cache(disableInDev = false) { const cache = new WeakMap>>(); return function ( _target: unknown, _propertyKey: string, descriptor: PropertyDescriptor, ) { const original = descriptor.value; descriptor.value = function (...args: Arguments) { const key = args.join("-"); let thisCache = cache.get(this); if (!thisCache) { thisCache = new Map(); cache.set(this, thisCache); } const disabled = disableInDev && import.meta.env.DEV; if (thisCache.has(key) && !disabled) { return thisCache.get(key); } const promise = (async () => { try { return await original.apply(this, args); } catch (error) { thisCache!.delete(key); throw error; } })(); thisCache.set(key, promise); return promise; }; }; }