2018-09-18 18:22:03 -06:00
|
|
|
<!DOCTYPE html>
|
|
|
|
<html>
|
|
|
|
<head>
|
2018-09-18 21:29:05 -06:00
|
|
|
<title>Scoreboard</title>
|
2018-09-18 18:22:03 -06:00
|
|
|
<link rel="stylesheet" href="basic.css">
|
|
|
|
<meta name="viewport" content="width=device-width">
|
|
|
|
<script>
|
2018-10-10 17:33:26 -06:00
|
|
|
function update(state) {
|
|
|
|
let element = document.getElementById("scoreboard");
|
|
|
|
let teamnames = state["teams"];
|
|
|
|
let pointslog = state["points"];
|
|
|
|
let highscore = {};
|
|
|
|
let teams = {};
|
|
|
|
|
|
|
|
// Every machine that's displaying the scoreboard helpfully stores the last 20 values of
|
|
|
|
// points.json for us, in case of catastrophe. Thanks, y'all!
|
|
|
|
//
|
|
|
|
// We have been doing some letiation on this "everybody backs up the server state" trick since 2009.
|
|
|
|
// We have needed it 0 times.
|
|
|
|
let pointshistory = JSON.parse(localStorage.getItem("pointshistory")) || [];
|
|
|
|
if (pointshistory.length >= 20){
|
|
|
|
pointshistory.shift();
|
2018-10-02 09:22:09 -06:00
|
|
|
}
|
2018-10-10 17:33:26 -06:00
|
|
|
pointshistory.push(pointslog);
|
|
|
|
localStorage.setItem("pointshistory", JSON.stringify(pointshistory));
|
2018-10-02 09:22:09 -06:00
|
|
|
|
2018-10-10 17:33:26 -06:00
|
|
|
// Dole out points
|
|
|
|
for (let i in pointslog) {
|
|
|
|
let entry = pointslog[i];
|
|
|
|
let timestamp = entry[0];
|
|
|
|
let teamhash = entry[1];
|
|
|
|
let category = entry[2];
|
|
|
|
let points = entry[3];
|
2018-10-02 09:22:09 -06:00
|
|
|
|
2018-10-10 17:33:26 -06:00
|
|
|
let team = teams[teamhash] || {__hash__: teamhash};
|
2018-10-02 09:22:09 -06:00
|
|
|
|
2018-10-10 17:33:26 -06:00
|
|
|
// Add points to team's points for that category
|
|
|
|
team[category] = (team[category] || 0) + points;
|
2018-10-02 09:22:09 -06:00
|
|
|
|
2018-10-10 17:33:26 -06:00
|
|
|
// Record highest score in a category
|
|
|
|
highscore[category] = Math.max(highscore[category] || 0, team[category]);
|
2018-10-02 09:22:09 -06:00
|
|
|
|
2018-10-10 17:33:26 -06:00
|
|
|
teams[teamhash] = team;
|
|
|
|
}
|
2018-10-02 09:22:09 -06:00
|
|
|
|
2018-10-10 17:33:26 -06:00
|
|
|
// Sort by team score
|
|
|
|
function teamScore(t) {
|
|
|
|
let score = 0;
|
|
|
|
for (let category in highscore) {
|
|
|
|
score += (t[category] || 0) / highscore[category];
|
2018-10-02 09:22:09 -06:00
|
|
|
}
|
2018-10-10 17:33:26 -06:00
|
|
|
return score;
|
|
|
|
}
|
|
|
|
function teamCompare(a, b) {
|
|
|
|
return teamScore(a) - teamScore(b);
|
|
|
|
}
|
2018-10-02 09:22:09 -06:00
|
|
|
|
2018-10-10 17:33:26 -06:00
|
|
|
// Figure out how to order each team on the scoreboard
|
|
|
|
let winners = [];
|
|
|
|
for (let i in teams) {
|
|
|
|
winners.push(teams[i]);
|
|
|
|
}
|
|
|
|
winners.sort(teamCompare);
|
|
|
|
winners.reverse();
|
|
|
|
|
|
|
|
// Clear out the element we're about to populate
|
|
|
|
Array.from(element.childNodes).map(e => e.remove());
|
|
|
|
|
|
|
|
let maxWidth = 100 / Object.keys(highscore).length;
|
|
|
|
for (let i in winners) {
|
|
|
|
let team = winners[i];
|
|
|
|
let row = document.createElement("div");
|
|
|
|
let ncat = 0;
|
|
|
|
for (let category in highscore) {
|
|
|
|
let catHigh = highscore[category];
|
|
|
|
let catTeam = team[category] || 0;
|
|
|
|
let catPct = catTeam / catHigh;
|
|
|
|
let width = maxWidth * catPct;
|
|
|
|
|
|
|
|
let bar = document.createElement("span");
|
|
|
|
bar.classList.add("category");
|
|
|
|
bar.classList.add("cat" + ncat);
|
|
|
|
bar.style.width = width + "%";
|
|
|
|
bar.textContent = category + ": " + catTeam;
|
|
|
|
bar.title = bar.textContent;
|
|
|
|
|
|
|
|
row.appendChild(bar);
|
|
|
|
ncat += 1;
|
2018-10-02 09:22:09 -06:00
|
|
|
}
|
|
|
|
|
2018-10-10 17:33:26 -06:00
|
|
|
let te = document.createElement("span");
|
|
|
|
te.classList.add("teamname");
|
|
|
|
te.textContent = teamnames[team.__hash__];
|
|
|
|
row.appendChild(te);
|
2018-10-02 09:22:09 -06:00
|
|
|
|
2018-10-10 17:33:26 -06:00
|
|
|
element.appendChild(row);
|
2018-10-02 09:22:09 -06:00
|
|
|
}
|
2018-10-10 17:33:26 -06:00
|
|
|
}
|
2018-10-02 09:22:09 -06:00
|
|
|
|
2018-10-10 17:33:26 -06:00
|
|
|
function once() {
|
|
|
|
fetch("points.json")
|
|
|
|
.then(resp => {
|
|
|
|
return resp.json();
|
|
|
|
})
|
|
|
|
.then(obj => {
|
|
|
|
update(obj);
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
console.log(err);
|
|
|
|
});
|
2018-09-18 18:22:03 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
function init() {
|
2018-10-10 17:33:26 -06:00
|
|
|
setInterval(once, 60000);
|
|
|
|
once();
|
|
|
|
}
|
|
|
|
if (document.readyState === "loading") {
|
|
|
|
document.addEventListener("DOMContentLoaded", init);
|
|
|
|
} else {
|
|
|
|
init();
|
2018-09-18 18:22:03 -06:00
|
|
|
}
|
|
|
|
</script>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<h1 class="Success">Scoreboard</h1>
|
|
|
|
<section>
|
|
|
|
<div id="scoreboard"></div>
|
|
|
|
</section>
|
|
|
|
<nav>
|
|
|
|
<ul>
|
2019-02-22 19:09:38 -07:00
|
|
|
<li><a href="index.html">Puzzles</a></li>
|
2018-09-18 18:22:03 -06:00
|
|
|
<li><a href="scoreboard.html">Scoreboard</a></li>
|
|
|
|
</ul>
|
|
|
|
</nav>
|
|
|
|
</body>
|
|
|
|
</html>
|