X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=score.js;h=ab3e465cc74f10d98b0f7db17a6a8b21a0766635;hb=4d2991a8d05b23222912fb904b436af5f4d740c2;hp=5f8771cf6301d6370a0d8089eb25a2be94ab6c1d;hpb=525ab020c0d57fd40fab90d61a59796e985d978f;p=ultimatescore diff --git a/score.js b/score.js index 5f8771c..ab3e465 100644 --- a/score.js +++ b/score.js @@ -1,17 +1,39 @@ -var clock_running = false; -var clock_visible = false; -var comment_visible = false; -var lowerthird_visible = false; -var clock_left = 25 * 60; -var scoreA = 0; -var scoreB = 0; -var clock_origin; -var state = {}; +'use strict'; + +let num_clocks = 3; +let clocks = [ + { 'running': false, 'elapsed': 0, 'origin': undefined, 'id': 'clock' }, + { 'running': false, 'elapsed': 0, 'origin': undefined, 'id': 'clock2' }, + { 'running': false, 'elapsed': 0, 'origin': undefined, 'id': 'clock3' } +]; + +let clock_visible = false; +let comment_visible = false; +let lowerthird_visible = false; +let clock_limit = 30 * 60; +let scoreA = 0; +let scoreB = 0; +let state = {}; function setteams() { document.getElementById('team1').innerHTML = state['team1']; document.getElementById('team2').innerHTML = state['team2']; + + let sheets = { 'A': state['team1'], 'B': state['team2'] }; + loadquickl3s(sheets); +} + +function setteams2() +{ + document.getElementById('score2_team1').innerHTML = state['team1']; + document.getElementById('score2_team2').innerHTML = state['team2']; +} + +function setteams3() +{ + document.getElementById('score3_team1').innerHTML = state['team1']; + document.getElementById('score3_team2').innerHTML = state['team2']; } function setcolors() @@ -27,40 +49,79 @@ function setscore() update_score(); } -function startclock() +function setscore2() { - if (!clock_running) { - clock_origin = Date.now(); - clock_running = true; + scoreA = state['score1']; + scoreB = state['score2']; + document.getElementById('score2_score').innerHTML = scoreA + " â€“ " + scoreB; +} + +function setscore3() +{ + scoreA = state['score1']; + scoreB = state['score2']; + document.getElementById('score3_score').innerHTML = scoreA + " â€“ " + scoreB; +} + +function startclock(num) +{ + if (!clocks[num].running) { + clocks[num].origin = Date.now(); + clocks[num].running = true; + } + if (num == 0) { + showclock(); } - showclock(); } -function stopclock() +function stopclock(num) { - if (!clock_running) return; - clock_left = time_left(); - clock_origin = Date.now(); - clock_running = false; + if (!clocks[num].running) return; + clocks[num].elapsed = time_elapsed(num); + clocks[num].origin = Date.now(); + clocks[num].running = false; } -function setclock(amount) +function setclock(num, amount) { - clock_left = amount; - clock_origin = Date.now(); - update_clock(); + clocks[num].elapsed = amount; + clocks[num].origin = Date.now(); + update_clock(num); + + // The BT-6000 likes to reset to 0:00 a while after the match is over, + // so go hide it then. + if (num == 0 && !clocks[num].running) { + hideclock(); + } } function setclockfromstate() { - var amount = parseInt(state['clock_min']) * 60 + parseInt(state['clock_sec']); - setclock(amount); + let amount = parseInt(state['clock_min']) * 60 + parseInt(state['clock_sec']); + setclock(0, amount); +} + +// No setclock2fromstate. + +function adjustclockfromstate(num) +{ + let amount = parseInt(state['clock_min']) * 60 + parseInt(state['clock_sec']); + let elapsed = time_elapsed_raw(num); + if (Math.abs(amount - elapsed) >= 2.0) { + setclock(num, 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; - var clockbug = document.getElementById('clockbug'); + let clockbug = document.getElementById('clockbug'); clockbug.className = 'clockbug clockbug-animate-in'; clock_visible = true; } @@ -68,7 +129,7 @@ function showclock() function hideclock() { if (!clock_visible) return; - var clockbug = document.getElementById('clockbug'); + let clockbug = document.getElementById('clockbug'); clockbug.className = 'clockbug clockbug-animate-out'; clock_visible = false; } @@ -81,7 +142,7 @@ function setcomment() function showcomment() { if (comment_visible) return; - var commentbug = document.getElementById('commentbug'); + let commentbug = document.getElementById('commentbug'); commentbug.className = 'commentbug commentbug-animate-in'; comment_visible = true; } @@ -89,7 +150,7 @@ function showcomment() function hidecomment() { if (!comment_visible) return; - var commentbug = document.getElementById('commentbug'); + let commentbug = document.getElementById('commentbug'); commentbug.className = 'commentbug commentbug-animate-out'; comment_visible = false; } @@ -98,19 +159,6 @@ function showlowerthird() { if (lowerthird_visible) return; - // With no flexbox, this is how it has to be... - var f = document.getElementById('lowerthird-headline'); - var 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'; @@ -124,11 +172,11 @@ function setandshowlowerthird() { document.getElementById('lowerthird-headline-content').innerHTML = state['text1']; document.getElementById('lowerthird-subheading-content').innerHTML = state['text2']; - var img = document.getElementById('lowerthird-img'); + let img = document.getElementById('lowerthird-img'); if (state['image'] === undefined) { img.style.display = 'none'; } else { - img.src = state['image']; + img.src = state['image']; img.style.display = 'inline'; } showlowerthird(); @@ -141,26 +189,88 @@ 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 set_sound_shark_volume_db(volume_db) +{ + let img = document.getElementById('lowerthird-img'); + let should_enable = (volume_db > -40.0); + let currently_enabled = (img.style.display === 'inline' && img.src.match(/microphone\.png$/)); + + if (should_enable === currently_enabled) { + return; + } + + if (should_enable) { + img.style.display = 'inline'; + img.src = 'generic/microphone.png'; + document.getElementById('lowerthird-picture').className = 'lowerthird-picture lowerthird-picture-animate-in'; + document.getElementById('lowerthird-picture-content').className = 'lowerthird-picture-content lowerthird-picture-content-animate-in'; + } else { + 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'; + } +} + +function time_elapsed_raw(num) +{ + let elapsed = (Date.now() - clocks[num].origin) * 1e-3; + if (clocks[num].elapsed + elapsed >= clock_limit) { + console.log("limit for", num); + if (num == 0) { + clocks[num].elapsed = clock_limit; + clocks[num].origin = Date.now(); + clocks[num].running = false; + + if (state['autocomment_on_clock_limit'] == '1' && !comment_visible) { + state['comment'] = state['autocomment']; + setcomment(); + showcomment(); + } + } + + return clock_limit; + } + return clocks[num].elapsed + elapsed; +} + +function time_elapsed(num) { - var elapsed = (Date.now() - clock_origin) * 1e-3; - if (elapsed > clock_left) return 0; - return Math.ceil(clock_left - elapsed); + return Math.floor(time_elapsed_raw(num)); } -function update_clock() +function update_given_clock(elapsed, id) { - var left = time_left(); - var min = Math.floor(left / 60); - var sec = left % 60; + 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; + + if (ultimateconfig['exohack']) { + // 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 += "
" + text.charAt(i) + "
"; + } + } + document.getElementById(id).innerHTML = html; + } else { + document.getElementById(id).innerHTML = text; + } +} + +function update_clock(num) +{ + update_given_clock(time_elapsed(num), clocks[num].id); } function goalA() @@ -198,29 +308,65 @@ function update_score() document.getElementById('score').innerHTML = scoreA + " â€“ " + 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) { console.log('[[[' + v + ']]]'); - var j = JSON.parse(v); - for(var key in j) state[key] = j[key]; + let j = JSON.parse(v); + for(let key in j) state[key] = j[key]; } setInterval(function() { - if (clock_running) { - update_clock(); + for (let i = 0; i < num_clocks; ++i) { + if (clocks[i].running) { + update_clock(i); + } } }, 100); 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();