]> git.sesse.net Git - ultimatescore/blob - score.js
Make the roster scripts executable.
[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_clock()
249 {
250         let elapsed = time_elapsed();
251         let min = Math.floor(elapsed / 60);
252         let sec = elapsed % 60;
253
254         if (sec < 10) sec = "0" + sec;
255         let text = min + ":" + sec;
256
257         if (ultimateconfig['exohack']) {
258                 // This is a hack around the fact that Exo has variable-width numerals.
259                 // It doesn't look fantastic, but for the clock, it's better not to have
260                 // the text jumping around.
261                 let html = "";
262                 for (let i = 0; i < text.length; ++i) {
263                         if (text.charAt(i) === ':') {
264                                 html += ':';
265                         } else {
266                                 html += "<div style='display: inline-block; width: 15px'>" + text.charAt(i) + "</div>";
267                         }
268                 }
269                 document.getElementById('clock').innerHTML = html;
270         } else {
271                 document.getElementById('clock').innerHTML = text;
272         }
273 }
274
275 function update_clock2()
276 {
277         let elapsed = time_elapsed2();
278         let min = Math.floor(elapsed / 60);
279         let sec = elapsed % 60;
280
281         if (sec < 10) sec = "0" + sec;
282         let text = min + ":" + sec;
283
284         if (ultimateconfig['exohack']) {
285                 // This is a hack around the fact that Exo has variable-width numerals.
286                 // It doesn't look fantastic, but for the clock, it's better not to have
287                 // the text jumping around.
288                 let html = "";
289                 for (let i = 0; i < text.length; ++i) {
290                         if (text.charAt(i) === ':') {
291                                 html += ':';
292                         } else {
293                                 html += "<div style='display: inline-block; width: 15px'>" + text.charAt(i) + "</div>";
294                         }
295                 }
296                 document.getElementById('clock2').innerHTML = html;
297         } else {
298                 document.getElementById('clock2').innerHTML = text;
299         }
300 }
301
302 function goalA()
303 {
304         ++scoreA;
305         update_score();
306 }
307
308 function goalB()
309 {
310         ++scoreB;
311         update_score();
312 }
313
314 function ungoalA()
315 {
316         if (scoreA > 0) --scoreA;
317         update_score();
318 }
319
320 function ungoalB()
321 {
322         if (scoreB > 0) --scoreB;
323         update_score();
324 }
325
326 function resetscore()
327 {
328         scoreA = scoreB = 0;
329         update_score();
330 }
331
332 function update_score()
333 {
334         document.getElementById('score').innerHTML = scoreA + "&nbsp;–&nbsp;" + scoreB;
335 }
336
337 /* called by the Nageru theme only */
338 function play()
339 {
340         document.getElementById('manualcontrols').style.display = 'none';
341         document.getElementById('area').style.display = 'none';
342 }
343
344 function update(v)
345 {
346         console.log('[[[' + v + ']]]');
347         let j = JSON.parse(v);
348         for(let key in j) state[key] = j[key];
349 }
350
351 setInterval(function() {
352         if (clock_running) {
353                 update_clock();
354         }
355         if (clock2_running) {
356                 update_clock2();
357         }
358 }, 100);
359 update_score();
360
361 //play();
362 //startclock();
363
364 let websocket = null;
365
366 function open_ws()
367 {
368         console.log("Connecting...");
369         try {
370                 if (websocket)
371                         websocket.close();
372                 websocket = new WebSocket("ws://127.0.0.1:5250/");
373                 websocket.onopen = function(evt) {
374                         console.log("Connected to client.");
375                 };
376                 websocket.onclose = function(evt) {
377                         console.log("Disconnected from client.");
378                         setTimeout(open_ws, 100);
379                 };
380                 websocket.onmessage = function(evt) {
381                         let msg = evt.data;
382                         let m = msg.match(/^update (.*)/);
383                         if (m !== null) {
384                                 update(m[1]);
385                         }
386                         m = msg.match(/^eval (.*)/);
387                         if (m !== null) {
388                                 eval(m[1]);
389                         }
390                 };
391                 websocket.onerror = function(evt) {
392                         console.log('Error: ' + evt.data);
393                 };
394         } catch (exception) {
395                 console.log('Error: ' + exception);
396                 setTimeout(open_ws, 100);
397         }
398 };
399 open_ws();