Added user point merging utility.
This commit is contained in:
parent
d510caa123
commit
bad5dc52f7
|
@ -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();
|
Loading…
Reference in New Issue