Skip to content

Instantly share code, notes, and snippets.

@sleepygarden
Created September 6, 2016 15:46
Show Gist options
  • Save sleepygarden/a894cba25bb1503b952153f22e4b5f62 to your computer and use it in GitHub Desktop.
Save sleepygarden/a894cba25bb1503b952153f22e4b5f62 to your computer and use it in GitHub Desktop.
(function(){
var csv = require('fast-csv');
var jsonfile = require('jsonfile');
var monsters = {};
var skipHeaderRow = true;
var inputFile = "Breeding_Combinations.csv"; // implied to sibling of convert_siralim_csv.js
var outputFile = "MonsterInfo.json"; // explicitly sibling of convert_siralim_csv.js
var csvMap = [ // maps row columns to property names
"family",
"offspring",
"parent",
"mate"
];
// family, result, parent A, parent B
function processRow(row){
if (skipHeaderRow){
skipHeaderRow = false;
return;
}
var breedCombo = {};
for (var idx=0; idx < csvMap.length; idx++){
breedCombo[csvMap[idx]] = row[idx].trim();
}
// new monster
if (monsters[breedCombo.offspring] === undefined){
monsters[breedCombo.offspring] = {
name: breedCombo.offspring,
family: breedCombo.family,
from: []
};
}
monsters[breedCombo.offspring].from.push({
type: "breeding", // other types could be quest rewards, gifts from gods, etc. tbd
parent: breedCombo.parent,
mate: breedCombo.mate
});
}
function onCompleteCSV(){
var file = __dirname + '/' + outputFile;
var obj = {monsters: monsters};
jsonfile.writeFile(file, obj, {spaces: 4}, function(err) {
if (err !== null){
console.error("Error writing json",err);
}
else {
console.log("Finished CSV -> JSON transform, wrote to ./"+outputFile);
}
});
}
function convert(){
console.log("Reading CSV at ./"+inputFile);
csv.fromPath(inputFile)
.on("data", processRow)
.on("end", onCompleteCSV);
}
convert();
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment