Added weekly points back-up, writing points to dedicated directory.

This commit is contained in:
ThePendulum 2025-02-18 22:13:24 +01:00
parent 19feb9a55f
commit a9f733227c
2 changed files with 8 additions and 4 deletions

2
.gitignore vendored
View File

@ -4,5 +4,5 @@ config/*
*.config.js
!ecosystem.config.js
*.sqlite
points*.json
points/*
assets/mash-words.json

View File

@ -3,6 +3,7 @@
const config = require('config');
const fs = require('fs').promises;
const logger = require('simple-node-logger').createSimpleLogger();
const { getWeek } = require('date-fns');
const { argv } = require('yargs');
// const timers = require('timers/promises');
@ -15,14 +16,16 @@ const points = {};
async function initPoints(identifier) {
try {
const pointsFile = await fs.readFile(`./points-${identifier}.json`, 'utf-8');
const pointsFile = await fs.readFile(`./points/points-${identifier}.json`, 'utf-8');
Object.assign(points, JSON.parse(pointsFile));
} catch (error) {
if (error.code === 'ENOENT') {
logger.info('Creating new points file');
await fs.writeFile(`./points-${identifier}.json`, '{}');
await fs.mkdir('./points', { recursive: true });
await fs.writeFile(`./points/points-${identifier}.json`, '{}');
await initPoints(identifier);
}
}
@ -50,7 +53,8 @@ async function setPoints(identifier, defaultKey, user, value, { mode = 'add', ke
points[gameKey][userKey] = value;
}
await fs.writeFile(`./points-${identifier}.json`, JSON.stringify(points, null, 4));
await fs.writeFile(`./points/points-${identifier}_backup${getWeek(new Date())}.json`, JSON.stringify(points, null, 4)); // weekly back-up
await fs.writeFile(`./points/points-${identifier}.json`, JSON.stringify(points, null, 4));
}
function getPoints(game, rawUsername, { user, room, command }) {