]> git.sesse.net Git - ultimatescore/blob - score.js
Update to three groups.
[ultimatescore] / score.js
1 'use strict';
2
3 let clock_running = false;
4 let clock2_running = false;
5 let clock_visible = false;
6 let scorebug2_visible = false;
7 let comment_visible = false;
8 let lowerthird_visible = false;
9 let clock_elapsed = 0;
10 let clock2_elapsed = 0;
11 let clock_limit = 30 * 60;
12 let scoreA = 0;
13 let scoreB = 0;
14 let clock_origin;
15 let clock2_origin;
16 let state = {};
17
18 function setteams()
19 {
20         document.getElementById('team1').innerHTML = state['team1'];
21         document.getElementById('team2').innerHTML = state['team2'];
22
23         let sheets = { 'A': state['team1'], 'B': state['team2'] };
24         loadquickl3s(sheets);
25 }
26
27 function setteams2()
28 {
29         document.getElementById('score2_team1').innerHTML = state['team1'];
30         document.getElementById('score2_team2').innerHTML = state['team2'];
31 }
32
33 function setcolors()
34 {
35         document.getElementById('team1color').style.backgroundColor = state['team1color'];
36         document.getElementById('team2color').style.backgroundColor = state['team2color'];
37 }
38
39 function setscore()
40 {
41         scoreA = state['score1'];
42         scoreB = state['score2'];
43         update_score();
44 }
45
46 function setscore2()
47 {
48         scoreA = state['score1'];
49         scoreB = state['score2'];
50         document.getElementById('score2_score').innerHTML = scoreA + " – " + scoreB;
51 }
52
53 function startclock()
54 {
55         if (!clock_running) {
56                 clock_origin = Date.now();
57                 clock_running = true;
58         }
59         showclock();
60 }
61
62 function startclock2()
63 {
64         if (!clock2_running) {
65                 clock2_origin = Date.now();
66                 clock2_running = true;
67         }
68         // No showclock.
69 }
70
71 function stopclock()
72 {
73         if (!clock_running) return;
74         clock_elapsed = time_elapsed();
75         clock_origin = Date.now();
76         clock_running = false;
77 }
78
79 function stopclock2()
80 {
81         if (!clock2_running) return;
82         clock2_elapsed = time2_elapsed();
83         clock2_origin = Date.now();
84         clock2_running = false;
85 }
86
87 function setclock(amount)
88 {
89         clock_elapsed = amount;
90         clock_origin = Date.now();
91         update_clock();
92 }
93
94 function setclock2(amount)
95 {
96         clock2_elapsed = amount;
97         clock2_origin = Date.now();
98         update_clock2();
99 }
100
101 function setclockfromstate()
102 {
103         let amount = parseInt(state['clock_min']) * 60 + parseInt(state['clock_sec']);
104         setclock(amount);
105 }
106
107 // No setclock2fromstate.
108
109 function adjustclockfromstate()
110 {
111         let amount = parseInt(state['clock_min']) * 60 + parseInt(state['clock_sec']);
112         let elapsed = time_elapsed_raw();
113         if (Math.abs(amount - elapsed) >= 2.0) {
114                 setclock(amount);
115         }
116 }
117
118 function adjustclock2fromstate()
119 {
120         let amount = parseInt(state['clock_min']) * 60 + parseInt(state['clock_sec']);
121         let elapsed = time_elapsed2_raw();
122         if (Math.abs(amount - elapsed) >= 2.0) {
123                 setclock2(amount);
124         }
125 }
126
127 function setclocklimitfromstate()
128 {
129         let amount = parseInt(state['clock_limit_min']) * 60 + parseInt(state['clock_limit_sec']);
130         clock_limit = amount;
131 }
132
133 function showclock()
134 {
135         if (clock_visible) return;
136         let clockbug = document.getElementById('clockbug');
137         clockbug.className = 'clockbug clockbug-animate-in';
138         clock_visible = true;
139 }
140
141 function hideclock()
142 {
143         if (!clock_visible) return;
144         let clockbug = document.getElementById('clockbug');
145         clockbug.className = 'clockbug clockbug-animate-out';
146         clock_visible = false;
147 }
148
149 function setcomment()
150 {
151         document.getElementById('comment').innerHTML = state['comment'];
152 }
153
154 function showcomment()
155 {
156         if (comment_visible) return;
157         let commentbug = document.getElementById('commentbug');
158         commentbug.className = 'commentbug commentbug-animate-in';
159         comment_visible = true;
160 }
161
162 function hidecomment()
163 {
164         if (!comment_visible) return;
165         let commentbug = document.getElementById('commentbug');
166         commentbug.className = 'commentbug commentbug-animate-out';
167         comment_visible = false;
168 }
169
170 function showlowerthird()
171 {
172         if (lowerthird_visible) return;
173
174         document.getElementById('lowerthird-headline').className = 'lowerthird-headline lowerthird-headline-animate-in';
175         document.getElementById('lowerthird-headline-content').className = 'lowerthird-headline-content lowerthird-headline-content-animate-in';
176         document.getElementById('lowerthird-subheading').className = 'lowerthird-subheading lowerthird-subheading-animate-in';
177         document.getElementById('lowerthird-subheading-content').className = 'lowerthird-subheading-content lowerthird-subheading-content-animate-in';
178         document.getElementById('lowerthird-picture').className = 'lowerthird-picture lowerthird-picture-animate-in';
179         document.getElementById('lowerthird-picture-content').className = 'lowerthird-picture-content lowerthird-picture-content-animate-in';
180         lowerthird_visible = true;
181 }
182
183 function setandshowlowerthird()
184 {
185         document.getElementById('lowerthird-headline-content').innerHTML = state['text1'];
186         document.getElementById('lowerthird-subheading-content').innerHTML = state['text2'];
187         let img = document.getElementById('lowerthird-img');
188         if (state['image'] === undefined) {
189                 img.style.display = 'none';
190         } else {
191                 img.src = state['image'];       
192                 img.style.display = 'inline';
193         }
194         showlowerthird();
195 }
196
197 function hidelowerthird()
198 {
199         if (!lowerthird_visible) return;
200         document.getElementById('lowerthird-headline').className = 'lowerthird-headline lowerthird-headline-hidden lowerthird-headline-animate-out';
201         document.getElementById('lowerthird-headline-content').className = 'lowerthird-headline-content lowerthird-headline-content-animate-out';
202         document.getElementById('lowerthird-subheading').className = 'lowerthird-subheading lowerthird-subheading-animate-out';
203         document.getElementById('lowerthird-subheading-content').className = 'lowerthird-subheading-content lowerthird-subheading-content-animate-out';
204         document.getElementById('lowerthird-picture').className = 'lowerthird-picture lowerthird-picture-animate-out';
205         document.getElementById('lowerthird-picture-content').className = 'lowerthird-picture-content lowerthird-picture-content-animate-out';
206         lowerthird_visible = false;
207 }
208
209 function time_elapsed_raw()
210 {
211         let elapsed = (Date.now() - clock_origin) * 1e-3;
212         if (clock_elapsed + elapsed >= clock_limit) {
213                 clock_elapsed = clock_limit;
214                 clock_origin = Date.now();
215                 clock_running = false;
216
217                 if (state['autocomment_on_clock_limit'] == '1' && !comment_visible) {
218                         state['comment'] = state['autocomment'];
219                         setcomment();
220                         showcomment();
221                 }
222
223                 return clock_limit;
224         }
225         return clock_elapsed + elapsed;
226 }
227
228 function time_elapsed2_raw()
229 {
230         let elapsed = (Date.now() - clock2_origin) * 1e-3;
231         if (clock2_elapsed + elapsed >= clock_limit) {
232                 // No separate clock 2 limit.
233                 return clock_limit;
234         }
235         return clock2_elapsed + elapsed;
236 }
237
238 function time_elapsed()
239 {
240         return Math.floor(time_elapsed_raw());
241 }
242
243 function time_elapsed2()
244 {
245         return Math.floor(time_elapsed2_raw());
246 }
247
248 function update_given_clock(elapsed, id)
249 {
250         let min = Math.floor(elapsed / 60);
251         let sec = elapsed % 60;
252
253         if (sec < 10) sec = "0" + sec;
254         let text = min + ":" + sec;
255
256         if (ultimateconfig['exohack']) {
257                 // This is a hack around the fact that Exo has variable-width numerals.
258                 // It doesn't look fantastic, but for the clock, it's better not to have
259                 // the text jumping around.
260                 let html = "";
261                 for (let i = 0; i < text.length; ++i) {
262                         if (text.charAt(i) === ':') {
263                                 html += ':';
264                         } else {
265                                 html += "<div style='display: inline-block; width: 15px'>" + text.charAt(i) + "</div>";
266                         }
267                 }
268                 document.getElementById(id).innerHTML = html;
269         } else {
270                 document.getElementById(id).innerHTML = text;
271         }
272 }
273
274 function update_clock()
275 {
276         update_given_clock(time_elapsed(), 'clock');
277 }
278
279 function update_clock2()
280 {
281         update_given_clock(time_elapsed2(), 'clock2');
282 }
283
284 function goalA()
285 {
286         ++scoreA;
287         update_score();
288 }
289
290 function goalB()
291 {
292         ++scoreB;
293         update_score();
294 }
295
296 function ungoalA()
297 {
298         if (scoreA > 0) --scoreA;
299         update_score();
300 }
301
302 function ungoalB()
303 {
304         if (scoreB > 0) --scoreB;
305         update_score();
306 }
307
308 function resetscore()
309 {
310         scoreA = scoreB = 0;
311         update_score();
312 }
313
314 function update_score()
315 {
316         document.getElementById('score').innerHTML = scoreA + "&nbsp;–&nbsp;" + scoreB;
317 }
318
319 /* called by the Nageru theme only */
320 function play()
321 {
322         document.getElementById('manualcontrols').style.display = 'none';
323         document.getElementById('area').style.display = 'none';
324 }
325
326 function update(v)
327 {
328         console.log('[[[' + v + ']]]');
329         let j = JSON.parse(v);
330         for(let key in j) state[key] = j[key];
331 }
332
333 setInterval(function() {
334         if (clock_running) {
335                 update_clock();
336         }
337         if (clock2_running) {
338                 update_clock2();
339         }
340 }, 100);
341 update_score();
342
343 //play();
344 //startclock();
345
346 let websocket = null;
347
348 function open_ws()
349 {
350         console.log("Connecting...");
351         try {
352                 if (websocket)
353                         websocket.close();
354                 websocket = new WebSocket("ws://127.0.0.1:5250/");
355                 websocket.onopen = function(evt) {
356                         console.log("Connected to client.");
357                 };
358                 websocket.onclose = function(evt) {
359                         console.log("Disconnected from client.");
360                         setTimeout(open_ws, 100);
361                 };
362                 websocket.onmessage = function(evt) {
363                         let msg = evt.data;
364                         let m = msg.match(/^update (.*)/);
365                         if (m !== null) {
366                                 update(m[1]);
367                         }
368                         m = msg.match(/^eval (.*)/);
369                         if (m !== null) {
370                                 eval(m[1]);
371                         }
372                 };
373                 websocket.onerror = function(evt) {
374                         console.log('Error: ' + evt.data);
375                 };
376         } catch (exception) {
377                 console.log('Error: ' + exception);
378                 setTimeout(open_ws, 100);
379         }
380 };
381 open_ws();