Instant update index, close tab on correct answer

Fixes #201
This commit is contained in:
Neale Pickett 2024-04-08 14:38:08 -06:00
parent c9bd05c4ef
commit afae394618
3 changed files with 24 additions and 0 deletions

View File

@ -8,6 +8,9 @@ const Minute = Second * 60
/** URL to the top of this MOTH server */
const BaseURL = new URL(".", location)
/** A channel to monitor for state updates (or to notify of state updates) */
const StateUpdateChannel = new BroadcastChannel("StateUpdate")
/**
* Display a transient message to the user.
*
@ -76,6 +79,7 @@ export {
Millisecond,
Second,
Minute,
StateUpdateChannel,
BaseURL,
Toast,
WhenDOMLoaded,

View File

@ -17,6 +17,11 @@ class App {
e.addEventListener("click", () => this.Logout())
}
common.StateUpdateChannel.addEventListener("message", () => {
// Give mothd time to catch up
setTimeout(() => this.UpdateState(), 1/2 * common.Second)
})
setInterval(() => this.UpdateState(), common.Minute/3)
setInterval(() => this.UpdateConfig(), common.Minute* 5)

View File

@ -28,6 +28,8 @@ async function formSubmitHandler(event) {
try {
message = await window.app.puzzle.SubmitAnswer(proposed)
common.Toast(message)
common.StateUpdateChannel.postMessage({})
document.dispatchEvent(new CustomEvent("answerCorrect"))
}
catch (err) {
common.Toast(err)
@ -233,6 +235,16 @@ async function loadPuzzle(category, points) {
return puzzle
}
const confettiPromise = import("https://cdn.jsdelivr.net/npm/canvas-conefetti@1.9.2/+esm")
async function CorrectAnswer() {
setInterval(window.close, 3 * common.Second)
let confetti = await confettiPromise
confetti.default({
disableForReducedMotion: true,
})
}
async function init() {
window.app = {}
window.setanswer = (str => SetAnswer(str))
@ -250,6 +262,9 @@ async function init() {
// Workspaces may trigger a "this is the answer" event
document.addEventListener("setAnswer", e => SetAnswer(e.detail.value))
// Celebrate on correct answer
document.addEventListener("answerCorrect", e => CorrectAnswer())
// Make all links absolute, because we're going to be changing the base URL
for (let e of document.querySelectorAll("[href]")) {
e.href = new URL(e.href, common.BaseURL)