moth/www/js/scoreboard.js

117 lines
2.7 KiB
JavaScript
Raw Normal View History

2015-04-03 16:52:43 -06:00
function loadJSON(url, callback) {
function loaded(e) {
callback(e.target.response);
}
var xhr = new XMLHttpRequest()
xhr.onload = loaded;
xhr.open("GET", url, true);
xhr.responseType = "json";
xhr.send();
}
2015-04-21 07:57:11 -06:00
function scoreboard(element, continuous) {
2015-04-03 16:52:43 -06:00
function update(state) {
var teamnames = state["teams"];
var pointslog = state["points"];
var pointshistory = JSON.parse(localStorage.getItem("pointshistory")) || [];
if (pointshistory.length >= 20){
pointshistory.shift();
}
pointshistory.push(pointslog);
localStorage.setItem("pointshistory", JSON.stringify(pointshistory));
2015-04-03 16:52:43 -06:00
var highscore = {};
var teams = {};
2015-04-03 16:52:43 -06:00
// Dole out points
for (var i in pointslog) {
var entry = pointslog[i];
var timestamp = entry[0];
var teamhash = entry[1];
var category = entry[2];
var points = entry[3];
2015-04-03 16:52:43 -06:00
var team = teams[teamhash] || {__hash__: teamhash};
2015-04-03 16:52:43 -06:00
// Add points to team's points for that category
team[category] = (team[category] || 0) + points;
2015-04-03 16:52:43 -06:00
// Record highest score in a category
highscore[category] = Math.max(highscore[category] || 0, team[category]);
2015-04-03 16:52:43 -06:00
teams[teamhash] = team;
}
2015-04-03 16:52:43 -06:00
// Sort by team score
function teamScore(t) {
var score = 0;
for (var category in highscore) {
score += (t[category] || 0) / highscore[category];
}
// XXX: This function really shouldn't have side effects.
t.__score__ = score;
return score;
}
function teamCompare(a, b) {
return teamScore(a) - teamScore(b);
}
2015-04-03 16:52:43 -06:00
var winners = [];
for (var i in teams) {
winners.push(teams[i]);
}
if (winners.length == 0) {
// No teams!
return;
}
2015-04-03 16:52:43 -06:00
winners.sort(teamCompare);
winners.reverse();
2015-04-03 16:52:43 -06:00
// Clear out the element we're about to populate
while (element.lastChild) {
element.removeChild(element.lastChild);
}
2015-04-03 16:52:43 -06:00
// Populate!
var topActualScore = winners[0].__score__;
// (100 / ncats) * (ncats / topActualScore);
var maxWidth = 100 / topActualScore;
for (var i in winners) {
var team = winners[i];
var row = document.createElement("div");
var ncat = 0;
for (var category in highscore) {
var catHigh = highscore[category];
var catTeam = team[category] || 0;
var catPct = catTeam / catHigh;
var width = maxWidth * catPct;
2015-04-03 16:52:43 -06:00
var bar = document.createElement("span");
bar.classList.add("cat" + ncat);
bar.style.width = width + "%";
bar.textContent = category + ": " + catTeam;
bar.title = bar.textContent;
2015-04-03 16:52:43 -06:00
row.appendChild(bar);
ncat += 1;
}
2015-04-03 16:52:43 -06:00
var te = document.createElement("span");
te.classList.add("teamname");
te.textContent = teamnames[team.__hash__];
row.appendChild(te);
2015-04-03 16:52:43 -06:00
element.appendChild(row);
}
}
2015-04-21 07:57:11 -06:00
function once() {
loadJSON("points.json", update);
}
if (continuous) {
setInterval(once, 60000);
}
once();
2015-04-03 16:52:43 -06:00
}