mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-10-23 19:49:50 +02:00
feat: game strings api
This commit is contained in:
1 parent
a2d0d2d8c0
commit
2d282a3261
3 files changed
+83
-18
No files matched your search
@@ -63,29 +63,41 @@ export abstract class AbstractAPI {
|
|||||||
return this._stringsCache[locale];
|
return this._stringsCache[locale];
|
||||||
}
|
}
|
||||||
|
|
||||||
private _gameStringsCache: Record<
|
private _gameStringsCache: Record<string, Record<string, string>> = {};
|
||||||
string,
|
|
||||||
Record<string, Record<string, string>>
|
|
||||||
> = {};
|
|
||||||
protected abstract _gameStrings(
|
protected abstract _gameStrings(
|
||||||
locale: string,
|
locale: string
|
||||||
prefix: string
|
|
||||||
): Promise<Record<string, string>>;
|
): Promise<Record<string, string>>;
|
||||||
async gameStrings(locale: string, prefix: string) {
|
async gameStrings(locale: string) {
|
||||||
this.assertLocale(locale);
|
this.assertLocale(locale);
|
||||||
|
|
||||||
if (this._gameStringsCache[locale] === undefined) {
|
if (this._gameStringsCache[locale] === undefined) {
|
||||||
this._gameStringsCache[locale] = {};
|
this._gameStringsCache[locale] = await this._gameStrings(locale);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this._gameStringsCache[locale][prefix] === undefined) {
|
return this._gameStringsCache[locale];
|
||||||
this._gameStringsCache[locale][prefix] = await this._gameStrings(
|
}
|
||||||
locale,
|
|
||||||
prefix
|
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> = {};
|
private _avatarCache: Record<string, Avatar> = {};
|
||||||
@@ -127,4 +139,14 @@ export abstract class AbstractAPI {
|
|||||||
|
|
||||||
return this._backgroundsCache;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -29,7 +29,7 @@ export class ServerAPI extends AbstractAPI {
|
|||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected async _gameStrings(locale: string, prefix: string) {
|
protected async _gameStrings(locale: string) {
|
||||||
const group = this.metadata.group("ClientConfigsEntity");
|
const group = this.metadata.group("ClientConfigsEntity");
|
||||||
|
|
||||||
if (group.length !== 1) {
|
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> = {};
|
const filtered: Record<string, string> = {};
|
||||||
|
|
||||||
for (const key in strings) {
|
for (const key in strings) {
|
||||||
if (key.startsWith(`${prefix}__`)) {
|
if (key.startsWith(`${group}__`)) {
|
||||||
filtered[key] = strings[key];
|
const trimmed = key.substring(group.length + 2);
|
||||||
|
filtered[trimmed] = strings[key];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -124,7 +130,7 @@ export class ServerAPI extends AbstractAPI {
|
|||||||
protected async _tankList() {
|
protected async _tankList() {
|
||||||
const group = this.metadata.group("TankEntity");
|
const group = this.metadata.group("TankEntity");
|
||||||
const list: TankListEntry[] = [];
|
const list: TankListEntry[] = [];
|
||||||
const strings = await this.gameStrings("en", "TankEntity");
|
const strings = await this.groupedGameStrings("en", "TankEntity");
|
||||||
|
|
||||||
for (const item of group) {
|
for (const item of group) {
|
||||||
const tankCatalog = item.TankCatalog();
|
const tankCatalog = item.TankCatalog();
|
||||||
@@ -214,4 +220,16 @@ export class ServerAPI extends AbstractAPI {
|
|||||||
|
|
||||||
return { name, stuff_ui, profile_background, sellable };
|
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);
|
||||||
|
}
|
||||||
Reference in new issue
Block a user