2021-02-02 00:42:02 +01:00
|
|
|
"use strict"
|
|
|
|
|
|
|
|
|
|
/* global document, window */
|
|
|
|
|
const location = document.location
|
|
|
|
|
const performance = window.performance
|
|
|
|
|
|
|
|
|
|
import {
|
|
|
|
|
clear,
|
2021-02-02 21:02:30 +01:00
|
|
|
Connection,
|
2021-02-02 00:42:02 +01:00
|
|
|
ms_ns,
|
|
|
|
|
node,
|
|
|
|
|
q,
|
|
|
|
|
s_ns,
|
|
|
|
|
session_id,
|
|
|
|
|
session_url,
|
|
|
|
|
} from "./shared.js"
|
|
|
|
|
|
2021-02-02 21:02:30 +01:00
|
|
|
let conn,
|
2021-02-02 00:42:02 +01:00
|
|
|
clients = {},
|
|
|
|
|
me
|
|
|
|
|
|
|
|
|
|
function setup_url() {
|
|
|
|
|
const sid = session_id() || ""
|
|
|
|
|
location.hash = sid
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function prettynum(n) {
|
|
|
|
|
let i = Math.abs(n) | 0
|
|
|
|
|
let s = []
|
|
|
|
|
while (true) {
|
|
|
|
|
const m = i % 1000
|
|
|
|
|
i = (i / 1000) | 0
|
|
|
|
|
if (i === 0) {
|
|
|
|
|
s.unshift(String(m))
|
|
|
|
|
break
|
|
|
|
|
} else {
|
|
|
|
|
s.unshift(String(m).padStart(3, "0"))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return (n < 0 ? "-" : "") + s.join("'")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function assert(expected, got) {
|
|
|
|
|
if (expected !== got) {
|
|
|
|
|
throw Error("Assertion failed.")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function test_prettynum() {
|
|
|
|
|
assert("0", prettynum(0))
|
|
|
|
|
assert("1", prettynum(1))
|
|
|
|
|
assert("1'000", prettynum(1000))
|
|
|
|
|
assert("36'000", prettynum(36000))
|
|
|
|
|
assert("0", prettynum(-0))
|
|
|
|
|
assert("-1", prettynum(-1))
|
|
|
|
|
assert("-1'000", prettynum(-1000))
|
|
|
|
|
assert("-36'000", prettynum(-36000))
|
|
|
|
|
assert("-36'600", prettynum(-36600))
|
|
|
|
|
}
|
|
|
|
|
test_prettynum()
|
|
|
|
|
|
|
|
|
|
const player_list = q("#info")
|
|
|
|
|
function redraw_clients(me, clients) {
|
|
|
|
|
if (!me) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
clear(player_list)
|
2021-02-02 19:56:48 +01:00
|
|
|
const player_tpl = q("template#player").content.firstElementChild
|
2021-02-02 00:42:02 +01:00
|
|
|
for (const c of Object.values(clients)) {
|
|
|
|
|
if (c.id === me.id) {
|
|
|
|
|
continue
|
|
|
|
|
}
|
2021-02-02 19:56:48 +01:00
|
|
|
const player_el = node(player_tpl.cloneNode(true), {
|
|
|
|
|
data: { cid: c.id },
|
|
|
|
|
appendTo: player_list,
|
|
|
|
|
})
|
|
|
|
|
q(".name", player_el).textContent = c.name
|
|
|
|
|
q(".points.fg", player_el).textContent = prettynum(c.points)
|
|
|
|
|
q(".points.bg", player_el).textContent = prettynum(c.points)
|
2021-02-02 21:02:30 +01:00
|
|
|
q(".tokens", player_el).textContent = "🔴 ".repeat(c.tokens)
|
2021-02-02 00:42:02 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let highlighted = false
|
|
|
|
|
function highlight(client_id, until_ns) {
|
|
|
|
|
if (highlighted) {
|
|
|
|
|
return
|
|
|
|
|
}
|
2021-02-02 21:02:30 +01:00
|
|
|
const timeout_ms = (until_ns - conn.servertime_now_ns()) / ms_ns
|
2021-02-02 00:42:02 +01:00
|
|
|
if (timeout_ms <= 10) {
|
|
|
|
|
console.warn("That highlight timeout was ridiculously low:", client_id, timeout_ms)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
for (const li of player_list.children) {
|
|
|
|
|
if (li.dataset.cid === client_id) {
|
|
|
|
|
li.classList.add("buzzing")
|
|
|
|
|
highlighted = true
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
highlighted = false
|
|
|
|
|
li.classList.remove("buzzing")
|
|
|
|
|
}, timeout_ms)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function setup_ws() {
|
|
|
|
|
const sid = session_id()
|
2021-02-02 21:02:30 +01:00
|
|
|
conn = new Connection()
|
|
|
|
|
conn.on("helo", () => {
|
|
|
|
|
conn.send("name", "Monitor")
|
2021-02-02 00:42:02 +01:00
|
|
|
})
|
2021-02-02 21:02:30 +01:00
|
|
|
conn.on("id", ({ value }) => {
|
|
|
|
|
me = value
|
|
|
|
|
redraw_clients(me, clients)
|
|
|
|
|
})
|
|
|
|
|
conn.on("buzz", ({ value }) => {
|
|
|
|
|
const { time: buzztime_ns, client: client_id } = value
|
|
|
|
|
const duration_ns = 12 * s_ns
|
|
|
|
|
const until_ns = buzztime_ns + duration_ns
|
|
|
|
|
highlight(client_id, until_ns)
|
|
|
|
|
})
|
|
|
|
|
conn.on("clients", ({ value }) => {
|
|
|
|
|
clients = Object.fromEntries(value.clients.map((c) => [c.id, c]))
|
|
|
|
|
redraw_clients(me, clients)
|
|
|
|
|
})
|
|
|
|
|
conn.on("client", ({ value: { client } }) => {
|
|
|
|
|
clients[client.id] = client
|
|
|
|
|
redraw_clients(me, clients)
|
|
|
|
|
})
|
|
|
|
|
conn.on("control", ({ value }) => {
|
|
|
|
|
// ...
|
2021-02-02 00:42:02 +01:00
|
|
|
})
|
2021-02-02 21:02:30 +01:00
|
|
|
conn.connect(session_url(sid))
|
2021-02-02 00:42:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setup_url()
|
|
|
|
|
setup_ws()
|