Files
blitzkit/packages/website-ue/src/core/api/abstract.ts
T

112 lines
3.1 KiB
TypeScript
Raw Normal View History

2026-01-02 14:20:29 -06:00
import type { CatalogItemAccessor } from "@blitzkit/closed";
2026-01-09 13:38:00 -06:00
import type { Strings } from "@blitzkit/i18n";
import locales from "@blitzkit/i18n/locales.json";
2026-01-11 13:40:01 -06:00
import type { Avatar } from "../../protos/avatar";
import type { Avatars } from "../../protos/avatars";
2026-01-09 13:38:00 -06:00
import type { Tank } from "../../protos/tank";
import type { TankList } from "../../protos/tank_list";
import type { Tanks } from "../../protos/tanks";
2025-11-28 13:51:17 -06:00
export abstract class AbstractAPI {
2026-01-11 13:40:01 -06:00
private _tanksCache: Tanks | undefined;
protected abstract _tanks(): Promise<Tanks>;
async tanks() {
if (this._tanksCache === undefined) {
this._tanksCache = await this._tanks();
}
2025-11-28 14:13:03 -06:00
2026-01-11 13:40:01 -06:00
return this._tanksCache;
}
2025-12-26 14:59:09 -06:00
2026-01-11 13:40:01 -06:00
private _tankListCache: TankList | undefined;
protected abstract _tankList(): Promise<TankList>;
async tankList() {
if (this._tankListCache === undefined) {
this._tankListCache = await this._tankList();
}
return this._tankListCache;
}
2025-11-28 14:21:26 -06:00
2026-01-09 13:38:00 -06:00
private assertLocale(locale: string) {
const supported = locales.supported.find(
(supported) => supported.locale === locale
);
if (supported === undefined) {
throw new Error(`Unsupported locale: ${locale}`);
}
return supported;
}
2026-01-11 13:40:01 -06:00
private _stringsCache: Record<string, Strings> = {};
protected abstract _strings(locale: string): Promise<Strings>;
2026-01-09 13:38:00 -06:00
async strings(locale: string) {
this.assertLocale(locale);
2026-01-11 13:40:01 -06:00
if (this._stringsCache[locale] === undefined) {
this._stringsCache[locale] = await this._strings(locale);
}
return this._stringsCache[locale];
2026-01-09 13:38:00 -06:00
}
2026-01-11 13:40:01 -06:00
private _gameStringsCache: Record<
string,
Record<string, Record<string, string>>
> = {};
protected abstract _gameStrings(
locale: string,
prefix: string
): Promise<Record<string, string>>;
2026-01-09 13:38:00 -06:00
async gameStrings(locale: string, prefix: string) {
2026-01-09 13:57:28 -06:00
this.assertLocale(locale);
2026-01-11 13:40:01 -06:00
if (this._gameStringsCache[locale] === undefined) {
this._gameStringsCache[locale] = {};
}
if (this._gameStringsCache[locale][prefix] === undefined) {
this._gameStringsCache[locale][prefix] = await this._gameStrings(
locale,
prefix
);
}
return this._gameStringsCache[locale][prefix];
2026-01-09 13:38:00 -06:00
}
2026-01-11 13:40:01 -06:00
private _tankCache: Record<string, Tank> = {};
protected abstract _tank(id: string): Promise<Tank>;
async tank(id: string) {
if (this._tankCache[id] === undefined) {
this._tankCache[id] = await this._tank(id);
}
2026-01-02 14:20:29 -06:00
2026-01-11 13:40:01 -06:00
return this._tankCache[id];
}
private _avatarsCache: Avatars | undefined;
protected abstract _avatars(): Promise<Avatars>;
async avatars() {
if (this._avatarsCache === undefined) {
this._avatarsCache = await this._avatars();
}
return this._avatarsCache;
}
private _avatarCache: Record<string, Avatar> = {};
protected abstract _avatar(id: string): Promise<Avatar>;
async avatar(id: string) {
if (this._avatarCache[id] === undefined) {
this._avatarCache[id] = await this._avatar(id);
}
return this._avatarCache[id];
}
2026-01-02 20:17:24 -06:00
abstract backgrounds(): Promise<CatalogItemAccessor[]>;
abstract background(id: string): Promise<CatalogItemAccessor>;
2025-11-28 13:51:17 -06:00
}