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

153 lines
4.4 KiB
TypeScript
Raw Normal View History

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-11 13:46:54 -06:00
import type { Background } from "../../protos/background";
import type { Backgrounds } from "../../protos/backgrounds";
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 _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-11 13:46:54 -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);
}
return this._tankCache[id];
}
private _tanksCache: Tanks | undefined;
protected abstract _tanks(): Promise<Tanks>;
async tanks() {
if (this._tanksCache === undefined) {
this._tanksCache = await this._tanks();
}
return this._tanksCache;
}
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
2026-01-13 13:18:55 -06:00
private _gameStringsCache: Record<string, Record<string, string>> = {};
2026-01-11 13:40:01 -06:00
protected abstract _gameStrings(
2026-01-13 13:18:55 -06:00
locale: string
2026-01-11 13:40:01 -06:00
): Promise<Record<string, string>>;
2026-01-13 13:18:55 -06:00
async gameStrings(locale: 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) {
2026-01-13 13:18:55 -06:00
this._gameStringsCache[locale] = await this._gameStrings(locale);
2026-01-11 13:40:01 -06:00
}
2026-01-13 13:18:55 -06:00
return this._gameStringsCache[locale];
}
private _groupedGameStringsCache: Record<
string,
Record<string, Record<string, string>>
> = {};
protected abstract _groupedGameStrings(
locale: string,
2026-01-13 13:30:09 -06:00
group: string,
prefix: boolean
2026-01-13 13:18:55 -06:00
): Promise<Record<string, string>>;
2026-01-13 13:30:09 -06:00
async groupedGameStrings(locale: string, group: string, prefix = true) {
2026-01-13 13:18:55 -06:00
this.assertLocale(locale);
if (this._groupedGameStringsCache[locale] === undefined) {
this._groupedGameStringsCache[locale] = {};
2026-01-11 13:40:01 -06:00
}
2026-01-13 13:18:55 -06:00
if (this._groupedGameStringsCache[locale][group] === undefined) {
this._groupedGameStringsCache[locale][group] =
2026-01-13 13:30:09 -06:00
await this._groupedGameStrings(locale, group, prefix);
2026-01-13 13:18:55 -06:00
}
return this._groupedGameStringsCache[locale][group];
2026-01-09 13:38:00 -06:00
}
2026-01-11 13:46:54 -06:00
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);
2026-01-11 13:40:01 -06:00
}
2026-01-02 14:20:29 -06:00
2026-01-11 13:46:54 -06:00
return this._avatarCache[id];
2026-01-11 13:40:01 -06:00
}
private _avatarsCache: Avatars | undefined;
protected abstract _avatars(): Promise<Avatars>;
async avatars() {
if (this._avatarsCache === undefined) {
this._avatarsCache = await this._avatars();
}
return this._avatarsCache;
}
2026-01-11 13:46:54 -06:00
private _backgroundCache: Record<string, Background> = {};
protected abstract _background(id: string): Promise<Background>;
async background(id: string) {
if (this._backgroundCache[id] === undefined) {
this._backgroundCache[id] = await this._background(id);
2026-01-11 13:40:01 -06:00
}
2026-01-11 13:46:54 -06:00
return this._backgroundCache[id];
2026-01-11 13:40:01 -06:00
}
2026-01-02 20:17:24 -06:00
2026-01-11 13:46:54 -06:00
private _backgroundsCache: Backgrounds | undefined;
protected abstract _backgrounds(): Promise<Backgrounds>;
async backgrounds() {
if (this._backgroundsCache === undefined) {
this._backgroundsCache = await this._backgrounds();
}
return this._backgroundsCache;
}
2026-01-13 13:18:55 -06:00
private _gameStringGroupsCache: string[] | undefined;
protected abstract _gameStringGroups(): Promise<string[]>;
async gameStringGroups() {
if (this._gameStringGroupsCache === undefined) {
this._gameStringGroupsCache = await this._gameStringGroups();
}
return this._gameStringGroupsCache;
}
2025-11-28 13:51:17 -06:00
}