fix connection time syncing handling

Share the time syncing code, and while we're at it wrap the whole
connection thing in a class.  Makes it easier to pass the connection
around & later on add more advanced handler registration if we want to.
This commit is contained in:
ducklet 2021-02-02 21:02:30 +01:00
parent f900542765
commit 9e7000054b
3 changed files with 132 additions and 112 deletions

View file

@ -6,18 +6,16 @@ const performance = window.performance
import {
clear,
Connection,
ms_ns,
node,
q,
s_ns,
servertime_now_ns,
session_id,
session_url,
} from "./shared.js"
let socket,
servertime,
toffset_ms,
let conn,
clients = {},
me
@ -26,11 +24,6 @@ function setup_url() {
location.hash = sid
}
function send(type, value) {
// console.debug('sending', value)
socket.send(JSON.stringify({ type, value }))
}
function prettynum(n) {
let i = Math.abs(n) | 0
let s = []
@ -84,7 +77,7 @@ function redraw_clients(me, clients) {
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)
q(".tokens", player_el).textContent = '🔴 '.repeat(c.tokens)
q(".tokens", player_el).textContent = "🔴 ".repeat(c.tokens)
}
}
@ -93,7 +86,7 @@ function highlight(client_id, until_ns) {
if (highlighted) {
return
}
const timeout_ms = (until_ns - servertime_now_ns()) / ms_ns
const timeout_ms = (until_ns - conn.servertime_now_ns()) / ms_ns
if (timeout_ms <= 10) {
console.warn("That highlight timeout was ridiculously low:", client_id, timeout_ms)
return
@ -113,42 +106,32 @@ function highlight(client_id, until_ns) {
function setup_ws() {
const sid = session_id()
socket = new WebSocket(session_url(sid))
socket.addEventListener("open", function (event) {
send("name", "Monitor")
conn = new Connection()
conn.on("helo", () => {
conn.send("name", "Monitor")
})
socket.addEventListener("message", function (event) {
const msg = JSON.parse(event.data)
const { type, value } = msg
if (msg.type === "time") {
servertime = value.time
toffset_ms = performance.now()
} else if (msg.type === "id") {
me = value
redraw_clients(me, clients)
} else if (msg.type === "buzz") {
const buzztime_ns = value.time
const client_id = value.client
const duration_ns = 12 * s_ns
const until_ns = buzztime_ns + duration_ns
highlight(client_id, until_ns)
} else if (msg.type === "clients") {
clients = Object.fromEntries(value.clients.map((c) => [c.id, c]))
redraw_clients(me, clients)
} else if (msg.type === "client") {
const client = value.client
clients[client.id] = client
redraw_clients(me, clients)
} else if (type === "control") {
// ignore
} else if (type === "session_key") {
// ignore
} else if (type === "error") {
console.error(`Error: ${value.reason}`)
} else {
console.error(`Unknown message: ${event.data}`)
}
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 }) => {
// ...
})
conn.connect(session_url(sid))
}
setup_url()