2026-01-09 13:38:00 -06:00
|
|
|
import type { Strings } from "@blitzkit/i18n";
|
|
|
|
|
import locales from "@blitzkit/i18n/locales.json";
|
2026-04-14 20:34:28 -05:00
|
|
|
import type { Avatar } from "@protos/blitzkit/avatar";
|
|
|
|
|
import type { AvatarList } from "@protos/blitzkit/avatar_list";
|
|
|
|
|
import type { Avatars } from "@protos/blitzkit/avatars";
|
|
|
|
|
import type { Background } from "@protos/blitzkit/background";
|
|
|
|
|
import type { Backgrounds } from "@protos/blitzkit/backgrounds";
|
|
|
|
|
import type { PopularTanks } from "@protos/blitzkit/popular_tanks";
|
|
|
|
|
import type { Tank } from "@protos/blitzkit/tank";
|
|
|
|
|
import type { TankList } from "@protos/blitzkit/tank_list";
|
|
|
|
|
import type { Tanks } from "@protos/blitzkit/tanks";
|
2025-11-28 13:51:17 -06:00
|
|
|
|
|
|
|
|
export abstract class AbstractAPI {
|
2026-03-20 20:38:04 -05:00
|
|
|
abstract tankList(): Promise<TankList>;
|
|
|
|
|
abstract tank(id: string): Promise<Tank>;
|
2026-03-23 18:06:26 -05:00
|
|
|
abstract tanks(): Promise<Tanks>;
|
|
|
|
|
abstract popularTanks(): Promise<PopularTanks>;
|
2026-01-11 13:46:54 -06:00
|
|
|
|
2026-03-23 18:06:26 -05:00
|
|
|
abstract strings(locale: string): Promise<Strings>;
|
|
|
|
|
abstract gameStrings(locale: string): Promise<Record<string, string>>;
|
|
|
|
|
protected abstract _groupedGameStrings(
|
|
|
|
|
locale: string,
|
|
|
|
|
group: string,
|
|
|
|
|
): Promise<Record<string, string>>;
|
|
|
|
|
abstract gameStringGroups(): Promise<string[]>;
|
2026-01-11 13:46:54 -06:00
|
|
|
|
2026-03-23 18:06:26 -05:00
|
|
|
abstract avatar(id: string): Promise<Avatar>;
|
|
|
|
|
abstract avatars(): Promise<Avatars>;
|
|
|
|
|
abstract avatarList(): Promise<AvatarList>;
|
|
|
|
|
|
|
|
|
|
abstract background(id: string): Promise<Background>;
|
|
|
|
|
abstract backgrounds(): Promise<Backgrounds>;
|
2026-01-11 13:46:54 -06:00
|
|
|
|
2026-01-09 13:38:00 -06:00
|
|
|
private assertLocale(locale: string) {
|
|
|
|
|
const supported = locales.supported.find(
|
2026-02-03 09:04:36 -06:00
|
|
|
(supported) => supported.locale === locale,
|
2026-01-09 13:38:00 -06:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (supported === undefined) {
|
|
|
|
|
throw new Error(`Unsupported locale: ${locale}`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return supported;
|
|
|
|
|
}
|
2026-01-11 13:40:01 -06:00
|
|
|
|
2026-03-23 18:06:26 -05:00
|
|
|
@Cache<[string, string, boolean]>((...args) => args.join("-"))
|
2026-01-14 11:48:55 -06:00
|
|
|
async groupedGameStrings(locale: string, group: string, prefix: boolean) {
|
2026-01-13 13:18:55 -06:00
|
|
|
this.assertLocale(locale);
|
|
|
|
|
|
2026-03-23 18:06:26 -05:00
|
|
|
const gameStrings = await this._groupedGameStrings(locale, group);
|
2026-01-11 13:40:01 -06:00
|
|
|
|
2026-03-23 18:06:26 -05:00
|
|
|
if (!prefix) return gameStrings;
|
2026-01-14 11:48:55 -06:00
|
|
|
|
|
|
|
|
const strings: Record<string, string> = {};
|
|
|
|
|
|
2026-03-23 18:06:26 -05:00
|
|
|
for (const key in gameStrings) {
|
|
|
|
|
strings[`${group}__${key}`] = gameStrings[key];
|
2026-01-14 11:48:55 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return strings;
|
2026-01-09 13:38:00 -06:00
|
|
|
}
|
2025-11-28 13:51:17 -06:00
|
|
|
}
|
2026-03-20 20:38:04 -05:00
|
|
|
|
2026-03-20 20:40:45 -05:00
|
|
|
export function Cache<Arguments extends unknown[]>(
|
2026-03-23 18:06:26 -05:00
|
|
|
discriminator: (...args: Arguments) => string = (...args) => args.join("-"),
|
2026-03-20 20:38:04 -05:00
|
|
|
) {
|
|
|
|
|
const cache = new WeakMap<object, Map<string, unknown>>();
|
|
|
|
|
|
|
|
|
|
return function (
|
|
|
|
|
_target: unknown,
|
|
|
|
|
_propertyKey: string,
|
|
|
|
|
descriptor: PropertyDescriptor,
|
|
|
|
|
) {
|
|
|
|
|
const original = descriptor.value;
|
|
|
|
|
|
2026-03-20 20:40:45 -05:00
|
|
|
descriptor.value = async function (...args: Arguments) {
|
2026-03-20 20:38:04 -05:00
|
|
|
const key = discriminator(...args);
|
|
|
|
|
const thisCache = cache.get(this) ?? new Map<string, unknown>();
|
|
|
|
|
|
|
|
|
|
if (!cache.has(this)) cache.set(this, thisCache);
|
|
|
|
|
if (thisCache.has(key)) return thisCache.get(key);
|
|
|
|
|
|
|
|
|
|
const result = await original.apply(this, args);
|
|
|
|
|
|
|
|
|
|
thisCache.set(key, result);
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
}
|