Added user point merging utility.

This commit is contained in:
ThePendulum 2024-03-08 00:49:37 +01:00
parent d510caa123
commit bad5dc52f7
2 changed files with 35 additions and 0 deletions

View File

@ -0,0 +1,35 @@
const path = require('path');
const args = require('yargs').argv;
const fs = require('fs').promises;
async function init() {
await fs.copyFile(args.file, `${path.basename(args.file, path.extname(args.file))}_backup_${Date.now()}.json`);
const filepath = path.join(process.cwd(), args.file);
const points = require(filepath); // eslint-disable-line
Object.entries(points).forEach(([game, scores]) => {
const originKey = Object.keys(scores).find((userKey) => userKey.includes(`:${args.origin}`));
const originScore = scores[originKey];
const targetKey = Object.keys(scores).find((userKey) => userKey.includes(`:${args.target}`));
const targetScore = scores[targetKey];
if (typeof originScore === 'undefined' || typeof targetScore === 'undefined') {
return;
}
const totalScore = targetScore + originScore;
points[game][targetKey] = targetScore + originScore;
delete points[game][originKey];
console.log(`${game} ${targetScore} (${args.target}) + ${originScore} (${args.origin}) = ${totalScore}`);
});
await fs.writeFile(filepath, JSON.stringify(points, null, 4));
console.log(`Saved ${filepath}`);
}
init();