Last active
January 2, 2020 20:33
-
-
Save roccodev/eaf4c132b88d4416715b2ae8d200aa41 to your computer and use it in GitHub Desktop.
Bedwars monthlies file merger
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright (C) 2020 RoccoDev | |
// Licensed under the MIT license. | |
// <https://opensource.org/licenses/MIT> | |
"use strict"; | |
const fs = require("fs") | |
const jsonDir = process.argv[2]; | |
let result = {}; | |
let sortForPlace = {}; | |
fs.readdir(jsonDir, (err, files) => { | |
if(err) throw err | |
files.forEach(parseFile) | |
done() | |
}) | |
function parseFile(file) { | |
const data = JSON.parse(fs.readFileSync(jsonDir + file)) | |
Object.keys(data).forEach(uuid => { | |
const profile = data[uuid] | |
let edit = result[uuid] | |
if(!edit) edit = { | |
"_____place": 0, | |
"____name": "", | |
"__points": 0, | |
"_kills": 0, | |
"_kjdeaths": 0, | |
"_victories": 0, | |
"played": 0, | |
"zBeds": 0, | |
"zTeams": 0 | |
} | |
edit["____name"] = profile["____name"] | |
edit["_kills"] += profile["_kills"] | |
edit["_kjdeaths"] += profile["_kjdeaths"] | |
edit["_victories"] += profile["_victories"] | |
edit["played"] += profile["played"] | |
edit["zBeds"] += profile["zBeds"] | |
edit["zTeams"] += profile["zTeams"] | |
if(!sortForPlace[uuid]) { | |
sortForPlace[uuid] = 0; | |
} | |
sortForPlace[uuid] += profile["__points"] | |
result[uuid] = edit | |
}) | |
} | |
function done() { | |
let sorting = []; | |
Object.keys(sortForPlace).forEach(k => sorting.push([k, sortForPlace[k]])) | |
sorting.sort((obj1, obj2) => { | |
const a = obj1[1] | |
const b = obj2[1] | |
return a > b ? -1 : a < b ? 1 : 0 | |
}) | |
sorting.forEach((arr, i) => { | |
result[arr[0]]["_____place"] = i + 1 | |
result[arr[0]]["__points"] = arr[1] | |
}) | |
fs.writeFile(jsonDir + (process.argv[3] || "out.json"), JSON.stringify(result), () => {}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment