1
0

Refactoring commit

This commit is contained in:
James Ketr 2025-10-06 11:54:17 -07:00
parent 2c15f57ca9
commit c0e9b9a23c
3 changed files with 90 additions and 37 deletions

View File

@ -112,7 +112,9 @@ process.on('SIGINT', () => {
server.close(() => process.exit(1)); server.close(() => process.exit(1));
}); });
require("./db/games").then(function(db) { import { initGameDB } from './routes/games/store';
initGameDB().then(function(db) {
gameDB = db; gameDB = db;
}).then(function() { }).then(function() {
return require("./db/users").then(function(db) { return require("./db/users").then(function(db) {

View File

@ -620,31 +620,40 @@ const loadGame = async (id) => {
return cached; return cached;
} }
let game = await readFile(`/db/games/${id}`) // Try to load from the configured game DB first (if available). Fall
.catch(() => { // back to the original file-based storage for compatibility.
return; let game: any = null;
}); if (gameDB && gameDB.getGameById) {
if (game) {
try { try {
game = JSON.parse(game); game = await gameDB.getGameById(id);
console.log(`${info}: Creating backup of /db/games/${id}`); } catch (e) {
await writeFile(`/db/games/${id}.bk`, JSON.stringify(game)); console.error(`${info}: gameDB.getGameById error`, e);
} catch (error) { game = null;
console.log(`Load or parse error from /db/games/${id}:`, error); }
console.log(`Attempting to load backup from /db/games/${id}.bk`); }
game = await readFile(`/db/games/${id}.bk`)
.catch(() => { // If DB didn't return a game, try the original filesystem-backed storage
console.error(error, game); // including the existing backup/restore behavior.
}); if (!game) {
if (game) { let raw = await readFile(`/db/games/${id}`).catch(() => { return; });
try { if (raw) {
game = JSON.parse(game); try {
console.log(`Saving backup to /db/games/${id}`); game = JSON.parse(raw as any);
await writeFile(`/db/games/${id}`, JSON.stringify(game, null, 2)); console.log(`${info}: Creating backup of /db/games/${id}`);
} catch (error) { await writeFile(`/db/games/${id}.bk`, JSON.stringify(game));
console.error(error); } catch (error) {
game = null; console.log(`Load or parse error from /db/games/${id}:`, error);
console.log(`Attempting to load backup from /db/games/${id}.bk`);
raw = await readFile(`/db/games/${id}.bk`).catch(() => { return; });
if (raw) {
try {
game = JSON.parse(raw as any);
console.log(`Saving backup to /db/games/${id}`);
await writeFile(`/db/games/${id}`, JSON.stringify(game, null, 2));
} catch (error) {
console.error(error);
game = null;
}
} }
} }
} }
@ -3481,12 +3490,30 @@ const saveGame = async (game) => {
console.error(error); console.error(error);
}); });
*/ */
await mkdir('/db/games', { recursive: true }); // Prefer DB persistence when available, but gracefully fall back to the
await writeFile(`/db/games/${game.id}`, JSON.stringify(reducedGame, null, 2)) // original file-based storage on error or when DB is not configured.
.catch((error) => { if (gameDB && gameDB.saveGameState) {
console.error(`Unable to write to /db/games/${game.id}`); try {
console.error(error); await gameDB.saveGameState(game.id, reducedGame);
}); } catch (e) {
console.error(`${info}: gameDB.saveGameState failed for ${game.id}`, e);
try {
await mkdir('/db/games', { recursive: true });
await writeFile(`/db/games/${game.id}`, JSON.stringify(reducedGame, null, 2));
} catch (error) {
console.error(`Unable to write to /db/games/${game.id}`);
console.error(error);
}
}
} else {
try {
await mkdir('/db/games', { recursive: true });
await writeFile(`/db/games/${game.id}`, JSON.stringify(reducedGame, null, 2));
} catch (error) {
console.error(`Unable to write to /db/games/${game.id}`);
console.error(error);
}
}
} }
const departLobby = (game, session, color) => { const departLobby = (game, session, color) => {
@ -4081,7 +4108,11 @@ router.ws("/ws/:id", async (ws, req) => {
delete audio[id]; delete audio[id];
delete games[id]; delete games[id];
try { try {
fs.unlinkSync(`/db/games/${id}`); if (gameDB && gameDB.deleteGame) {
await gameDB.deleteGame(id);
} else {
fs.unlinkSync(`/db/games/${id}`);
}
} catch (error) { } catch (error) {
console.error(`${session.id}: Unable to remove /db/games/${id}`); console.error(`${session.id}: Unable to remove /db/games/${id}`);
} }
@ -4988,9 +5019,27 @@ const createGame = (id) => {
while (!id) { while (!id) {
id = randomWords(4).join('-'); id = randomWords(4).join('-');
try { try {
/* If file can be read, it already exists so look for a new name */ /* If a game with this id exists in the DB or filesystem, look for a new name */
accessSync(`/db/games/${id}`, fs.F_OK); let exists = false;
id = ''; if (gameDB && gameDB.getGameById) {
try {
const g = await gameDB.getGameById(id);
if (g) exists = true;
} catch (e) {
// ignore DB errors and fall back to filesystem check
}
}
if (!exists) {
try {
accessSync(`/db/games/${id}`, fs.F_OK);
exists = true;
} catch (err) {
// file does not exist
}
}
if (exists) {
id = '';
}
} catch (error) { } catch (error) {
break; break;
} }

View File

@ -95,8 +95,10 @@ process.on('SIGINT', () => {
// database initializers // database initializers
// eslint-disable-next-line @typescript-eslint/no-var-requires // eslint-disable-next-line @typescript-eslint/no-var-requires
Promise.resolve((require("../db/games") as any).default || require("../db/games")).then(function(_db: any) { import { initGameDB } from '../routes/games/store';
// games DB initialized
initGameDB().then(function(_db: any) {
// games DB initialized via store facade
}).then(function() { }).then(function() {
// eslint-disable-next-line @typescript-eslint/no-var-requires // eslint-disable-next-line @typescript-eslint/no-var-requires
return Promise.resolve((require("../db/users") as any).default || require("../db/users")).then(function(_db: any) { return Promise.resolve((require("../db/users") as any).default || require("../db/users")).then(function(_db: any) {