Skip to content

Instantly share code, notes, and snippets.

@Sascha-T
Created May 29, 2021 18:58
Show Gist options
  • Save Sascha-T/573b41ce50fe9cfe3cc8814d379d83bc to your computer and use it in GitHub Desktop.
Save Sascha-T/573b41ce50fe9cfe3cc8814d379d83bc to your computer and use it in GitHub Desktop.
MultiMC Time Counter (place in instances folder)
let fs = require("fs")
let path = require("path")
let os = require("os")
let totalTime = 0;
let dir = fs.readdirSync(".")
for(let name of dir) {
let dir = fs.lstatSync(name)
if(dir.isDirectory()) {
let dataFile = path.join(name, "instance.cfg")
if(!fs.existsSync(dataFile))
continue
let file = fs.readFileSync(dataFile, "utf8")
let lines = file.split(/\r?\n/)
let time = 0;
for(let line of lines) {
let lineParts = line.split("=")
if(lineParts[0]=="totalTimePlayed") {
time = parseInt(lineParts[1])
break
}
}
totalTime += time;
console.log(`"${name}":`)
if(time != 0) {
console.log(` - ${time}s`)
} else {
console.log(" - Unplayed")
}
}
}
// Copied from https://www.codegrepper.com/code-examples/javascript/typescript+convert+seconds+to+days+hours+minutes
function secondsToDhms(seconds) {
let d = Math.floor(seconds / (3600*24));
let h = Math.floor(seconds % (3600*24) / 3600);
let m = Math.floor(seconds % 3600 / 60);
let s = Math.floor(seconds % 60);
let dDisplay = d > 0 ? d + (d == 1 ? " day, " : " days, ") : "";
let hDisplay = h > 0 ? h + (h == 1 ? " hour, " : " hours, ") : "";
let mDisplay = m > 0 ? m + (m == 1 ? " minute, " : " minutes, ") : "";
let sDisplay = s > 0 ? s + (s == 1 ? " second" : " seconds") : "";
return dDisplay + hDisplay + mDisplay + sDisplay;
}
let time = secondsToDhms(totalTime);
console.log(time)
console.log("Total time: "+time)
console.log(` Alternatively ${Math.round(totalTime / 60 / 60)} hours`)
@Sascha-T
Copy link
Author

no warranty btw

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment