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