]> git.sesse.net Git - pkanalytics/blob - ultimate.js
54348cdbace06e06524a88dd77c445463490e9b7
[pkanalytics] / ultimate.js
1 'use strict';
2
3 // No frameworks, no compilers, no npm, just JavaScript. :-)
4
5 let global_json;
6
7 addEventListener('hashchange', () => { process_matches(global_json); });
8 fetch('ultimate.json')
9    .then(response => response.json())
10    .then(response => { global_json = response; process_matches(global_json); });
11
12 function attribute_player_time(player, to, from) {
13         if (player.on_field_since > from) {
14                 // Player came in while play happened (without a stoppage!?).
15                 player.playing_time_ms += to - player.on_field_since;
16         } else {
17                 player.playing_time_ms += to - from;
18         }
19 }
20
21 function take_off_field(player, t, live_since) {
22         if (live_since === null) {
23                 // Play isn't live, so nothing to do.
24         } else {
25                 attribute_player_time(player, t, live_since);
26         }
27         if (player.on_field_since !== null) {  // Just a safeguard; out without in should never happen.
28                 player.field_time_ms += t - player.on_field_since;
29         }
30         player.on_field_since = null;
31 }
32
33 function add_cell(tr, element_type, text) {
34         let element = document.createElement(element_type);
35         element.textContent = text;
36         if (element_type === 'th') {
37                 element.setAttribute('colspan', '3');
38         }
39         tr.appendChild(element);
40         return element;
41 }
42
43 function add_3cell(tr, text, cls) {
44         let p1 = add_cell(tr, 'td', '');
45         let element = add_cell(tr, 'td', text);
46         let p2 = add_cell(tr, 'td', '');
47
48         p1.classList.add('pad');
49         p2.classList.add('pad');
50         if (cls === undefined) {
51                 element.classList.add('num');
52         } else {
53                 element.classList.add(cls);
54         }
55         return element;
56 }
57
58 function add_3cell_ci(tr, ci) {
59         if (isNaN(ci.val)) {
60                 add_3cell(tr, 'N/A');
61                 return;  // FIXME: some SVG padding needed
62         }
63
64         let text;
65         if (ci.format === 'percentage') {
66                 text = (100 * ci.val).toFixed(0) + '%';
67         } else {
68                 text = ci.val.toFixed(2);
69         }
70         let element = add_3cell(tr, text);
71         let to_x = (val) => { return width * (val - ci.min) / (ci.max - ci.min); };
72
73         // Container.
74         const width = 100;
75         const height = 20;
76         let svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
77         svg.classList.add('ci');
78         svg.setAttribute('width', width);
79         svg.setAttribute('height', height);
80
81         // The good (green) and red (bad) ranges.
82         let s0 = document.createElementNS('http://www.w3.org/2000/svg', 'rect');
83         s0.classList.add('range');
84         s0.classList.add('s0');
85         s0.setAttribute('width', to_x(ci.desired));
86         s0.setAttribute('height', height);
87         s0.setAttribute('x', '0');
88
89         let s1 = document.createElementNS('http://www.w3.org/2000/svg', 'rect');
90         s1.classList.add('range');
91         s1.classList.add('s1');
92         s1.setAttribute('width', width - to_x(ci.desired));
93         s1.setAttribute('height', height);
94         s1.setAttribute('x', to_x(ci.desired));
95
96         // Confidence bar.
97         let bar = document.createElementNS('http://www.w3.org/2000/svg', 'rect');
98         bar.classList.add('bar');
99         bar.setAttribute('width', to_x(ci.upper_ci) - to_x(ci.lower_ci));
100         bar.setAttribute('height', height / 3);
101         bar.setAttribute('x', to_x(ci.lower_ci));
102         bar.setAttribute('y', height / 3);
103
104         // Marker line for average.
105         let marker = document.createElementNS('http://www.w3.org/2000/svg', 'line');
106         marker.classList.add('marker');
107         marker.setAttribute('x1', to_x(ci.val));
108         marker.setAttribute('x2', to_x(ci.val));
109         marker.setAttribute('y1', height / 6);
110         marker.setAttribute('y2', height * 5 / 6);
111
112         svg.appendChild(s0);
113         svg.appendChild(s1);
114         svg.appendChild(bar);
115         svg.appendChild(marker);
116
117         element.appendChild(svg);
118 }
119
120 function process_matches(json) {
121         let players = {};
122         for (const player of json['players']) {
123                 players[player['player_id']] = {
124                         'name': player['name'],
125                         'number': player['number'],
126
127                         'goals': 0,
128                         'assists': 0,
129                         'hockey_assists': 0,
130                         'catches': 0,
131                         'touches': 0,
132                         'num_throws': 0,
133                         'throwaways': 0,
134                         'drops': 0,
135
136                         'defenses': 0,
137                         'interceptions': 0,
138                         'points_played': 0,
139                         'playing_time_ms': 0,
140                         'field_time_ms': 0,
141
142                         // For efficiency.
143                         'offensive_points_completed': 0,
144                         'offensive_points_won': 0,
145                         'defensive_points_completed': 0,
146                         'defensive_points_won': 0,
147
148                         'offensive_soft_plus': 0,
149                         'offensive_soft_minus': 0,
150                         'defensive_soft_plus': 0,
151                         'defensive_soft_minus': 0,
152
153                         'pulls': 0,
154                         'pull_times': [],
155                         'oob_pulls': 0,
156
157                         // Internal.
158                         'last_point_seen': null,
159                         'on_field_since': null,
160                 };
161         }
162
163         // Globals.
164         players['globals'] = {
165                 'points_played': 0,
166                 'playing_time_ms': 0,
167                 'field_time_ms': 0,
168
169                 'offensive_points_completed': 0,
170                 'offensive_points_won': 0,
171                 'defensive_points_completed': 0,
172                 'defensive_points_won': 0,
173         };
174         let globals = players['globals'];
175
176         for (const match of json['matches']) {
177                 let our_score = 0;
178                 let their_score = 0;
179                 let handler = null;
180                 let prev_handler = null;
181                 let live_since = null;
182                 let offense = null;
183                 let puller = null;
184                 let pull_started = null;
185                 let last_pull_was_ours = null;  // Effectively whether we're playing an O or D point (not affected by turnovers).
186                 let point_num = 0;
187                 let game_started = null;
188                 let last_goal = null;
189                 for (const [q,p] of Object.entries(players)) {
190                         p.on_field_since = null;
191                         p.last_point_seen = null;
192                 }
193                 for (const e of match['events']) {
194                         let t = e['t'];
195                         let type = e['type'];
196                         let p = players[e['player']];
197
198                         // Sub management
199                         if (type === 'in' && p.on_field_since === null) {
200                                 p.on_field_since = t;
201                         } else if (type === 'out') {
202                                 take_off_field(p, t, live_since);
203                         }
204
205                         // Liveness management
206                         if (type === 'pull' || type === 'their_pull' || type === 'restart') {
207                                 live_since = t;
208                         } else if (type === 'catch' && last_pull_was_ours === null) {
209                                 // Someone forgot to add the pull, so we'll need to wing it.
210                                 console.log('Missing pull on ' + our_score + '\u2013' + their_score + ' in ' + match['description'] + '; pretending to have one.');
211                                 live_since = t;
212                                 last_pull_was_ours = !offense;
213                         } else if (type === 'goal' || type === 'their_goal' || type === 'stoppage') {
214                                 for (const [q,p] of Object.entries(players)) {
215                                         if (p.on_field_since === null) {
216                                                 continue;
217                                         }
218                                         if (type !== 'stoppage' && p.last_point_seen !== point_num) {
219                                                 // In case the player did nothing this point,
220                                                 // not even subbing in.
221                                                 p.last_point_seen = point_num;
222                                                 ++p.points_played;
223                                         }
224                                         attribute_player_time(p, t, live_since);
225
226                                         if (type !== 'stoppage') {
227                                                 if (last_pull_was_ours === true) {  // D point.
228                                                         ++p.defensive_points_completed;
229                                                         if (type === 'goal') {
230                                                                 ++p.defensive_points_won;
231                                                         }
232                                                 } else if (last_pull_was_ours === false) {  // O point.
233                                                         ++p.offensive_points_completed;
234                                                         if (type === 'goal') {
235                                                                 ++p.offensive_points_won;
236                                                         }
237                                                 }
238                                         }
239                                 }
240
241                                 if (type !== 'stoppage') {
242                                         // Update globals.
243                                         ++globals.points_played;
244                                         if (last_pull_was_ours === true) {  // D point.
245                                                 ++globals.defensive_points_completed;
246                                                 if (type === 'goal') {
247                                                         ++globals.defensive_points_won;
248                                                 }
249                                         } else if (last_pull_was_ours === false) {  // O point.
250                                                 ++globals.offensive_points_completed;
251                                                 if (type === 'goal') {
252                                                         ++globals.offensive_points_won;
253                                                 }
254                                         }
255                                 }
256                                 if (live_since !== null) {
257                                         globals.playing_time_ms += t - live_since;
258                                 }
259
260                                 live_since = null;
261                         }
262
263                         // Score management
264                         if (type === 'goal') {
265                                 ++our_score;
266                         } else if (type === 'their_goal') {
267                                 ++their_score;
268                         }
269
270                         // Point count management
271                         if (p !== undefined && type !== 'out' && p.last_point_seen !== point_num) {
272                                 p.last_point_seen = point_num;
273                                 ++p.points_played;
274                         }
275                         if (type === 'goal' || type === 'their_goal') {
276                                 ++point_num;
277                                 last_goal = t;
278                         }
279                         if (type !== 'out' && game_started === null) {
280                                 game_started = t;
281                         }
282
283                         // Pull management
284                         if (type === 'pull') {
285                                 puller = e['player'];
286                                 pull_started = t;
287                                 ++p.pulls;
288                         } else if (type === 'in' || type === 'out' || type === 'stoppage' || type === 'restart' || type === 'unknown' || type === 'set_defense' || type === 'set_offense') {
289                                 // No effect on pull.
290                         } else if (type === 'pull_landed' && puller !== null) {
291                                 players[puller].pull_times.push(t - pull_started);
292                         } else if (type === 'pull_oob' && puller !== null) {
293                                 ++players[puller].oob_pulls;
294                         } else {
295                                 // Not pulling (if there was one, we never recorded its outcome, but still count it).
296                                 puller = pull_started = null;
297                         }
298
299                         // Offense/defense management (TODO: use it for actual counting)
300                         if (type === 'set_defense' || type === 'goal' || type === 'throwaway' || type === 'drop') {
301                                 offense = false;
302                         } else if (type === 'set_offense' || type === 'their_goal' || type === 'their_throwaway' || type === 'defense' || type === 'interception') {
303                                 offense = true;
304                         }
305                         if (type === 'pull') {
306                                 last_pull_was_ours = true;
307                         } else if (type === 'their_pull') {
308                                 last_pull_was_ours = false;
309                         } else if (type === 'set_offense' && last_pull_was_ours === null) {
310                                 // set_offense could either be “changed to offense for some reason
311                                 // we could not express”, or “we started in the middle of a point,
312                                 // and we are offense”. We assume that if we already saw the pull,
313                                 // it's the former, and if not, it's the latter; thus, the === null
314                                 // test above. (It could also be “we set offense before the pull,
315                                 // so that we get the right button enabled”, in which case it will
316                                 // be overwritten by the next pull/their_pull event anyway.)
317                                 last_pull_was_ours = false;
318                         } else if (type === 'set_defense' && last_pull_was_ours === null) {
319                                 // Similar.
320                                 last_pull_was_ours = true;
321                         } else if (type === 'goal' || type === 'their_goal') {
322                                 last_pull_was_ours = null;
323                         }
324
325                         // Event management
326                         if (type === 'catch' || type === 'goal') {
327                                 if (handler !== null) {
328                                         ++players[handler].num_throws;
329                                         ++p.catches;
330                                 }
331
332                                 ++p.touches;
333                                 if (type === 'goal') {
334                                         if (prev_handler !== null) {
335                                                 ++players[prev_handler].hockey_assists;
336                                         }
337                                         if (handler !== null) {
338                                                 ++players[handler].assists;
339                                         }
340                                         ++p.goals;
341                                         handler = prev_handler = null;
342                                 } else {
343                                         // Update hold history.
344                                         prev_handler = handler;
345                                         handler = e['player'];
346                                 }
347                         } else if (type === 'throwaway') {
348                                 ++p.num_throws;
349                                 ++p.throwaways;
350                                 handler = prev_handler = null;
351                         } else if (type === 'drop') {
352                                 ++p.drops;
353                                 handler = prev_handler = null;
354                         } else if (type === 'defense') {
355                                 ++p.defenses;
356                         } else if (type === 'interception') {
357                                 ++p.interceptions;
358                                 ++p.defenses;
359                                 ++p.touches;
360                                 prev_handler = null;
361                                 handler = e['player'];
362                         } else if (type === 'offensive_soft_plus' || type === 'offensive_soft_minus' || type === 'defensive_soft_plus' || type === 'defensive_soft_minus') {
363                                 ++p[type];
364                         } else if (type !== 'in' && type !== 'out' && type !== 'pull' &&
365                                    type !== 'their_goal' && type !== 'stoppage' && type !== 'restart' && type !== 'unknown' &&
366                                    type !== 'set_defense' && type !== 'goal' && type !== 'throwaway' &&
367                                    type !== 'drop' && type !== 'set_offense' && type !== 'their_goal' &&
368                                    type !== 'pull' && type !== 'pull_landed' && type !== 'pull_oob' && type !== 'their_pull' &&
369                                    type !== 'their_throwaway' && type !== 'defense' && type !== 'interception') {
370                                 console.log("Unknown event:", e);
371                         }
372                 }
373
374                 // Add field time for all players still left at match end.
375                 for (const [q,p] of Object.entries(players)) {
376                         if (p.on_field_since !== null && last_goal !== null) {
377                                 p.field_time_ms += last_goal - p.on_field_since;
378                         }
379                 }
380                 if (game_started !== null && last_goal !== null) {
381                         globals.field_time_ms += last_goal - game_started;
382                 }
383                 if (live_since !== null && last_goal !== null) {
384                         globals.playing_time_ms += last_goal - live_since;
385                 }
386         }
387
388         let chosen_category = get_chosen_category();
389         write_main_menu(chosen_category);
390
391         let rows = [];
392         if (chosen_category === 'general') {
393                 rows = make_table_general(players);
394         } else if (chosen_category === 'offense') {
395                 rows = make_table_offense(players);
396         } else if (chosen_category === 'defense') {
397                 rows = make_table_defense(players);
398         } else if (chosen_category === 'playing_time') {
399                 rows = make_table_playing_time(players);
400         } else if (chosen_category === 'per_point') {
401                 rows = make_table_per_point(players);
402         }
403         document.getElementById('stats').replaceChildren(...rows);
404 }
405
406 function get_chosen_category() {
407         if (window.location.hash === '#offense') {
408                 return 'offense';
409         } else if (window.location.hash === '#defense') {
410                 return 'defense';
411         } else if (window.location.hash === '#playing_time') {
412                 return 'playing_time';
413         } else if (window.location.hash === '#per_point') {
414                 return 'per_point';
415         } else {
416                 return 'general';
417         }
418 }
419
420 function write_main_menu(chosen_category) {
421         let elems = [];
422         if (chosen_category === 'general') {
423                 let span = document.createElement('span');
424                 span.innerText = 'General';
425                 elems.push(span);
426         } else {
427                 let a = document.createElement('a');
428                 a.appendChild(document.createTextNode('General'));
429                 a.setAttribute('href', '#general');
430                 elems.push(a);
431         }
432
433         if (chosen_category === 'offense') {
434                 let span = document.createElement('span');
435                 span.innerText = 'Offense';
436                 elems.push(span);
437         } else {
438                 let a = document.createElement('a');
439                 a.appendChild(document.createTextNode('Offense'));
440                 a.setAttribute('href', '#offense');
441                 elems.push(a);
442         }
443
444         if (chosen_category === 'defense') {
445                 let span = document.createElement('span');
446                 span.innerText = 'Defense';
447                 elems.push(span);
448         } else {
449                 let a = document.createElement('a');
450                 a.appendChild(document.createTextNode('Defense'));
451                 a.setAttribute('href', '#defense');
452                 elems.push(a);
453         }
454
455         if (chosen_category === 'playing_time') {
456                 let span = document.createElement('span');
457                 span.innerText = 'Playing time';
458                 elems.push(span);
459         } else {
460                 let a = document.createElement('a');
461                 a.appendChild(document.createTextNode('Playing time'));
462                 a.setAttribute('href', '#playing_time');
463                 elems.push(a);
464         }
465
466         if (chosen_category === 'per_point') {
467                 let span = document.createElement('span');
468                 span.innerText = 'Per point';
469                 elems.push(span);
470         } else {
471                 let a = document.createElement('a');
472                 a.appendChild(document.createTextNode('Per point'));
473                 a.setAttribute('href', '#per_point');
474                 elems.push(a);
475         }
476
477         document.getElementById('mainmenu').replaceChildren(...elems);
478 }
479
480 // https://en.wikipedia.org/wiki/1.96#History
481 const z = 1.959964;
482
483 function make_binomial_ci(val, num, z) {
484         let avg = val / num;
485
486         // https://en.wikipedia.org/wiki/Binomial_proportion_confidence_interval#Wilson_score_interval
487         let low  = (avg + z*z/(2*num) - z * Math.sqrt(avg * (1.0 - avg) / num + z*z/(4*num*num))) / (1 + z*z/num);   
488         let high = (avg + z*z/(2*num) + z * Math.sqrt(avg * (1.0 - avg) / num + z*z/(4*num*num))) / (1 + z*z/num); 
489
490         // Fix the signs so that we don't get -0.00.
491         low = Math.max(low, 0.0);
492         return {
493                 'val': avg,
494                 'lower_ci': low,
495                 'upper_ci': high,
496                 'min': 0.0,
497                 'max': 1.0,
498         };
499 }
500
501 // These can only happen once per point, but you get -1 and +1
502 // instead of 0 and +1. After we rewrite to 0 and 1, it's a binomial,
503 // and then we can rewrite back.
504 function make_efficiency_ci(points_won, points_completed, z)
505 {
506         let ci = make_binomial_ci(points_won, points_completed, z);
507         ci.val = 2.0 * ci.val - 1.0;
508         ci.lower_ci = 2.0 * ci.lower_ci - 1.0;
509         ci.upper_ci = 2.0 * ci.upper_ci - 1.0;
510         ci.min = -1.0;
511         ci.max = 1.0;
512         ci.desired = 0.0;  // Desired = positive efficiency.
513         return ci;
514 }
515
516 function make_table_general(players) {
517         let rows = [];
518         {
519                 let header = document.createElement('tr');
520                 add_cell(header, 'th', 'Player');
521                 add_cell(header, 'th', '+/-');
522                 add_cell(header, 'th', 'Soft +/-');
523                 add_cell(header, 'th', 'O efficiency');
524                 add_cell(header, 'th', 'D efficiency');
525                 add_cell(header, 'th', 'Points played');
526                 rows.push(header);
527         }
528
529         for (const [q,p] of Object.entries(players)) {
530                 if (q === 'globals') continue;
531                 let row = document.createElement('tr');
532                 let pm = p.goals + p.assists + p.hockey_assists + p.defenses - p.throwaways - p.drops;
533                 let soft_pm = p.offensive_soft_plus + p.defensive_soft_plus - p.offensive_soft_minus - p.defensive_soft_minus;
534                 let o_efficiency = make_efficiency_ci(p.offensive_points_won, p.offensive_points_completed, z);
535                 let d_efficiency = make_efficiency_ci(p.defensive_points_won, p.defensive_points_completed, z);
536                 add_3cell(row, p.name, 'name');  // TODO: number?
537                 add_3cell(row, pm > 0 ? ('+' + pm) : pm);
538                 add_3cell(row, soft_pm > 0 ? ('+' + soft_pm) : soft_pm);
539                 add_3cell_ci(row, o_efficiency);
540                 add_3cell_ci(row, d_efficiency);
541                 add_3cell(row, p.points_played);
542                 rows.push(row);
543         }
544
545         // Globals.
546         let globals = players['globals'];
547         let o_efficiency = make_efficiency_ci(globals.offensive_points_won, globals.offensive_points_completed, z);
548         let d_efficiency = make_efficiency_ci(globals.defensive_points_won, globals.defensive_points_completed, z);
549         let row = document.createElement('tr');
550         add_3cell(row, '');
551         add_3cell(row, '');
552         add_3cell(row, '');
553         add_3cell_ci(row, o_efficiency);
554         add_3cell_ci(row, d_efficiency);
555         add_3cell(row, globals.points_played);
556         rows.push(row);
557
558         return rows;
559 }
560
561 function make_table_offense(players) {
562         let rows = [];
563         {
564                 let header = document.createElement('tr');
565                 add_cell(header, 'th', 'Player');
566                 add_cell(header, 'th', 'Goals');
567                 add_cell(header, 'th', 'Assists');
568                 add_cell(header, 'th', 'Hockey assists');
569                 add_cell(header, 'th', 'Throws');
570                 add_cell(header, 'th', 'Throwaways');
571                 add_cell(header, 'th', '%OK');
572                 add_cell(header, 'th', 'Catches');
573                 add_cell(header, 'th', 'Drops');
574                 add_cell(header, 'th', '%OK');
575                 add_cell(header, 'th', 'Soft +/-');
576                 rows.push(header);
577         }
578
579         let num_throws = 0;
580         let throwaways = 0;
581         let catches = 0;
582         let drops = 0;
583         for (const [q,p] of Object.entries(players)) {
584                 if (q === 'globals') continue;
585                 let throw_ok = make_binomial_ci(p.num_throws - p.throwaways, p.num_throws, z);
586                 let catch_ok = make_binomial_ci(p.catches, p.catches + p.drops, z);
587
588                 throw_ok.format = 'percentage';
589                 catch_ok.format = 'percentage';
590
591                 // Desire at least 90% percentage. Fairly arbitrary.
592                 throw_ok.desired = 0.9;
593                 catch_ok.desired = 0.9;
594
595                 let row = document.createElement('tr');
596                 add_3cell(row, p.name, 'name');  // TODO: number?
597                 add_3cell(row, p.goals);
598                 add_3cell(row, p.assists);
599                 add_3cell(row, p.hockey_assists);
600                 add_3cell(row, p.num_throws);
601                 add_3cell(row, p.throwaways);
602                 add_3cell_ci(row, throw_ok);
603                 add_3cell(row, p.catches);
604                 add_3cell(row, p.drops);
605                 add_3cell_ci(row, catch_ok);
606                 add_3cell(row, '+' + p.offensive_soft_plus);
607                 add_3cell(row, '-' + p.offensive_soft_minus);
608                 rows.push(row);
609
610                 num_throws += p.num_throws;
611                 throwaways += p.throwaways;
612                 catches += p.catches;
613                 drops += p.drops;
614         }
615
616         // Globals.
617         let throw_ok = make_binomial_ci(num_throws - throwaways, num_throws, z);
618         let catch_ok = make_binomial_ci(catches, catches + drops, z);
619         throw_ok.format = 'percentage';
620         catch_ok.format = 'percentage';
621         throw_ok.desired = 0.9;
622         catch_ok.desired = 0.9;
623
624         let row = document.createElement('tr');
625         add_3cell(row, '');
626         add_3cell(row, '');
627         add_3cell(row, '');
628         add_3cell(row, '');
629         add_3cell(row, num_throws);
630         add_3cell(row, throwaways);
631         add_3cell_ci(row, throw_ok);
632         add_3cell(row, catches);
633         add_3cell(row, drops);
634         add_3cell_ci(row, catch_ok);
635         add_3cell(row, '');
636         add_3cell(row, '');
637         rows.push(row);
638
639         return rows;
640 }
641
642 function make_table_defense(players) {
643         let rows = [];
644         {
645                 let header = document.createElement('tr');
646                 add_cell(header, 'th', 'Player');
647                 add_cell(header, 'th', 'Ds');
648                 add_cell(header, 'th', 'Pulls');
649                 add_cell(header, 'th', 'OOB pulls');
650                 add_cell(header, 'th', 'Avg. hang time (IB)');
651                 add_cell(header, 'th', 'Soft +/-');
652                 rows.push(header);
653         }
654         for (const [q,p] of Object.entries(players)) {
655                 if (q === 'globals') continue;
656                 let sum_time = 0;
657                 for (const t of p.pull_times) {
658                         sum_time += t;
659                 }
660                 let avg_time = 1e-3 * sum_time / p.pulls;
661                 let oob_pct = 100 * p.oob_pulls / p.pulls;
662
663                 let row = document.createElement('tr');
664                 add_3cell(row, p.name, 'name');  // TODO: number?
665                 add_3cell(row, p.defenses);
666                 add_3cell(row, p.pulls);
667                 if (p.pulls === 0) {
668                         add_3cell(row, 'N/A');
669                 } else {
670                         add_3cell(row, p.oob_pulls + ' (' + oob_pct.toFixed(0) + '%)');
671                 }
672                 if (p.pulls > p.oob_pulls) {
673                         add_3cell(row, avg_time.toFixed(1) + ' sec');
674                 } else {
675                         add_3cell(row, 'N/A');
676                 }
677                 add_3cell(row, '+' + p.defensive_soft_plus);
678                 add_3cell(row, '-' + p.defensive_soft_minus);
679                 rows.push(row);
680         }
681         return rows;
682 }
683
684 function make_table_playing_time(players) {
685         let rows = [];
686         {
687                 let header = document.createElement('tr');
688                 add_cell(header, 'th', 'Player');
689                 add_cell(header, 'th', 'Points played');
690                 add_cell(header, 'th', 'Time played');
691                 add_cell(header, 'th', 'Time on field');
692                 add_cell(header, 'th', 'O points');
693                 add_cell(header, 'th', 'D points');
694                 rows.push(header);
695         }
696
697         for (const [q,p] of Object.entries(players)) {
698                 if (q === 'globals') continue;
699                 let row = document.createElement('tr');
700                 add_3cell(row, p.name, 'name');  // TODO: number?
701                 add_3cell(row, p.points_played);
702                 add_3cell(row, Math.floor(p.playing_time_ms / 60000) + ' min');
703                 add_3cell(row, Math.floor(p.field_time_ms / 60000) + ' min');
704                 add_3cell(row, p.offensive_points_completed);
705                 add_3cell(row, p.defensive_points_completed);
706                 rows.push(row);
707         }
708
709         // Globals.
710         let globals = players['globals'];
711         let row = document.createElement('tr');
712         add_3cell(row, '');
713         add_3cell(row, globals.points_played);
714         add_3cell(row, Math.floor(globals.playing_time_ms / 60000) + ' min');
715         add_3cell(row, Math.floor(globals.field_time_ms / 60000) + ' min');
716         add_3cell(row, globals.offensive_points_completed);
717         add_3cell(row, globals.defensive_points_completed);
718         rows.push(row);
719
720         return rows;
721 }
722
723 function make_table_per_point(players) {
724         let rows = [];
725         {
726                 let header = document.createElement('tr');
727                 add_cell(header, 'th', 'Player');
728                 add_cell(header, 'th', 'Goals');
729                 add_cell(header, 'th', 'Assists');
730                 add_cell(header, 'th', 'Hockey assists');
731                 add_cell(header, 'th', 'Ds');
732                 add_cell(header, 'th', 'Throwaways');
733                 add_cell(header, 'th', 'Drops');
734                 add_cell(header, 'th', 'Touches');
735                 rows.push(header);
736         }
737
738         let goals = 0;
739         let assists = 0;
740         let hockey_assists = 0;
741         let defenses = 0;
742         let throwaways = 0;
743         let drops = 0;
744         let touches = 0;
745         for (const [q,p] of Object.entries(players)) {
746                 if (q === 'globals') continue;
747                 let row = document.createElement('tr');
748                 add_3cell(row, p.name, 'name');  // TODO: number?
749                 if (p.points_played > 0) {
750                         add_3cell(row, p.goals == 0 ? 0 : (p.goals / p.points_played).toFixed(2));
751                         add_3cell(row, p.assists == 0 ? 0 : (p.assists / p.points_played).toFixed(2));
752                         add_3cell(row, p.hockey_assists == 0 ? 0 : (p.hockey_assists / p.points_played).toFixed(2));
753                         add_3cell(row, p.defenses == 0 ? 0 : (p.defenses / p.points_played).toFixed(2));
754                         add_3cell(row, p.throwaways == 0 ? 0 : (p.throwaways / p.points_played).toFixed(2));
755                         add_3cell(row, p.drops == 0 ? 0 : (p.drops / p.points_played).toFixed(2));
756                         add_3cell(row, p.touches == 0 ? 0 : (p.touches / p.points_played).toFixed(2));
757                 } else {
758                         add_3cell(row, 'N/A');
759                         add_3cell(row, 'N/A');
760                         add_3cell(row, 'N/A');
761                         add_3cell(row, 'N/A');
762                         add_3cell(row, 'N/A');
763                         add_3cell(row, 'N/A');
764                         add_3cell(row, 'N/A');
765                 }
766                 rows.push(row);
767
768                 goals += p.goals;
769                 assists += p.assists;
770                 hockey_assists += p.hockey_assists;
771                 defenses += p.defenses;
772                 throwaways += p.throwaways;
773                 drops += p.drops;
774                 touches += p.touches;
775         }
776
777         // Globals.
778         let globals = players['globals'];
779         let row = document.createElement('tr');
780         add_3cell(row, '');
781         if (globals.points_played > 0) {
782                 add_3cell(row, goals == 0 ? 0 : (goals / globals.points_played).toFixed(2));
783                 add_3cell(row, assists == 0 ? 0 : (assists / globals.points_played).toFixed(2));
784                 add_3cell(row, hockey_assists == 0 ? 0 : (hockey_assists / globals.points_played).toFixed(2));
785                 add_3cell(row, defenses == 0 ? 0 : (defenses / globals.points_played).toFixed(2));
786                 add_3cell(row, throwaways == 0 ? 0 : (throwaways / globals.points_played).toFixed(2));
787                 add_3cell(row, drops == 0 ? 0 : (drops / globals.points_played).toFixed(2));
788                 add_3cell(row, touches == 0 ? 0 : (touches / globals.points_played).toFixed(2));
789         } else {
790                 add_3cell(row, 'N/A');
791                 add_3cell(row, 'N/A');
792                 add_3cell(row, 'N/A');
793                 add_3cell(row, 'N/A');
794                 add_3cell(row, 'N/A');
795                 add_3cell(row, 'N/A');
796                 add_3cell(row, 'N/A');
797         }
798         rows.push(row);
799
800         return rows;
801 }