3 // No frameworks, no compilers, no npm, just JavaScript. :-)
6 let global_filters = [];
8 addEventListener('hashchange', () => { process_matches(global_json, global_filters); });
9 addEventListener('click', possibly_close_menu);
10 fetch('ultimate.json')
11 .then(response => response.json())
12 .then(response => { global_json = response; process_matches(global_json, global_filters); });
14 function attribute_player_time(player, to, from, offense) {
16 if (player.on_field_since > from) {
17 // Player came in while play happened (without a stoppage!?).
18 delta_time = to - player.on_field_since;
20 delta_time = to - from;
22 player.playing_time_ms += delta_time;
23 if (offense === true) {
24 player.offensive_playing_time_ms += delta_time;
25 } else if (offense === false) {
26 player.defensive_playing_time_ms += delta_time;
30 function take_off_field(player, t, live_since, offense, keep) {
32 if (live_since === null) {
33 // Play isn't live, so nothing to do.
35 attribute_player_time(player, t, live_since, offense);
37 if (player.on_field_since !== null) { // Just a safeguard; out without in should never happen.
38 player.field_time_ms += t - player.on_field_since;
41 player.on_field_since = null;
44 function add_cell(tr, element_type, text) {
45 let element = document.createElement(element_type);
46 element.textContent = text;
47 tr.appendChild(element);
51 function add_th(tr, text, colspan) {
52 let element = add_cell(tr, 'th', text);
54 element.setAttribute('colspan', colspan);
56 element.setAttribute('colspan', '3');
61 function add_3cell(tr, text, cls) {
62 let p1 = add_cell(tr, 'td', '');
63 let element = add_cell(tr, 'td', text);
64 let p2 = add_cell(tr, 'td', '');
66 p1.classList.add('pad');
67 p2.classList.add('pad');
68 if (cls === undefined) {
69 element.classList.add('num');
71 element.classList.add(cls);
76 function add_3cell_with_filler_ci(tr, text, cls) {
77 let element = add_3cell(tr, text, cls);
79 let svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
80 svg.classList.add('fillerci');
81 svg.setAttribute('width', ci_width);
82 svg.setAttribute('height', ci_height);
83 element.appendChild(svg);
88 function add_3cell_ci(tr, ci) {
90 add_3cell_with_filler_ci(tr, 'N/A');
95 if (ci.format === 'percentage') {
96 text = (100 * ci.val).toFixed(0) + '%';
98 text = ci.val.toFixed(2);
100 let element = add_3cell(tr, text);
101 let to_x = (val) => { return ci_width * (val - ci.min) / (ci.max - ci.min); };
104 let svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
105 if (ci.inverted === true) {
106 svg.classList.add('invertedci');
108 svg.classList.add('ci');
110 svg.setAttribute('width', ci_width);
111 svg.setAttribute('height', ci_height);
113 // The good (green) and red (bad) ranges.
114 let s0 = document.createElementNS('http://www.w3.org/2000/svg', 'rect');
115 s0.classList.add('range');
116 s0.classList.add('s0');
117 s0.setAttribute('width', to_x(ci.desired));
118 s0.setAttribute('height', ci_height);
119 s0.setAttribute('x', '0');
121 let s1 = document.createElementNS('http://www.w3.org/2000/svg', 'rect');
122 s1.classList.add('range');
123 s1.classList.add('s1');
124 s1.setAttribute('width', ci_width - to_x(ci.desired));
125 s1.setAttribute('height', ci_height);
126 s1.setAttribute('x', to_x(ci.desired));
129 let bar = document.createElementNS('http://www.w3.org/2000/svg', 'rect');
130 bar.classList.add('bar');
131 bar.setAttribute('width', to_x(ci.upper_ci) - to_x(ci.lower_ci));
132 bar.setAttribute('height', ci_height / 3);
133 bar.setAttribute('x', to_x(ci.lower_ci));
134 bar.setAttribute('y', ci_height / 3);
136 // Marker line for average.
137 let marker = document.createElementNS('http://www.w3.org/2000/svg', 'line');
138 marker.classList.add('marker');
139 marker.setAttribute('x1', to_x(ci.val));
140 marker.setAttribute('x2', to_x(ci.val));
141 marker.setAttribute('y1', ci_height / 6);
142 marker.setAttribute('y2', ci_height * 5 / 6);
146 svg.appendChild(bar);
147 svg.appendChild(marker);
149 element.appendChild(svg);
152 function process_matches(json, filters) {
154 for (const player of json['players']) {
155 players[player['player_id']] = {
156 'name': player['name'],
157 'number': player['number'],
172 'playing_time_ms': 0,
173 'offensive_playing_time_ms': 0,
174 'defensive_playing_time_ms': 0,
178 'offensive_points_completed': 0,
179 'offensive_points_won': 0,
180 'defensive_points_completed': 0,
181 'defensive_points_won': 0,
183 'offensive_soft_plus': 0,
184 'offensive_soft_minus': 0,
185 'defensive_soft_plus': 0,
186 'defensive_soft_minus': 0,
193 'last_point_seen': null,
194 'on_field_since': null,
199 players['globals'] = {
201 'playing_time_ms': 0,
202 'offensive_playing_time_ms': 0,
203 'defensive_playing_time_ms': 0,
206 'offensive_points_completed': 0,
207 'offensive_points_won': 0,
208 'defensive_points_completed': 0,
209 'defensive_points_won': 0,
211 let globals = players['globals'];
213 for (const match of json['matches']) {
214 if (!keep_match(match['match_id'], filters)) {
221 let prev_handler = null;
222 let live_since = null;
223 let offense = null; // True/false/null (unknown).
225 let pull_started = null;
226 let last_pull_was_ours = null; // Effectively whether we're playing an O or D point (not affected by turnovers).
228 let game_started = null;
229 let last_goal = null;
231 // The last used formations of the given kind, if any; they may be reused
232 // when the point starts, if nothing else is set.
233 let last_offensive_formation = null;
234 let last_defensive_formation = null;
236 // Formations we've set, but haven't really had the chance to use yet
237 // (e.g., we get a “use zone defense” event while we're still on offense).
238 let pending_offensive_formation = null;
239 let pending_defensive_formation = null;
241 // Formations that we have played at least once this point, after all
242 // heuristics and similar.
243 let formations_used_this_point = new Set();
245 for (const [q,p] of Object.entries(players)) {
246 p.on_field_since = null;
247 p.last_point_seen = null;
249 for (const e of match['events']) {
251 let type = e['type'];
252 let p = players[e['player']];
255 let keep = keep_event(players, formations_used_this_point, filters);
256 if (type === 'in' && p.on_field_since === null) {
257 p.on_field_since = t;
258 if (!keep && keep_event(players, formations_used_this_point, filters)) {
259 // A player needed for the filters went onto the field,
260 // so pretend people walked on right now (to start their
262 for (const [q,p2] of Object.entries(players)) {
263 if (p2.on_field_since !== null) {
264 p2.on_field_since = t;
268 } else if (type === 'out') {
269 take_off_field(p, t, live_since, offense, keep);
270 if (keep && !keep_event(players, formations_used_this_point, filters)) {
271 // A player needed for the filters went off the field,
272 // so we need to attribute time for all the others.
273 // Pretend they walked off and then immediately on again.
275 // TODO: We also need to take care of this to get the globals right.
276 for (const [q,p2] of Object.entries(players)) {
277 if (p2.on_field_since !== null) {
278 take_off_field(p2, t, live_since, offense, keep);
279 p2.on_field_since = t;
285 keep = keep_event(players, formations_used_this_point, filters); // Recompute after in/out.
287 // Liveness management
288 if (type === 'pull' || type === 'their_pull' || type === 'restart') {
290 } else if (type === 'catch' && last_pull_was_ours === null) {
291 // Someone forgot to add the pull, so we'll need to wing it.
292 console.log('Missing pull on ' + our_score + '\u2013' + their_score + ' in ' + match['description'] + '; pretending to have one.');
294 last_pull_was_ours = !offense;
295 } else if (type === 'goal' || type === 'their_goal' || type === 'stoppage') {
296 for (const [q,p] of Object.entries(players)) {
297 if (p.on_field_since === null) {
300 if (type !== 'stoppage' && p.last_point_seen !== point_num) {
302 // In case the player did nothing this point,
303 // not even subbing in.
304 p.last_point_seen = point_num;
308 if (keep) attribute_player_time(p, t, live_since, offense);
310 if (type !== 'stoppage') {
312 if (last_pull_was_ours === true) { // D point.
313 ++p.defensive_points_completed;
314 if (type === 'goal') {
315 ++p.defensive_points_won;
317 } else if (last_pull_was_ours === false) { // O point.
318 ++p.offensive_points_completed;
319 if (type === 'goal') {
320 ++p.offensive_points_won;
328 if (type !== 'stoppage') {
330 ++globals.points_played;
331 if (last_pull_was_ours === true) { // D point.
332 ++globals.defensive_points_completed;
333 if (type === 'goal') {
334 ++globals.defensive_points_won;
336 } else if (last_pull_was_ours === false) { // O point.
337 ++globals.offensive_points_completed;
338 if (type === 'goal') {
339 ++globals.offensive_points_won;
343 if (live_since !== null) {
344 globals.playing_time_ms += t - live_since;
345 if (offense === true) {
346 globals.offensive_playing_time_ms += t - live_since;
347 } else if (offense === false) {
348 globals.defensive_playing_time_ms += t - live_since;
357 if (type === 'goal') {
359 } else if (type === 'their_goal') {
363 // Point count management
364 if (p !== undefined && type !== 'out' && p.last_point_seen !== point_num) {
366 p.last_point_seen = point_num;
370 if (type === 'goal' || type === 'their_goal') {
374 if (type !== 'out' && game_started === null) {
379 if (type === 'pull') {
380 puller = e['player'];
383 } else if (type === 'in' || type === 'out' || type === 'stoppage' || type === 'restart' || type === 'unknown' || type === 'set_defense' || type === 'set_offense') {
384 // No effect on pull.
385 } else if (type === 'pull_landed' && puller !== null) {
386 if (keep) players[puller].pull_times.push(t - pull_started);
387 } else if (type === 'pull_oob' && puller !== null) {
388 if (keep) ++players[puller].oob_pulls;
390 // Not pulling (if there was one, we never recorded its outcome, but still count it).
391 puller = pull_started = null;
394 // Offense/defense management
395 let last_offense = offense;
396 if (type === 'set_defense' || type === 'goal' || type === 'throwaway' || type === 'drop' || type === 'was_d') {
398 } else if (type === 'set_offense' || type === 'their_goal' || type === 'their_throwaway' || type === 'defense' || type === 'interception') {
401 if (last_offense !== offense && live_since !== null) {
402 // Switched offense/defense status, so attribute this drive as needed,
403 // and update live_since to take that into account.
405 for (const [q,p] of Object.entries(players)) {
406 if (p.on_field_since === null) {
409 attribute_player_time(p, t, live_since, last_offense);
411 globals.playing_time_ms += t - live_since;
412 if (offense === true) {
413 globals.offensive_playing_time_ms += t - live_since;
414 } else if (offense === false) {
415 globals.defensive_playing_time_ms += t - live_since;
421 if (type === 'pull') {
422 last_pull_was_ours = true;
423 } else if (type === 'their_pull') {
424 last_pull_was_ours = false;
425 } else if (type === 'set_offense' && last_pull_was_ours === null) {
426 // set_offense could either be “changed to offense for some reason
427 // we could not express”, or “we started in the middle of a point,
428 // and we are offense”. We assume that if we already saw the pull,
429 // it's the former, and if not, it's the latter; thus, the === null
430 // test above. (It could also be “we set offense before the pull,
431 // so that we get the right button enabled”, in which case it will
432 // be overwritten by the next pull/their_pull event anyway.)
433 last_pull_was_ours = false;
434 } else if (type === 'set_defense' && last_pull_was_ours === null) {
436 last_pull_was_ours = true;
437 } else if (type === 'goal' || type === 'their_goal') {
438 last_pull_was_ours = null;
441 // Formation management
442 if (type === 'formation_offense' || type === 'formation_defense') {
443 let id = e.formation === null ? 0 : e.formation;
444 let for_offense = (type === 'formation_offense');
445 if (offense === for_offense) {
446 formations_used_this_point.add(id);
447 } else if (for_offense) {
448 pending_offensive_formation = id;
450 pending_defensive_formation = id;
453 last_offensive_formation = id;
455 last_defensive_formation = id;
457 } else if (last_offense !== offense) {
458 if (offense === true && pending_offensive_formation !== null) {
459 formations_used_this_point.add(pending_offensive_formation);
460 pending_offensive_formation = null;
461 } else if (offense === false && pending_defensive_formation !== null) {
462 formations_used_this_point.add(pending_defensive_formation);
463 pending_defensive_formation = null;
464 } else if (offense === true && last_defensive_formation !== null) {
465 if (should_reuse_last_formation(match['events'], t)) {
466 formations_used_this_point.add(last_defensive_formation);
468 } else if (offense === false && last_offensive_formation !== null) {
469 if (should_reuse_last_formation(match['events'], t)) {
470 formations_used_this_point.add(last_offensive_formation);
476 if (type === 'catch' || type === 'goal') {
477 if (handler !== null) {
479 ++players[handler].num_throws;
484 if (keep) ++p.touches;
485 if (type === 'goal') {
487 if (prev_handler !== null) {
488 ++players[prev_handler].hockey_assists;
490 if (handler !== null) {
491 ++players[handler].assists;
495 handler = prev_handler = null;
497 // Update hold history.
498 prev_handler = handler;
499 handler = e['player'];
501 } else if (type === 'throwaway') {
506 handler = prev_handler = null;
507 } else if (type === 'drop') {
509 handler = prev_handler = null;
510 } else if (type === 'was_d') {
511 if (keep) ++p.was_ds;
512 handler = prev_handler = null;
513 } else if (type === 'defense') {
514 if (keep) ++p.defenses;
515 } else if (type === 'interception') {
522 handler = e['player'];
523 } else if (type === 'offensive_soft_plus' || type === 'offensive_soft_minus' || type === 'defensive_soft_plus' || type === 'defensive_soft_minus') {
525 } else if (type !== 'in' && type !== 'out' && type !== 'pull' &&
526 type !== 'their_goal' && type !== 'stoppage' && type !== 'restart' && type !== 'unknown' &&
527 type !== 'set_defense' && type !== 'goal' && type !== 'throwaway' &&
528 type !== 'drop' && type !== 'was_d' && type !== 'set_offense' && type !== 'their_goal' &&
529 type !== 'pull' && type !== 'pull_landed' && type !== 'pull_oob' && type !== 'their_pull' &&
530 type !== 'their_throwaway' && type !== 'defense' && type !== 'interception' &&
531 type !== 'formation_offense' && type !== 'formation_defense') {
532 console.log("Unknown event:", e);
535 if (type === 'goal' || type === 'their_goal') {
536 formations_used_this_point.clear();
540 // Add field time for all players still left at match end.
541 const keep = keep_event(players, formations_used_this_point, filters);
543 for (const [q,p] of Object.entries(players)) {
544 if (p.on_field_since !== null && last_goal !== null) {
545 p.field_time_ms += last_goal - p.on_field_since;
548 if (game_started !== null && last_goal !== null) {
549 globals.field_time_ms += last_goal - game_started;
551 if (live_since !== null && last_goal !== null) {
552 globals.playing_time_ms += last_goal - live_since;
553 if (offense === true) {
554 globals.offensive_playing_time_ms += last_goal - live_since;
555 } else if (offense === false) {
556 globals.defensive_playing_time_ms += last_goal - live_since;
562 let chosen_category = get_chosen_category();
563 write_main_menu(chosen_category);
566 if (chosen_category === 'general') {
567 rows = make_table_general(players);
568 } else if (chosen_category === 'offense') {
569 rows = make_table_offense(players);
570 } else if (chosen_category === 'defense') {
571 rows = make_table_defense(players);
572 } else if (chosen_category === 'playing_time') {
573 rows = make_table_playing_time(players);
574 } else if (chosen_category === 'per_point') {
575 rows = make_table_per_point(players);
577 document.getElementById('stats').replaceChildren(...rows);
580 function get_chosen_category() {
581 if (window.location.hash === '#offense') {
583 } else if (window.location.hash === '#defense') {
585 } else if (window.location.hash === '#playing_time') {
586 return 'playing_time';
587 } else if (window.location.hash === '#per_point') {
594 function write_main_menu(chosen_category) {
596 if (chosen_category === 'general') {
597 let span = document.createElement('span');
598 span.innerText = 'General';
601 let a = document.createElement('a');
602 a.appendChild(document.createTextNode('General'));
603 a.setAttribute('href', '#general');
607 if (chosen_category === 'offense') {
608 let span = document.createElement('span');
609 span.innerText = 'Offense';
612 let a = document.createElement('a');
613 a.appendChild(document.createTextNode('Offense'));
614 a.setAttribute('href', '#offense');
618 if (chosen_category === 'defense') {
619 let span = document.createElement('span');
620 span.innerText = 'Defense';
623 let a = document.createElement('a');
624 a.appendChild(document.createTextNode('Defense'));
625 a.setAttribute('href', '#defense');
629 if (chosen_category === 'playing_time') {
630 let span = document.createElement('span');
631 span.innerText = 'Playing time';
634 let a = document.createElement('a');
635 a.appendChild(document.createTextNode('Playing time'));
636 a.setAttribute('href', '#playing_time');
640 if (chosen_category === 'per_point') {
641 let span = document.createElement('span');
642 span.innerText = 'Per point';
645 let a = document.createElement('a');
646 a.appendChild(document.createTextNode('Per point'));
647 a.setAttribute('href', '#per_point');
651 document.getElementById('mainmenu').replaceChildren(...elems);
654 // https://en.wikipedia.org/wiki/1.96#History
657 const ci_width = 100;
658 const ci_height = 20;
660 function make_binomial_ci(val, num, z) {
663 // https://en.wikipedia.org/wiki/Binomial_proportion_confidence_interval#Wilson_score_interval
664 let low = (avg + z*z/(2*num) - z * Math.sqrt(avg * (1.0 - avg) / num + z*z/(4*num*num))) / (1 + z*z/num);
665 let high = (avg + z*z/(2*num) + z * Math.sqrt(avg * (1.0 - avg) / num + z*z/(4*num*num))) / (1 + z*z/num);
667 // Fix the signs so that we don't get -0.00.
668 low = Math.max(low, 0.0);
678 // These can only happen once per point, but you get -1 and +1
679 // instead of 0 and +1. After we rewrite to 0 and 1, it's a binomial,
680 // and then we can rewrite back.
681 function make_efficiency_ci(points_won, points_completed, z)
683 let ci = make_binomial_ci(points_won, points_completed, z);
684 ci.val = 2.0 * ci.val - 1.0;
685 ci.lower_ci = 2.0 * ci.lower_ci - 1.0;
686 ci.upper_ci = 2.0 * ci.upper_ci - 1.0;
689 ci.desired = 0.0; // Desired = positive efficiency.
693 // Ds, throwaways and drops can happen multiple times per point,
694 // so they are Poisson distributed.
696 // Modified Wald (recommended by http://www.ine.pt/revstat/pdf/rs120203.pdf
697 // since our rates are definitely below 2 per point).
698 function make_poisson_ci(val, num, z, inverted)
700 let low = (val == 0) ? 0.0 : ((val - 0.5) - Math.sqrt(val - 0.5)) / num;
701 let high = (val == 0) ? -Math.log(0.025) / num : ((val + 0.5) + Math.sqrt(val + 0.5)) / num;
703 // Fix the signs so that we don't get -0.00.
704 low = Math.max(low, 0.0);
706 // The display range of 0 to 0.25 is fairly arbitrary. So is the desired 0.05 per point.
715 'inverted': inverted,
719 function make_table_general(players) {
722 let header = document.createElement('tr');
723 add_th(header, 'Player');
724 add_th(header, '+/-');
725 add_th(header, 'Soft +/-');
726 add_th(header, 'O efficiency');
727 add_th(header, 'D efficiency');
728 add_th(header, 'Points played');
732 for (const [q,p] of Object.entries(players)) {
733 if (q === 'globals') continue;
734 let row = document.createElement('tr');
735 let pm = p.goals + p.assists + p.hockey_assists + p.defenses - p.throwaways - p.drops - p.was_ds;
736 let soft_pm = p.offensive_soft_plus + p.defensive_soft_plus - p.offensive_soft_minus - p.defensive_soft_minus;
737 let o_efficiency = make_efficiency_ci(p.offensive_points_won, p.offensive_points_completed, z);
738 let d_efficiency = make_efficiency_ci(p.defensive_points_won, p.defensive_points_completed, z);
739 add_3cell(row, p.name, 'name'); // TODO: number?
740 add_3cell(row, pm > 0 ? ('+' + pm) : pm);
741 add_3cell(row, soft_pm > 0 ? ('+' + soft_pm) : soft_pm);
742 add_3cell_ci(row, o_efficiency);
743 add_3cell_ci(row, d_efficiency);
744 add_3cell(row, p.points_played);
749 let globals = players['globals'];
750 let o_efficiency = make_efficiency_ci(globals.offensive_points_won, globals.offensive_points_completed, z);
751 let d_efficiency = make_efficiency_ci(globals.defensive_points_won, globals.defensive_points_completed, z);
752 let row = document.createElement('tr');
756 add_3cell_ci(row, o_efficiency);
757 add_3cell_ci(row, d_efficiency);
758 add_3cell(row, globals.points_played);
764 function make_table_offense(players) {
767 let header = document.createElement('tr');
768 add_th(header, 'Player');
769 add_th(header, 'Goals');
770 add_th(header, 'Assists');
771 add_th(header, 'Hockey assists');
772 add_th(header, 'Throws');
773 add_th(header, 'Throwaways');
774 add_th(header, '%OK');
775 add_th(header, 'Catches');
776 add_th(header, 'Drops');
777 add_th(header, 'D-ed');
778 add_th(header, '%OK');
779 add_th(header, 'Soft +/-', 6);
788 for (const [q,p] of Object.entries(players)) {
789 if (q === 'globals') continue;
790 let throw_ok = make_binomial_ci(p.num_throws - p.throwaways, p.num_throws, z);
791 let catch_ok = make_binomial_ci(p.catches, p.catches + p.drops + p.was_ds, z);
793 throw_ok.format = 'percentage';
794 catch_ok.format = 'percentage';
796 // Desire at least 90% percentage. Fairly arbitrary.
797 throw_ok.desired = 0.9;
798 catch_ok.desired = 0.9;
800 let row = document.createElement('tr');
801 add_3cell(row, p.name, 'name'); // TODO: number?
802 add_3cell(row, p.goals);
803 add_3cell(row, p.assists);
804 add_3cell(row, p.hockey_assists);
805 add_3cell(row, p.num_throws);
806 add_3cell(row, p.throwaways);
807 add_3cell_ci(row, throw_ok);
808 add_3cell(row, p.catches);
809 add_3cell(row, p.drops);
810 add_3cell(row, p.was_ds);
811 add_3cell_ci(row, catch_ok);
812 add_3cell(row, '+' + p.offensive_soft_plus);
813 add_3cell(row, '-' + p.offensive_soft_minus);
816 num_throws += p.num_throws;
817 throwaways += p.throwaways;
818 catches += p.catches;
824 let throw_ok = make_binomial_ci(num_throws - throwaways, num_throws, z);
825 let catch_ok = make_binomial_ci(catches, catches + drops + was_ds, z);
826 throw_ok.format = 'percentage';
827 catch_ok.format = 'percentage';
828 throw_ok.desired = 0.9;
829 catch_ok.desired = 0.9;
831 let row = document.createElement('tr');
836 add_3cell(row, num_throws);
837 add_3cell(row, throwaways);
838 add_3cell_ci(row, throw_ok);
839 add_3cell(row, catches);
840 add_3cell(row, drops);
841 add_3cell(row, was_ds);
842 add_3cell_ci(row, catch_ok);
850 function make_table_defense(players) {
853 let header = document.createElement('tr');
854 add_th(header, 'Player');
855 add_th(header, 'Ds');
856 add_th(header, 'Pulls');
857 add_th(header, 'OOB pulls');
858 add_th(header, 'OOB%');
859 add_th(header, 'Avg. hang time (IB)');
860 add_th(header, 'Soft +/-', 6);
863 for (const [q,p] of Object.entries(players)) {
864 if (q === 'globals') continue;
866 for (const t of p.pull_times) {
869 let avg_time = 1e-3 * sum_time / (p.pulls - p.oob_pulls);
870 let oob_pct = 100 * p.oob_pulls / p.pulls;
872 let ci_oob = make_binomial_ci(p.oob_pulls, p.pulls, z);
873 ci_oob.format = 'percentage';
874 ci_oob.desired = 0.2; // Arbitrary.
875 ci_oob.inverted = true;
877 let row = document.createElement('tr');
878 add_3cell(row, p.name, 'name'); // TODO: number?
879 add_3cell(row, p.defenses);
880 add_3cell(row, p.pulls);
881 add_3cell(row, p.oob_pulls);
882 add_3cell_ci(row, ci_oob);
883 if (p.pulls > p.oob_pulls) {
884 add_3cell(row, avg_time.toFixed(1) + ' sec');
886 add_3cell(row, 'N/A');
888 add_3cell(row, '+' + p.defensive_soft_plus);
889 add_3cell(row, '-' + p.defensive_soft_minus);
895 function make_table_playing_time(players) {
898 let header = document.createElement('tr');
899 add_th(header, 'Player');
900 add_th(header, 'Points played');
901 add_th(header, 'Time played');
902 add_th(header, 'O time');
903 add_th(header, 'D time');
904 add_th(header, 'Time on field');
905 add_th(header, 'O points');
906 add_th(header, 'D points');
910 for (const [q,p] of Object.entries(players)) {
911 if (q === 'globals') continue;
912 let row = document.createElement('tr');
913 add_3cell(row, p.name, 'name'); // TODO: number?
914 add_3cell(row, p.points_played);
915 add_3cell(row, Math.floor(p.playing_time_ms / 60000) + ' min');
916 add_3cell(row, Math.floor(p.offensive_playing_time_ms / 60000) + ' min');
917 add_3cell(row, Math.floor(p.defensive_playing_time_ms / 60000) + ' min');
918 add_3cell(row, Math.floor(p.field_time_ms / 60000) + ' min');
919 add_3cell(row, p.offensive_points_completed);
920 add_3cell(row, p.defensive_points_completed);
925 let globals = players['globals'];
926 let row = document.createElement('tr');
928 add_3cell(row, globals.points_played);
929 add_3cell(row, Math.floor(globals.playing_time_ms / 60000) + ' min');
930 add_3cell(row, Math.floor(globals.offensive_playing_time_ms / 60000) + ' min');
931 add_3cell(row, Math.floor(globals.defensive_playing_time_ms / 60000) + ' min');
932 add_3cell(row, Math.floor(globals.field_time_ms / 60000) + ' min');
933 add_3cell(row, globals.offensive_points_completed);
934 add_3cell(row, globals.defensive_points_completed);
940 function make_table_per_point(players) {
943 let header = document.createElement('tr');
944 add_th(header, 'Player');
945 add_th(header, 'Goals');
946 add_th(header, 'Assists');
947 add_th(header, 'Hockey assists');
948 add_th(header, 'Ds');
949 add_th(header, 'Throwaways');
950 add_th(header, 'Recv. errors');
951 add_th(header, 'Touches');
957 let hockey_assists = 0;
960 let receiver_errors = 0;
962 for (const [q,p] of Object.entries(players)) {
963 if (q === 'globals') continue;
965 // Can only happen once per point, so these are binomials.
966 let ci_goals = make_binomial_ci(p.goals, p.points_played, z);
967 let ci_assists = make_binomial_ci(p.assists, p.points_played, z);
968 let ci_hockey_assists = make_binomial_ci(p.hockey_assists, p.points_played, z);
969 // Arbitrarily desire at least 10% (not everybody can score or assist).
970 ci_goals.desired = 0.1;
971 ci_assists.desired = 0.1;
972 ci_hockey_assists.desired = 0.1;
974 let row = document.createElement('tr');
975 add_3cell(row, p.name, 'name'); // TODO: number?
976 add_3cell_ci(row, ci_goals);
977 add_3cell_ci(row, ci_assists);
978 add_3cell_ci(row, ci_hockey_assists);
979 add_3cell_ci(row, make_poisson_ci(p.defenses, p.points_played, z));
980 add_3cell_ci(row, make_poisson_ci(p.throwaways, p.points_played, z, true));
981 add_3cell_ci(row, make_poisson_ci(p.drops + p.was_ds, p.points_played, z, true));
982 if (p.points_played > 0) {
983 add_3cell(row, p.touches == 0 ? 0 : (p.touches / p.points_played).toFixed(2));
985 add_3cell(row, 'N/A');
990 assists += p.assists;
991 hockey_assists += p.hockey_assists;
992 defenses += p.defenses;
993 throwaways += p.throwaways;
994 receiver_errors += p.drops + p.was_ds;
995 touches += p.touches;
999 let globals = players['globals'];
1000 let row = document.createElement('tr');
1002 if (globals.points_played > 0) {
1003 add_3cell_with_filler_ci(row, goals == 0 ? 0 : (goals / globals.points_played).toFixed(2));
1004 add_3cell_with_filler_ci(row, assists == 0 ? 0 : (assists / globals.points_played).toFixed(2));
1005 add_3cell_with_filler_ci(row, hockey_assists == 0 ? 0 : (hockey_assists / globals.points_played).toFixed(2));
1006 add_3cell_with_filler_ci(row, defenses == 0 ? 0 : (defenses / globals.points_played).toFixed(2));
1007 add_3cell_with_filler_ci(row, throwaways == 0 ? 0 : (throwaways / globals.points_played).toFixed(2));
1008 add_3cell_with_filler_ci(row, receiver_errors == 0 ? 0 : (receiver_errors / globals.points_played).toFixed(2));
1009 add_3cell(row, touches == 0 ? 0 : (touches / globals.points_played).toFixed(2));
1011 add_3cell_with_filler_ci(row, 'N/A');
1012 add_3cell_with_filler_ci(row, 'N/A');
1013 add_3cell_with_filler_ci(row, 'N/A');
1014 add_3cell_with_filler_ci(row, 'N/A');
1015 add_3cell_with_filler_ci(row, 'N/A');
1016 add_3cell_with_filler_ci(row, 'N/A');
1017 add_3cell(row, 'N/A');
1024 function open_filter_menu() {
1025 document.getElementById('filter-submenu').style.display = 'none';
1027 let menu = document.getElementById('filter-add-menu');
1028 menu.style.display = 'block';
1029 menu.replaceChildren();
1031 // Place the menu directly under the “click to add” label;
1032 // we don't anchor it since that label will move around
1033 // and the menu shouldn't.
1034 let rect = document.getElementById('filter-click-to-add').getBoundingClientRect();
1035 menu.style.left = rect.left + 'px';
1036 menu.style.top = (rect.bottom + 10) + 'px';
1038 add_menu_item(menu, 0, 'match', 'Match (any)');
1039 add_menu_item(menu, 1, 'player_any', 'Player on field (any)');
1040 add_menu_item(menu, 2, 'player_all', 'Player on field (all)');
1041 add_menu_item(menu, 3, 'formation_offense', 'Offense played (any)');
1042 add_menu_item(menu, 4, 'formation_defense', 'Defense played (any)');
1045 function add_menu_item(menu, menu_idx, filter_type, title) {
1046 let item = document.createElement('div');
1047 item.classList.add('option');
1048 item.appendChild(document.createTextNode(title));
1050 let arrow = document.createElement('div');
1051 arrow.classList.add('arrow');
1052 arrow.textContent = '▸';
1053 item.appendChild(arrow);
1055 menu.appendChild(item);
1057 item.addEventListener('click', (e) => { show_submenu(menu_idx, null, filter_type); });
1060 function show_submenu(menu_idx, pill, filter_type) {
1061 let submenu = document.getElementById('filter-submenu');
1063 const filter = find_filter(filter_type);
1066 if (filter_type === 'match') {
1067 for (const match of global_json['matches']) {
1069 'title': match['description'],
1070 'id': match['match_id']
1073 } else if (filter_type === 'player_any' || filter_type === 'player_all') {
1074 for (const player of global_json['players']) {
1076 'title': player['name'],
1077 'id': player['player_id']
1080 } else if (filter_type === 'formation_offense') {
1082 'title': '(None/unknown)',
1085 for (const formation of global_json['formations']) {
1086 if (formation['offense']) {
1088 'title': formation['name'],
1089 'id': formation['formation_id']
1093 } else if (filter_type === 'formation_defense') {
1095 'title': '(None/unknown)',
1098 for (const formation of global_json['formations']) {
1099 if (!formation['offense']) {
1101 'title': formation['name'],
1102 'id': formation['formation_id']
1108 for (const choice of choices) {
1109 let label = document.createElement('label');
1111 let subitem = document.createElement('div');
1112 subitem.classList.add('option');
1114 let check = document.createElement('input');
1115 check.setAttribute('type', 'checkbox');
1116 check.setAttribute('id', 'choice' + choice.id);
1117 if (filter !== null && filter.elements.has(choice.id)) {
1118 check.setAttribute('checked', 'checked');
1120 check.addEventListener('change', (e) => { checkbox_changed(e, filter_type, choice.id); });
1122 subitem.appendChild(check);
1123 subitem.appendChild(document.createTextNode(choice.title));
1125 label.appendChild(subitem);
1126 subitems.push(label);
1128 submenu.replaceChildren(...subitems);
1129 submenu.style.display = 'block';
1131 if (pill !== null) {
1132 let rect = pill.getBoundingClientRect();
1133 submenu.style.top = (rect.bottom + 10) + 'px';
1134 submenu.style.left = rect.left + 'px';
1136 // Position just outside the selected menu.
1137 let rect = document.getElementById('filter-add-menu').getBoundingClientRect();
1138 submenu.style.top = (rect.top + menu_idx * 35) + 'px';
1139 submenu.style.left = (rect.right - 1) + 'px';
1143 // Find the right filter, if it exists.
1144 function find_filter(filter_type) {
1145 for (let f of global_filters) {
1146 if (f.type === filter_type) {
1153 function checkbox_changed(e, filter_type, id) {
1154 let filter = find_filter(filter_type);
1155 if (e.target.checked) {
1156 // See if we must add a new filter to the list.
1157 if (filter === null) {
1159 'type': filter_type,
1160 'elements': new Set([ id ]),
1162 filter.pill = make_filter_pill(filter);
1163 global_filters.push(filter);
1164 document.getElementById('filters').appendChild(filter.pill);
1166 filter.elements.add(id);
1167 let new_pill = make_filter_pill(filter);
1168 document.getElementById('filters').replaceChild(new_pill, filter.pill);
1169 filter.pill = new_pill;
1172 filter.elements.delete(id);
1173 if (filter.elements.size === 0) {
1174 document.getElementById('filters').removeChild(filter.pill);
1175 global_filters = global_filters.filter(f => f !== filter);
1177 let new_pill = make_filter_pill(filter);
1178 document.getElementById('filters').replaceChild(new_pill, filter.pill);
1179 filter.pill = new_pill;
1183 process_matches(global_json, global_filters);
1186 function make_filter_pill(filter) {
1187 let pill = document.createElement('div');
1188 pill.classList.add('filter-pill');
1190 if (filter.type === 'match') {
1194 for (const match_id of filter.elements) {
1195 all_names.push(find_match(match_id)['description']);
1197 let common_prefix = find_common_prefix_of_all(all_names);
1198 if (common_prefix !== null) {
1199 text += common_prefix + '(';
1203 let sorted_match_id = Array.from(filter.elements).sort((a, b) => a - b);
1204 for (const match_id of sorted_match_id) {
1208 let desc = find_match(match_id)['description'];
1209 if (common_prefix === null) {
1212 text += desc.substr(common_prefix.length);
1217 if (common_prefix !== null) {
1220 } else if (filter.type === 'player_any') {
1221 text = 'Player (any): ';
1222 let sorted_players = Array.from(filter.elements).sort((a, b) => player_pos(a) - player_pos(b));
1224 for (const player_id of sorted_players) {
1228 text += find_player(player_id)['name'];
1231 } else if (filter.type === 'player_all') {
1233 let sorted_players = Array.from(filter.elements).sort((a, b) => player_pos(a) - player_pos(b));
1235 for (const player_id of sorted_players) {
1239 text += find_player(player_id)['name'];
1242 } else if (filter.type === 'formation_offense' || filter.type === 'formation_defense') {
1243 const offense = (filter.type === 'formation_offense');
1251 for (const formation_id of filter.elements) {
1252 all_names.push(find_formation(formation_id)['name']);
1254 let common_prefix = find_common_prefix_of_all(all_names);
1255 if (common_prefix !== null) {
1256 text += common_prefix + '(';
1260 let sorted_formation_id = Array.from(filter.elements).sort((a, b) => a - b);
1261 for (const formation_id of sorted_formation_id) {
1265 let desc = find_formation(formation_id)['name'];
1266 if (common_prefix === null) {
1269 text += desc.substr(common_prefix.length);
1274 if (common_prefix !== null) {
1279 let text_node = document.createElement('span');
1280 text_node.innerText = text;
1281 text_node.addEventListener('click', (e) => show_submenu(null, pill, filter.type));
1282 pill.appendChild(text_node);
1284 pill.appendChild(document.createTextNode(' '));
1286 let delete_node = document.createElement('span');
1287 delete_node.innerText = '✖';
1288 delete_node.addEventListener('click', (e) => {
1289 // Delete this filter entirely.
1290 document.getElementById('filters').removeChild(pill);
1291 global_filters = global_filters.filter(f => f !== filter);
1292 process_matches(global_json, global_filters);
1294 let add_menu = document.getElementById('filter-add-menu');
1295 let add_submenu = document.getElementById('filter-submenu');
1296 add_menu.style.display = 'none';
1297 add_submenu.style.display = 'none';
1299 pill.appendChild(delete_node);
1300 pill.style.cursor = 'pointer';
1305 function find_common_prefix(a, b) {
1307 for (let i = 0; i < Math.min(a.length, b.length); ++i) {
1308 if (a[i] === b[i]) {
1317 function find_common_prefix_of_all(values) {
1318 if (values.length < 2) {
1321 let common_prefix = null;
1322 for (const desc of values) {
1323 if (common_prefix === null) {
1324 common_prefix = desc;
1326 common_prefix = find_common_prefix(common_prefix, desc);
1329 if (common_prefix.length >= 3) {
1330 return common_prefix;
1336 function find_match(match_id) {
1337 for (const match of global_json['matches']) {
1338 if (match['match_id'] === match_id) {
1345 function find_formation(formation_id) {
1346 for (const formation of global_json['formations']) {
1347 if (formation['formation_id'] === formation_id) {
1354 function find_player(player_id) {
1355 for (const player of global_json['players']) {
1356 if (player['player_id'] === player_id) {
1363 function player_pos(player_id) {
1365 for (const player of global_json['players']) {
1366 if (player['player_id'] === player_id) {
1374 function keep_match(match_id, filters) {
1375 for (const filter of filters) {
1376 if (filter.type === 'match') {
1377 return filter.elements.has(match_id);
1383 function filter_passes(players, formations_used_this_point, filter) {
1384 if (filter.type === 'player_any') {
1385 for (const p of Array.from(filter.elements)) {
1386 if (players[p].on_field_since !== null) {
1391 } else if (filter.type === 'player_all') {
1392 for (const p of Array.from(filter.elements)) {
1393 if (players[p].on_field_since === null) {
1398 } else if (filter.type === 'formation_offense' || filter.type === 'formation_defense') {
1399 for (const f of Array.from(filter.elements)) {
1400 if (formations_used_this_point.has(f)) {
1409 function keep_event(players, formations_used_this_point, filters) {
1410 for (const filter of filters) {
1411 if (!filter_passes(players, formations_used_this_point, filter)) {
1418 // Heuristic: If we go at least ten seconds without the possession changing
1419 // or the operator specifying some other formation, we probably play the
1420 // same formation as the last point.
1421 function should_reuse_last_formation(events, t) {
1422 for (const e of events) {
1426 if (e.t > t + 10000) {
1429 const type = e.type;
1430 if (type === 'their_goal' || type === 'goal' ||
1431 type === 'set_defense' || type === 'set_offense' ||
1432 type === 'throwaway' || type === 'their_throwaway' ||
1433 type === 'drop' || type === 'defense' || type === 'interception' ||
1434 type === 'pull' || type === 'pull_landed' || type === 'pull_oob' || type === 'their_pull' ||
1435 type === 'formation_offense' || type === 'formation_defense') {
1442 function possibly_close_menu(e) {
1443 if (e.target.closest('#filter-click-to-add') === null &&
1444 e.target.closest('#filter-add-menu') === null &&
1445 e.target.closest('#filter-submenu') === null &&
1446 e.target.closest('.filter-pill') === null) {
1447 let add_menu = document.getElementById('filter-add-menu');
1448 let add_submenu = document.getElementById('filter-submenu');
1449 add_menu.style.display = 'none';
1450 add_submenu.style.display = 'none';