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