Created
September 29, 2016 03:46
-
-
Save clayperez/99369bdb529152739bb4e51c7d5b3280 to your computer and use it in GitHub Desktop.
Time shift function to take an array of times and shift them. Works with Rhesus too.
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
// timeshift.js | |
// node timeshift --file='newEvent.rhesus' --shift=+3000 --last | |
// node timeshift --file='lgf.starts.json' --shift=+690000 | |
console.log('\nTIMESHIFT.JS'); | |
console.log('> Automatically works with simple times or entire rhesus files.\n'); | |
let argv = require('yargs').argv; | |
if (!argv.file) { | |
console.log('> Please specify a --file flag.'); | |
process.exit(1); | |
} | |
if (!argv.shift) { | |
console.log('> No --shift flag specified. Shifting by 0 time. Nothing will be changed.'); | |
}else{ | |
console.log('> Shifting all ' + (argv.last ? 'last lap' : 'start') + ' times by ' + argv.shift + ' milliseconds.'); | |
} | |
let | |
fs = require('fs'), | |
filename = argv.file, //either starttimes or rhesus file. Automatic detection | |
shift = argv.shift || 0, // how much time to change (+ / -) | |
shiftFirstTime = argv.last ? false : true, //whether to shif the first time (if not, then shift the last time) | |
sourceData, | |
modifiedData, | |
outputFile = argv.outfile || filename.split('.')[ 0 ] + '('+shift+').' + filename.split('.')[ filename.split('.').length-1 ]; | |
sourceData = JSON.parse(fs.readFileSync(filename, 'utf8')); | |
function isCategoryData(obj){return obj.hasOwnProperty('racers');} | |
// Assumes simple array of timestamps or durrations | |
if( Array.isArray(sourceData) ){ | |
if(!shiftFirstTime) { console.log('> Source data is a simple array: Ignoring "--last" flag.'); } | |
let i = sourceData.length; | |
while(i--){ | |
if(sourceData[i] !== null){ | |
sourceData[i] += shift; | |
} | |
} | |
modifiedData = JSON.stringify(sourceData, null, 2); | |
fs.writeFile(outputFile, modifiedData, { flag : 'w' }, function(err) { | |
if (err) throw err; | |
console.log('> Saving ' + outputFile); | |
console.log('> ALL DONE'); | |
}); | |
} | |
// Assumes *.rhesus race file | |
// Operate accordingly | |
else if( sourceData.hasOwnProperty('eventName') ){ | |
// Shift event start time | |
if(shiftFirstTime) { sourceData['eventStart'] += shift; } | |
for(OBJ in sourceData){ | |
let CAT = sourceData[OBJ]; | |
if(isCategoryData(CAT)){ | |
// Shift category start time | |
if(shiftFirstTime) { sourceData[OBJ]['startTime'] += shift; } | |
for(let i = 0; i < CAT['racers'].length; i++){ | |
let | |
RACER = CAT['racers'][i], | |
TIMES = RACER[5], | |
STARTTIME = TIMES[0][2], | |
ENDTIME = TIMES[ RACER[5].length-1 ][2]; | |
// Shift racer start time | |
if(shiftFirstTime) { sourceData[OBJ]['racers'][i][5][0][2] += shift; } | |
// Shift racer finish time | |
else { sourceData[OBJ]['racers'][i][5][ RACER[5].length-1 ][2] = ENDTIME + shift; } | |
} | |
} | |
} | |
modifiedData = JSON.stringify(sourceData, null, 2); | |
fs.writeFile(outputFile, modifiedData, { flag : 'w' }, function(err) { | |
if (err) throw err; | |
console.log('> Saving ' + outputFile); | |
console.log('> ALL DONE'); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment