mirror of
https://github.com/Pandipipas/scoreko-dev.git
synced 2026-06-06 03:32:06 +00:00
Cache game titles for offline selector labels
This commit is contained in:
@@ -6,6 +6,7 @@ import { nodecg } from './util/nodecg.js';
|
|||||||
const CHARACTER_NAMES_FILE = 'fighting-characters.json';
|
const CHARACTER_NAMES_FILE = 'fighting-characters.json';
|
||||||
const LOCAL_MANIFEST_FILE = 'manifest.json';
|
const LOCAL_MANIFEST_FILE = 'manifest.json';
|
||||||
const GAME_TITLES_FILE = 'games.json';
|
const GAME_TITLES_FILE = 'games.json';
|
||||||
|
const CACHED_GAME_TITLES_FILE = 'games-cache.json';
|
||||||
|
|
||||||
type RemoteGame = {
|
type RemoteGame = {
|
||||||
title: string;
|
title: string;
|
||||||
@@ -211,6 +212,26 @@ const fetchCustomGameTitles = async (): Promise<Map<string, string>> => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const loadCachedGameTitles = async (): Promise<Map<string, string>> => {
|
||||||
|
await ensureAssetsStorageReady();
|
||||||
|
const cachePath = path.join(assetsRoot, CACHED_GAME_TITLES_FILE);
|
||||||
|
|
||||||
|
try {
|
||||||
|
const raw = await readFile(cachePath, 'utf8');
|
||||||
|
const parsed = JSON.parse(raw) as unknown;
|
||||||
|
return parseGameTitlesMap(parsed);
|
||||||
|
} catch {
|
||||||
|
return new Map<string, string>();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const saveCachedGameTitles = async (titles: Map<string, string>) => {
|
||||||
|
await ensureAssetsStorageReady();
|
||||||
|
const cachePath = path.join(assetsRoot, CACHED_GAME_TITLES_FILE);
|
||||||
|
const payload = Object.fromEntries([...titles.entries()].sort((left, right) => left[0].localeCompare(right[0])));
|
||||||
|
await writeFile(cachePath, JSON.stringify(payload, null, 2));
|
||||||
|
};
|
||||||
|
|
||||||
const listRemoteGames = async (): Promise<RemoteGame[]> => {
|
const listRemoteGames = async (): Promise<RemoteGame[]> => {
|
||||||
const baseUrl = getConfiguredAssetsBaseUrl();
|
const baseUrl = getConfiguredAssetsBaseUrl();
|
||||||
const gamesIndexUrl = `${baseUrl}/games/`;
|
const gamesIndexUrl = `${baseUrl}/games/`;
|
||||||
@@ -275,10 +296,11 @@ const listInstalledGames = async () => {
|
|||||||
|
|
||||||
const listInstalledGamesAsRemote = async (): Promise<RemoteGame[]> => {
|
const listInstalledGamesAsRemote = async (): Promise<RemoteGame[]> => {
|
||||||
const installedGames = await listInstalledGames();
|
const installedGames = await listInstalledGames();
|
||||||
|
const cachedTitles = await loadCachedGameTitles();
|
||||||
return installedGames.map((slug) => ({
|
return installedGames.map((slug) => ({
|
||||||
slug,
|
slug,
|
||||||
repoFolder: slug,
|
repoFolder: slug,
|
||||||
title: titleFromSlug(slug),
|
title: cachedTitles.get(slug) ?? titleFromSlug(slug),
|
||||||
logoFile: `${slug}.png`,
|
logoFile: `${slug}.png`,
|
||||||
}));
|
}));
|
||||||
};
|
};
|
||||||
@@ -388,6 +410,11 @@ nodecg.listenFor('scoreko-assets:listRemoteGames', async (_payload: unknown, ack
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
const remoteGames = await listRemoteGames();
|
const remoteGames = await listRemoteGames();
|
||||||
|
const titlesToCache = new Map<string, string>();
|
||||||
|
remoteGames.forEach((game) => {
|
||||||
|
titlesToCache.set(game.slug, game.title);
|
||||||
|
});
|
||||||
|
await saveCachedGameTitles(titlesToCache);
|
||||||
ack(null, remoteGames);
|
ack(null, remoteGames);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
try {
|
try {
|
||||||
|
|||||||
Reference in New Issue
Block a user