]> git.sesse.net Git - ultimatescore/blob - score.js
Check in some scripts based on OR-tools to try to generate good group schedules.
[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                 return clock_limit;
217         }
218         return clock_elapsed + elapsed;
219 }
220
221 function time_elapsed2_raw()
222 {
223         let elapsed = (Date.now() - clock2_origin) * 1e-3;
224         if (clock2_elapsed + elapsed >= clock_limit) {
225                 // No separate clock 2 limit.
226                 return clock_limit;
227         }
228         return clock2_elapsed + elapsed;
229 }
230
231 function time_elapsed()
232 {
233         return Math.floor(time_elapsed_raw());
234 }
235
236 function time_elapsed2()
237 {
238         return Math.floor(time_elapsed2_raw());
239 }
240
241 function update_clock()
242 {
243         let elapsed = time_elapsed();
244         let min = Math.floor(elapsed / 60);
245         let sec = elapsed % 60;
246
247         if (sec < 10) sec = "0" + sec;
248         let text = min + ":" + sec;
249
250         if (false) {
251                 // This is a hack around the fact that Exo has variable-width numerals.
252                 // It doesn't look fantastic, but for the clock, it's better not to have
253                 // the text jumping around.
254                 let html = "";
255                 for (let i = 0; i < text.length; ++i) {
256                         if (text.charAt(i) === ':') {
257                                 html += ':';
258                         } else {
259                                 html += "<div style='display: inline-block; width: 15px'>" + text.charAt(i) + "</div>";
260                         }
261                 }
262                 document.getElementById('clock').innerHTML = html;
263         } else {
264                 document.getElementById('clock').innerHTML = text;
265         }
266 }
267
268 function update_clock2()
269 {
270         let elapsed = time_elapsed2();
271         let min = Math.floor(elapsed / 60);
272         let sec = elapsed % 60;
273
274         if (sec < 10) sec = "0" + sec;
275         let text = min + ":" + sec;
276
277         document.getElementById('clock2').innerHTML = text;
278 }
279
280 function goalA()
281 {
282         ++scoreA;
283         update_score();
284 }
285
286 function goalB()
287 {
288         ++scoreB;
289         update_score();
290 }
291
292 function ungoalA()
293 {
294         if (scoreA > 0) --scoreA;
295         update_score();
296 }
297
298 function ungoalB()
299 {
300         if (scoreB > 0) --scoreB;
301         update_score();
302 }
303
304 function resetscore()
305 {
306         scoreA = scoreB = 0;
307         update_score();
308 }
309
310 function update_score()
311 {
312         document.getElementById('score').innerHTML = scoreA + "&nbsp;–&nbsp;" + scoreB;
313 }
314
315 /* called by the Nageru theme only */
316 function play()
317 {
318         document.getElementById('manualcontrols').style.display = 'none';
319         document.getElementById('area').style.display = 'none';
320 }
321
322 function update(v)
323 {
324         console.log('[[[' + v + ']]]');
325         let j = JSON.parse(v);
326         for(let key in j) state[key] = j[key];
327 }
328
329 setInterval(function() {
330         if (clock_running) {
331                 update_clock();
332         }
333         if (clock2_running) {
334                 update_clock2();
335         }
336 }, 100);
337 update_score();
338
339 //play();
340 //startclock();
341
342 let websocket = null;
343
344 function open_ws()
345 {
346         console.log("Connecting...");
347         try {
348                 if (websocket)
349                         websocket.close();
350                 websocket = new WebSocket("ws://127.0.0.1:5250/");
351                 websocket.onopen = function(evt) {
352                         console.log("Connected to client.");
353                 };
354                 websocket.onclose = function(evt) {
355                         console.log("Disconnected from client.");
356                         setTimeout(open_ws, 100);
357                 };
358                 websocket.onmessage = function(evt) {
359                         let msg = evt.data;
360                         let m = msg.match(/^update (.*)/);
361                         if (m !== null) {
362                                 update(m[1]);
363                         }
364                         m = msg.match(/^eval (.*)/);
365                         if (m !== null) {
366                                 eval(m[1]);
367                         }
368                 };
369                 websocket.onerror = function(evt) {
370                         console.log('Error: ' + evt.data);
371                 };
372         } catch (exception) {
373                 console.log('Error: ' + exception);
374                 setTimeout(open_ws, 100);
375         }
376 };
377 open_ws();