]> git.sesse.net Git - ultimatescore/blobdiff - score.js
Wrap up the state as an upvalue in the prepare function.
[ultimatescore] / score.js
index 21a2cde5b71ad71034dd1fd4d944995497795b39..3f0a4d055daad4353f9d96ceffedd3adaf9679f8 100644 (file)
--- a/score.js
+++ b/score.js
@@ -4,7 +4,8 @@ let clock_running = false;
 let clock_visible = false;
 let comment_visible = false;
 let lowerthird_visible = false;
-let clock_left = 30 * 60;
+let clock_elapsed = 0;
+let clock_limit = 30 * 60;
 let scoreA = 0;
 let scoreB = 0;
 let clock_origin;
@@ -44,14 +45,14 @@ function startclock()
 function stopclock()
 {
        if (!clock_running) return;
-       clock_left = time_left();
+       clock_elapsed = time_elapsed();
        clock_origin = Date.now();
        clock_running = false;
 }
 
 function setclock(amount)
 {
-       clock_left = amount;
+       clock_elapsed = amount;
        clock_origin = Date.now();
        update_clock();
 }
@@ -62,6 +63,12 @@ function setclockfromstate()
        setclock(amount);
 }
 
+function setclocklimitfromstate()
+{
+       let amount = parseInt(state['clock_limit_min']) * 60 + parseInt(state['clock_limit_sec']);
+       clock_limit = amount;
+}
+
 function showclock()
 {
        if (clock_visible) return;
@@ -103,19 +110,6 @@ function showlowerthird()
 {
        if (lowerthird_visible) return;
 
-       // With no flexbox, this is how it has to be...
-       let f = document.getElementById('lowerthird-headline');
-       let g = document.getElementById('lowerthird-headline-content');
-       f.style.paddingTop = Math.round((f.clientHeight - g.clientHeight) / 2) + 'px';
-
-       f = document.getElementById('lowerthird-subheading');
-       g = document.getElementById('lowerthird-subheading-content');
-       f.style.paddingTop = Math.round((f.clientHeight - g.clientHeight) / 2) + 'px';
-
-       f = document.getElementById('lowerthird-picture');
-       g = document.getElementById('lowerthird-picture-content');
-       f.style.paddingTop = Math.round((f.clientHeight - g.clientHeight) / 2) + 'px';
-
        document.getElementById('lowerthird-headline').className = 'lowerthird-headline lowerthird-headline-animate-in';
        document.getElementById('lowerthird-headline-content').className = 'lowerthird-headline-content lowerthird-headline-content-animate-in';
        document.getElementById('lowerthird-subheading').className = 'lowerthird-subheading lowerthird-subheading-animate-in';
@@ -146,26 +140,44 @@ function hidelowerthird()
        document.getElementById('lowerthird-headline-content').className = 'lowerthird-headline-content lowerthird-headline-content-animate-out';
        document.getElementById('lowerthird-subheading').className = 'lowerthird-subheading lowerthird-subheading-animate-out';
        document.getElementById('lowerthird-subheading-content').className = 'lowerthird-subheading-content lowerthird-subheading-content-animate-out';
-       document.getElementById('lowerthird-picture').className = 'lowerthird-picture lowerthird-picture-hidden lowerthird-picture-animate-out';
+       document.getElementById('lowerthird-picture').className = 'lowerthird-picture lowerthird-picture-animate-out';
        document.getElementById('lowerthird-picture-content').className = 'lowerthird-picture-content lowerthird-picture-content-animate-out';
        lowerthird_visible = false;
 }
 
-function time_left()
+function time_elapsed()
 {
        let elapsed = (Date.now() - clock_origin) * 1e-3;
-       if (elapsed > clock_left) return 0;
-       return Math.ceil(clock_left - elapsed);
+       if (clock_elapsed + elapsed >= clock_limit) {
+               clock_elapsed = clock_limit;
+               clock_origin = Date.now();
+               clock_running = false;
+               return clock_limit;
+       }
+       return Math.floor(clock_elapsed + elapsed);
 }
 
 function update_clock()
 {
-       let left = time_left();
-       let min = Math.floor(left / 60);
-       let sec = left % 60;
+       let elapsed = time_elapsed();
+       let min = Math.floor(elapsed / 60);
+       let sec = elapsed % 60;
 
        if (sec < 10) sec = "0" + sec;
-       document.getElementById('clock').innerHTML = min + ":" + sec;
+       let text = min + ":" + sec;
+
+       // This is a hack around the fact that Exo has variable-width numerals.
+       // It doesn't look fantastic, but for the clock, it's better not to have
+       // the text jumping around.
+       let html = "";
+       for (let i = 0; i < text.length; ++i) {
+               if (text.charAt(i) === ':') {
+                       html += ':';
+               } else {
+                       html += "<div style='display: inline-block; width: 15px'>" + text.charAt(i) + "</div>";
+               }
+       }
+       document.getElementById('clock').innerHTML = html;
 }
 
 function goalA()
@@ -203,14 +215,11 @@ function update_score()
        document.getElementById('score').innerHTML = scoreA + "&nbsp;–&nbsp;" + scoreB;
 }
 
-/* called by caspar only */
+/* called by the Nageru theme only */
 function play()
 {
        document.getElementById('manualcontrols').style.display = 'none';
        document.getElementById('area').style.display = 'none';
-
-       // Old CEF workaround
-       document.getElementById('lowerthird-subheading').style.top = '638px';
 }
 
 function update(v)
@@ -229,3 +238,40 @@ update_score();
 
 //play();
 //startclock();
+
+let websocket = null;
+
+function open_ws()
+{
+       console.log("Connecting...");
+       try {
+               if (websocket)
+                       websocket.close();
+               websocket = new WebSocket("ws://127.0.0.1:5250/");
+               websocket.onopen = function(evt) {
+                       console.log("Connected to client.");
+               };
+               websocket.onclose = function(evt) {
+                       console.log("Disconnected from client.");
+                       setTimeout(open_ws, 100);
+               };
+               websocket.onmessage = function(evt) {
+                       let msg = evt.data;
+                       let m = msg.match(/^update (.*)/);
+                       if (m !== null) {
+                               update(m[1]);
+                       }
+                       m = msg.match(/^eval (.*)/);
+                       if (m !== null) {
+                               eval(m[1]);
+                       }
+               };
+               websocket.onerror = function(evt) {
+                       console.log('Error: ' + evt.data);
+               };
+       } catch (exception) {
+               console.log('Error: ' + exception);
+               setTimeout(open_ws, 100);
+       }
+};
+open_ws();