Skip to content

Instantly share code, notes, and snippets.

@phloe
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phloe/11399422 to your computer and use it in GitHub Desktop.
Save phloe/11399422 to your computer and use it in GitHub Desktop.
Snippet to get live data from ESC DR Battle
(function (global, doc) {
// expose a function to recieve data via JSONP
global.getBattle = function getBattle (data) {
// calculate ranks of songs. ("questions" are songs)
var rankedData = rankData(data.reply.battle.questions);
// make the data more readable...
var sanetizedData = sanetizeData(rankedData);
// do stuff with sanetizedData here...
};
// get data via JSONP
var url = "http://dr-battle-api.azurewebsites.net/drbattle_api/battle/getbattle?battleId=17&callback=getBattle";
var script = document.createElement("script");
script.src = url;
document.body.appendChild(script);
function rankData (questions) {
// positions to points translation
var values = {
"1st": 12,
"2nd": 10,
"3rd": 8,
"4th - 5th": 0,
"6th -10th": 0,
"-": 0
};
// calculate points for each song
questions.forEach(function (question) {
question.points = question.options.map(function (a) {
return values[a.shortValue] * a.votes;
}).reduce(function (a, b) {
return a + b;
}, 0);
});
// sort songs by points
questions.sort(function (a, b) {
return b.points - a.points;
});
return questions;
}
function sanetizeData (questions) {
return questions.map(function (question) {
return {
title: question.text,
country: question.asset1,
points: question.points
};
});
}
} (this, document));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment