-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
275 lines (222 loc) · 8.36 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
let is_counting_down = false;
let is_taking_pictures = false;
// Setup events
events.sort(function(a, b){
return a.start - b.start;
});
window.onload = function() {
build_sidebar();
startSponsorCycle();
const now = Date.now();
for(let event of events){
let time_to_event = event.start - now;
if(time_to_event > 0){
setTimeout(function(){runEvent(event);}, time_to_event);
}
if(event.is_now()){
runEvent(event);
}
else if(event.countdown){
if(time_to_event > 0 && time_to_event < count_down_time){
startEventCountDown(event);
}
else if(event.countdown){
setTimeout(function(){startEventCountDown(event);}, time_to_event - count_down_time);
}
}
}
}
let sponsor_cycle_handle;
function startSponsorCycle(){
update_sponsor_logo();
sponsor_cycle_handle = setInterval(update_sponsor_logo, sponsor_cycle_time);
}
document.addEventListener('keydown', handleKeyboardInput);
function handleKeyboardInput(event){
let index = event.keyCode - 48; // char '0' == int 48
if(index >= events.length || index < 0){
return;
}
let flan_event = events.find(function(element){
return element.id == index && element.sponsor != undefined;
});
if(!flan_event) return;
showSponsorCycle();
clearInterval(sponsor_cycle_handle);
set_sponsor_logo(flan_event.sponsor.logo, "#000");
is_taking_pictures = true;
console.log(flan_event);
document.addEventListener('keydown', handleGoBackEvent);
document.removeEventListener('keydown', handleKeyboardInput);
};
function handleGoBackEvent(event){
if(event.keyCode != 27) return; // 27 == ESC
startSponsorCycle();
is_taking_pictures = false;
if(is_counting_down) showCountDownScreen();
document.addEventListener('keydown', handleKeyboardInput);
document.removeEventListener('keydown', handleGoBackEvent);
};
function runEvent(event){
console.log("Event " + event.id + " Started.");
const element = document.getElementById("event" + event.id);
if(!element) {
console.log("Failed to find event " + event.id);
return;
}
element.classList.add("active_event");
setTimeout(function(){
element.classList.remove("active_event");
console.log("Event " + event.id + " Stopped.");
}, event.end - Date.now());
}
function showSponsorCycle(){
let sponsors = document.getElementById("sponsor_logo");
let tournement = document.getElementById("tournement_start");
sponsors.style.display = "block";
tournement.style.display = "none";
}
function showCountDownScreen(){
if(is_taking_pictures) return;
let sponsors = document.getElementById("sponsor_logo");
let tournement = document.getElementById("tournement_start");
sponsors.style.display = "none";
tournement.style.display = "block";
}
function show_responsible(responsible){
let responsible_table = document.getElementById("responsible_table");
responsible_table.innerHTML = "";
let responsible_image_row = document.createElement("tr");
let responsible_name_row = document.createElement("tr");
for(let member of responsible){
let image = document.createElement("td");
image.style.overflow = "hidden";
let name = document.createElement("td");
name.align = "center";
image.innerHTML = '<img src="' + member.image + '" class="portrait"/>';
name.innerHTML = member.name;
responsible_image_row.appendChild(image);
responsible_name_row.appendChild(name);
}
responsible_table.appendChild(responsible_image_row);
responsible_table.appendChild(responsible_name_row);
}
function startEventCountDown(event){
console.log("Starting Event Countdown");
show_responsible(event.responsible);
let message = document.getElementById("tournement_message");
if(event.message == undefined)
message.innerHTML = "";
else
message.innerHTML = event.message;
let name = document.getElementById("tournement_name");
name.innerHTML = event.title;
let sponsor_name = document.getElementById("tournement_sponsor_name");
sponsor_name.innerHTML = event.sponsor.name;
let sponsor_logo = document.getElementById("tournement_sponsor_logo");
sponsor_logo.src = event.sponsor.logo;
let event_logo = document.getElementById("game_logo");
event_logo.src = event.image;
showCountDownScreen();
const t = new Timer(event.start - Date.now(),
function(){ // Timer finished callback
showSponsorCycle();
is_counting_down = false;
}
);
t.start();
is_counting_down = true;
}
const sponsor_iterator = infinite_sponsor_iterator();
function* infinite_sponsor_iterator(){
while(true){
for(const sponsor in sponsors){
yield sponsors[sponsor];
}
}
}
function update_sponsor_logo(){
const sponsor_logo = document.getElementById("sponsor_logo");
const sponsor = sponsor_iterator.next().value;
set_sponsor_logo(sponsor.logo);
}
function set_sponsor_logo(uri, background_color = "#FFF"){
sponsor_logo.style.backgroundImage = "url(" + uri + ")";
sponsor_logo.style.backgroundColor = background_color;
//sponsor_logo.src = uri;
}
function build_sidebar() {
clear_plan();
const header = document.createElement("h2");
header.innerHTML = getWeekday(new Date(), "dk");
document.getElementById("plan").appendChild(header);
events.filter(function(event){
return event.is_today();
}).forEach(add_to_plan);
}
function getWeekday(date, locale = "en") {
const days = {
dk:["Søndag","Mandag","Tirsdag","Onsdag","Torsdag","Fredag","Lørdag"],
en:["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]
};
return days[locale][date.getDay()];
}
function clear_plan() {
document.getElementById("plan").innerHTML = "";
}
const hour_minutes_format = {hour: '2-digit', minute:'2-digit'};
function add_to_plan(event){
const row_time = document.createElement("td");
row_time.innerHTML = event.start.toLocaleTimeString([], hour_minutes_format);
const row_title = document.createElement("td");
row_title.innerHTML = event.title;
const table_row = document.createElement("tr");
table_row.id = "event" + event.id;
table_row.appendChild(row_time);
table_row.appendChild(row_title);
document.getElementById("plan").appendChild(table_row);
}
function polarToCartesian(centerX, centerY, radius, angleInDegrees) {
var angleInRadians = (angleInDegrees-90) * Math.PI / 180.0;
return {
x: centerX + (radius * Math.cos(angleInRadians)),
y: centerY + (radius * Math.sin(angleInRadians))
};
}
function describeArc(x, y, radius, startAngle, endAngle){
var start = polarToCartesian(x, y, radius, endAngle);
var end = polarToCartesian(x, y, radius, startAngle);
var largeArcFlag = endAngle - startAngle <= 180 ? "0" : "1";
var d = [
"M", start.x, start.y,
"A", radius, radius, 0, largeArcFlag, 0, end.x, end.y
].join(" ");
return d;
}
function Timer(duration, callback = undefined){
console.log("Timer::constructor called with parameter duration: " + duration);
const fill = document.getElementById("timer_fill");
const text = document.getElementById("timer_text");
let end_time = undefined;
const drawClock = function() {
//console.log("Timer::drawClock called");
const time_left = new Date(end_time - Date.now());
fill.setAttribute("d", describeArc(150, 150, 67, 0, time_left/count_down_time*360));
text.innerHTML = time_left.toLocaleTimeString([],{minute: "2-digit", second: "2-digit"});
if(time_left.getMinutes() + time_left.getSeconds() == 0){
console.log("Timer::completed called");
clearInterval(this.interval_id);
if(callback) callback();
}
}
this.start = function(){
console.log("Timer::start called");
end_time = Date.now() + duration;
this.interval_id = setInterval(drawClock, 500);
drawClock();
}
this.stop = function(){
console.log("Timer::stop called");
clearInterval(this.interval_id);
}
}