Writing points to instance-specific file.

This commit is contained in:
ThePendulum 2021-11-09 13:17:53 +01:00
parent d9f1181fcb
commit d56b3f456b
1 changed files with 4 additions and 3 deletions

View File

@ -8,6 +8,7 @@ const fs = require('fs').promises;
const logger = require('simple-node-logger').createSimpleLogger(); const logger = require('simple-node-logger').createSimpleLogger();
const { argv } = require('yargs'); const { argv } = require('yargs');
const instance = process.env.NODE_APP_INSTANCE || 'main';
const points = {}; const points = {};
logger.setLevel(argv.level || 'info'); logger.setLevel(argv.level || 'info');
@ -66,7 +67,7 @@ async function setPoints(gameKey, user, value, mode = 'add') {
points[gameKey][userKey] = value; points[gameKey][userKey] = value;
} }
await fs.writeFile('./points.json', JSON.stringify(points, null, 4)); await fs.writeFile(`./points-${instance}.json`, JSON.stringify(points, null, 4));
} }
function getPoints(game, { user, room }) { function getPoints(game, { user, room }) {
@ -188,14 +189,14 @@ const messageHandlers = {
async function initPoints() { async function initPoints() {
try { try {
const pointsFile = await fs.readFile('./points.json', 'utf-8'); const pointsFile = await fs.readFile(`./points-${instance}.json`, 'utf-8');
Object.assign(points, JSON.parse(pointsFile)); Object.assign(points, JSON.parse(pointsFile));
} catch (error) { } catch (error) {
if (error.code === 'ENOENT') { if (error.code === 'ENOENT') {
logger.info('Creating new points file'); logger.info('Creating new points file');
await fs.writeFile('./points.json', '{}'); await fs.writeFile(`./points-${instance}.json`, '{}');
initPoints(); initPoints();
} }
} }