This commit is contained in:
Neale Pickett 2019-11-04 16:28:35 +00:00
parent b5b578e08a
commit a3c214da1d
1 changed files with 58 additions and 58 deletions

View File

@ -7,121 +7,121 @@
<script src="moth-pwa.js" type="text/javascript"></script> <script src="moth-pwa.js" type="text/javascript"></script>
<script> <script>
function update(state) { function update(state) {
let element = document.getElementById("scoreboard"); let element = document.getElementById("scoreboard")
let teamnames = state["teams"]; let teamnames = state["teams"]
let pointslog = state["points"]; let pointslog = state["points"]
let highscore = {}; let highscore = {}
let teams = {}; let teams = {}
// Every machine that's displaying the scoreboard helpfully stores the last 20 values of // 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! // 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 been doing some letiation on this "everybody backs up the server state" trick since 2009.
// We have needed it 0 times. // We have needed it 0 times.
let pointshistory = JSON.parse(localStorage.getItem("pointshistory")) || []; let pointshistory = JSON.parse(localStorage.getItem("pointshistory")) || []
if (pointshistory.length >= 20){ if (pointshistory.length >= 20){
pointshistory.shift(); pointshistory.shift()
} }
pointshistory.push(pointslog); pointshistory.push(pointslog)
localStorage.setItem("pointshistory", JSON.stringify(pointshistory)); localStorage.setItem("pointshistory", JSON.stringify(pointshistory))
// Dole out points // Dole out points
for (let i in pointslog) { for (let i in pointslog) {
let entry = pointslog[i]; let entry = pointslog[i]
let timestamp = entry[0]; let timestamp = entry[0]
let teamhash = entry[1]; let teamhash = entry[1]
let category = entry[2]; let category = entry[2]
let points = entry[3]; let points = entry[3]
let team = teams[teamhash] || {__hash__: teamhash}; let team = teams[teamhash] || {__hash__: teamhash}
// Add points to team's points for that category // Add points to team's points for that category
team[category] = (team[category] || 0) + points; team[category] = (team[category] || 0) + points
// Record highest score in a category // Record highest score in a category
highscore[category] = Math.max(highscore[category] || 0, team[category]); highscore[category] = Math.max(highscore[category] || 0, team[category])
teams[teamhash] = team; teams[teamhash] = team
} }
// Sort by team score // Sort by team score
function teamScore(t) { function teamScore(t) {
let score = 0; let score = 0
for (let category in highscore) { for (let category in highscore) {
score += (t[category] || 0) / highscore[category]; score += (t[category] || 0) / highscore[category]
} }
return score; return score
} }
function teamCompare(a, b) { function teamCompare(a, b) {
return teamScore(a) - teamScore(b); return teamScore(a) - teamScore(b)
} }
// Figure out how to order each team on the scoreboard // Figure out how to order each team on the scoreboard
let winners = []; let winners = []
for (let i in teams) { for (let i in teams) {
winners.push(teams[i]); winners.push(teams[i])
} }
winners.sort(teamCompare); winners.sort(teamCompare)
winners.reverse(); winners.reverse()
// Clear out the element we're about to populate // Clear out the element we're about to populate
Array.from(element.childNodes).map(e => e.remove()); Array.from(element.childNodes).map(e => e.remove())
let maxWidth = 100 / Object.keys(highscore).length; let maxWidth = 100 / Object.keys(highscore).length
for (let i in winners) { for (let i in winners) {
let team = winners[i]; let team = winners[i]
let row = document.createElement("div"); let row = document.createElement("div")
let ncat = 0; let ncat = 0
for (let category in highscore) { for (let category in highscore) {
let catHigh = highscore[category]; let catHigh = highscore[category]
let catTeam = team[category] || 0; let catTeam = team[category] || 0
let catPct = catTeam / catHigh; let catPct = catTeam / catHigh
let width = maxWidth * catPct; let width = maxWidth * catPct
let bar = document.createElement("span"); let bar = document.createElement("span")
bar.classList.add("category"); bar.classList.add("category")
bar.classList.add("cat" + ncat); bar.classList.add("cat" + ncat)
bar.style.width = width + "%"; bar.style.width = width + "%"
bar.textContent = category + ": " + catTeam; bar.textContent = category + ": " + catTeam
bar.title = bar.textContent; bar.title = bar.textContent
row.appendChild(bar); row.appendChild(bar)
ncat += 1; ncat += 1
} }
let te = document.createElement("span"); let te = document.createElement("span")
te.classList.add("teamname"); te.classList.add("teamname")
te.textContent = teamnames[team.__hash__]; te.textContent = teamnames[team.__hash__]
row.appendChild(te); row.appendChild(te)
element.appendChild(row); element.appendChild(row)
} }
} }
function once() { function once() {
fetch("points.json") fetch("points.json")
.then(resp => { .then(resp => {
return resp.json(); return resp.json()
}) })
.then(obj => { .then(obj => {
update(obj); update(obj)
}) })
.catch(err => { .catch(err => {
console.log(err); console.log(err)
}); })
} }
function init() { function init() {
let base = window.location.href.replace("scoreboard.html", ""); let base = window.location.href.replace("scoreboard.html", "")
document.querySelector("#location").textContent = base; document.querySelector("#location").textContent = base
setInterval(once, 60000); setInterval(once, 60000)
once(); once()
} }
if (document.readyState === "loading") { if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", init); document.addEventListener("DOMContentLoaded", init)
} else { } else {
init(); init()
} }
</script> </script>
</head> </head>