Created
March 6, 2018 05:38
-
-
Save apottere/5b9cf1608d64d73551a8d7b8efa929da to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/usr/bin/env node | |
const path = require('path'); | |
const fs = require('fs'); | |
const arrow = "➞"; | |
const cwdFile = (name) => { | |
return path.join(process.cwd(), name); | |
}; | |
const loadFile = (name) => { | |
return JSON.parse(fs.readFileSync(cwdFile(name))); | |
}; | |
const backup = () => { | |
const file = cwdFile('QuestProgress.json'); | |
const newFile = cwdFile('QuestProgress.json.' + Date.now()); | |
console.log(`Backing up ${file} => ${newFile}`); | |
fs.copyFileSync(file, newFile); | |
}; | |
const sync = (from, to, dryRun) => { | |
const nameToUuid = Object.values(loadFile('NameCache.json')["nameCache:9"]).reduce((names, value) => { | |
names[value["name:8"]] = value["uuid:8"]; | |
return names; | |
}, {}); | |
const questIdToName = Object.values(loadFile('QuestDatabase.json')["questDatabase:9"]).reduce((quests, quest) => { | |
quests[quest["questID:3"]] = quest["properties:10"]["betterquesting:10"]["name:8"]; | |
return quests; | |
}, {}); | |
const fromUuid = nameToUuid[from]; | |
const toUuid = nameToUuid[to]; | |
if(!fromUuid) throw new Error(`Player ${from} not found!`); | |
if(!toUuid) throw new Error(`Player ${to} not found!`); | |
const questProgressObject = loadFile('QuestProgress.json'); | |
const questsToSync = Object.values(questProgressObject["questProgress:9"]).filter(quest => { | |
const completed = Object.values(quest["completed:9"]); | |
return completed.some(completer => completer["uuid:8"] === fromUuid) && | |
!completed.some(completer => completer["uuid:8"] === toUuid); | |
}); | |
if(!questsToSync.length) { | |
console.log(`No quests to sync from ${from} (${fromUuid}) to ${to} (${toUuid}).`); | |
return; | |
} | |
if(dryRun) { | |
console.log(`A sync from ${from} (${fromUuid}) to ${to} (${toUuid}) would sync the following quests:`); | |
questsToSync.forEach(quest => { | |
const id = quest["questID:3"]; | |
console.log(` ${arrow} ${padQuestId(id)}: ${questIdToName[id]}`); | |
}); | |
return; | |
} | |
console.log(`Syncing quests from ${from} (${fromUuid}) to ${to} (${toUuid})...`); | |
questsToSync.forEach(quest => { | |
const id = quest["questID:3"]; | |
console.log(` ${arrow} ${padQuestId(id)}: ${questIdToName[id]}`); | |
const oldCompleted = quest["completed:9"] | |
const completed = Object.values(oldCompleted); | |
const completerFrom = completed.find(completer => completer["uuid:8"] === fromUuid); | |
const completerTo = { | |
"claimed:1": 0, | |
"uuid:8": toUuid, | |
"timestamp.4": Math.max(...(completed.map(completer => completer["timestamp:4"]))) + 1 | |
} | |
completed.push(completerTo); | |
const newCompleted = completed.reduce((obj, completer, index) => { | |
obj[index + ":10"] = completer; | |
return obj; | |
}, {}); | |
quest["completed:9"] = newCompleted; | |
}); | |
fs.writeFileSync(cwdFile('QuestProgress.json'), JSON.stringify(questProgressObject, null, 2)); | |
}; | |
const padQuestId = (id) => { | |
const pad = " "; | |
return (pad + id).slice(-pad.length); | |
}; | |
const args = process.argv; | |
if(args[2] === 'backup') { | |
backup(); | |
return; | |
} | |
if(args[2] === 'sync-dry') { | |
sync(args[3], args[4], true); | |
return; | |
}; | |
if(args[2] === 'sync') { | |
sync(args[3], args[4], false); | |
return; | |
}; | |
console.log(args); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment