mirror of https://github.com/dirtbags/moth.git
commit
f057da87fb
|
@ -2,15 +2,15 @@ function pwa_init() {
|
|||
if ('serviceWorker' in navigator) {
|
||||
navigator.serviceWorker.register("./sw.js").then(function(reg) {
|
||||
}).catch(function(err) {
|
||||
console.warn("Error while registering service worker", err);
|
||||
});
|
||||
console.warn("Error while registering service worker", err)
|
||||
})
|
||||
} else {
|
||||
console.log("Service workers not supported. Some offline functionality may not work");
|
||||
console.log("Service workers not supported. Some offline functionality may not work")
|
||||
}
|
||||
}
|
||||
|
||||
if (document.readyState === "loading") {
|
||||
document.addEventListener("DOMContentLoaded", pwa_init);
|
||||
document.addEventListener("DOMContentLoaded", pwa_init)
|
||||
} else {
|
||||
pwa_init();
|
||||
pwa_init()
|
||||
}
|
||||
|
|
|
@ -109,68 +109,68 @@ function showPuzzles(teamId) {
|
|||
}
|
||||
|
||||
function drawCacheButton(teamId) {
|
||||
let cacher = document.createElement("li");
|
||||
let cache_button = document.createElement("a");
|
||||
cache_button.innerText = "Cache";
|
||||
cache_button.title = "Cache an offine copy of current content";
|
||||
cache_button.href = "#";
|
||||
let cacher = document.createElement("li")
|
||||
let cache_button = document.createElement("a")
|
||||
cache_button.innerText = "Cache"
|
||||
cache_button.title = "Cache an offine copy of current content"
|
||||
cache_button.href = "#"
|
||||
cache_button.addEventListener("click", async function() {
|
||||
toast("Caching all currently-open content");
|
||||
await fetchAll(teamId);
|
||||
toast("Done caching content");
|
||||
});
|
||||
cacher.appendChild(cache_button);
|
||||
document.getElementsByTagName("nav")[0].getElementsByTagName("ul")[0].appendChild(cacher);
|
||||
toast("Caching all currently-open content")
|
||||
await fetchAll(teamId)
|
||||
toast("Done caching content")
|
||||
})
|
||||
cacher.appendChild(cache_button)
|
||||
document.getElementsByTagName("nav")[0].getElementsByTagName("ul")[0].appendChild(cacher)
|
||||
|
||||
function updateCacheButton() {
|
||||
let headers = new Headers();
|
||||
headers.append("pragma", "no-cache");
|
||||
headers.append("cache-control", "no-cache");
|
||||
let headers = new Headers()
|
||||
headers.append("pragma", "no-cache")
|
||||
headers.append("cache-control", "no-cache")
|
||||
fetch("current_manifest.json?id=" + teamId, {method: "HEAD", headers: headers})
|
||||
.then( resp => {
|
||||
if (resp.ok) {
|
||||
cacher.style.disply = "initial";
|
||||
cacher.style.disply = "initial"
|
||||
} else {
|
||||
cacher.style.display = "none";
|
||||
cacher.style.display = "none"
|
||||
}
|
||||
}).catch( ex => {
|
||||
cacher.style.display = "none";
|
||||
cacher.style.display = "none"
|
||||
})
|
||||
}
|
||||
|
||||
setInterval ( updateCacheButton , 30000);
|
||||
updateCacheButton();
|
||||
setInterval ( updateCacheButton , 30000)
|
||||
updateCacheButton()
|
||||
}
|
||||
|
||||
async function fetchAll(teamId) {
|
||||
let headers = new Headers();
|
||||
headers.append("pragma", "no-cache");
|
||||
headers.append("cache-control", "no-cache");
|
||||
requests = [];
|
||||
let headers = new Headers()
|
||||
headers.append("pragma", "no-cache")
|
||||
headers.append("cache-control", "no-cache")
|
||||
requests = []
|
||||
|
||||
requests.push( fetch("current_manifest.json?id=" + teamId, {headers: headers})
|
||||
.then( resp => {
|
||||
if (resp.ok) {
|
||||
resp.json()
|
||||
.then( contents => {
|
||||
console.log("Processing manifest");
|
||||
console.log("Processing manifest")
|
||||
for (let resource of contents) {
|
||||
if (resource == "puzzles.json") {
|
||||
continue;
|
||||
continue
|
||||
}
|
||||
fetch(resource)
|
||||
.then( e => {
|
||||
console.log("Fetched " + resource);
|
||||
console.log("Fetched " + resource)
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
}));
|
||||
}))
|
||||
|
||||
let resp = await fetch("puzzles.json?id=" + teamId, {headers: headers});
|
||||
let resp = await fetch("puzzles.json?id=" + teamId, {headers: headers})
|
||||
|
||||
if (resp.ok) {
|
||||
let categories = await resp.json();
|
||||
let categories = await resp.json()
|
||||
let cat_names = Object.keys(categories)
|
||||
cat_names.sort()
|
||||
for (let cat_name of cat_names) {
|
||||
|
@ -178,17 +178,17 @@ async function fetchAll(teamId) {
|
|||
// Skip metadata
|
||||
continue
|
||||
}
|
||||
let puzzles = categories[cat_name];
|
||||
let puzzles = categories[cat_name]
|
||||
for (let puzzle of puzzles) {
|
||||
let url = "puzzle.html?cat=" + cat_name + "&points=" + puzzle[0] + "&pid=" + puzzle[1];
|
||||
let url = "puzzle.html?cat=" + cat_name + "&points=" + puzzle[0] + "&pid=" + puzzle[1]
|
||||
requests.push( fetch(url)
|
||||
.then( e => {
|
||||
console.log("Fetched " + url);
|
||||
}));
|
||||
console.log("Fetched " + url)
|
||||
}))
|
||||
}
|
||||
}
|
||||
}
|
||||
return Promise.all(requests);
|
||||
return Promise.all(requests)
|
||||
}
|
||||
|
||||
function login(e) {
|
||||
|
@ -240,8 +240,8 @@ function init() {
|
|||
}
|
||||
|
||||
if (document.readyState === "loading") {
|
||||
document.addEventListener("DOMContentLoaded", init);
|
||||
document.addEventListener("DOMContentLoaded", init)
|
||||
} else {
|
||||
init();
|
||||
init()
|
||||
}
|
||||
|
||||
|
|
|
@ -7,121 +7,121 @@
|
|||
<script src="moth-pwa.js" type="text/javascript"></script>
|
||||
<script>
|
||||
function update(state) {
|
||||
let element = document.getElementById("scoreboard");
|
||||
let teamnames = state["teams"];
|
||||
let pointslog = state["points"];
|
||||
let highscore = {};
|
||||
let teams = {};
|
||||
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")) || [];
|
||||
let pointshistory = JSON.parse(localStorage.getItem("pointshistory")) || []
|
||||
if (pointshistory.length >= 20){
|
||||
pointshistory.shift();
|
||||
pointshistory.shift()
|
||||
}
|
||||
pointshistory.push(pointslog);
|
||||
localStorage.setItem("pointshistory", JSON.stringify(pointshistory));
|
||||
pointshistory.push(pointslog)
|
||||
localStorage.setItem("pointshistory", JSON.stringify(pointshistory))
|
||||
|
||||
// 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];
|
||||
let entry = pointslog[i]
|
||||
let timestamp = entry[0]
|
||||
let teamhash = entry[1]
|
||||
let category = entry[2]
|
||||
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
|
||||
team[category] = (team[category] || 0) + points;
|
||||
team[category] = (team[category] || 0) + points
|
||||
|
||||
// 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
|
||||
function teamScore(t) {
|
||||
let score = 0;
|
||||
let score = 0
|
||||
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) {
|
||||
return teamScore(a) - teamScore(b);
|
||||
return teamScore(a) - teamScore(b)
|
||||
}
|
||||
|
||||
// Figure out how to order each team on the scoreboard
|
||||
let winners = [];
|
||||
let winners = []
|
||||
for (let i in teams) {
|
||||
winners.push(teams[i]);
|
||||
winners.push(teams[i])
|
||||
}
|
||||
winners.sort(teamCompare);
|
||||
winners.reverse();
|
||||
winners.sort(teamCompare)
|
||||
winners.reverse()
|
||||
|
||||
// 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) {
|
||||
let team = winners[i];
|
||||
let row = document.createElement("div");
|
||||
let ncat = 0;
|
||||
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 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;
|
||||
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;
|
||||
row.appendChild(bar)
|
||||
ncat += 1
|
||||
}
|
||||
|
||||
let te = document.createElement("span");
|
||||
te.classList.add("teamname");
|
||||
te.textContent = teamnames[team.__hash__];
|
||||
row.appendChild(te);
|
||||
let te = document.createElement("span")
|
||||
te.classList.add("teamname")
|
||||
te.textContent = teamnames[team.__hash__]
|
||||
row.appendChild(te)
|
||||
|
||||
element.appendChild(row);
|
||||
element.appendChild(row)
|
||||
}
|
||||
}
|
||||
|
||||
function once() {
|
||||
fetch("points.json")
|
||||
.then(resp => {
|
||||
return resp.json();
|
||||
return resp.json()
|
||||
})
|
||||
.then(obj => {
|
||||
update(obj);
|
||||
update(obj)
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(err);
|
||||
});
|
||||
console.log(err)
|
||||
})
|
||||
}
|
||||
|
||||
function init() {
|
||||
let base = window.location.href.replace("scoreboard.html", "");
|
||||
document.querySelector("#location").textContent = base;
|
||||
let base = window.location.href.replace("scoreboard.html", "")
|
||||
document.querySelector("#location").textContent = base
|
||||
|
||||
setInterval(once, 60000);
|
||||
once();
|
||||
setInterval(once, 60000)
|
||||
once()
|
||||
}
|
||||
if (document.readyState === "loading") {
|
||||
document.addEventListener("DOMContentLoaded", init);
|
||||
document.addEventListener("DOMContentLoaded", init)
|
||||
} else {
|
||||
init();
|
||||
init()
|
||||
}
|
||||
</script>
|
||||
</head>
|
||||
|
|
34
theme/sw.js
34
theme/sw.js
|
@ -8,42 +8,42 @@ var content = [
|
|||
"moth.js",
|
||||
"sw.js",
|
||||
"points.json",
|
||||
];
|
||||
]
|
||||
|
||||
self.addEventListener("install", function(e) {
|
||||
e.waitUntil(
|
||||
caches.open(cacheName).then(function(cache) {
|
||||
return cache.addAll(content).then(
|
||||
function() {
|
||||
self.skipWaiting();
|
||||
});
|
||||
self.skipWaiting()
|
||||
})
|
||||
})
|
||||
);
|
||||
});
|
||||
)
|
||||
})
|
||||
|
||||
/* Attempt to fetch live resources, first, then fall back to cache */
|
||||
self.addEventListener('fetch', function(event) {
|
||||
let cache_used = false;
|
||||
let cache_used = false
|
||||
|
||||
event.respondWith(
|
||||
fetch(event.request)
|
||||
.catch(function(evt) {
|
||||
//console.log("Falling back to cache for " + event.request.url);
|
||||
cache_used = true;
|
||||
return caches.match(event.request, {ignoreSearch: true});
|
||||
//console.log("Falling back to cache for " + event.request.url)
|
||||
cache_used = true
|
||||
return caches.match(event.request, {ignoreSearch: true})
|
||||
}).then(function(res) {
|
||||
if (res && res.ok) {
|
||||
let res_clone = res.clone();
|
||||
let res_clone = res.clone()
|
||||
if (! cache_used && event.request.method == "GET" ) {
|
||||
caches.open(cacheName).then(function(cache) {
|
||||
cache.put(event.request, res_clone);
|
||||
//console.log("Storing " + event.request.url + " in cache");
|
||||
});
|
||||
cache.put(event.request, res_clone)
|
||||
//console.log("Storing " + event.request.url + " in cache")
|
||||
})
|
||||
}
|
||||
return res;
|
||||
return res
|
||||
} else {
|
||||
console.log("Failed to retrieve resource");
|
||||
console.log("Failed to retrieve resource")
|
||||
}
|
||||
})
|
||||
);
|
||||
});
|
||||
)
|
||||
})
|
||||
|
|
Loading…
Reference in New Issue