-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
480 lines (426 loc) · 14.5 KB
/
Copy pathindex.html
File metadata and controls
480 lines (426 loc) · 14.5 KB
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
<html>
<head>
<title>Fifa Turnier 2018</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
*,
*:before,
*:after {
margin: 0;
padding: 0;
}
section {
display: none;
}
input[type="radio"] {
display: none;
}
#tab1:checked~#content1,
#tab2:checked~#content2,
#tab3:checked~#content3 {
display: block;
}
label {
display: inline-block;
text-align: center;
color: #222;
background-color: #999;
width: 32%;
padding-top: 20px;
padding-bottom: 20px;
font-size: 2rem;
margin: 0px !important;
border-top-width: 5px;
border-top-style: solid;
border-top-color: transparent;
}
label:hover {
background-color: #BBB;
cursor: pointer;
}
input:checked+label {
background-color: #FFF;
border-top-color: #555;
}
.topbar {
width: 100%;
}
section {
margin-top: 20px;
}
.tab_content {
font-size: 1.3rem;
text-align: center;
}
.player_name_input {
margin-bottom: 5px;
}
.player_input {
font-size: 1rem;
}
.new_turn_button {
margin-top: 20px;
padding: 15px;
font-size: 2rem;
}
.load_turn_button {
margin-bottom: 20px;
padding: 15px;
font-size: 2rem;
}
.left_half {
width: 49%;
text-align: right;
}
.right_half {
width: 30%;
text-align: left;
}
input[type="number"] {
width: 3rem;
}
table {
margin: auto;
font-size: 1.3rem;
}
.save-button {
padding: 5px;
margin-left: 10px;
}
.winner {
background-color: #0F0;
}
.loser {
background-color: #F00;
}
.draw {
background-color: #FF0;
}
#ligatabelle tr td {
padding: 5px;
font-size: 2rem;
}
</style>
</head>
<body>
<input id="tab1" type="radio" name="tabs" checked>
<label for="tab1">Turnier</label>
<input id="tab2" type="radio" name="tabs">
<label for="tab2">Spiele</label>
<input id="tab3" type="radio" name="tabs">
<label for="tab3">Tabelle</label>
<section id="content1">
<div class="tab_content">
<button class="load_turn_button" onclick="load_turn()">Turnier Laden</button>
<br>
<div class="player_name_input">
Spieler 01:
<input type="text" id="player_name_1" class="player_input" />
</div>
<div class="player_name_input">
Spieler 02:
<input type="text" id="player_name_2" class="player_input" />
</div>
<div class="player_name_input">Spieler 03:
<input type="text" id="player_name_3" class="player_input" />
</div>
<div class="player_name_input">Spieler 04:
<input type="text" id="player_name_4" class="player_input" />
</div>
<div class="player_name_input">Spieler 05:
<input type="text" id="player_name_5" class="player_input" />
</div>
<div class="player_name_input">Spieler 06:
<input type="text" id="player_name_6" class="player_input" />
</div>
<div class="player_name_input">Spieler 07:
<input type="text" id="player_name_7" class="player_input" />
</div>
<div class="player_name_input">Spieler 08:
<input type="text" id="player_name_8" class="player_input" />
</div>
<div class="player_name_input">Spieler 09:
<input type="text" id="player_name_9" class="player_input" />
</div>
<div class="player_name_input">Spieler 10:
<input type="text" id="player_name_10" class="player_input" />
</div>
<button class="new_turn_button" onclick="new_turn()">Neues Turnier</button>
</div>
</section>
<section id="content2">
<div class="tab_content">
<table id="gamestable">
<tr>
<td>Kein Turnier geladen!</td>
</tr>
</table>
</div>
</section>
<section id="content3">
<div class="tab_content">
<table id="ligatabelle">
<tr>
<td>Kein Turnier geladen!</td>
</tr>
</table>
</div>
</section>
<script>
var db = openDatabase('fifx', '1', 'Fifa Turnier DB', 2 * 1024 * 1024);
var resetted = false;
var pairings = [
[1, 2],
[3, 4],
[5, 6],
[7, 8],
[9, 10],
//----
[1, 3],
[2, 5],
[4, 6],
[7, 9],
[8, 10],
//----
[1, 4],
[2, 3],
[5, 7],
[6, 10],
[8, 9],
//----
[3, 5],
[4, 9],
[2, 10],
[1, 8],
[6, 7],
//----
[4, 10],
[5, 9],
[2, 8],
[1, 7],
[3, 8],
//----
[2, 9],
[4, 7],
[5, 10],
[3, 6],
[1, 9],
//----
[4, 5],
[1, 10],
[6, 8],
[3, 9],
[4, 8],
//----
[2, 7],
[5, 8],
[6, 9],
[3, 10],
[1, 5],
//----
[3, 7],
[2, 6],
[7, 10],
[2, 4],
[1, 6]
]
function validNumber(n) {
if (n === 0) {
return true;
}
if (typeof (n) !== 'number') {
return false;
}
if (isNaN(n) || n === "" || n === " " || !(n > -1)) {
return false;
} return true;
}
function shuffle(array) {
var currentIndex = array.length, temporaryValue, randomIndex;
while (0 !== currentIndex) {
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
function setup(cb) {
db.transaction(function (tx) {
tx.executeSql('CREATE TABLE IF NOT EXISTS FIFX (id unique, p1_id, p2_id, p1_name, p2_name, p1_score, p2_score, p1_points, p2_points)');
console.log("TABLE CREATED!");
cb();
});
}
function reset() {
setup(function () {
db.transaction(function (tx) {
tx.executeSql('DELETE FROM FIFX');
console.log("DELETED ALL!");
let id = 1;
for (let pairing of pairings) {
tx.executeSql('INSERT INTO FIFX (id, p1_id, p2_id) VALUES (?,?,?)', [id, pairing[0], pairing[1]]);
id++;
}
console.log("CREATED PAIRINGS!");
resetted = true;
});
})
}
function new_turn() {
if (!resetted) {
alert("Reset before creating a new Tournament!")
return;
}
resetted=false;
var player_names = [];
for (var i = 1; i < 11; i++) {
let name = document.getElementById("player_name_" + i).value;
if (!name) {
name = " ";
}
player_names.push(name);
}
player_names = shuffle(player_names);
console.table(player_names);
db.transaction(function (tx) {
for (let i = 0; i < player_names.length; i++) {
tx.executeSql('UPDATE FIFX SET p1_name=? where p1_id=?', [player_names[i], i + 1]);
tx.executeSql('UPDATE FIFX SET p2_name=? where p2_id=?', [player_names[i], i + 1]);
}
console.log("PLAYERS INSERTED!");
load_turn();
});
}
function create_game_table_row(id, p1, p2, score1, score2) {
let scorestring1 = "";
let scorestring2 = "";
if (validNumber(parseInt(score1)) && validNumber(parseInt(score2))) {
scorestring1 = `value="${score1}"`;
scorestring2 = `value="${score2}"`;
}
return `
<tr id="game${id}">
<td id="game${id}_p1">
${p1}
<td>
<td>
<input type="number" maxlength="2" id="game${id}_p1_score" class="player_input" ${scorestring1} />
</td>
<td>
:
</td>
<td>
<input type="number" maxlength="2" id="game${id}_p2_score" class="player_input" ${scorestring2}/>
</td>
<td id="game${id}_p2">
${p2}
</td>
<td>
<button class="save-button" onclick="save_game(${id})">Ok</button>
</td>
</tr>
`;
}
function create_league_table_row(place, name, points) {
points = parseInt(points);
if (isNaN(points)) {
points = 0;
}
return `
<tr>
<td>${place}.</td>
<td>${name}</td>
<td>(${points})</td>
</tr>
`
}
function load_turn() {
db.transaction(function (tx) {
tx.executeSql('SELECT * FROM FIFX WHERE id>0 AND p1_name <> " " AND p2_name <> " "', [], function (t, r) {
let tablestring = "";
for (let row of r.rows) {
tablestring = tablestring + create_game_table_row(row.id, row.p1_name, row.p2_name, row.p1_score, row.p2_score);
}
document.getElementById("gamestable").innerHTML = tablestring;
for (let row of r.rows) {
refresh_game(row.id);
}
refresh_table();
});
});
}
function save_game(id) {
let p1_name = document.getElementById("game" + id + "_p1").innerText;
let p2_name = document.getElementById("game" + id + "_p2").innerText;
let p1_score = parseInt(document.getElementById("game" + id + "_p1_score").value);
let p2_score = parseInt(document.getElementById("game" + id + "_p2_score").value);
if (!validNumber(p1_score) || !validNumber(p2_score)) {
alert("Ivalid Scores!");
return;
}
let p1_points = 0;
let p2_points = 0;
if (p1_score == p2_score) {
p1_points = 1;
p2_points = 1;
} else if (p1_score > p2_score) {
p1_points = 3;
} else if (p2_score > p1_score) {
p2_points = 3;
}
db.transaction(function (tx) {
tx.executeSql('UPDATE FIFX SET p1_score=?, p2_score=?, p1_points=?, p2_points=? where id=?', [p1_score, p2_score, p1_points, p2_points, id], function () {
//cb
refresh_game(id);
refresh_table();
});
});
}
function refresh_game(id) {
db.transaction(function (tx) {
tx.executeSql('SELECT * FROM FIFX where id=?', [id], function (t, r) {
let id = r.rows[0].id;
let p1 = r.rows[0].p1_points;
let p2 = r.rows[0].p2_points;
if (!validNumber(p1) || !validNumber(p2)) {
return;
}
if (p1 > p2) {
document.getElementById("game" + id + "_p1").className = "winner";
document.getElementById("game" + id + "_p2").className = "loser";
} else if (p2 > p1) {
document.getElementById("game" + id + "_p1").className = "loser";
document.getElementById("game" + id + "_p2").className = "winner";
} else if (p1 == p2) {
document.getElementById("game" + id + "_p1").className = "draw";
document.getElementById("game" + id + "_p2").className = "draw";
}
});
});
}
function refresh_table() {
db.transaction(function (tx) {
tx.executeSql(`SELECT name, SUM(points) AS total FROM
(
SELECT p1_name AS name, SUM(p1_points) AS points FROM FIFX WHERE p1_name <> " " GROUP BY p1_name
UNION ALL
SELECT p2_name AS name, SUM(p2_points) AS points FROM FIFX WHERE p2_name <> " " GROUP BY p2_name
) GROUP BY name ORDER BY total DESC`, [], function (t, r) {
let ligatabelleString = "";
let place = 1;
for (let row of r.rows) {
ligatabelleString = ligatabelleString + create_league_table_row(place, row.name, row.total);
place++;
}
document.getElementById("ligatabelle").innerHTML = ligatabelleString;
});
});
}
/*
SELECT name, SUM(points) AS total FROM (SELECT p1_name AS name, SUM(p1_points) AS points FROM FIFX GROUP BY p1_name UNION ALL SELECT p2_name AS name, SUM(p2_points) AS points FROM FIFX GROUP BY p2_name) GROUP BY name ORDER BY total DESC
*/
</script>
</body>
</html>