2019-02-26 16:52:23 -07:00
|
|
|
// jshint asi:true
|
|
|
|
|
2020-09-11 13:03:19 -06:00
|
|
|
// prettify adds classes to various types, returning an HTML string.
|
|
|
|
function prettify(key, val) {
|
|
|
|
switch (key) {
|
|
|
|
case "Body":
|
|
|
|
return '[HTML]'
|
|
|
|
}
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
|
2019-02-22 20:46:52 -07:00
|
|
|
// devel_addin drops a bunch of development extensions into element e.
|
|
|
|
// It will only modify stuff inside e.
|
2020-09-11 13:03:19 -06:00
|
|
|
function devel_addin(e) {
|
2020-09-11 17:51:43 -06:00
|
|
|
let h = e.appendChild(document.createElement("h2"))
|
|
|
|
h.textContent = "Developer Output"
|
2019-02-22 20:46:52 -07:00
|
|
|
|
2020-09-11 17:51:43 -06:00
|
|
|
let log = window.puzzle.Debug.Log || []
|
|
|
|
if (log.length > 0) {
|
|
|
|
e.appendChild(document.createElement("h3")).textContent = "Log"
|
|
|
|
let le = e.appendChild(document.createElement("ul"))
|
|
|
|
for (entry of log) {
|
|
|
|
le.appendChild(document.createElement("li")).textContent = entry
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
e.appendChild(document.createElement("h3")).textContent = "Puzzle object"
|
2019-02-22 20:46:52 -07:00
|
|
|
|
2020-09-11 13:03:19 -06:00
|
|
|
let hobj = JSON.stringify(window.puzzle, prettify, 2)
|
|
|
|
let d = e.appendChild(document.createElement("pre"))
|
|
|
|
d.classList.add("object")
|
|
|
|
d.innerHTML = hobj
|
2020-09-11 17:51:43 -06:00
|
|
|
|
|
|
|
e.appendChild(document.createElement("p")).textContent = "This debugging information will not be available to participants."
|
2019-02-22 20:46:52 -07:00
|
|
|
}
|
|
|
|
|
2020-03-10 16:40:37 -06:00
|
|
|
// Hash routine used in v3.4 and earlier
|
|
|
|
function djb2hash(buf) {
|
|
|
|
let h = 5381
|
|
|
|
for (let c of (new TextEncoder()).encode(buf)) { // Encode as UTF-8 and read in each byte
|
|
|
|
// JavaScript converts everything to a signed 32-bit integer when you do bitwise operations.
|
|
|
|
// So we have to do "unsigned right shift" by zero to get it back to unsigned.
|
|
|
|
h = (((h * 33) + c) & 0xffffffff) >>> 0
|
|
|
|
}
|
|
|
|
return h
|
|
|
|
}
|
2019-02-24 17:02:28 -07:00
|
|
|
|
2019-02-24 11:53:22 -07:00
|
|
|
// The routine used to hash answers in compiled puzzle packages
|
2020-03-10 13:31:38 -06:00
|
|
|
async function sha256Hash(message) {
|
|
|
|
const msgUint8 = new TextEncoder().encode(message); // encode as (utf-8) Uint8Array
|
|
|
|
const hashBuffer = await crypto.subtle.digest('SHA-256', msgUint8); // hash the message
|
|
|
|
const hashArray = Array.from(new Uint8Array(hashBuffer)); // convert buffer to byte array
|
|
|
|
const hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join(''); // convert bytes to hex string
|
|
|
|
return hashHex;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Is the provided answer possibly correct?
|
2020-09-11 17:33:43 -06:00
|
|
|
async function checkAnswer(answer) {
|
|
|
|
let answerHashes = []
|
|
|
|
answerHashes.push(djb2hash(answer))
|
|
|
|
answerHashes.push(await sha256Hash(answer))
|
|
|
|
|
|
|
|
for (let hash of answerHashes) {
|
2021-02-24 17:01:44 -07:00
|
|
|
for (let correctHash of window.puzzle.AnswerHashes) {
|
2020-09-11 17:33:43 -06:00
|
|
|
if (hash == correctHash) {
|
|
|
|
return true
|
2020-03-10 13:31:38 -06:00
|
|
|
}
|
|
|
|
}
|
2019-02-24 11:53:22 -07:00
|
|
|
}
|
2020-03-10 13:31:38 -06:00
|
|
|
return false
|
2019-02-24 11:53:22 -07:00
|
|
|
}
|
2019-02-22 20:46:52 -07:00
|
|
|
|
2019-02-24 11:53:22 -07:00
|
|
|
// Pop up a message
|
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
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-02-24 11:53:22 -07:00
|
|
|
// When the user submits an answer
|
2019-02-22 20:46:52 -07:00
|
|
|
function submit(e) {
|
|
|
|
e.preventDefault()
|
2020-03-10 13:31:38 -06:00
|
|
|
let data = new FormData(e.target)
|
|
|
|
|
|
|
|
window.data = data
|
2019-02-23 12:04:42 -07:00
|
|
|
fetch("answer", {
|
|
|
|
method: "POST",
|
2020-03-10 14:28:50 -06:00
|
|
|
body: data,
|
2019-02-22 20:46:52 -07:00
|
|
|
})
|
|
|
|
.then(resp => {
|
|
|
|
if (resp.ok) {
|
|
|
|
resp.json()
|
|
|
|
.then(obj => {
|
|
|
|
toast(obj.data.description)
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
toast("Error submitting your answer. Try again in a few seconds.")
|
|
|
|
console.log(resp)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
toast("Error submitting your answer. Try again in a few seconds.")
|
|
|
|
console.log(err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-09-11 13:03:19 -06:00
|
|
|
async function loadPuzzle(categoryName, points, puzzleId) {
|
2019-02-22 20:46:52 -07:00
|
|
|
let puzzle = document.getElementById("puzzle")
|
|
|
|
let base = "content/" + categoryName + "/" + puzzleId + "/"
|
|
|
|
|
2020-09-11 13:03:19 -06:00
|
|
|
let resp = await fetch(base + "puzzle.json")
|
|
|
|
if (! resp.ok) {
|
|
|
|
console.log(resp)
|
|
|
|
let err = await resp.text()
|
2019-02-22 20:46:52 -07:00
|
|
|
Array.from(puzzle.childNodes).map(e => e.remove())
|
2020-09-11 13:03:19 -06:00
|
|
|
p = puzzle.appendChild(document.createElement("p"))
|
2019-02-22 20:46:52 -07:00
|
|
|
p.classList.add("Error")
|
|
|
|
p.textContent = err
|
2020-09-11 13:03:19 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make the whole puzzle available
|
|
|
|
window.puzzle = await resp.json()
|
|
|
|
|
|
|
|
// Populate authors
|
2021-02-24 17:01:44 -07:00
|
|
|
document.getElementById("authors").textContent = window.puzzle.Authors.join(", ")
|
2020-09-11 13:03:19 -06:00
|
|
|
|
|
|
|
// If answers are provided, this is the devel server
|
2020-10-13 16:41:50 -06:00
|
|
|
if (window.puzzle.Answers.length > 0) {
|
2020-09-11 13:03:19 -06:00
|
|
|
devel_addin(document.getElementById("devel"))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load scripts
|
2021-02-24 17:01:44 -07:00
|
|
|
for (let script of (window.puzzle.Scripts || [])) {
|
2020-09-11 13:03:19 -06:00
|
|
|
let st = document.createElement("script")
|
|
|
|
document.head.appendChild(st)
|
|
|
|
st.src = base + script
|
|
|
|
}
|
|
|
|
|
|
|
|
// List associated files
|
2021-02-24 17:01:44 -07:00
|
|
|
for (let fn of (window.puzzle.Attachments || [])) {
|
2020-09-11 13:03:19 -06:00
|
|
|
let li = document.createElement("li")
|
|
|
|
let a = document.createElement("a")
|
|
|
|
a.href = base + fn
|
|
|
|
a.innerText = fn
|
|
|
|
li.appendChild(a)
|
|
|
|
document.getElementById("files").appendChild(li)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prefix `base` to relative URLs in the puzzle body
|
2021-02-24 17:01:44 -07:00
|
|
|
let doc = new DOMParser().parseFromString(window.puzzle.Body, "text/html")
|
2020-09-11 13:03:19 -06:00
|
|
|
for (let se of doc.querySelectorAll("[src],[href]")) {
|
|
|
|
se.outerHTML = se.outerHTML.replace(/(src|href)="([^/]+)"/i, "$1=\"" + base + "$2\"")
|
|
|
|
}
|
|
|
|
|
|
|
|
// If a validation pattern was provided, set that
|
2021-02-24 17:01:44 -07:00
|
|
|
if (window.puzzle.AnswerPattern) {
|
|
|
|
document.querySelector("#answer").pattern = window.puzzle.AnswerPattern
|
2020-09-11 13:03:19 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Replace puzzle children with what's in `doc`
|
|
|
|
Array.from(puzzle.childNodes).map(e => e.remove())
|
|
|
|
Array.from(doc.body.childNodes).map(e => puzzle.appendChild(e))
|
2019-02-22 20:46:52 -07:00
|
|
|
|
|
|
|
document.title = categoryName + " " + points
|
|
|
|
document.querySelector("body > h1").innerText = document.title
|
|
|
|
document.querySelector("input[name=cat]").value = categoryName
|
|
|
|
document.querySelector("input[name=points]").value = points
|
|
|
|
}
|
|
|
|
|
2019-02-24 17:13:26 -07:00
|
|
|
// Check to see if the answer might be correct
|
|
|
|
// This might be better done with the "constraint validation API"
|
|
|
|
// https://developer.mozilla.org/en-US/docs/Learn/HTML/Forms/Form_validation#Validating_forms_using_JavaScript
|
2019-02-24 11:53:22 -07:00
|
|
|
function answerCheck(e) {
|
|
|
|
let answer = e.target.value
|
|
|
|
let ok = document.querySelector("#answer_ok")
|
|
|
|
|
|
|
|
// You have to provide someplace to put the check
|
|
|
|
if (! ok) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-09-11 17:33:43 -06:00
|
|
|
checkAnswer(answer)
|
2020-03-10 13:31:38 -06:00
|
|
|
.then (correct => {
|
|
|
|
if (correct) {
|
|
|
|
ok.textContent = "⭕"
|
|
|
|
ok.title = "Possibly correct"
|
|
|
|
} else {
|
|
|
|
ok.textContent = "❌"
|
|
|
|
ok.title = "Definitely not correct"
|
2019-02-24 11:53:22 -07:00
|
|
|
}
|
2020-03-10 13:31:38 -06:00
|
|
|
})
|
2019-02-24 11:53:22 -07:00
|
|
|
}
|
|
|
|
|
2019-02-22 20:46:52 -07:00
|
|
|
function init() {
|
|
|
|
let params = new URLSearchParams(window.location.search)
|
|
|
|
let categoryName = params.get("cat")
|
|
|
|
let points = params.get("points")
|
|
|
|
let puzzleId = params.get("pid")
|
|
|
|
|
2020-10-13 18:33:12 -06:00
|
|
|
if (categoryName && points) {
|
|
|
|
loadPuzzle(categoryName, points, puzzleId || points)
|
2019-02-22 20:46:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
let teamId = sessionStorage.getItem("id")
|
|
|
|
if (teamId) {
|
|
|
|
document.querySelector("input[name=id]").value = teamId
|
|
|
|
}
|
|
|
|
|
2019-02-24 11:53:22 -07:00
|
|
|
if (document.querySelector("#answer")) {
|
|
|
|
document.querySelector("#answer").addEventListener("input", answerCheck)
|
|
|
|
}
|
2019-02-22 20:46:52 -07:00
|
|
|
document.querySelector("form").addEventListener("submit", submit)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (document.readyState === "loading") {
|
|
|
|
document.addEventListener("DOMContentLoaded", init)
|
|
|
|
} else {
|
|
|
|
init()
|
|
|
|
}
|