feat: game strings api

This commit is contained in:
TrèsAbhi committed 2026-01-13 13:18:55 -06:00
1 parent a2d0d2d8c0
commit 2d282a3261
3 files changed
+83 -18

No files matched your search

+36 -14
View File
@@ -63,29 +63,41 @@ export abstract class AbstractAPI {
return this._stringsCache[locale];
}
private _gameStringsCache: Record<
string,
Record<string, Record<string, string>>
> = {};
private _gameStringsCache: Record<string, Record<string, string>> = {};
protected abstract _gameStrings(
locale: string,
prefix: string
locale: string
): Promise<Record<string, string>>;
async gameStrings(locale: string, prefix: string) {
async gameStrings(locale: string) {
this.assertLocale(locale);
if (this._gameStringsCache[locale] === undefined) {
this._gameStringsCache[locale] = {};
this._gameStringsCache[locale] = await this._gameStrings(locale);
}
if (this._gameStringsCache[locale][prefix] === undefined) {
this._gameStringsCache[locale][prefix] = await this._gameStrings(
locale,
prefix
);
return this._gameStringsCache[locale];
}
private _groupedGameStringsCache: Record<
string,
Record<string, Record<string, string>>
> = {};
protected abstract _groupedGameStrings(
locale: string,
group: string
): Promise<Record<string, string>>;
async groupedGameStrings(locale: string, group: string) {
this.assertLocale(locale);
if (this._groupedGameStringsCache[locale] === undefined) {
this._groupedGameStringsCache[locale] = {};
}
return this._gameStringsCache[locale][prefix];
if (this._groupedGameStringsCache[locale][group] === undefined) {
this._groupedGameStringsCache[locale][group] =
await this._groupedGameStrings(locale, group);
}
return this._groupedGameStringsCache[locale][group];
}
private _avatarCache: Record<string, Avatar> = {};
@@ -127,4 +139,14 @@ export abstract class AbstractAPI {
return this._backgroundsCache;
}
private _gameStringGroupsCache: string[] | undefined;
protected abstract _gameStringGroups(): Promise<string[]>;
async gameStringGroups() {
if (this._gameStringGroupsCache === undefined) {
this._gameStringGroupsCache = await this._gameStringGroups();
}
return this._gameStringGroupsCache;
}
}
+22 -4
View File
@@ -29,7 +29,7 @@ export class ServerAPI extends AbstractAPI {
super();
}
protected async _gameStrings(locale: string, prefix: string) {
protected async _gameStrings(locale: string) {
const group = this.metadata.group("ClientConfigsEntity");
if (group.length !== 1) {
@@ -98,11 +98,17 @@ export class ServerAPI extends AbstractAPI {
);
}
return strings;
}
protected async _groupedGameStrings(locale: string, group: string) {
const strings = await this.gameStrings(locale);
const filtered: Record<string, string> = {};
for (const key in strings) {
if (key.startsWith(`${prefix}__`)) {
filtered[key] = strings[key];
if (key.startsWith(`${group}__`)) {
const trimmed = key.substring(group.length + 2);
filtered[trimmed] = strings[key];
}
}
@@ -124,7 +130,7 @@ export class ServerAPI extends AbstractAPI {
protected async _tankList() {
const group = this.metadata.group("TankEntity");
const list: TankListEntry[] = [];
const strings = await this.gameStrings("en", "TankEntity");
const strings = await this.groupedGameStrings("en", "TankEntity");
for (const item of group) {
const tankCatalog = item.TankCatalog();
@@ -214,4 +220,16 @@ export class ServerAPI extends AbstractAPI {
return { name, stuff_ui, profile_background, sellable };
}
protected async _gameStringGroups() {
const defaultGameStrings = await this.gameStrings(locales.default);
const groups = new Set<string>();
for (const key in defaultGameStrings) {
const [group] = key.split("__");
groups.add(group);
}
return Array.from(groups.values());
}
}
@@ -0,0 +1,25 @@
import locales from "@blitzkit/i18n/locales.json";
import type { GetStaticPaths, GetStaticPathsResult } from "astro";
import { api } from "../../../../core/api/dynamic";
export const getStaticPaths = (async () => {
const paths: GetStaticPathsResult = [];
const groups = await api.gameStringGroups();
for (const { locale } of locales.supported) {
for (const group of groups) {
paths.push({ params: { locale, group } });
}
}
return paths;
}) satisfies GetStaticPaths;
export async function GET({
params,
}: {
params: { locale: string; group: string };
}) {
const strings = await api.groupedGameStrings(params.locale, params.group);
return Response.json(strings);
}