]> git.sesse.net Git - ultimatescore/blob - score.js
Make the timer count upwards instead of downwards, and make it stop when reaching...
[ultimatescore] / score.js
1 'use strict';
2
3 let clock_running = false;
4 let clock_visible = false;
5 let comment_visible = false;
6 let lowerthird_visible = false;
7 let clock_elapsed = 0;
8 let clock_limit = 30 * 60;
9 let scoreA = 0;
10 let scoreB = 0;
11 let clock_origin;
12 let state = {};
13
14 function setteams()
15 {
16         document.getElementById('team1').innerHTML = state['team1'];
17         document.getElementById('team2').innerHTML = state['team2'];
18
19         let sheets = { 'A': state['team1'], 'B': state['team2'] };
20         loadquickl3s(sheets);
21 }
22
23 function setcolors()
24 {
25         document.getElementById('team1color').style.backgroundColor = state['team1color'];
26         document.getElementById('team2color').style.backgroundColor = state['team2color'];
27 }
28
29 function setscore()
30 {
31         scoreA = state['score1'];
32         scoreB = state['score2'];
33         update_score();
34 }
35
36 function startclock()
37 {
38         if (!clock_running) {
39                 clock_origin = Date.now();
40                 clock_running = true;
41         }
42         showclock();
43 }
44
45 function stopclock()
46 {
47         if (!clock_running) return;
48         clock_elapsed = time_elapsed();
49         clock_origin = Date.now();
50         clock_running = false;
51 }
52
53 function setclock(amount)
54 {
55         clock_elapsed = amount;
56         clock_origin = Date.now();
57         update_clock();
58 }
59
60 function setclockfromstate()
61 {
62         let amount = parseInt(state['clock_min']) * 60 + parseInt(state['clock_sec']);
63         setclock(amount);
64 }
65
66 function showclock()
67 {
68         if (clock_visible) return;
69         let clockbug = document.getElementById('clockbug');
70         clockbug.className = 'clockbug clockbug-animate-in';
71         clock_visible = true;
72 }
73
74 function hideclock()
75 {
76         if (!clock_visible) return;
77         let clockbug = document.getElementById('clockbug');
78         clockbug.className = 'clockbug clockbug-animate-out';
79         clock_visible = false;
80 }
81
82 function setcomment()
83 {
84         document.getElementById('comment').innerHTML = state['comment'];
85 }
86
87 function showcomment()
88 {
89         if (comment_visible) return;
90         let commentbug = document.getElementById('commentbug');
91         commentbug.className = 'commentbug commentbug-animate-in';
92         comment_visible = true;
93 }
94
95 function hidecomment()
96 {
97         if (!comment_visible) return;
98         let commentbug = document.getElementById('commentbug');
99         commentbug.className = 'commentbug commentbug-animate-out';
100         comment_visible = false;
101 }
102
103 function showlowerthird()
104 {
105         if (lowerthird_visible) return;
106
107         // With no flexbox, this is how it has to be...
108         let f = document.getElementById('lowerthird-headline');
109         let g = document.getElementById('lowerthird-headline-content');
110         f.style.paddingTop = Math.round((f.clientHeight - g.clientHeight) / 2) + 'px';
111
112         f = document.getElementById('lowerthird-subheading');
113         g = document.getElementById('lowerthird-subheading-content');
114         f.style.paddingTop = Math.round((f.clientHeight - g.clientHeight) / 2) + 'px';
115
116         f = document.getElementById('lowerthird-picture');
117         g = document.getElementById('lowerthird-picture-content');
118         f.style.paddingTop = Math.round((f.clientHeight - g.clientHeight) / 2) + 'px';
119
120         document.getElementById('lowerthird-headline').className = 'lowerthird-headline lowerthird-headline-animate-in';
121         document.getElementById('lowerthird-headline-content').className = 'lowerthird-headline-content lowerthird-headline-content-animate-in';
122         document.getElementById('lowerthird-subheading').className = 'lowerthird-subheading lowerthird-subheading-animate-in';
123         document.getElementById('lowerthird-subheading-content').className = 'lowerthird-subheading-content lowerthird-subheading-content-animate-in';
124         document.getElementById('lowerthird-picture').className = 'lowerthird-picture lowerthird-picture-animate-in';
125         document.getElementById('lowerthird-picture-content').className = 'lowerthird-picture-content lowerthird-picture-content-animate-in';
126         lowerthird_visible = true;
127 }
128
129 function setandshowlowerthird()
130 {
131         document.getElementById('lowerthird-headline-content').innerHTML = state['text1'];
132         document.getElementById('lowerthird-subheading-content').innerHTML = state['text2'];
133         let img = document.getElementById('lowerthird-img');
134         if (state['image'] === undefined) {
135                 img.style.display = 'none';
136         } else {
137                 img.src = state['image'];       
138                 img.style.display = 'inline';
139         }
140         showlowerthird();
141 }
142
143 function hidelowerthird()
144 {
145         if (!lowerthird_visible) return;
146         document.getElementById('lowerthird-headline').className = 'lowerthird-headline lowerthird-headline-hidden lowerthird-headline-animate-out';
147         document.getElementById('lowerthird-headline-content').className = 'lowerthird-headline-content lowerthird-headline-content-animate-out';
148         document.getElementById('lowerthird-subheading').className = 'lowerthird-subheading lowerthird-subheading-animate-out';
149         document.getElementById('lowerthird-subheading-content').className = 'lowerthird-subheading-content lowerthird-subheading-content-animate-out';
150         document.getElementById('lowerthird-picture').className = 'lowerthird-picture lowerthird-picture-hidden lowerthird-picture-animate-out';
151         document.getElementById('lowerthird-picture-content').className = 'lowerthird-picture-content lowerthird-picture-content-animate-out';
152         lowerthird_visible = false;
153 }
154
155 function time_elapsed()
156 {
157         let elapsed = (Date.now() - clock_origin) * 1e-3;
158         if (clock_elapsed + elapsed >= clock_limit) {
159                 clock_elapsed = clock_limit;
160                 clock_origin = Date.now();
161                 clock_running = false;
162                 return clock_limit;
163         }
164         return Math.floor(clock_elapsed + elapsed);
165 }
166
167 function update_clock()
168 {
169         let elapsed = time_elapsed();
170         let min = Math.floor(elapsed / 60);
171         let sec = elapsed % 60;
172
173         if (sec < 10) sec = "0" + sec;
174         document.getElementById('clock').innerHTML = min + ":" + sec;
175 }
176
177 function goalA()
178 {
179         ++scoreA;
180         update_score();
181 }
182
183 function goalB()
184 {
185         ++scoreB;
186         update_score();
187 }
188
189 function ungoalA()
190 {
191         if (scoreA > 0) --scoreA;
192         update_score();
193 }
194
195 function ungoalB()
196 {
197         if (scoreB > 0) --scoreB;
198         update_score();
199 }
200
201 function resetscore()
202 {
203         scoreA = scoreB = 0;
204         update_score();
205 }
206
207 function update_score()
208 {
209         document.getElementById('score').innerHTML = scoreA + "&nbsp;–&nbsp;" + scoreB;
210 }
211
212 /* called by caspar only */
213 function play()
214 {
215         document.getElementById('manualcontrols').style.display = 'none';
216         document.getElementById('area').style.display = 'none';
217
218         // Old CEF workaround
219         document.getElementById('lowerthird-subheading').style.top = '638px';
220 }
221
222 function update(v)
223 {
224         console.log('[[[' + v + ']]]');
225         let j = JSON.parse(v);
226         for(let key in j) state[key] = j[key];
227 }
228
229 setInterval(function() {
230         if (clock_running) {
231                 update_clock();
232         }
233 }, 100);
234 update_score();
235
236 //play();
237 //startclock();