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