Fixed timer

nm_live
noah metz 2024-01-27 16:34:02 -07:00
parent 1b1c914d5d
commit 3fbfee0db0
2 changed files with 20 additions and 4 deletions

@ -29,7 +29,7 @@ iframe{
</head> </head>
<body> <body>
<iframe src="index.html?field=1/1" width="1176" height="168"></iframe> <iframe src="index.html?field=1/1" width="1176" height="168"></iframe>
<iframe src="index.html?field=1/2" width="896" height="128"></iframe> <iframe src="index.html?field=1/2" width="1176" height="168"></iframe>
<iframe src="index.html?field=1/3" width="896" height="128"></iframe> <iframe src="index.html?field=1/3" width="896" height="128"></iframe>
<iframe src="index.html?field=1/4" width="896" height="128"></iframe> <iframe src="index.html?field=1/4" width="896" height="128"></iframe>
</body> </body>

@ -48,10 +48,20 @@ let display_state = "init";
/** @type {string} */ /** @type {string} */
let display_class = "side-display"; 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", () => { client.on("connect", () => {
console.log("connected to mqtt"); 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) => { client.subscribe(display_state_topic, (err) => {
if (err) { if (err) {
console.log(err); console.log(err);
@ -109,7 +119,7 @@ function tick_timer() {
} }
const now = new Date() 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) { if (time_diff <= 0) {
timer = "0:00"; timer = "0:00";
stop_timer(); stop_timer();
@ -128,6 +138,12 @@ client.on("message", (topic, message) => {
console.log(`${topic_str} - ${message_str}`); console.log(`${topic_str} - ${message_str}`);
switch(topic_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: case field_metadata_topic:
const field_obj = JSON.parse(message_str); const field_obj = JSON.parse(message_str);
teams_red = [field_obj.red_teams[0], field_obj.red_teams[1]]; teams_red = [field_obj.red_teams[0], field_obj.red_teams[1]];
@ -281,7 +297,7 @@ p {
font-family: "apple2"; font-family: "apple2";
margin: 0px; margin: 0px;
color: white; color: white;
-webkit-text-stroke: 0.02em black; -webkit-text-stroke: 1px black;
} }
#score-background { #score-background {