diff --git a/.gitignore b/.gitignore index 2fea49a..1c323dc 100755 --- a/.gitignore +++ b/.gitignore @@ -4,5 +4,5 @@ config/* *.config.js !ecosystem.config.js *.sqlite -points*.json +points/* assets/mash-words.json diff --git a/src/play.js b/src/play.js index 9fa8ff4..ff2e009 100755 --- a/src/play.js +++ b/src/play.js @@ -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 }) {