2021-01-29 01:20:17 +01:00
|
|
|
"use strict"
|
|
|
|
|
|
2021-01-30 13:41:25 +01:00
|
|
|
/* global document, window */
|
|
|
|
|
const crypto = window.crypto
|
|
|
|
|
const location = document.location
|
2021-01-31 00:19:35 +01:00
|
|
|
const performance = window.performance
|
2021-01-30 13:41:25 +01:00
|
|
|
const storage = window.sessionStorage
|
|
|
|
|
|
2021-01-29 01:20:17 +01:00
|
|
|
// TODOs
|
|
|
|
|
// - measure/report latency
|
|
|
|
|
// - use server reported time to find winner
|
|
|
|
|
|
|
|
|
|
import config from "./config.js"
|
|
|
|
|
|
|
|
|
|
const buzzer_key = {
|
|
|
|
|
code: 0x20,
|
|
|
|
|
name: "space bar",
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-01 23:07:49 +01:00
|
|
|
function isString(x) {
|
|
|
|
|
return typeof x === "string"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function isEmpty(x) {
|
|
|
|
|
return (Array.isArray(x) ? x : Object.keys(x)).length === 0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const q = (selector, root = document) => root.querySelector(selector)
|
|
|
|
|
|
|
|
|
|
const _evlisteners = {}
|
|
|
|
|
/**
|
|
|
|
|
* @param {String} event [description]
|
|
|
|
|
* @param {Function} cb [description]
|
|
|
|
|
* @param {?{once: bool, target: Element = document}} options
|
|
|
|
|
*/
|
|
|
|
|
function on(event, cb, options = {}) {
|
|
|
|
|
const target = options.target || document
|
|
|
|
|
if ((_evlisteners[target] ?? {})[event]) {
|
|
|
|
|
console.warn(`Replacing previous event listener for '${event}' on target:`, target)
|
|
|
|
|
off(event, target)
|
|
|
|
|
}
|
|
|
|
|
target.addEventListener(event, cb, { once: !!options.once })
|
|
|
|
|
if (!_evlisteners[target]) {
|
|
|
|
|
_evlisteners[target] = {}
|
|
|
|
|
}
|
|
|
|
|
_evlisteners[target][event] = cb
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function off(event, target = document) {
|
|
|
|
|
if (!_evlisteners[target] || !_evlisteners[target][event]) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
target.removeEventListener(event, _evlisteners[target][event])
|
|
|
|
|
delete _evlisteners[target][event]
|
|
|
|
|
if (isEmpty(_evlisteners[target])) {
|
|
|
|
|
delete _evlisteners[target]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-31 00:19:35 +01:00
|
|
|
function node(type, { appendTo, cls, text, data, style, ...attrs } = {}) {
|
2021-02-01 23:07:49 +01:00
|
|
|
let elem = isString(type) ? document.createElement(type) : type
|
2021-01-29 01:20:17 +01:00
|
|
|
if (cls) {
|
2021-02-01 23:07:49 +01:00
|
|
|
if (isString(cls)) {
|
2021-01-31 00:56:30 +01:00
|
|
|
elem.className = cls
|
|
|
|
|
} else {
|
|
|
|
|
for (const [name, on] of Object.entries(cls)) {
|
|
|
|
|
if (on) {
|
|
|
|
|
elem.classList.add(name)
|
|
|
|
|
} else {
|
|
|
|
|
elem.classList.remove(name)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-01-29 01:20:17 +01:00
|
|
|
}
|
|
|
|
|
if (text) {
|
|
|
|
|
elem.textContent = text
|
|
|
|
|
}
|
2021-01-31 00:19:35 +01:00
|
|
|
Object.assign(elem.dataset, data ?? {})
|
|
|
|
|
Object.assign(elem.style, style ?? {})
|
2021-01-29 01:20:17 +01:00
|
|
|
for (const name in attrs) {
|
|
|
|
|
elem.setAttribute(name, attrs[name])
|
|
|
|
|
}
|
|
|
|
|
if (appendTo) {
|
|
|
|
|
elem = appendTo.appendChild(elem)
|
|
|
|
|
}
|
|
|
|
|
return elem
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Some duration conversion constants.
|
|
|
|
|
*/
|
|
|
|
|
const ms_ns = 1_000_000 // nanoseconds in a millisecond
|
|
|
|
|
const s_ms = 1_000 // milliseconds in a second
|
|
|
|
|
const s_ns = 1_000_000_000 // nanoseconds in a second
|
|
|
|
|
|
|
|
|
|
let socket,
|
|
|
|
|
servertime,
|
|
|
|
|
toffset_ms,
|
|
|
|
|
clients = [],
|
2021-01-30 13:41:25 +01:00
|
|
|
me,
|
|
|
|
|
session_key
|
2021-01-29 01:20:17 +01:00
|
|
|
|
|
|
|
|
function hide(e) {
|
2021-02-01 23:07:49 +01:00
|
|
|
e = isString(e) ? q(`#${e}`) : e
|
2021-01-31 00:19:35 +01:00
|
|
|
e.style.display = "none"
|
2021-01-29 01:20:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function show(e) {
|
2021-02-01 23:07:49 +01:00
|
|
|
e = isString(e) ? q(`#${e}`) : e
|
2021-01-31 00:19:35 +01:00
|
|
|
e.style.display = "block"
|
2021-01-29 01:20:17 +01:00
|
|
|
}
|
|
|
|
|
|
2021-02-01 23:07:49 +01:00
|
|
|
function find_client(client_id) {
|
|
|
|
|
return clients.find((c) => c.id === client_id)
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-29 01:20:17 +01:00
|
|
|
function session_id() {
|
|
|
|
|
const match = /^#?(.+)/.exec(document.location.hash)
|
|
|
|
|
return match ? match[1] : null
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function new_session_id() {
|
2021-01-30 13:41:25 +01:00
|
|
|
if (!crypto) {
|
2021-01-29 01:20:17 +01:00
|
|
|
return Math.random().toString(36).substr(2)
|
|
|
|
|
}
|
|
|
|
|
const data = new Uint8Array(10)
|
|
|
|
|
crypto.getRandomValues(data)
|
|
|
|
|
return Array.from(data, (v) => v.toString(36)).join("")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function setup_url() {
|
|
|
|
|
const sid = session_id() || new_session_id()
|
|
|
|
|
document.location.hash = sid
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function send(type, value) {
|
|
|
|
|
// console.debug('sending', value)
|
|
|
|
|
socket.send(JSON.stringify({ type, value }))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function clear(container) {
|
|
|
|
|
while (container.children.length > 0) {
|
|
|
|
|
const child = container.children[0]
|
|
|
|
|
child.remove()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-01 23:07:49 +01:00
|
|
|
const player_list = q("#info ul")
|
2021-01-29 01:20:17 +01:00
|
|
|
function redraw_clients(me, clients) {
|
|
|
|
|
if (!me) {
|
|
|
|
|
return
|
|
|
|
|
}
|
2021-02-01 23:07:49 +01:00
|
|
|
clear(player_list)
|
2021-01-31 00:19:35 +01:00
|
|
|
const player_tpl = q("template#player").content.firstElementChild
|
2021-01-29 01:20:17 +01:00
|
|
|
for (const c of clients) {
|
2021-02-01 23:07:49 +01:00
|
|
|
const player_el = node(player_tpl.cloneNode(true), {
|
2021-01-29 01:20:17 +01:00
|
|
|
data: { cid: c.id },
|
2021-02-01 23:07:49 +01:00
|
|
|
appendTo: player_list,
|
2021-01-31 00:56:30 +01:00
|
|
|
cls: {
|
|
|
|
|
me: c.id === me.id,
|
|
|
|
|
inactive: !c.active,
|
|
|
|
|
},
|
2021-01-29 01:20:17 +01:00
|
|
|
})
|
2021-02-01 23:07:49 +01:00
|
|
|
q(".name", player_el).textContent = c.name
|
|
|
|
|
q(".points", player_el).textContent = c.points
|
|
|
|
|
q(".tokens", player_el).textContent = c.tokens
|
2021-01-29 01:20:17 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const highlights = {}
|
|
|
|
|
function highlight(client_id, until_ns) {
|
|
|
|
|
if (highlights[client_id]) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
const timeout_ms = (until_ns - servertime_now_ns()) / ms_ns
|
|
|
|
|
if (timeout_ms <= 10) {
|
|
|
|
|
console.warn("That highlight timeout was ridiculously low:", client_id, timeout_ms)
|
|
|
|
|
return
|
|
|
|
|
}
|
2021-02-01 23:07:49 +01:00
|
|
|
for (const li of player_list.children) {
|
2021-01-29 01:20:17 +01:00
|
|
|
if (li.dataset.cid === client_id) {
|
|
|
|
|
li.classList.add("buzzing")
|
2021-02-01 23:07:49 +01:00
|
|
|
if (!isEmpty(highlights)) {
|
2021-01-29 01:20:17 +01:00
|
|
|
li.classList.add("too-late")
|
|
|
|
|
} else {
|
|
|
|
|
li.classList.add("first")
|
|
|
|
|
}
|
|
|
|
|
highlights[client_id] = setTimeout(() => {
|
|
|
|
|
delete highlights[client_id]
|
|
|
|
|
li.classList.remove("buzzing")
|
|
|
|
|
li.classList.remove("too-late")
|
|
|
|
|
li.classList.remove("first")
|
|
|
|
|
}, timeout_ms)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Guess the exact current server time.
|
|
|
|
|
*/
|
|
|
|
|
function servertime_now_ns() {
|
|
|
|
|
const now_ms = performance.now()
|
|
|
|
|
const delta_ns = ms_ns * (now_ms - toffset_ms)
|
|
|
|
|
return servertime + delta_ns
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function setup_ui() {
|
|
|
|
|
on("focus", (event) => {
|
|
|
|
|
hide("active")
|
|
|
|
|
hide("inactive")
|
|
|
|
|
show("ready")
|
|
|
|
|
})
|
|
|
|
|
on("blur", (event) => {
|
|
|
|
|
hide("active")
|
|
|
|
|
show("inactive")
|
|
|
|
|
hide("ready")
|
|
|
|
|
})
|
|
|
|
|
if (document.hasFocus()) {
|
|
|
|
|
hide("inactive")
|
|
|
|
|
} else {
|
|
|
|
|
hide("ready")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
q("#bkey").textContent = buzzer_key.name
|
|
|
|
|
let buzzing = false
|
|
|
|
|
on("keydown", (event) => {
|
|
|
|
|
if (!buzzing && event.keyCode === buzzer_key.code) {
|
|
|
|
|
buzzing = true
|
|
|
|
|
send("buzz", servertime_now_ns())
|
|
|
|
|
show("active")
|
|
|
|
|
hide("ready")
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
on("keyup", (event) => {
|
|
|
|
|
if (event.keyCode === buzzer_key.code) {
|
|
|
|
|
buzzing = false
|
|
|
|
|
hide("active")
|
|
|
|
|
show("ready")
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
2021-01-31 00:19:35 +01:00
|
|
|
const username_el = q("#username")
|
|
|
|
|
if (storage["my_name"]) {
|
|
|
|
|
username_el.value = storage["my_name"]
|
|
|
|
|
}
|
2021-02-01 23:07:49 +01:00
|
|
|
on(
|
|
|
|
|
"change",
|
|
|
|
|
(event) => {
|
|
|
|
|
set_name(event.target.value)
|
|
|
|
|
},
|
|
|
|
|
{ target: username_el },
|
|
|
|
|
)
|
|
|
|
|
}
|
2021-01-29 01:20:17 +01:00
|
|
|
|
2021-02-01 23:07:49 +01:00
|
|
|
function disable_player_ui() {
|
|
|
|
|
off("focus")
|
|
|
|
|
off("blur")
|
|
|
|
|
off("keydown")
|
|
|
|
|
off("keyup")
|
|
|
|
|
node(q("body"), { cls: { player: 0 } })
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function enable_admin_ui() {
|
|
|
|
|
const body = q("body")
|
|
|
|
|
if (body.classList.contains("admin")) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
body.classList.add("admin")
|
|
|
|
|
|
|
|
|
|
const named = Object.fromEntries(
|
|
|
|
|
Array.from(document.querySelectorAll("[name]")).map((el) => [el.name, el]),
|
|
|
|
|
)
|
|
|
|
|
on(
|
|
|
|
|
"click",
|
|
|
|
|
({ target }) => {
|
|
|
|
|
if (!target.dataset.eval) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
eval(target.dataset.eval)
|
|
|
|
|
},
|
|
|
|
|
{ target: q("#points-admin") },
|
|
|
|
|
)
|
|
|
|
|
on(
|
|
|
|
|
"click",
|
|
|
|
|
({ target }) => {
|
|
|
|
|
let node = target
|
|
|
|
|
while (!node.dataset.cid && node.parentElement) {
|
|
|
|
|
node = node.parentElement
|
|
|
|
|
}
|
|
|
|
|
if (!target.dataset.eval) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
const client = find_client(node.dataset.cid)
|
|
|
|
|
if (!client) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
eval(target.dataset.eval)
|
|
|
|
|
},
|
|
|
|
|
{ target: q("#info .players") },
|
|
|
|
|
)
|
2021-01-29 01:20:17 +01:00
|
|
|
}
|
|
|
|
|
|
2021-01-31 00:19:35 +01:00
|
|
|
function set_name(name) {
|
|
|
|
|
storage["my_name"] = name
|
|
|
|
|
send("name", name)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function session_url(sid) {
|
|
|
|
|
return `${config.wsurl}/${sid}`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function session_id_from_url(url) {
|
|
|
|
|
const wsurl = new URL(config.wsurl)
|
|
|
|
|
const match = RegExp(`${wsurl.pathname}/([^/]+)$`).exec(url)
|
|
|
|
|
return !match ? null : match[1]
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-29 01:20:17 +01:00
|
|
|
function setup_ws() {
|
|
|
|
|
const sid = session_id()
|
2021-01-31 00:19:35 +01:00
|
|
|
const credentials = { id: storage["my_uid"], key: storage["my_key"] }
|
|
|
|
|
socket = new WebSocket(`${session_url(sid)}`)
|
2021-01-29 01:20:17 +01:00
|
|
|
socket.addEventListener("open", function (event) {
|
2021-01-31 00:19:35 +01:00
|
|
|
if (sid === storage["my_sid"]) {
|
|
|
|
|
send("login", credentials)
|
|
|
|
|
}
|
|
|
|
|
set_name(q("#username").value)
|
2021-01-29 01:20:17 +01:00
|
|
|
})
|
|
|
|
|
socket.addEventListener("message", function (event) {
|
|
|
|
|
const msg = JSON.parse(event.data)
|
2021-02-01 23:07:49 +01:00
|
|
|
const { type, value } = msg
|
|
|
|
|
if (type === "time") {
|
|
|
|
|
servertime = value.time
|
2021-01-29 01:20:17 +01:00
|
|
|
toffset_ms = performance.now()
|
2021-02-01 23:07:49 +01:00
|
|
|
} else if (type === "id") {
|
|
|
|
|
me = value
|
2021-01-31 00:19:35 +01:00
|
|
|
storage["my_uid"] = me.id
|
2021-01-30 13:41:25 +01:00
|
|
|
storage["my_key"] = me.key
|
2021-01-31 00:19:35 +01:00
|
|
|
storage["my_sid"] = session_id_from_url(me.path)
|
2021-01-29 01:20:17 +01:00
|
|
|
redraw_clients(me, clients)
|
2021-02-01 23:07:49 +01:00
|
|
|
} else if (type === "session_key") {
|
|
|
|
|
session_key = value
|
2021-01-30 13:41:25 +01:00
|
|
|
storage["session_path"] = session_key.path
|
|
|
|
|
storage["session_key"] = session_key.key
|
2021-02-01 23:07:49 +01:00
|
|
|
disable_player_ui()
|
|
|
|
|
enable_admin_ui()
|
|
|
|
|
} else if (type === "buzz") {
|
|
|
|
|
const buzztime_ns = value.time
|
|
|
|
|
const client_id = value.client
|
2021-01-29 01:20:17 +01:00
|
|
|
const duration_ns = 3 * s_ns
|
|
|
|
|
const until_ns = buzztime_ns + duration_ns
|
|
|
|
|
highlight(client_id, until_ns)
|
2021-02-01 23:07:49 +01:00
|
|
|
} else if (type === "clients") {
|
|
|
|
|
clients = value.clients
|
2021-01-29 01:20:17 +01:00
|
|
|
redraw_clients(me, clients)
|
2021-02-01 23:07:49 +01:00
|
|
|
} else if (type === "client") {
|
|
|
|
|
const client = value.client
|
|
|
|
|
const idx = clients.findIndex((c) => c.id === client.id)
|
|
|
|
|
if (idx >= 0) {
|
|
|
|
|
clients[idx] = client
|
|
|
|
|
} else {
|
|
|
|
|
clients.push(client)
|
2021-01-29 01:20:17 +01:00
|
|
|
}
|
|
|
|
|
redraw_clients(me, clients)
|
2021-02-01 23:07:49 +01:00
|
|
|
} else if (type === "error") {
|
|
|
|
|
console.error(`Error: ${value.reason}`)
|
2021-01-31 00:19:35 +01:00
|
|
|
const errorbox = q("#error")
|
|
|
|
|
q("code", errorbox).textContent = JSON.stringify(msg, null, 2)
|
2021-02-01 23:07:49 +01:00
|
|
|
q("body").classList.add("error")
|
2021-01-29 01:20:17 +01:00
|
|
|
} else {
|
|
|
|
|
console.error(`Unknown message: ${event.data}`)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setup_url()
|
|
|
|
|
setup_ui()
|
|
|
|
|
setup_ws()
|