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