Refactoring commit
This commit is contained in:
parent
2c15f57ca9
commit
c0e9b9a23c
@ -112,7 +112,9 @@ process.on('SIGINT', () => {
|
||||
server.close(() => process.exit(1));
|
||||
});
|
||||
|
||||
require("./db/games").then(function(db) {
|
||||
import { initGameDB } from './routes/games/store';
|
||||
|
||||
initGameDB().then(function(db) {
|
||||
gameDB = db;
|
||||
}).then(function() {
|
||||
return require("./db/users").then(function(db) {
|
||||
|
@ -620,31 +620,40 @@ const loadGame = async (id) => {
|
||||
return cached;
|
||||
}
|
||||
|
||||
let game = await readFile(`/db/games/${id}`)
|
||||
.catch(() => {
|
||||
return;
|
||||
});
|
||||
|
||||
if (game) {
|
||||
// Try to load from the configured game DB first (if available). Fall
|
||||
// back to the original file-based storage for compatibility.
|
||||
let game: any = null;
|
||||
if (gameDB && gameDB.getGameById) {
|
||||
try {
|
||||
game = JSON.parse(game);
|
||||
console.log(`${info}: Creating backup of /db/games/${id}`);
|
||||
await writeFile(`/db/games/${id}.bk`, JSON.stringify(game));
|
||||
} catch (error) {
|
||||
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(() => {
|
||||
console.error(error, game);
|
||||
});
|
||||
if (game) {
|
||||
try {
|
||||
game = JSON.parse(game);
|
||||
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;
|
||||
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}`);
|
||||
await writeFile(`/db/games/${id}.bk`, JSON.stringify(game));
|
||||
} catch (error) {
|
||||
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);
|
||||
});
|
||||
*/
|
||||
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);
|
||||
});
|
||||
// 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 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) => {
|
||||
@ -4081,7 +4108,11 @@ router.ws("/ws/:id", async (ws, req) => {
|
||||
delete audio[id];
|
||||
delete games[id];
|
||||
try {
|
||||
fs.unlinkSync(`/db/games/${id}`);
|
||||
if (gameDB && gameDB.deleteGame) {
|
||||
await gameDB.deleteGame(id);
|
||||
} else {
|
||||
fs.unlinkSync(`/db/games/${id}`);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`${session.id}: Unable to remove /db/games/${id}`);
|
||||
}
|
||||
@ -4988,9 +5019,27 @@ const createGame = (id) => {
|
||||
while (!id) {
|
||||
id = randomWords(4).join('-');
|
||||
try {
|
||||
/* If file can be read, it already exists so look for a new name */
|
||||
accessSync(`/db/games/${id}`, fs.F_OK);
|
||||
id = '';
|
||||
/* 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);
|
||||
exists = true;
|
||||
} catch (err) {
|
||||
// file does not exist
|
||||
}
|
||||
}
|
||||
if (exists) {
|
||||
id = '';
|
||||
}
|
||||
} catch (error) {
|
||||
break;
|
||||
}
|
||||
|
@ -95,8 +95,10 @@ process.on('SIGINT', () => {
|
||||
|
||||
// database initializers
|
||||
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
||||
Promise.resolve((require("../db/games") as any).default || require("../db/games")).then(function(_db: any) {
|
||||
// games DB initialized
|
||||
import { initGameDB } from '../routes/games/store';
|
||||
|
||||
initGameDB().then(function(_db: any) {
|
||||
// games DB initialized via store facade
|
||||
}).then(function() {
|
||||
// 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) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user