Skip to content

Commit

Permalink
Instant update index, close tab on correct answer
Browse files Browse the repository at this point in the history
Fixes #201
  • Loading branch information
Neale Pickett committed Apr 8, 2024
1 parent c9bd05c commit afae394
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
4 changes: 4 additions & 0 deletions theme/common.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -76,6 +79,7 @@ export {
Millisecond,
Second,
Minute,
StateUpdateChannel,
BaseURL,
Toast,
WhenDOMLoaded,
Expand Down
5 changes: 5 additions & 0 deletions theme/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
15 changes: 15 additions & 0 deletions theme/puzzle.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -233,6 +235,16 @@ async function loadPuzzle(category, points) {
return puzzle
}

const confettiPromise = import("https://cdn.jsdelivr.net/npm/[email protected]/+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))
Expand All @@ -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)
Expand Down

0 comments on commit afae394

Please sign in to comment.