Skip to content

Instantly share code, notes, and snippets.

@ov3rk1ll
Last active March 15, 2018 08:28
Show Gist options
  • Select an option

  • Save ov3rk1ll/52632c96c2d87d0dbcef29012efac62e to your computer and use it in GitHub Desktop.

Select an option

Save ov3rk1ll/52632c96c2d87d0dbcef29012efac62e to your computer and use it in GitHub Desktop.
Tool zum Lesen des Salzburger CityCaching JSON Formats
// node citycaching.js file [start uuid]
// Make sure lodash is installed!
var fs = require('fs');
var _ = require('lodash');
String.prototype.replaceAll = function(search, replacement) {
var target = this;
return target.replace(new RegExp(search, 'g'), replacement);
};
var componentHandler = {
'location-riddle': function(node, prefix){
var text = node.attributes.text; if(text == null) text = "[NULL]";
var hint = node.attributes.hint; if(hint == null) hint = "[NULL]";
console.log(prefix + "\t" + "Text: " + text.replaceAll("\n", " "));
console.log(prefix + "\t" + "Hint: " + hint.replaceAll("\n", " "));
},
'location': function(node, prefix){
console.log(prefix + "\t" + "Location: " + node.attributes.latitude + " " + node.attributes.longitude);
console.log(prefix + "\t" + "Radius: " + node.attributes.radius);
maps += node.attributes.latitude + "+" + node.attributes.longitude + "/";
if(startLocation == "") {
startLocation = node.attributes.latitude + "+" + node.attributes.longitude + "/";
}
},
'multiquiz': function(node, prefix){
var question = node.attributes.question; if(question == null) question = "[NULL]";
console.log(prefix + "\t" + "Question: " + question.replaceAll("\n", " "));
var answers = JSON.parse(node.attributes.answers);
var correct = JSON.parse(node.attributes.correct);
for(var i = 0; i < answers.length; i++){
console.log(prefix + "\t\t" + (i + 1) + ": " + answers[i] + " (" + (correct.indexOf(i) != -1 ? 'correct' : 'incorrect') + ")");
}
}
};
console.log("Reading " + process.argv[2] + "...");
var obj = JSON.parse(fs.readFileSync(process.argv[2], 'utf8'));
if(process.argv.length == 3){ // Show list and stop
for(i = 0; i < obj.data.relationships.campaigns.data.length; i++){
id = obj.data.relationships.campaigns.data[i].id;
campaign = _.find(obj.included, {id: id});
console.log(id + "\t[" + campaign.attributes.state + "]\t" + campaign.attributes.name);
}
return;
}
var start = _.find(obj.included, {id: process.argv[3]});
var maps = 'https://www.google.de/maps/dir/';
var startLocation = "";
console.log("Starting to trace " + start.attributes.name + " (type: " + start.type + ")");
var done = false;
for(i = 0; i < start.relationships.nodes.data.length; i++){
handleNode(obj, start.relationships.nodes.data[i].id, "", i);
}
console.log("End has been reached");
console.log("Route " + maps + startLocation);
function handleNode(obj, id, prefix, count){
var node = _.find(obj.included, {id: id});
if(node.type == 'transition'){
handleNode(obj, node.attributes.data.target, prefix, count);
} else if(node.type == 'node'){
var items = _.sortBy(node.relationships.components.data, function(o){
tmp = _.find(obj.included, {id: o.id});
return tmp.attributes.orderId;
});
console.log(prefix + "Found #" + (count + 1) + " \"" + node.attributes.name + "\" with " + items.length + " item(s)");
for(var i = 0; i < items.length; i++){
handleNode(obj, items[i].id, prefix + "\t", count);
}
} else if(node.type == 'component'){
console.log(prefix + 'component::' + node.attributes.type + " (" + node.id + "):");
if(componentHandler.hasOwnProperty(node.attributes.type)){
componentHandler[node.attributes.type](node, prefix);
} else {
console.log(prefix + "\t" + "Not used");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment