-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
357 lines (344 loc) · 14.6 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
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
"use strict";
window.onload = function _onload() {
let chart = undefined;
let players = new Array(2); // default 2 Players
const labels = [];
const datasets = [];
const ROUNDTEXT = 'Round';
const COOKIETAG = 'players=';
const COLORS = [
'#0000ff', // blue
'#ff0000', // red
'#00ff00', // green
'#ffff00', // yellow
'#ff00ff', // cyan
'#00ffff', // magenta
'#ff9300', // orange
'#942192', // purple
'#aa7942', // brown
'#919191', // gray
];
feather.replace();
const trName = document.getElementById('player');
const trScore = document.getElementById('score');
const tbodyScores = document.getElementById('scores');
window.setPlayers = setPlayers; // will be called by html
window.onunload = function _onunload(e) {
document.cookie = storeCookie();
}
document.cookie.split(';').forEach( cookie => parseCookie(cookie) );
setPlayers();
function setPlayers(addPlayers) {
if ((addPlayers < 0) && (tbodyScores.rows.length > 1)) {
if (!confirm('You will obviously lose the score of the last player.\nDo you want to proceed?'))
return;
}
const newLen = players.length + ((addPlayers === undefined) ? 0 : addPlayers);
players.length = Math.min(COLORS.length, Math.max(1, newLen));
// destroy the chart and reset the player struture and the datasets
if (chart !== undefined) {
chart.destroy();
chart = undefined;
}
datasets.length = 0;
labels.length = 1;
for (let player = 0; player < players.length; player++) {
if ((players[player] === undefined) ||
(players[player].constructor !== Object)) {
players[player] = { };
}
if (!players[player].name) {
players[player].name = 'Player ' + (player+1);
}
if (!players[player].color) {
players[player].color = COLORS[player];
}
if ((addPlayers === 0 /* trash bin button */) ||
(players[player].score === undefined) ||
(players[player].score.constructor !== Array)) {
players[player].score = [];
}
datasets[player] = {
label: players[player].name,
data: [],
tension: 0.1,
borderColor: players[player].color,
backgroundColor: bgndColor(players[player].color),
spanGaps: true
};
}
// reset the table clear players data and score rows
trName.replaceChildren(trName.firstElementChild);
trScore.replaceChildren(trScore.firstElementChild);
tbodyScores.replaceChildren();
for (let player = 0; player < players.length; player ++) {
const input = document.createElement('INPUT');
input.type = 'text';
input.value = players[player].name;
input.player = player;
input.style.borderColor = players[player].color;
input.style.backgroundColor = bgndColor(players[player].color);
input.onkeyup = function _onKeyUp(e) {
updateScore();
}
input.onchange = function _onChangeName(e) {
const name = this.value;
players[this.player].name = name;
datasets[this.player].label = name;
chart.update();
}
// the color input selector (not visible)
const inputColor = document.createElement('INPUT');
inputColor.type = 'color';
inputColor.style.opacity = 0;
inputColor.player = player;
inputColor.onchange = function _onChangeColor(e) {
const input = this.closest('TH').querySelector('INPUT[type="text"]');
const color = this.value;
players[input.player].color = color;
datasets[input.player].borderColor = color;
input.style.borderColor = color;
const background = bgndColor(color);
datasets[input.player].backgroundColor = background;
input.style.backgroundColor = background;
chart.update();
};
// color right icon
const iconColor = document.createElement('SPAN');
iconColor.className = 'iconright';
iconColor.title = 'Change this player\'s color';
iconColor.textContent = '🎨'; // palette emoji
iconColor.onclick = function _onClickColor(e) {
const input = this.closest('TH').querySelector('INPUT[type="color"]');
input.value = players[input.player].color;
input.focus();
};
// <th><input ...>name</input><span ...>icon</span><span ...>icon<input>color</input></span><th>
const thName = document.createElement('TH');
thName.appendChild(input);
thName.appendChild(createEditIcon('Edit this player\'s name.'));
iconColor.appendChild(inputColor);
thName.appendChild(iconColor);
trName.appendChild(thName);
// the score <th>score<span ...>icon></span></th>
const thScore = document.createElement('TH');
thScore.textContent = '0'; // zero score
const iconRank = document.createElement('SPAN');
iconRank.className = 'iconrank';
iconRank.textContent = '';
thScore.appendChild(iconRank);
// add to row
trScore.appendChild(thScore);
}
updateScore();
};
function updateScore() {
let numRows = 1; // at least space for one complete row
for (let player = 0; player < players.length; player++) {
const len = players[player].score.length;
if (len > 0) {
const val = parseInt(players[player].score[len - 1]);
numRows = Math.max(numRows, len + (isNaN(val) ? 0 : 1));
}
}
// calc the cumulative sums and update the chart (datasets + label)
const sum = [];
const numRounds = numRows - 1;
for (let player = 0; player < players.length; player++) {
sum[player] = 0;
for (let round = 0; round < numRounds; round++) {
labels[round] = 1 + round;
const val = parseInt(players[player].score[round]);
if (!isNaN(val)) {
sum[player] += val;
datasets[player].data[round] = sum[player];
} else {
datasets[player].data[round] = NaN;
}
}
}
// update the score
const sortedSum = [...sum].sort((a, b) => b - a);
const ranks = sum.map(s => sortedSum.indexOf(s));
const MEDALS = [ '🥇', '🥈', '🥉' ]; // medal emojis
const RANKS = [ 'first', 'second', 'third', 'fourth', 'fifth',
'sixth', 'seventh', 'eighth', 'ninth', 'tenth' ];
const last = Math.max(...ranks);
for (let player = 0; player < players.length; player++) {
const rank = ranks[player];
const medal = (MEDALS[rank] !== undefined) ? MEDALS[rank] :
(rank == last) ? '🤞' : '';
trScore.cells[1 + player].firstChild.textContent = sum[player];
const icon = trScore.cells[1 + player].querySelector('SPAN');
icon.textContent = medal;
const rankText = (rank == last) ? 'last' : RANKS[rank];
icon.title = rankText ? 'Player is currently in the ' + rankText + ' rank.': '';
}
// create the chart if not yet done
if (chart === undefined) {
const ctx = document.getElementById('chart').getContext('2d');
const config = {
type: 'line',
data: {
labels: labels,
datasets: datasets,
},
options: {
interaction: {
intersect: false,
mode: 'index',
},
scales: {
x: { title: { text: ROUNDTEXT, display: true } },
y: { beginAtZero: true, title: { text: 'Score', display: true } }
},
maintainAspectRatio: false,
plugins: {
legend: {
display: false,
},
tooltip: {
callbacks: {
title: function(context) {
return ROUNDTEXT + ' ' + context[0].label;
},
},
itemSort: function(item0, item1) {
var y0 = item0.raw;
var y1 = item1.raw;
return (y0 < y1) ? +1 : (y0 > y1) ? -1 : 0;
}
}
}
}
};
chart = new Chart(ctx, config);
function _onMouseMove(e) {
const points = chart.getElementsAtEventForMode(e, 'index', {intersect: false}, true);
const trs = tbodyScores.querySelectorAll('tr');
trs.forEach((tr) => { tr.removeAttribute('highlight'); })
if (points[0] && (trs.length > 0)) {
const index = trs.length - 1 - points[0].index;
if (index > 0 && index < trs.length)
trs[index].setAttribute('highlight','');
}
}
chart.canvas.onmousemove = _onMouseMove;
chart.canvas.onmouseleave = _onMouseMove;
}
chart.update();
// add as many rows as needed
for (let row = tbodyScores.rows.length; row < numRows; row++) {
const tr = tbodyScores.insertRow(0);
const td = tr.insertCell();
td.textContent = ROUNDTEXT + ' ' + (row+1);
for (let player = 0; player < players.length; player++) {
// we build <td><input ... >score</input><span ... >icon</span></td>
const input = document.createElement('INPUT');
input.type = 'text';
input.inputmode = 'numeric';
input.pattern = '[0-9]*';
input.player = player;
input.round = row;
input.value = parseValue(players[player].score[row]);
input.discardValue = function _discardValue() {
const val = parseValue(this.oldValue);
this.value = val;
players[this.player].score[this.round] = val;
}
input.acceptValue = function _acceptValue() {
players[this.player].score[this.round] = parseValue(this.value);
}
input.onfocus = function _onfocus(e) {
this.oldValue = parseValue(this.value);
}
input.onblur = function _onblur(e) {
if (!this.checkValidity()) {
this.discardValue()
updateScore();
} else {
this.acceptValue();
updateScore();
}
}
input.onkeyup = function _onkeyup(e) {
if (e.key === 'Enter') {
e.preventDefault();
// advance the cell or row
const nextPlayer = (this.player + 1) % players.length;
const tr = this.closest('TR');
let nextInput = tr.cells[1 + nextPlayer].querySelector(this.tagName);
if ((nextInput.value != '') && (tr === tbodyScores.rows[1])) {
nextInput = tbodyScores.rows[0].cells[1 + nextPlayer].querySelector(this.tagName);
}
if(nextInput.value == '') {
nextInput.focus();
}
this.blur();
} else if (e.key === 'Escape') {
e.preventDefault();
this.discardValue();
this.blur();
} else {
this.acceptValue();
updateScore();
}
}
const td = tr.insertCell();
td.appendChild(input);
td.appendChild(createEditIcon("Edit this score."));
}
}
}
function createEditIcon(hint) {
const icon = document.createElement('SPAN');
icon.className = 'iconleft';
icon.textContent = '✏️'; // pen emoji
icon.onclick = function _onClick(e) {
const input = this.closest('TD, TH').querySelector('INPUT[type="text"]');
input.focus();
input.setSelectionRange(0, input.value.length);
};
if (hint !== undefined) {
icon.title = hint;
}
return icon;
}
function parseValue(val) {
val = parseInt(val);
return isNaN(val) ? '' : val;
}
function parseCookie(cookie) {
try {
if (cookie.startsWith(COOKIETAG)) {
players = JSON.parse(cookie.substring(COOKIETAG.length));
}
} catch (e) {
}
}
function storeCookie(days = 365) {
let expires = '';
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
expires = '; expires=' + date.toGMTString();
}
const value = JSON.stringify(players);
return COOKIETAG + value + expires + '; path=/';
}
function bgndColor(color) {
const r = parseInt(color.slice(1, 3), 16);
const g = parseInt(color.slice(3, 5), 16);
const b = parseInt(color.slice(5, 7), 16);
return 'rgba(' + r + ',' + g + ',' + b + ',0.5)';
}
function textColor(color) {
const r = parseInt(color.slice(1, 3), 16);
const g = parseInt(color.slice(3, 5), 16);
const b = parseInt(color.slice(5, 7), 16);
const max = Math.max(r, g, b);
const min = Math.min(r, g, b);
const lightness = (min + max) / 2;
return (lightness >= 127) ? '#000000' : '#ffffff';
}
};