-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
451 lines (429 loc) · 13 KB
/
index.html
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
<!DOCTYPE html>
<html>
<head>
<title>Tulostaulu</title>
<style>
@import url("https://fonts.googleapis.com/css2?family=Chakra+Petch:ital,wght@0,300;0,400;0,700;1,300;1,400;1,700&display=swap");
html {
font-family: sans-serif;
font-weight: bold;
-ms-text-size-adjust: 100%;
-webkit-text-size-adjust: 100%;
}
:root {
--statbox-bg: #ffc107;
}
body {
font-family: "Chakra Petch", Arial, Helvetica, sans-serif;
color: lightgoldenrodyellow;
background-color: black;
}
#otteluvalinta {
display: none;
}
input,
select {
display: block;
margin: 10px;
}
#tulostaulu {
display: none;
width: 100%;
height: 10vw;
grid-template-columns: 2fr 4fr 2fr 4fr 2fr;
grid-template-rows: 2fr 3fr 2fr;
}
#tulostaulu > div {
display: flex;
align-items: center;
justify-content: center;
z-index: 2;
}
#teamAlogoWrap {
grid-row: 1 / span 3;
grid-column: 1;
}
#teamBlogoWrap {
grid-row: 1 / span 3;
grid-column: 5;
}
#teamAlogo,
#teamBlogo {
width: 100%;
height: 100%;
object-fit: contain;
}
#teamA {
grid-row: 2;
grid-column: 2;
}
#teamB {
grid-row: 2;
grid-column: 4;
}
#teamA,
#teamB {
font-size: 3vw;
}
#score {
grid-row: 2;
grid-column: 3;
font-size: 3vw;
}
#liveTime {
grid-row: 3;
grid-column: 3;
font-size: 2vw;
background-color: var(--statbox-bg);
color: darkslategray;
border-radius: 0 0 2vw 2vw;
}
@keyframes colorPulse {
0% {
color: darkslategray; /* replace with your first color */
}
50% {
color: red; /* replace with your second color */
}
100% {
color: darkslategray; /* replace with your first color */
}
}
#liveTime.liveTimerOn {
color: darkslategray;
border-radius: 0 0 2vw 2vw;
animation: colorPulse 1s infinite;
}
#periodWrap {
grid-row: 1;
grid-column: 3;
font-size: 2vw;
background-color: var(--statbox-bg);
color: darkslategray;
border-radius: 2vw 2vw 0 0;
}
#tulostaulu > #timestamp {
font-size: small;
display: none;
}
#tulostaulu > #liveTimerOn {
font-size: small;
display: none;
}
#tulostaulu > #bgBar {
grid-row: 2;
grid-column: 1 / span 5;
background-color: red;
opacity: 0.75;
z-index: 1;
}
</style>
</head>
<body>
<div id="otteluvalinta">
<h1>Ottelun valinta</h1>
<input
type="text"
id="api_key"
value=""
placeholder="Torneopal API key"
onchange="apiKeyChanged()"
/>
<input
type="text"
id="match_url"
value=""
placeholder="Pelin URL tulospalvelusta"
onchange="matchUrlChanged()"
/>
<select id="season_id" onchange="populateCompetitions()">
<option value="valitse" default>Valitse kausi</option>
</select>
<select id="competition_id" onchange="populateCategories()">
<option value="valitse" default>Valitse alue</option>
</select>
<select id="category_id" onchange="populateTeams()">
<option value="valitse" default>Valitse sarja</option>
</select>
<select id="team_id" onchange="populateMatches()">
<option value="valitse" default>Valitse joukkue</option>
</select>
<select id="match_id" onchange="matchChanged()">
<option value="valitse">Valitse ottelu</option>
</select>
</div>
<div id="tulostaulu">
<div id="teamAlogoWrap">
<img id="teamAlogo" src="" />
</div>
<div id="teamBlogoWrap">
<img id="teamBlogo" src="" />
</div>
<div id="teamA"></div>
<div id="teamB"></div>
<div id="score">
<span id="scoreA"></span>
-
<span id="scoreB"></span>
</div>
<div id="periodWrap"><span id="period"></span></div>
<div id="timestamp"></div>
<div id="liveTime"></div>
<div id="liveTimerOn"></div>
<div id="bgBar"></div>
</div>
<script>
const SCORE_POLL_INTERVAL = 5000;
let poll = null;
document.addEventListener("DOMContentLoaded", async function () {
const context = await populateContextFromUrl();
if (context.match_id) {
setMatch(context.match_id);
} else if (context.api_key) {
apiKeyChanged();
} else {
showMatchSelection();
}
});
async function apiKeyChanged() {
const { api_key } = getContext("api_key");
setUrlParams("api_key", api_key);
await populateSeasons();
await populateCompetitions();
await populateCategories();
await populateTeams();
await populateMatches();
showMatchSelection();
}
function matchUrlChanged() {
const url = document.getElementById("match_url").value;
const matchId = url.match(/match\/(\d+)\//);
if (matchId) {
setMatch(matchId[1]);
} else {
console.error("Invalid match URL", url);
}
}
function showMatchSelection(show = true) {
document.getElementById("tulostaulu").style.display = show
? "none"
: "grid";
document.getElementById("otteluvalinta").style.display = show
? "block"
: "none";
}
function populateContextFromUrl() {
const urlParams = new URLSearchParams(window.location.search);
const params = {};
for (const [key, value] of urlParams) {
params[key] = value;
setContext(key, value);
}
return params;
}
function matchChanged() {
const { match_id } = getContext("match_id");
setMatch(match_id);
}
function setMatch(match_id) {
setUrlParams("match_id", match_id);
initScoreboard(match_id);
}
function setUrlParams(key, value) {
// update page url query params to include the new match_id
const urlParams = new URLSearchParams(window.location.search);
urlParams.set(key, value);
history.pushState({}, "", `${location.pathname}?${urlParams}`);
}
function setContext(key, value) {
const element = document.getElementById(key);
if (element) {
element.value = value;
} else {
console.error("Element not found", key);
}
}
function getContext(...paramNames) {
const context = {};
paramNames.forEach((paramName) => {
const value = document.getElementById(paramName).value;
if (value && value !== "valitse") {
context[paramName] = value;
}
});
return context;
}
async function populateSeasons() {
const data = await getTorneopal("Seasons");
populateSelect(
"season_id",
data.seasons.map((season) => ({
id: season.season_id,
name: season.season_description,
default: season.season_description === "2023-2024",
}))
);
}
async function populateCompetitions() {
const data = await getTorneopal(
"Competitions",
getContext("season_id")
);
populateSelect(
"competition_id",
data.competitions.map((competition) => ({
id: competition.competition_id,
name: competition.competition_name,
default:
competition.competition_name ===
"Valtakunnalliset sarjat 2023-24",
}))
);
}
function populateCategories() {
return getTorneopal(
"Categories",
getContext("season_id", "competition_id")
).then((data) => {
console.log("Categories", data);
populateSelect(
"category_id",
data.categories.map((category) => ({
id: category.category_id,
name: category.category_name,
default: category.category_name === "Miesten Divari",
}))
);
});
}
function populateTeams() {
return getTorneopal(
"Teams",
getContext("competition_id", "category_id")
).then((data) => {
console.log("Teams", data);
populateSelect(
"team_id",
data.teams.map((team) => ({
id: team.team_id,
name: team.team_name,
default: team.team_name === "O2-Jyväskylä",
}))
);
});
}
function populateMatches() {
getTorneopal(
"Matches",
getContext("season_id", "competition_id", "category_id", "team_id")
).then((data) => {
console.log("Matches", data);
populateSelect("match_id", [
{
id: "valitse",
name: "Valitse ottelu",
default: true,
},
...data.matches.map((match) => ({
id: match.match_id,
name: `${match.date} ${match.team_A_abbrevation} - ${match.team_B_abbrevation}`,
})),
]);
});
}
function populateSelect(selectId, options) {
console.log("populateSelect", selectId, options);
select = document.getElementById(selectId);
select.innerHTML = "";
options.forEach((option) => {
const optionElement = document.createElement("option");
optionElement.value = option.id;
optionElement.selected = option.default;
optionElement.textContent = option.name;
select.appendChild(optionElement);
});
}
function initScoreboard(matchId) {
if (matchId !== "valitse") {
getTorneopal("Match", { match_id: matchId })
.then((data) => {
console.log("Match", data);
const match = data.match;
const teamA = match.team_A_abbrevation || match.team_A_name;
const teamB = match.team_B_abbrevation || match.team_B_name;
const scoreA = match.fs_A;
const scoreB = match.fs_B;
updateMatchInfo({
teamA,
teamAlogo: match.club_A_crest,
teamB,
teamBlogo: match.club_B_crest,
scoreA,
scoreB,
timestamp: match.timestamp,
period: match.live_period,
status: match.status,
liveTime: match.live_time_mmss,
liveTimerOn: match.live_timer_on === "1",
});
if (poll) {
clearInterval(poll);
}
poll = setInterval(() => {
getTorneopal("Score", { match_id: matchId }).then((data) => {
updateMatchInfo({
scoreA: data.score.fs_A || data.score.live_A,
scoreB: data.score.fs_B || data.score.live_B,
timestamp: data.score.timestamp,
period: data.score.live_period,
status: data.score.status,
liveTime: data.score.live_time_mmss,
liveTimerOn: data.score.live_timer_on === "1",
});
});
}, SCORE_POLL_INTERVAL);
})
.then(() => {
showMatchSelection(false);
});
} else {
console.error("Match not provided");
}
}
function updateMatchInfo(info) {
if (info.status === "Fixture") {
// Not started yet
info.period = "";
info.liveTime = "";
} else if (info.status === "Played") {
info.period = "lopputulos";
} else {
info.period = `${info.period}. erä`;
}
delete info.status;
for (const key in info) {
if (Object.hasOwnProperty.call(info, key)) {
const value = info[key];
if (key.endsWith("logo")) {
document.getElementById(key).src = value;
} else if (key === "liveTimerOn") {
document.getElementById("liveTime").className = value
? "liveTimerOn"
: "";
} else {
document.getElementById(key).textContent = value;
}
}
}
}
async function getTorneopal(resource, params) {
params = { ...params, ...getContext("api_key") };
const urlParams = new URLSearchParams(params);
const url = `https://salibandy.api.torneopal.com/taso/rest/get${resource}?${urlParams}`;
return fetch(url)
.then((response) => response.json())
.catch((error) => console.error(error));
}
</script>
</body>
</html>