mirror of https://github.com/dirtbags/moth.git
move animated scoreboard into it's own file and resurrect static
scoreboard
This commit is contained in:
parent
f07ea5ff4d
commit
4950bbc190
|
@ -0,0 +1,164 @@
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
function scoreboardHistoryPush(pointslog) {
|
||||||
|
let pointsHistory = JSON.parse(localStorage.getItem("pointsHistory")) || [];
|
||||||
|
if (pointsHistory.length >= 20) {
|
||||||
|
pointsHistory.shift();
|
||||||
|
}
|
||||||
|
pointsHistory.push(pointslog);
|
||||||
|
localStorage.setItem("pointsHistory", JSON.stringify(pointsHistory));
|
||||||
|
}
|
||||||
|
|
||||||
|
function scoreboard(element, continuous) {
|
||||||
|
function update(state) {
|
||||||
|
let teamNames = state["teams"];
|
||||||
|
let pointsLog = state["points"];
|
||||||
|
|
||||||
|
// Establish scores, calculate category maximums
|
||||||
|
let categories = {};
|
||||||
|
let maxPointsByCategory = {};
|
||||||
|
let totalPointsByTeamByCategory = {};
|
||||||
|
for (let entry of pointsLog) {
|
||||||
|
let entryTimeStamp = entry[0];
|
||||||
|
let entryTeamHash = entry[1];
|
||||||
|
let entryCategory = entry[2];
|
||||||
|
let entryPoints = entry[3];
|
||||||
|
|
||||||
|
// Populate list of all categories
|
||||||
|
categories[entryCategory] = entryCategory;
|
||||||
|
|
||||||
|
// Add points to team's points for that category
|
||||||
|
let points = totalPointsByTeamByCategory[entryTeamHash] || {};
|
||||||
|
let categoryPoints = points[entryCategory] || 0;
|
||||||
|
categoryPoints += entryPoints;
|
||||||
|
points[entryCategory] = categoryPoints;
|
||||||
|
totalPointsByTeamByCategory[entryTeamHash] = points;
|
||||||
|
|
||||||
|
// Calculate maximum points scored in each category
|
||||||
|
let m = maxPointsByCategory[entryCategory] || 0;
|
||||||
|
maxPointsByCategory[entryCategory] = Math.max(m, categoryPoints);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Calculate overall scores
|
||||||
|
let overallScore = {};
|
||||||
|
let orderedOverallScores = [];
|
||||||
|
for (let teamHash in teamNames) {
|
||||||
|
var score = 0;
|
||||||
|
for (let cat in categories) {
|
||||||
|
var catPoints = totalPointsByTeamByCategory[teamHash][cat] || 0;
|
||||||
|
if (catPoints > 0) {
|
||||||
|
score += catPoints / maxPointsByCategory[cat];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
overallScore[teamHash] = score;
|
||||||
|
orderedOverallScores.push([score, teamHash]);
|
||||||
|
}
|
||||||
|
orderedOverallScores.sort();
|
||||||
|
orderedOverallScores.reverse();
|
||||||
|
|
||||||
|
// Clear out the element we're about to populate
|
||||||
|
while (element.lastChild) {
|
||||||
|
element.removeChild(element.lastChild);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set up scoreboard structure
|
||||||
|
let spansByTeamByCategory = {};
|
||||||
|
for (let pair of orderedOverallScores) {
|
||||||
|
let teamHash = pair[1];
|
||||||
|
let teamName = teamNames[teamHash];
|
||||||
|
let teamRow = document.createElement("div");
|
||||||
|
let ncat = 0;
|
||||||
|
spansByTeamByCategory[teamHash] = {};
|
||||||
|
for (let cat in categories) {
|
||||||
|
let catSpan = document.createElement("span");
|
||||||
|
catSpan.classList.add("cat" + ncat);
|
||||||
|
catSpan.style.width = "0%";
|
||||||
|
catSpan.textContent = cat + ": 0";
|
||||||
|
|
||||||
|
spansByTeamByCategory[teamHash][cat] = catSpan;
|
||||||
|
|
||||||
|
teamRow.appendChild(catSpan);
|
||||||
|
ncat += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
var te = document.createElement("span");
|
||||||
|
te.classList.add("teamname");
|
||||||
|
te.textContent = teamName;
|
||||||
|
teamRow.appendChild(te);
|
||||||
|
|
||||||
|
element.appendChild(teamRow);
|
||||||
|
}
|
||||||
|
|
||||||
|
// How many categories are there?
|
||||||
|
var numCategories = 0;
|
||||||
|
for (var cat in categories) {
|
||||||
|
numCategories += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Replay points log, displaying scoreboard at each step
|
||||||
|
let replayTimer = null;
|
||||||
|
let replayIndex = 0;
|
||||||
|
function replayStep(event) {
|
||||||
|
if (replayIndex > pointsLog.length) {
|
||||||
|
clearInterval(replayTimer);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Replay log up until replayIndex
|
||||||
|
let totalPointsByTeamByCategory = {};
|
||||||
|
for (let index = 0; index < replayIndex; index += 1) {
|
||||||
|
let entry = pointsLog[index];
|
||||||
|
let entryTimeStamp = entry[0];
|
||||||
|
let entryTeamHash = entry[1];
|
||||||
|
let entryCategory = entry[2];
|
||||||
|
let entryPoints = entry[3];
|
||||||
|
|
||||||
|
// Add points to team's points for that category
|
||||||
|
let points = totalPointsByTeamByCategory[entryTeamHash] || {};
|
||||||
|
let categoryPoints = points[entryCategory] || 0;
|
||||||
|
categoryPoints += entryPoints;
|
||||||
|
points[entryCategory] = categoryPoints;
|
||||||
|
totalPointsByTeamByCategory[entryTeamHash] = points;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Figure out everybody's score
|
||||||
|
for (let teamHash in teamNames) {
|
||||||
|
for (let cat in categories) {
|
||||||
|
let totalPointsByCategory = totalPointsByTeamByCategory[teamHash] || {};
|
||||||
|
let points = totalPointsByCategory[cat] || 0;
|
||||||
|
if (points > 0) {
|
||||||
|
let score = points / maxPointsByCategory[cat];
|
||||||
|
let span = spansByTeamByCategory[teamHash][cat];
|
||||||
|
let width = (100.0 / numCategories) * score;
|
||||||
|
|
||||||
|
span.style.width = width + "%";
|
||||||
|
span.textContent = cat + ": " + points;
|
||||||
|
span.title = span.textContent;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
replayIndex += 1;
|
||||||
|
}
|
||||||
|
replayStep();
|
||||||
|
replayTimer = setInterval(replayStep, 20);
|
||||||
|
}
|
||||||
|
|
||||||
|
function once() {
|
||||||
|
loadJSON("points.json", update);
|
||||||
|
}
|
||||||
|
if (continuous) {
|
||||||
|
setInterval(once, 60000);
|
||||||
|
}
|
||||||
|
once();
|
||||||
|
}
|
||||||
|
|
|
@ -9,148 +9,101 @@ function loadJSON(url, callback) {
|
||||||
xhr.send();
|
xhr.send();
|
||||||
}
|
}
|
||||||
|
|
||||||
function scoreboardHistoryPush(pointslog) {
|
|
||||||
let pointsHistory = JSON.parse(localStorage.getItem("pointsHistory")) || [];
|
|
||||||
if (pointsHistory.length >= 20) {
|
|
||||||
pointsHistory.shift();
|
|
||||||
}
|
|
||||||
pointsHistory.push(pointslog);
|
|
||||||
localStorage.setItem("pointsHistory", JSON.stringify(pointsHistory));
|
|
||||||
}
|
|
||||||
|
|
||||||
function scoreboard(element, continuous) {
|
function scoreboard(element, continuous) {
|
||||||
function update(state) {
|
function update(state) {
|
||||||
let teamNames = state["teams"];
|
var teamnames = state["teams"];
|
||||||
let pointsLog = state["points"];
|
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));
|
||||||
|
var highscore = {};
|
||||||
|
var teams = {};
|
||||||
|
|
||||||
// Establish scores, calculate category maximums
|
// Dole out points
|
||||||
let categories = {};
|
for (var i in pointslog) {
|
||||||
let maxPointsByCategory = {};
|
var entry = pointslog[i];
|
||||||
let totalPointsByTeamByCategory = {};
|
var timestamp = entry[0];
|
||||||
for (let entry of pointsLog) {
|
var teamhash = entry[1];
|
||||||
let entryTimeStamp = entry[0];
|
var category = entry[2];
|
||||||
let entryTeamHash = entry[1];
|
var points = entry[3];
|
||||||
let entryCategory = entry[2];
|
|
||||||
let entryPoints = entry[3];
|
|
||||||
|
|
||||||
// Populate list of all categories
|
var team = teams[teamhash] || {__hash__: teamhash};
|
||||||
categories[entryCategory] = entryCategory;
|
|
||||||
|
|
||||||
// Add points to team's points for that category
|
// Add points to team's points for that category
|
||||||
let points = totalPointsByTeamByCategory[entryTeamHash] || {};
|
team[category] = (team[category] || 0) + points;
|
||||||
let categoryPoints = points[entryCategory] || 0;
|
|
||||||
categoryPoints += entryPoints;
|
|
||||||
points[entryCategory] = categoryPoints;
|
|
||||||
totalPointsByTeamByCategory[entryTeamHash] = points;
|
|
||||||
|
|
||||||
// Calculate maximum points scored in each category
|
// Record highest score in a category
|
||||||
let m = maxPointsByCategory[entryCategory] || 0;
|
highscore[category] = Math.max(highscore[category] || 0, team[category]);
|
||||||
maxPointsByCategory[entryCategory] = Math.max(m, categoryPoints);
|
|
||||||
|
teams[teamhash] = team;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Calculate overall scores
|
// Sort by team score
|
||||||
let overallScore = {};
|
function teamScore(t) {
|
||||||
let orderedOverallScores = [];
|
|
||||||
for (let teamHash in teamNames) {
|
|
||||||
var score = 0;
|
var score = 0;
|
||||||
for (let cat in categories) {
|
|
||||||
var catPoints = totalPointsByTeamByCategory[teamHash][cat] || 0;
|
for (var category in highscore) {
|
||||||
if (catPoints > 0) {
|
score += (t[category] || 0) / highscore[category];
|
||||||
score += catPoints / maxPointsByCategory[cat];
|
|
||||||
}
|
}
|
||||||
|
// XXX: This function really shouldn't have side effects.
|
||||||
|
t.__score__ = score;
|
||||||
|
return score;
|
||||||
}
|
}
|
||||||
overallScore[teamHash] = score;
|
function teamCompare(a, b) {
|
||||||
orderedOverallScores.push([score, teamHash]);
|
return teamScore(a) - teamScore(b);
|
||||||
}
|
}
|
||||||
orderedOverallScores.sort();
|
|
||||||
orderedOverallScores.reverse();
|
var winners = [];
|
||||||
|
for (var i in teams) {
|
||||||
|
winners.push(teams[i]);
|
||||||
|
}
|
||||||
|
if (winners.length == 0) {
|
||||||
|
// No teams!
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
winners.sort(teamCompare);
|
||||||
|
winners.reverse();
|
||||||
|
|
||||||
// Clear out the element we're about to populate
|
// Clear out the element we're about to populate
|
||||||
while (element.lastChild) {
|
while (element.lastChild) {
|
||||||
element.removeChild(element.lastChild);
|
element.removeChild(element.lastChild);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set up scoreboard structure
|
// Populate!
|
||||||
let spansByTeamByCategory = {};
|
var topActualScore = winners[0].__score__;
|
||||||
for (let pair of orderedOverallScores) {
|
|
||||||
let teamHash = pair[1];
|
|
||||||
let teamName = teamNames[teamHash];
|
|
||||||
let teamRow = document.createElement("div");
|
|
||||||
let ncat = 0;
|
|
||||||
spansByTeamByCategory[teamHash] = {};
|
|
||||||
for (let cat in categories) {
|
|
||||||
let catSpan = document.createElement("span");
|
|
||||||
catSpan.classList.add("cat" + ncat);
|
|
||||||
catSpan.style.width = "0%";
|
|
||||||
catSpan.textContent = cat + ": 0";
|
|
||||||
|
|
||||||
spansByTeamByCategory[teamHash][cat] = catSpan;
|
// (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;
|
||||||
|
|
||||||
teamRow.appendChild(catSpan);
|
var bar = document.createElement("span");
|
||||||
|
bar.classList.add("cat" + ncat);
|
||||||
|
bar.style.width = width + "%";
|
||||||
|
bar.textContent = category + ": " + catTeam;
|
||||||
|
bar.title = bar.textContent;
|
||||||
|
|
||||||
|
row.appendChild(bar);
|
||||||
ncat += 1;
|
ncat += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
var te = document.createElement("span");
|
var te = document.createElement("span");
|
||||||
te.classList.add("teamname");
|
te.classList.add("teamname");
|
||||||
te.textContent = teamName;
|
te.textContent = teamnames[team.__hash__];
|
||||||
teamRow.appendChild(te);
|
row.appendChild(te);
|
||||||
|
|
||||||
element.appendChild(teamRow);
|
element.appendChild(row);
|
||||||
}
|
}
|
||||||
|
|
||||||
// How many categories are there?
|
|
||||||
var numCategories = 0;
|
|
||||||
for (var cat in categories) {
|
|
||||||
numCategories += 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Replay points log, displaying scoreboard at each step
|
|
||||||
let replayTimer = null;
|
|
||||||
let replayIndex = 0;
|
|
||||||
function replayStep(event) {
|
|
||||||
if (replayIndex > pointsLog.length) {
|
|
||||||
clearInterval(replayTimer);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Replay log up until replayIndex
|
|
||||||
let totalPointsByTeamByCategory = {};
|
|
||||||
for (let index = 0; index < replayIndex; index += 1) {
|
|
||||||
let entry = pointsLog[index];
|
|
||||||
let entryTimeStamp = entry[0];
|
|
||||||
let entryTeamHash = entry[1];
|
|
||||||
let entryCategory = entry[2];
|
|
||||||
let entryPoints = entry[3];
|
|
||||||
|
|
||||||
// Add points to team's points for that category
|
|
||||||
let points = totalPointsByTeamByCategory[entryTeamHash] || {};
|
|
||||||
let categoryPoints = points[entryCategory] || 0;
|
|
||||||
categoryPoints += entryPoints;
|
|
||||||
points[entryCategory] = categoryPoints;
|
|
||||||
totalPointsByTeamByCategory[entryTeamHash] = points;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Figure out everybody's score
|
|
||||||
for (let teamHash in teamNames) {
|
|
||||||
for (let cat in categories) {
|
|
||||||
let totalPointsByCategory = totalPointsByTeamByCategory[teamHash] || {};
|
|
||||||
let points = totalPointsByCategory[cat] || 0;
|
|
||||||
if (points > 0) {
|
|
||||||
let score = points / maxPointsByCategory[cat];
|
|
||||||
let span = spansByTeamByCategory[teamHash][cat];
|
|
||||||
let width = (100.0 / numCategories) * score;
|
|
||||||
|
|
||||||
span.style.width = width + "%";
|
|
||||||
span.textContent = cat + ": " + points;
|
|
||||||
span.title = span.textContent;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
replayIndex += 1;
|
|
||||||
}
|
|
||||||
replayStep();
|
|
||||||
replayTimer = setInterval(replayStep, 20);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function once() {
|
function once() {
|
||||||
|
@ -161,4 +114,3 @@ function scoreboard(element, continuous) {
|
||||||
}
|
}
|
||||||
once();
|
once();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue