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,26 +620,34 @@ 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);
} catch (e) {
console.error(`${info}: gameDB.getGameById error`, e);
game = null;
}
}
// If DB didn't return a game, try the original filesystem-backed storage
// including the existing backup/restore behavior.
if (!game) {
let raw = await readFile(`/db/games/${id}`).catch(() => { return; });
if (raw) {
try {
game = JSON.parse(raw as any);
console.log(`${info}: Creating backup of /db/games/${id}`); console.log(`${info}: Creating backup of /db/games/${id}`);
await writeFile(`/db/games/${id}.bk`, JSON.stringify(game)); await writeFile(`/db/games/${id}.bk`, JSON.stringify(game));
} catch (error) { } catch (error) {
console.log(`Load or parse error from /db/games/${id}:`, error); console.log(`Load or parse error from /db/games/${id}:`, error);
console.log(`Attempting to load backup from /db/games/${id}.bk`); console.log(`Attempting to load backup from /db/games/${id}.bk`);
game = await readFile(`/db/games/${id}.bk`) raw = await readFile(`/db/games/${id}.bk`).catch(() => { return; });
.catch(() => { if (raw) {
console.error(error, game);
});
if (game) {
try { try {
game = JSON.parse(game); game = JSON.parse(raw as any);
console.log(`Saving backup to /db/games/${id}`); console.log(`Saving backup to /db/games/${id}`);
await writeFile(`/db/games/${id}`, JSON.stringify(game, null, 2)); await writeFile(`/db/games/${id}`, JSON.stringify(game, null, 2));
} catch (error) { } catch (error) {
@ -649,6 +657,7 @@ const loadGame = async (id) => {
} }
} }
} }
}
if (!game) { if (!game) {
game = createGame(id); game = createGame(id);
@ -3481,12 +3490,30 @@ const saveGame = async (game) => {
console.error(error); console.error(error);
}); });
*/ */
// Prefer DB persistence when available, but gracefully fall back to the
// original file-based storage on error or when DB is not configured.
if (gameDB && gameDB.saveGameState) {
try {
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 mkdir('/db/games', { recursive: true });
await writeFile(`/db/games/${game.id}`, JSON.stringify(reducedGame, null, 2)) await writeFile(`/db/games/${game.id}`, JSON.stringify(reducedGame, null, 2));
.catch((error) => { } catch (error) {
console.error(`Unable to write to /db/games/${game.id}`); console.error(`Unable to write to /db/games/${game.id}`);
console.error(error); 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 {
if (gameDB && gameDB.deleteGame) {
await gameDB.deleteGame(id);
} else {
fs.unlinkSync(`/db/games/${id}`); 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 */
let exists = false;
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); accessSync(`/db/games/${id}`, fs.F_OK);
exists = true;
} catch (err) {
// file does not exist
}
}
if (exists) {
id = ''; 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) {