]> git.sesse.net Git - ultimatescore/blob - score.js
17ac07c4f8a679b7ec2466b59a1c6a43135be7ce
[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 setclocklimitfromstate()
67 {
68         let amount = parseInt(state['clock_limit_min']) * 60 + parseInt(state['clock_limit_sec']);
69         clock_limit = amount;
70 }
71
72 function showclock()
73 {
74         if (clock_visible) return;
75         let clockbug = document.getElementById('clockbug');
76         clockbug.className = 'clockbug clockbug-animate-in';
77         clock_visible = true;
78 }
79
80 function hideclock()
81 {
82         if (!clock_visible) return;
83         let clockbug = document.getElementById('clockbug');
84         clockbug.className = 'clockbug clockbug-animate-out';
85         clock_visible = false;
86 }
87
88 function setcomment()
89 {
90         document.getElementById('comment').innerHTML = state['comment'];
91 }
92
93 function showcomment()
94 {
95         if (comment_visible) return;
96         let commentbug = document.getElementById('commentbug');
97         commentbug.className = 'commentbug commentbug-animate-in';
98         comment_visible = true;
99 }
100
101 function hidecomment()
102 {
103         if (!comment_visible) return;
104         let commentbug = document.getElementById('commentbug');
105         commentbug.className = 'commentbug commentbug-animate-out';
106         comment_visible = false;
107 }
108
109 function showlowerthird()
110 {
111         if (lowerthird_visible) return;
112
113         document.getElementById('lowerthird-headline').className = 'lowerthird-headline lowerthird-headline-animate-in';
114         document.getElementById('lowerthird-headline-content').className = 'lowerthird-headline-content lowerthird-headline-content-animate-in';
115         document.getElementById('lowerthird-subheading').className = 'lowerthird-subheading lowerthird-subheading-animate-in';
116         document.getElementById('lowerthird-subheading-content').className = 'lowerthird-subheading-content lowerthird-subheading-content-animate-in';
117         document.getElementById('lowerthird-picture').className = 'lowerthird-picture lowerthird-picture-animate-in';
118         document.getElementById('lowerthird-picture-content').className = 'lowerthird-picture-content lowerthird-picture-content-animate-in';
119         lowerthird_visible = true;
120 }
121
122 function setandshowlowerthird()
123 {
124         document.getElementById('lowerthird-headline-content').innerHTML = state['text1'];
125         document.getElementById('lowerthird-subheading-content').innerHTML = state['text2'];
126         let img = document.getElementById('lowerthird-img');
127         if (state['image'] === undefined) {
128                 img.style.display = 'none';
129         } else {
130                 img.src = state['image'];       
131                 img.style.display = 'inline';
132         }
133         showlowerthird();
134 }
135
136 function hidelowerthird()
137 {
138         if (!lowerthird_visible) return;
139         document.getElementById('lowerthird-headline').className = 'lowerthird-headline lowerthird-headline-hidden lowerthird-headline-animate-out';
140         document.getElementById('lowerthird-headline-content').className = 'lowerthird-headline-content lowerthird-headline-content-animate-out';
141         document.getElementById('lowerthird-subheading').className = 'lowerthird-subheading lowerthird-subheading-animate-out';
142         document.getElementById('lowerthird-subheading-content').className = 'lowerthird-subheading-content lowerthird-subheading-content-animate-out';
143         document.getElementById('lowerthird-picture').className = 'lowerthird-picture lowerthird-picture-animate-out';
144         document.getElementById('lowerthird-picture-content').className = 'lowerthird-picture-content lowerthird-picture-content-animate-out';
145         lowerthird_visible = false;
146 }
147
148 function time_elapsed()
149 {
150         let elapsed = (Date.now() - clock_origin) * 1e-3;
151         if (clock_elapsed + elapsed >= clock_limit) {
152                 clock_elapsed = clock_limit;
153                 clock_origin = Date.now();
154                 clock_running = false;
155                 return clock_limit;
156         }
157         return Math.floor(clock_elapsed + elapsed);
158 }
159
160 function update_clock()
161 {
162         let elapsed = time_elapsed();
163         let min = Math.floor(elapsed / 60);
164         let sec = elapsed % 60;
165
166         if (sec < 10) sec = "0" + sec;
167         document.getElementById('clock').innerHTML = min + ":" + sec;
168 }
169
170 function goalA()
171 {
172         ++scoreA;
173         update_score();
174 }
175
176 function goalB()
177 {
178         ++scoreB;
179         update_score();
180 }
181
182 function ungoalA()
183 {
184         if (scoreA > 0) --scoreA;
185         update_score();
186 }
187
188 function ungoalB()
189 {
190         if (scoreB > 0) --scoreB;
191         update_score();
192 }
193
194 function resetscore()
195 {
196         scoreA = scoreB = 0;
197         update_score();
198 }
199
200 function update_score()
201 {
202         document.getElementById('score').innerHTML = scoreA + "&nbsp;–&nbsp;" + scoreB;
203 }
204
205 /* called by the Nageru theme only */
206 function play()
207 {
208         document.getElementById('manualcontrols').style.display = 'none';
209         document.getElementById('area').style.display = 'none';
210 }
211
212 function update(v)
213 {
214         console.log('[[[' + v + ']]]');
215         let j = JSON.parse(v);
216         for(let key in j) state[key] = j[key];
217 }
218
219 setInterval(function() {
220         if (clock_running) {
221                 update_clock();
222         }
223 }, 100);
224 update_score();
225
226 //play();
227 //startclock();
228
229 let websocket = null;
230
231 function open_ws()
232 {
233         console.log("Connecting...");
234         try {
235                 if (websocket)
236                         websocket.close();
237                 websocket = new WebSocket("ws://127.0.0.1:5250/");
238                 websocket.onopen = function(evt) {
239                         console.log("Connected to client.");
240                 };
241                 websocket.onclose = function(evt) {
242                         console.log("Disconnected from client.");
243                         setTimeout(open_ws, 100);
244                 };
245                 websocket.onmessage = function(evt) {
246                         let msg = evt.data;
247                         let m = msg.match(/^update (.*)/);
248                         if (m !== null) {
249                                 update(m[1]);
250                         }
251                         m = msg.match(/^eval (.*)/);
252                         if (m !== null) {
253                                 eval(m[1]);
254                         }
255                 };
256                 websocket.onerror = function(evt) {
257                         console.log('Error: ' + evt.data);
258                 };
259         } catch (exception) {
260                 console.log('Error: ' + exception);
261                 setTimeout(open_ws, 100);
262         }
263 };
264 open_ws();