moth/theme/moth.js

261 lines
6.4 KiB
JavaScript
Raw Normal View History

2019-02-22 17:52:41 -07:00
// jshint asi:true
2020-02-29 17:12:35 -07:00
var devel = false
2019-02-22 17:52:41 -07:00
var heartbeatInterval = 40000
2019-02-22 20:46:52 -07:00
function toast(message, timeout=5000) {
let p = document.createElement("p")
p.innerText = message
document.getElementById("messages").appendChild(p)
setTimeout(
e => { p.remove() },
timeout
)
}
2020-02-29 17:12:35 -07:00
function renderNotices(obj) {
let ne = document.getElementById("notices")
if (ne) {
ne.innerHTML = obj
}
}
2019-02-22 17:52:41 -07:00
function renderPuzzles(obj) {
let puzzlesElement = document.createElement('div')
2020-02-29 17:12:35 -07:00
document.getElementById("login").style.display = "none"
2019-02-22 17:52:41 -07:00
// Create a sorted list of category names
let cats = Object.keys(obj)
cats.sort()
for (let cat of cats) {
if (cat.startsWith("__")) {
// Skip metadata
continue
}
let puzzles = obj[cat]
let pdiv = document.createElement('div')
pdiv.className = 'category'
let h = document.createElement('h2')
pdiv.appendChild(h)
h.textContent = cat
// Extras if we're running a devel server
2020-02-29 17:12:35 -07:00
if (devel) {
2019-02-22 17:52:41 -07:00
let a = document.createElement('a')
h.insertBefore(a, h.firstChild)
a.textContent = "⬇️"
2019-02-27 16:15:45 -07:00
a.href = "mothballer/" + cat + ".mb"
2019-02-22 17:52:41 -07:00
a.classList.add("mothball")
a.title = "Download a compiled puzzle for this category"
}
// List out puzzles in this category
let l = document.createElement('ul')
pdiv.appendChild(l)
for (let puzzle of puzzles) {
2020-02-29 22:37:22 -07:00
let points = puzzle
let id = puzzle
if (Array.isArray(puzzle)) {
points = puzzle[0]
id = puzzle[1]
}
2019-02-22 17:52:41 -07:00
let i = document.createElement('li')
l.appendChild(i)
i.textContent = " "
if (points === 0) {
// Sentry: there are no more puzzles in this category
i.textContent = "✿"
} else {
let a = document.createElement('a')
i.appendChild(a)
a.textContent = points
2020-02-29 17:12:35 -07:00
let url = new URL("puzzle.html", window.location)
url.searchParams.set("cat", cat)
url.searchParams.set("points", points)
url.searchParams.set("pid", id)
a.href = url.toString()
2019-02-22 17:52:41 -07:00
}
}
puzzlesElement.appendChild(pdiv)
}
// Drop that thing in
let container = document.getElementById("puzzles")
while (container.firstChild) {
container.firstChild.remove()
}
container.appendChild(puzzlesElement)
}
2020-02-29 17:12:35 -07:00
function renderState(obj) {
2020-02-29 22:37:22 -07:00
devel = obj.Config.Devel
2020-02-29 17:12:35 -07:00
if (devel) {
sessionStorage.id = "1234"
sessionStorage.pid = "rodney"
}
2020-02-29 22:37:22 -07:00
if (Object.keys(obj.Puzzles).length > 0) {
renderPuzzles(obj.Puzzles)
2020-02-29 17:12:35 -07:00
}
2020-02-29 22:37:22 -07:00
renderNotices(obj.Messages)
2020-02-29 17:12:35 -07:00
}
function heartbeat() {
let teamId = sessionStorage.id || ""
let participantId = sessionStorage.pid
let url = new URL("state", window.location)
url.searchParams.set("id", teamId)
if (participantId) {
url.searchParams.set("pid", participantId)
}
2019-02-23 12:04:42 -07:00
let fd = new FormData()
fd.append("id", teamId)
2020-02-29 17:12:35 -07:00
fetch(url)
2019-02-22 17:52:41 -07:00
.then(resp => {
if (resp.ok) {
resp.json()
2020-02-29 17:12:35 -07:00
.then(renderState)
2019-02-22 17:52:41 -07:00
.catch(err => {
2020-02-29 17:12:35 -07:00
toast("Error fetching recent state. I'll try again in a moment.")
2019-02-22 17:52:41 -07:00
console.log(err)
})
}
})
.catch(err => {
2020-02-29 17:12:35 -07:00
toast("Error fetching recent state. I'll try again in a moment.")
2019-02-22 17:52:41 -07:00
console.log(err)
})
}
2020-02-29 17:12:35 -07:00
function showPuzzles() {
2019-02-22 17:52:41 -07:00
let spinner = document.createElement("span")
spinner.classList.add("spinner")
document.getElementById("login").style.display = "none"
document.getElementById("puzzles").appendChild(spinner)
2020-02-29 17:12:35 -07:00
heartbeat()
}
async function fetchAll() {
let teamId = sessionStorage.id
let headers = new Headers()
headers.append("pragma", "no-cache")
headers.append("cache-control", "no-cache")
requests = []
let url = new URL("current_manifest.json", window.location)
url.searchParams.set("id", teamId)
toast("Caching all currently-open content")
requests.push( fetch(url, {headers: headers})
.then( resp => {
if (resp.ok) {
resp.json()
.then(contents => {
console.log("Processing manifest")
for (let resource of contents) {
if (resource == "puzzles.json") {
continue
}
fetch(resource)
.then(e => {
console.log("Fetched " + resource)
})
}
})
}
}))
let resp = await fetch("puzzles.json?id=" + teamId, {headers: headers})
if (resp.ok) {
let categories = await resp.json()
let cat_names = Object.keys(categories)
cat_names.sort()
for (let cat_name of cat_names) {
if (cat_name.startsWith("__")) {
// Skip metadata
continue
}
let puzzles = categories[cat_name]
for (let puzzle of puzzles) {
let url = new URL("puzzle.html", window.location)
url.searchParams.set("cat", cat_name)
url.searchParams.set("points", puzzle[0])
url.searchParams.set("pid", puzzle[1])
requests.push(
fetch(url)
.then(e => {
console.log("Fetched " + url)
})
)
}
}
}
await Promise.all(requests)
toast("Done caching content")
2019-02-22 17:52:41 -07:00
}
2019-02-22 19:09:38 -07:00
function login(e) {
2019-02-22 20:46:52 -07:00
e.preventDefault()
2019-02-22 17:52:41 -07:00
let name = document.querySelector("[name=name]").value
2020-02-29 17:12:35 -07:00
let teamId = document.querySelector("[name=id]").value
let pide = document.querySelector("[name=pid]")
let participantId = pide?pide.value:""
2019-02-22 17:52:41 -07:00
2019-02-23 12:04:42 -07:00
fetch("register", {
method: "POST",
body: new FormData(e.target),
2019-02-22 17:52:41 -07:00
})
.then(resp => {
if (resp.ok) {
resp.json()
.then(obj => {
2020-02-29 17:12:35 -07:00
if ((obj.status == "success") || (obj.data.short == "Already registered")) {
toast("Logged in")
sessionStorage.id = teamId
sessionStorage.pid = participantId
showPuzzles()
2019-02-22 17:52:41 -07:00
} else {
toast(obj.data.description)
}
})
.catch(err => {
toast("Oops, the server has lost its mind. You probably need to tell someone so they can fix it.")
console.log(err, resp)
})
} else {
toast("Oops, something's wrong with the server. Try again in a few seconds.")
console.log(resp)
}
})
.catch(err => {
toast("Oops, something went wrong. Try again in a few seconds.")
console.log(err)
})
}
function init() {
2019-02-22 19:09:38 -07:00
// Already signed in?
2020-02-29 17:12:35 -07:00
if (sessionStorage.id) {
showPuzzles()
2019-02-22 19:09:38 -07:00
}
2020-02-29 17:12:35 -07:00
heartbeat()
setInterval(e => heartbeat(), 40000)
2019-02-22 19:09:38 -07:00
document.getElementById("login").addEventListener("submit", login)
2019-02-22 17:52:41 -07:00
}
if (document.readyState === "loading") {
2020-02-29 17:12:35 -07:00
document.addEventListener("DOMContentLoaded", init)
2019-02-22 17:52:41 -07:00
} else {
2020-02-29 17:12:35 -07:00
init()
2019-02-22 17:52:41 -07:00
}
2020-02-29 17:12:35 -07:00