From 3fbfee0db075e5b587529b62c86c503f8f0d36e1 Mon Sep 17 00:00:00 2001 From: Noah Metz Date: Sat, 27 Jan 2024 16:34:02 -0700 Subject: [PATCH] Fixed timer --- iframe.html | 2 +- src/routes/+page.svelte | 22 +++++++++++++++++++--- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/iframe.html b/iframe.html index d794064..5464dcc 100644 --- a/iframe.html +++ b/iframe.html @@ -29,7 +29,7 @@ iframe{ - + diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte index eb5c014..f310007 100644 --- a/src/routes/+page.svelte +++ b/src/routes/+page.svelte @@ -48,10 +48,20 @@ let display_state = "init"; /** @type {string} */ let display_class = "side-display"; -const client = mqtt.connect("ws://127.0.0.1:8883"); +const client = mqtt.connect("ws://metznet.ca:8883"); + +let offset = 0; client.on("connect", () => { console.log("connected to mqtt"); + client.subscribe("time", (err) => { + if (err) { + console.log(err); + } else { + console.log(`subscribed to ${display_state_topic}`); + } + }); + client.subscribe(display_state_topic, (err) => { if (err) { console.log(err); @@ -109,7 +119,7 @@ function tick_timer() { } const now = new Date() - const time_diff = timer_end.getTime() - now.getTime(); + const time_diff = timer_end.getTime() - now.getTime() + offset; if (time_diff <= 0) { timer = "0:00"; stop_timer(); @@ -128,6 +138,12 @@ client.on("message", (topic, message) => { console.log(`${topic_str} - ${message_str}`); switch(topic_str){ + case "time": + let local = Date.now(); + let server = JSON.parse(message_str) * 1000; + offset = local - server; + console.log(`NEW offset: ${local} - ${server} = ${offset}`); + break; case field_metadata_topic: const field_obj = JSON.parse(message_str); teams_red = [field_obj.red_teams[0], field_obj.red_teams[1]]; @@ -281,7 +297,7 @@ p { font-family: "apple2"; margin: 0px; color: white; - -webkit-text-stroke: 0.02em black; + -webkit-text-stroke: 1px black; } #score-background {