Theme back to previous functionality with dev server

This commit is contained in:
Neale Pickett 2020-02-28 16:01:35 -07:00
parent 94acae13ca
commit 4a73bb0910
1 changed files with 39 additions and 34 deletions

View File

@ -25,6 +25,8 @@ function renderNotices(obj) {
function renderPuzzles(obj) { function renderPuzzles(obj) {
let puzzlesElement = document.createElement('div') let puzzlesElement = document.createElement('div')
document.getElementById("login").style.display = "none"
// Create a sorted list of category names // Create a sorted list of category names
let cats = Object.keys(obj) let cats = Object.keys(obj)
cats.sort() cats.sort()
@ -91,13 +93,20 @@ function renderPuzzles(obj) {
function renderState(obj) { function renderState(obj) {
devel = obj.config.devel devel = obj.config.devel
console.log(obj) if (devel) {
renderPuzzles(obj.puzzles) sessionStorage.id = "1234"
sessionStorage.pid = "rodney"
}
if (Object.keys(obj.puzzles).length > 0) {
renderPuzzles(obj.puzzles)
}
renderNotices(obj.messages) renderNotices(obj.messages)
} }
function heartbeat(teamId, participantId) { function heartbeat() {
let teamId = sessionStorage.id
let participantId = sessionStorage.pid
let url = new URL("state", window.location) let url = new URL("state", window.location)
url.searchParams.set("id", teamId) url.searchParams.set("id", teamId)
if (participantId) { if (participantId) {
@ -111,34 +120,29 @@ function heartbeat(teamId, participantId) {
resp.json() resp.json()
.then(renderState) .then(renderState)
.catch(err => { .catch(err => {
toast("Error fetching recent puzzles. I'll try again in a moment.") toast("Error fetching recent state. I'll try again in a moment.")
console.log(err) console.log(err)
}) })
} }
}) })
.catch(err => { .catch(err => {
toast("Error fetching recent puzzles. I'll try again in a moment.") toast("Error fetching recent state. I'll try again in a moment.")
console.log(err) console.log(err)
}) })
} }
function showPuzzles(teamId, participantId) { function showPuzzles() {
let spinner = document.createElement("span") let spinner = document.createElement("span")
spinner.classList.add("spinner") spinner.classList.add("spinner")
sessionStorage.setItem("id", teamId)
if (participantId) {
sessionStorage.setItem("pid", participantId)
}
document.getElementById("login").style.display = "none" document.getElementById("login").style.display = "none"
document.getElementById("puzzles").appendChild(spinner) document.getElementById("puzzles").appendChild(spinner)
heartbeat(teamId, participantId) heartbeat()
setInterval(e => { heartbeat(teamId) }, 40000) drawCacheButton()
drawCacheButton(teamId)
} }
function drawCacheButton(teamId) { function drawCacheButton() {
let teamId = sessionStorage.id
let cacher = document.querySelector("#cacheButton") let cacher = document.querySelector("#cacheButton")
function updateCacheButton() { function updateCacheButton() {
@ -165,7 +169,7 @@ function drawCacheButton(teamId) {
} }
async function fetchAll() { async function fetchAll() {
let teamId = sessionStorage.getItem("id") let teamId = sessionStorage.id
let headers = new Headers() let headers = new Headers()
headers.append("pragma", "no-cache") headers.append("pragma", "no-cache")
headers.append("cache-control", "no-cache") headers.append("cache-control", "no-cache")
@ -206,14 +210,16 @@ async function fetchAll() {
} }
let puzzles = categories[cat_name] let puzzles = categories[cat_name]
for (let puzzle of puzzles) { for (let puzzle of puzzles) {
let url = new URL("puzzle.html", window.location) let url = new URL("puzzle.html", window.location)
url.searchParams.set("cat", cat_name) url.searchParams.set("cat", cat_name)
url.searchParams.set("points", puzzle[0]) url.searchParams.set("points", puzzle[0])
url.searchParams.set("pid", puzzle[1]) url.searchParams.set("pid", puzzle[1])
requests.push( fetch(url) requests.push(
.then(e => { fetch(url)
console.log("Fetched " + url) .then(e => {
})) console.log("Fetched " + url)
})
)
} }
} }
} }
@ -236,12 +242,11 @@ function login(e) {
if (resp.ok) { if (resp.ok) {
resp.json() resp.json()
.then(obj => { .then(obj => {
if (obj.status == "success") { if ((obj.status == "success") || (obj.data.short == "Already registered")) {
toast("Team registered") toast("Logged in")
showPuzzles(teamId, participantId) sessionStorage.id = teamId
} else if (obj.data.short == "Already registered") { sessionStorage.pid = participantId
toast("Logged in with previously-registered team name") showPuzzles()
showPuzzles(teamId, participantId)
} else { } else {
toast(obj.data.description) toast(obj.data.description)
} }
@ -263,11 +268,11 @@ function login(e) {
function init() { function init() {
// Already signed in? // Already signed in?
let teamId = sessionStorage.getItem("id") if (sessionStorage.id) {
let participantId = sessionStorage.getItem("pid") showPuzzles()
if (teamId) {
showPuzzles(teamId, participantId)
} }
heartbeat()
setInterval(e => heartbeat(), 40000)
document.getElementById("login").addEventListener("submit", login) document.getElementById("login").addEventListener("submit", login)
} }