3 // No frameworks, no compilers, no npm, just JavaScript. :-)
6 let global_filters = {};
7 let next_filterset_id = 2; // Arbitrary IDs are fine as long as they never collide.
9 addEventListener('hashchange', () => { process_matches(global_json, global_filters); });
10 addEventListener('click', possibly_close_menu);
11 fetch('ultimate.json')
12 .then(response => response.json())
13 .then(response => { global_json = response; process_matches(global_json, global_filters); });
15 function format_time(t)
17 const ms = t % 1000 + '';
18 t = Math.floor(t / 1000);
19 const sec = t % 60 + '';
20 t = Math.floor(t / 60);
21 const min = t % 60 + '';
22 const hour = Math.floor(t / 60) + '';
23 return hour + ':' + min.padStart(2, '0') + ':' + sec.padStart(2, '0') + '.' + ms.padStart(3, '0');
26 function attribute_player_time(player, to, from, offense) {
28 if (player.on_field_since > from) {
29 // Player came in while play happened (without a stoppage!?).
30 delta_time = to - player.on_field_since;
32 delta_time = to - from;
34 player.playing_time_ms += delta_time;
35 if (offense === true) {
36 player.offensive_playing_time_ms += delta_time;
37 } else if (offense === false) {
38 player.defensive_playing_time_ms += delta_time;
42 function take_off_field(player, t, live_since, offense, keep) {
44 if (live_since === null) {
45 // Play isn't live, so nothing to do.
47 attribute_player_time(player, t, live_since, offense);
49 if (player.on_field_since !== null) { // Just a safeguard; out without in should never happen.
50 player.field_time_ms += t - player.on_field_since;
53 player.on_field_since = null;
56 function add_cell(tr, element_type, text) {
57 let element = document.createElement(element_type);
58 element.textContent = text;
59 tr.appendChild(element);
63 function add_th(tr, text, colspan) {
64 let element = document.createElement('th');
65 let link = document.createElement('a');
66 link.style.cursor = 'pointer';
67 link.addEventListener('click', (e) => {
69 process_matches(global_json, global_filters);
71 link.textContent = text;
72 element.appendChild(link);
73 tr.appendChild(element);
76 element.setAttribute('colspan', colspan);
78 element.setAttribute('colspan', '3');
83 function add_3cell(tr, text, cls) {
84 let p1 = add_cell(tr, 'td', '');
85 let element = add_cell(tr, 'td', text);
86 let p2 = add_cell(tr, 'td', '');
88 p1.classList.add('pad');
89 p2.classList.add('pad');
90 if (cls === undefined) {
91 element.classList.add('num');
93 element.classList.add(cls);
98 function add_3cell_with_filler_ci(tr, text, cls) {
99 let element = add_3cell(tr, text, cls);
101 let svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
102 svg.classList.add('fillerci');
103 svg.setAttribute('width', ci_width);
104 svg.setAttribute('height', ci_height);
105 element.appendChild(svg);
110 function add_3cell_ci(tr, ci) {
112 add_3cell_with_filler_ci(tr, 'N/A');
117 if (ci.format === 'percentage') {
118 text = (100 * ci.val).toFixed(0) + '%';
120 text = ci.val.toFixed(2);
122 let element = add_3cell(tr, text);
123 let to_x = (val) => { return ci_width * (val - ci.min) / (ci.max - ci.min); };
126 let svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
127 if (ci.inverted === true) {
128 svg.classList.add('invertedci');
130 svg.classList.add('ci');
132 svg.setAttribute('width', ci_width);
133 svg.setAttribute('height', ci_height);
135 // The good (green) and red (bad) ranges.
136 let s0 = document.createElementNS('http://www.w3.org/2000/svg', 'rect');
137 s0.classList.add('range');
138 s0.classList.add('s0');
139 s0.setAttribute('width', to_x(ci.desired));
140 s0.setAttribute('height', ci_height);
141 s0.setAttribute('x', '0');
143 let s1 = document.createElementNS('http://www.w3.org/2000/svg', 'rect');
144 s1.classList.add('range');
145 s1.classList.add('s1');
146 s1.setAttribute('width', ci_width - to_x(ci.desired));
147 s1.setAttribute('height', ci_height);
148 s1.setAttribute('x', to_x(ci.desired));
151 let bar = document.createElementNS('http://www.w3.org/2000/svg', 'rect');
152 bar.classList.add('bar');
153 bar.setAttribute('width', to_x(ci.upper_ci) - to_x(ci.lower_ci));
154 bar.setAttribute('height', ci_height / 3);
155 bar.setAttribute('x', to_x(ci.lower_ci));
156 bar.setAttribute('y', ci_height / 3);
158 // Marker line for average.
159 let marker = document.createElementNS('http://www.w3.org/2000/svg', 'line');
160 marker.classList.add('marker');
161 marker.setAttribute('x1', to_x(ci.val));
162 marker.setAttribute('x2', to_x(ci.val));
163 marker.setAttribute('y1', ci_height / 6);
164 marker.setAttribute('y2', ci_height * 5 / 6);
168 svg.appendChild(bar);
169 svg.appendChild(marker);
171 element.appendChild(svg);
174 function calc_stats(json, filters) {
176 for (const player of json['players']) {
177 players[player['player_id']] = {
178 'name': player['name'],
179 'gender': player['gender'],
180 'number': player['number'],
196 'playing_time_ms': 0,
197 'offensive_playing_time_ms': 0,
198 'defensive_playing_time_ms': 0,
202 'offensive_points_completed': 0,
203 'offensive_points_won': 0,
204 'defensive_points_completed': 0,
205 'defensive_points_won': 0,
207 'offensive_soft_plus': 0,
208 'offensive_soft_minus': 0,
209 'defensive_soft_plus': 0,
210 'defensive_soft_minus': 0,
217 'last_point_seen': null,
218 'on_field_since': null,
223 players['globals'] = {
225 'playing_time_ms': 0,
226 'offensive_playing_time_ms': 0,
227 'defensive_playing_time_ms': 0,
230 'offensive_points_completed': 0,
231 'offensive_points_won': 0,
232 'defensive_points_completed': 0,
233 'defensive_points_won': 0,
235 let globals = players['globals'];
237 for (const match of json['matches']) {
238 if (!keep_match(match['match_id'], filters)) {
244 let between_points = true;
246 let handler_got_by_interception = false; // Only relevant if handler !== null.
247 let prev_handler = null;
248 let live_since = null;
249 let offense = null; // True/false/null (unknown).
251 let pull_started = null;
252 let last_pull_was_ours = null; // Effectively whether we're playing an O or D point (not affected by turnovers).
254 let game_started = null;
255 let last_goal = null;
256 let current_predominant_gender = null;
257 let current_num_players_on_field = null;
259 // The last used formations of the given kind, if any; they may be reused
260 // when the point starts, if nothing else is set.
261 let last_offensive_formation = null;
262 let last_defensive_formation = null;
264 // Formations we've set, but haven't really had the chance to use yet
265 // (e.g., we get a “use zone defense” event while we're still on offense).
266 let pending_offensive_formation = null;
267 let pending_defensive_formation = null;
269 // Formations that we have played at least once this point, after all
270 // heuristics and similar.
271 let formations_used_this_point = new Set();
273 for (const [q,p] of Object.entries(players)) {
274 p.on_field_since = null;
275 p.last_point_seen = null;
277 for (const e of match['events']) {
279 let type = e['type'];
280 let p = players[e['player']];
283 let keep = keep_event(players, formations_used_this_point, last_pull_was_ours, filters);
284 if (type === 'in' && p.on_field_since === null) {
285 p.on_field_since = t;
286 if (!keep && keep_event(players, formations_used_this_point, last_pull_was_ours, filters)) {
287 // A player needed for the filters went onto the field,
288 // so pretend people walked on right now (to start their
290 for (const [q,p2] of Object.entries(players)) {
291 if (p2.on_field_since !== null) {
292 p2.on_field_since = t;
296 } else if (type === 'out') {
297 take_off_field(p, t, live_since, offense, keep);
298 if (keep && !keep_event(players, formations_used_this_point, last_pull_was_ours, filters)) {
299 // A player needed for the filters went off the field,
300 // so we need to attribute time for all the others.
301 // Pretend they walked off and then immediately on again.
303 // TODO: We also need to take care of this to get the globals right.
304 for (const [q,p2] of Object.entries(players)) {
305 if (p2.on_field_since !== null) {
306 take_off_field(p2, t, live_since, offense, keep);
307 p2.on_field_since = t;
313 keep = keep_event(players, formations_used_this_point, last_pull_was_ours, filters); // Recompute after in/out.
315 if (match['gender_rule_a']) {
316 if (type === 'restart' && !between_points) {
317 let predominant_gender = find_predominant_gender(players);
318 let num_players_on_field = find_num_players_on_field(players);
319 if (predominant_gender !== current_predominant_gender && current_predominant_gender !== null) {
320 console.log(match['description'] + ' ' + format_time(t) + ': Stoppage changed predominant gender from ' + current_predominant_gender + ' to ' + predominant_gender);
322 if (num_players_on_field !== current_num_players_on_field && current_num_players_on_field !== null) {
323 console.log(match['description'] + ' ' + format_time(t) + ': Stoppage changed number of players on field from ' + current_num_players_on_field + ' to ' + num_players_on_field);
325 current_predominant_gender = predominant_gender;
326 current_num_players_on_field = num_players_on_field;
327 } else if (type === 'pull' || type === 'their_pull') {
328 let predominant_gender = find_predominant_gender(players);
329 let num_players_on_field = find_num_players_on_field(players);
330 let changed = (predominant_gender !== current_predominant_gender);
331 if (point_num !== 0) {
332 let should_change = (point_num % 4 == 1 || point_num % 4 == 3); // ABBA changes on 1 and 3.
333 if (changed && !should_change) {
334 console.log(match['description'] + ' ' + format_time(t) + ': Gender ratio should have stayed the same, changed to predominance of ' + predominant_gender);
335 } else if (!changed && should_change) {
336 console.log(match['description'] + ' ' + format_time(t) + ': Gender ratio should have changed, remained predominantly ' + predominant_gender);
338 if (num_players_on_field !== current_num_players_on_field && current_num_players_on_field !== null) {
339 console.log(match['description'] + ' ' + format_time(t) + ': Number of players on field changed from ' + current_num_players_on_field + ' to ' + num_players_on_field);
342 current_predominant_gender = predominant_gender;
343 current_num_players_on_field = num_players_on_field;
346 if (match['gender_pull_rule']) {
347 if (type === 'pull') {
348 if (current_predominant_gender !== null &&
349 p.gender !== current_predominant_gender) {
350 console.log(match['description'] + ' ' + format_time(t) + ': ' + p.name + ' pulled, should have been ' + current_predominant_gender);
355 // Liveness management
356 if (type === 'pull' || type === 'their_pull' || type === 'restart') {
358 between_points = false;
359 } else if (type === 'catch' && last_pull_was_ours === null) {
360 // Someone forgot to add the pull, so we'll need to wing it.
361 console.log(match['description'] + ' ' + format_time(t) + ': Missing pull on ' + our_score + '\u2013' + their_score + '; pretending to have one.');
363 last_pull_was_ours = !offense;
364 between_points = false;
365 } else if (type === 'goal' || type === 'their_goal' || type === 'stoppage') {
366 for (const [q,p] of Object.entries(players)) {
367 if (p.on_field_since === null) {
370 if (type !== 'stoppage' && p.last_point_seen !== point_num) {
372 // In case the player did nothing this point,
373 // not even subbing in.
374 p.last_point_seen = point_num;
378 if (keep) attribute_player_time(p, t, live_since, offense);
380 if (type !== 'stoppage') {
382 if (last_pull_was_ours === true) { // D point.
383 ++p.defensive_points_completed;
384 if (type === 'goal') {
385 ++p.defensive_points_won;
387 } else if (last_pull_was_ours === false) { // O point.
388 ++p.offensive_points_completed;
389 if (type === 'goal') {
390 ++p.offensive_points_won;
398 if (type !== 'stoppage') {
400 ++globals.points_played;
401 if (last_pull_was_ours === true) { // D point.
402 ++globals.defensive_points_completed;
403 if (type === 'goal') {
404 ++globals.defensive_points_won;
406 } else if (last_pull_was_ours === false) { // O point.
407 ++globals.offensive_points_completed;
408 if (type === 'goal') {
409 ++globals.offensive_points_won;
413 if (live_since !== null) {
414 globals.playing_time_ms += t - live_since;
415 if (offense === true) {
416 globals.offensive_playing_time_ms += t - live_since;
417 } else if (offense === false) {
418 globals.defensive_playing_time_ms += t - live_since;
427 if (type === 'goal') {
429 between_points = true;
430 } else if (type === 'their_goal') {
432 between_points = true;
435 // Point count management
436 if (p !== undefined && type !== 'out' && p.last_point_seen !== point_num) {
438 p.last_point_seen = point_num;
442 if (type === 'goal' || type === 'their_goal') {
446 if (type !== 'out' && game_started === null) {
451 if (type === 'pull') {
452 puller = e['player'];
455 } else if (type === 'in' || type === 'out' || type === 'stoppage' || type === 'restart' || type === 'unknown' || type === 'set_defense' || type === 'set_offense') {
456 // No effect on pull.
457 } else if (type === 'pull_landed' && puller !== null) {
458 if (keep) players[puller].pull_times.push(t - pull_started);
459 } else if (type === 'pull_oob' && puller !== null) {
460 if (keep) ++players[puller].oob_pulls;
462 // Not pulling (if there was one, we never recorded its outcome, but still count it).
463 puller = pull_started = null;
466 // Offense/defense management
467 let last_offense = offense;
468 if (type === 'set_defense' || type === 'goal' || type === 'throwaway' || type === 'drop' || type === 'was_d' || type === 'stallout') {
470 } else if (type === 'set_offense' || type === 'their_goal' || type === 'their_throwaway' || type === 'defense' || type === 'interception') {
473 if (last_offense !== offense && live_since !== null) {
474 // Switched offense/defense status, so attribute this drive as needed,
475 // and update live_since to take that into account.
477 for (const [q,p] of Object.entries(players)) {
478 if (p.on_field_since === null) {
481 attribute_player_time(p, t, live_since, last_offense);
483 globals.playing_time_ms += t - live_since;
484 if (offense === true) {
485 globals.offensive_playing_time_ms += t - live_since;
486 } else if (offense === false) {
487 globals.defensive_playing_time_ms += t - live_since;
493 if (type === 'pull') {
494 last_pull_was_ours = true;
495 } else if (type === 'their_pull') {
496 last_pull_was_ours = false;
497 } else if (type === 'set_offense' && last_pull_was_ours === null) {
498 // set_offense could either be “changed to offense for some reason
499 // we could not express”, or “we started in the middle of a point,
500 // and we are offense”. We assume that if we already saw the pull,
501 // it's the former, and if not, it's the latter; thus, the === null
502 // test above. (It could also be “we set offense before the pull,
503 // so that we get the right button enabled”, in which case it will
504 // be overwritten by the next pull/their_pull event anyway.)
505 last_pull_was_ours = false;
506 } else if (type === 'set_defense' && last_pull_was_ours === null) {
508 last_pull_was_ours = true;
509 } else if (type === 'goal' || type === 'their_goal') {
510 last_pull_was_ours = null;
513 // Formation management
514 if (type === 'formation_offense' || type === 'formation_defense') {
515 let id = e.formation === null ? 0 : e.formation;
516 let for_offense = (type === 'formation_offense');
517 if (offense === for_offense) {
518 formations_used_this_point.add(id);
519 } else if (for_offense) {
520 pending_offensive_formation = id;
522 pending_defensive_formation = id;
525 last_offensive_formation = id;
527 last_defensive_formation = id;
529 } else if (last_offense !== offense) {
530 if (offense === true && pending_offensive_formation !== null) {
531 formations_used_this_point.add(pending_offensive_formation);
532 pending_offensive_formation = null;
533 } else if (offense === false && pending_defensive_formation !== null) {
534 formations_used_this_point.add(pending_defensive_formation);
535 pending_defensive_formation = null;
536 } else if (offense === true && last_defensive_formation !== null) {
537 if (should_reuse_last_formation(match['events'], t)) {
538 formations_used_this_point.add(last_defensive_formation);
540 } else if (offense === false && last_offensive_formation !== null) {
541 if (should_reuse_last_formation(match['events'], t)) {
542 formations_used_this_point.add(last_offensive_formation);
547 if (type !== 'out' && type !== 'in' && p !== undefined && p.on_field_since === null) {
548 console.log(match['description'] + ' ' + format_time(t) + ': Event “' + type + '” on subbed-out player ' + p.name);
550 if (type === 'catch' && handler !== null && players[handler].on_field_since === null) {
551 // The handler subbed out and was replaced with another handler,
552 // so this wasn't a pass.
557 if (type === 'goal' && handler === e['player'] && handler_got_by_interception) {
558 // Self-pass to goal after an interception; this is not a real pass,
559 // just how we represent a Callahan right now -- so don't
560 // count the throw, any assists or similar.
562 // It's an open question how we should handle a self-pass that is
563 // _not_ after an interception, or a self-pass that's not a goal.
564 // (It must mean we tipped off someone.) We'll count it as a regular one
565 // for the time being, although it will make hockey assists weird.
570 handler = prev_handler = null;
571 } else if (type === 'catch' || type === 'goal') {
572 if (handler !== null) {
574 ++players[handler].num_throws;
579 if (keep) ++p.touches;
580 if (type === 'goal') {
582 if (prev_handler !== null) {
583 ++players[prev_handler].hockey_assists;
585 if (handler !== null) {
586 ++players[handler].assists;
590 handler = prev_handler = null;
592 // Update hold history.
593 prev_handler = handler;
594 handler = e['player'];
595 handler_got_by_interception = false;
597 } else if (type === 'throwaway') {
602 handler = prev_handler = null;
603 } else if (type === 'drop') {
605 handler = prev_handler = null;
606 } else if (type === 'stallout') {
607 if (keep) ++p.stallouts;
608 handler = prev_handler = null;
609 } else if (type === 'was_d') {
610 if (keep) ++p.was_ds;
611 handler = prev_handler = null;
612 } else if (type === 'defense') {
613 if (keep) ++p.defenses;
614 } else if (type === 'interception') {
621 handler = e['player'];
622 handler_got_by_interception = true;
623 } else if (type === 'offensive_soft_plus' || type === 'offensive_soft_minus' || type === 'defensive_soft_plus' || type === 'defensive_soft_minus') {
625 } else if (type !== 'in' && type !== 'out' && type !== 'pull' &&
626 type !== 'their_goal' && type !== 'stoppage' && type !== 'restart' && type !== 'unknown' &&
627 type !== 'set_defense' && type !== 'goal' && type !== 'throwaway' &&
628 type !== 'drop' && type !== 'was_d' && type !== 'stallout' && type !== 'set_offense' && type !== 'their_goal' &&
629 type !== 'pull' && type !== 'pull_landed' && type !== 'pull_oob' && type !== 'their_pull' &&
630 type !== 'their_throwaway' && type !== 'defense' && type !== 'interception' &&
631 type !== 'formation_offense' && type !== 'formation_defense') {
632 console.log(format_time(t) + ": Unknown event “" + e + "”");
635 if (type === 'goal' || type === 'their_goal') {
636 formations_used_this_point.clear();
640 // Add field time for all players still left at match end.
641 const keep = keep_event(players, formations_used_this_point, last_pull_was_ours, filters);
643 for (const [q,p] of Object.entries(players)) {
644 if (p.on_field_since !== null && last_goal !== null) {
645 p.field_time_ms += last_goal - p.on_field_since;
648 if (game_started !== null && last_goal !== null) {
649 globals.field_time_ms += last_goal - game_started;
651 if (live_since !== null && last_goal !== null) {
652 globals.playing_time_ms += last_goal - live_since;
653 if (offense === true) {
654 globals.offensive_playing_time_ms += last_goal - live_since;
655 } else if (offense === false) {
656 globals.defensive_playing_time_ms += last_goal - live_since;
664 function process_matches(json, filtersets) {
665 let chosen_category = get_chosen_category();
666 write_main_menu(chosen_category);
668 let filterset_values = Object.values(filtersets);
669 if (filterset_values.length === 0) {
670 filterset_values = [[]];
674 for (const filter of filterset_values) {
675 let players = calc_stats(json, filter);
677 if (chosen_category === 'general') {
678 rows = make_table_general(players);
679 } else if (chosen_category === 'offense') {
680 rows = make_table_offense(players);
681 } else if (chosen_category === 'defense') {
682 rows = make_table_defense(players);
683 } else if (chosen_category === 'playing_time') {
684 rows = make_table_playing_time(players);
685 } else if (chosen_category === 'per_point') {
686 rows = make_table_per_point(players);
691 let merged_rows = [];
692 if (filterset_values.length > 1) {
693 for (let i = 0; i < rowsets.length; ++i) {
694 let marker = make_filter_marker(filterset_values[i]);
696 // Make filter header.
697 let tr = rowsets[i][0];
698 let th = document.createElement('th');
700 tr.insertBefore(th, tr.firstChild);
702 for (let j = 1; j < rowsets[i].length; ++j) {
703 let tr = rowsets[i][j];
705 // Remove the name for cleanness.
707 tr.firstChild.nextSibling.textContent = '';
711 tr.style.borderTop = '0px';
713 if (i != rowsets.length - 1) {
714 tr.style.borderBottom = '0px';
717 // Make filter marker.
718 let td = document.createElement('td');
719 td.textContent = marker;
720 td.classList.add('filtermarker');
721 tr.insertBefore(td, tr.firstChild);
725 for (let i = 0; i < rowsets[0].length; ++i) {
726 for (let j = 0; j < rowsets.length; ++j) {
727 merged_rows.push(rowsets[j][i]);
728 if (i === 0) break; // Don't merge the headings.
732 merged_rows = rowsets[0];
734 document.getElementById('stats').replaceChildren(...merged_rows);
737 function get_chosen_category() {
738 if (window.location.hash === '#offense') {
740 } else if (window.location.hash === '#defense') {
742 } else if (window.location.hash === '#playing_time') {
743 return 'playing_time';
744 } else if (window.location.hash === '#per_point') {
751 function write_main_menu(chosen_category) {
754 ['general', 'General'],
755 ['offense', 'Offense'],
756 ['defense', 'Defense'],
757 ['playing_time', 'Playing time'],
758 ['per_point', 'Per point']
760 for (const [id, title] of categories) {
761 if (chosen_category === id) {
762 let span = document.createElement('span');
763 span.innerText = title;
766 let a = document.createElement('a');
767 a.appendChild(document.createTextNode(title));
768 a.setAttribute('href', '#' + id);
773 document.getElementById('mainmenu').replaceChildren(...elems);
776 // https://en.wikipedia.org/wiki/1.96#History
779 const ci_width = 100;
780 const ci_height = 20;
782 function make_binomial_ci(val, num, z) {
785 // https://en.wikipedia.org/wiki/Binomial_proportion_confidence_interval#Wilson_score_interval
786 let low = (avg + z*z/(2*num) - z * Math.sqrt(avg * (1.0 - avg) / num + z*z/(4*num*num))) / (1 + z*z/num);
787 let high = (avg + z*z/(2*num) + z * Math.sqrt(avg * (1.0 - avg) / num + z*z/(4*num*num))) / (1 + z*z/num);
789 // Fix the signs so that we don't get -0.00.
790 low = Math.max(low, 0.0);
800 // These can only happen once per point, but you get -1 and +1
801 // instead of 0 and +1. After we rewrite to 0 and 1, it's a binomial,
802 // and then we can rewrite back.
803 function make_efficiency_ci(points_won, points_completed, z)
805 let ci = make_binomial_ci(points_won, points_completed, z);
806 ci.val = 2.0 * ci.val - 1.0;
807 ci.lower_ci = 2.0 * ci.lower_ci - 1.0;
808 ci.upper_ci = 2.0 * ci.upper_ci - 1.0;
811 ci.desired = 0.0; // Desired = positive efficiency.
815 // Ds, throwaways and drops can happen multiple times per point,
816 // so they are Poisson distributed.
818 // Modified Wald (recommended by http://www.ine.pt/revstat/pdf/rs120203.pdf
819 // since our rates are definitely below 2 per point).
820 function make_poisson_ci(val, num, z, inverted)
822 let low = (val == 0) ? 0.0 : ((val - 0.5) - Math.sqrt(val - 0.5)) / num;
823 let high = (val == 0) ? -Math.log(0.025) / num : ((val + 0.5) + Math.sqrt(val + 0.5)) / num;
825 // Fix the signs so that we don't get -0.00.
826 low = Math.max(low, 0.0);
828 // The display range of 0 to 0.5 is fairly arbitrary. So is the desired 0.05 per point.
837 'inverted': inverted,
841 function make_table_general(players) {
844 let header = document.createElement('tr');
845 add_th(header, 'Player');
846 add_th(header, '+/-');
847 add_th(header, 'Soft +/-');
848 add_th(header, 'O efficiency');
849 add_th(header, 'D efficiency');
850 add_th(header, 'Points played');
854 for (const [q,p] of get_sorted_players(players)) {
855 if (q === 'globals') continue;
856 let row = document.createElement('tr');
857 let pm = p.goals + p.assists + p.hockey_assists + p.defenses - p.throwaways - p.drops - p.was_ds - p.stallouts;
858 let soft_pm = p.offensive_soft_plus + p.defensive_soft_plus - p.offensive_soft_minus - p.defensive_soft_minus;
859 let o_efficiency = make_efficiency_ci(p.offensive_points_won, p.offensive_points_completed, z);
860 let d_efficiency = make_efficiency_ci(p.defensive_points_won, p.defensive_points_completed, z);
861 let name = add_3cell(row, p.name, 'name'); // TODO: number?
862 add_3cell(row, pm > 0 ? ('+' + pm) : pm);
863 add_3cell(row, soft_pm > 0 ? ('+' + soft_pm) : soft_pm);
864 add_3cell_ci(row, o_efficiency);
865 add_3cell_ci(row, d_efficiency);
866 add_3cell(row, p.points_played);
867 row.dataset.player = q;
872 let globals = players['globals'];
873 let o_efficiency = make_efficiency_ci(globals.offensive_points_won, globals.offensive_points_completed, z);
874 let d_efficiency = make_efficiency_ci(globals.defensive_points_won, globals.defensive_points_completed, z);
875 let row = document.createElement('tr');
879 add_3cell_ci(row, o_efficiency);
880 add_3cell_ci(row, d_efficiency);
881 add_3cell(row, globals.points_played);
887 function make_table_offense(players) {
890 let header = document.createElement('tr');
891 add_th(header, 'Player');
892 add_th(header, 'Goals');
893 add_th(header, 'Assists');
894 add_th(header, 'Hockey assists');
895 add_th(header, 'Throws');
896 add_th(header, 'Throwaways');
897 add_th(header, '%OK');
898 add_th(header, 'Catches');
899 add_th(header, 'Drops');
900 add_th(header, 'D-ed');
901 add_th(header, '%OK');
902 add_th(header, 'Stalls');
903 add_th(header, 'Soft +/-', 6);
914 for (const [q,p] of get_sorted_players(players)) {
915 if (q === 'globals') continue;
916 let throw_ok = make_binomial_ci(p.num_throws - p.throwaways, p.num_throws, z);
917 let catch_ok = make_binomial_ci(p.catches, p.catches + p.drops + p.was_ds, z);
919 throw_ok.format = 'percentage';
920 catch_ok.format = 'percentage';
922 // Desire at least 90% percentage. Fairly arbitrary.
923 throw_ok.desired = 0.9;
924 catch_ok.desired = 0.9;
926 let row = document.createElement('tr');
927 add_3cell(row, p.name, 'name'); // TODO: number?
928 add_3cell(row, p.goals);
929 add_3cell(row, p.assists);
930 add_3cell(row, p.hockey_assists);
931 add_3cell(row, p.num_throws);
932 add_3cell(row, p.throwaways);
933 add_3cell_ci(row, throw_ok);
934 add_3cell(row, p.catches);
935 add_3cell(row, p.drops);
936 add_3cell(row, p.was_ds);
937 add_3cell_ci(row, catch_ok);
938 add_3cell(row, p.stallouts);
939 add_3cell(row, '+' + p.offensive_soft_plus);
940 add_3cell(row, '-' + p.offensive_soft_minus);
941 row.dataset.player = q;
945 num_throws += p.num_throws;
946 throwaways += p.throwaways;
947 catches += p.catches;
950 stallouts += p.stallouts;
954 let throw_ok = make_binomial_ci(num_throws - throwaways, num_throws, z);
955 let catch_ok = make_binomial_ci(catches, catches + drops + was_ds, z);
956 throw_ok.format = 'percentage';
957 catch_ok.format = 'percentage';
958 throw_ok.desired = 0.9;
959 catch_ok.desired = 0.9;
961 let row = document.createElement('tr');
963 add_3cell(row, goals);
966 add_3cell(row, num_throws);
967 add_3cell(row, throwaways);
968 add_3cell_ci(row, throw_ok);
969 add_3cell(row, catches);
970 add_3cell(row, drops);
971 add_3cell(row, was_ds);
972 add_3cell_ci(row, catch_ok);
973 add_3cell(row, stallouts);
981 function make_table_defense(players) {
984 let header = document.createElement('tr');
985 add_th(header, 'Player');
986 add_th(header, 'Ds');
987 add_th(header, 'Pulls');
988 add_th(header, 'OOB pulls');
989 add_th(header, 'OOB%');
990 add_th(header, 'Avg. hang time (IB)');
991 add_th(header, 'Callahans');
992 add_th(header, 'Soft +/-', 6);
999 let sum_sum_time = 0;
1002 for (const [q,p] of get_sorted_players(players)) {
1003 if (q === 'globals') continue;
1005 for (const t of p.pull_times) {
1008 let avg_time = 1e-3 * sum_time / (p.pulls - p.oob_pulls);
1009 let oob_pct = 100 * p.oob_pulls / p.pulls;
1011 let ci_oob = make_binomial_ci(p.oob_pulls, p.pulls, z);
1012 ci_oob.format = 'percentage';
1013 ci_oob.desired = 0.2; // Arbitrary.
1014 ci_oob.inverted = true;
1016 let row = document.createElement('tr');
1017 add_3cell(row, p.name, 'name'); // TODO: number?
1018 add_3cell(row, p.defenses);
1019 add_3cell(row, p.pulls);
1020 add_3cell(row, p.oob_pulls);
1021 add_3cell_ci(row, ci_oob);
1022 if (p.pulls > p.oob_pulls) {
1023 add_3cell(row, avg_time.toFixed(1) + ' sec');
1025 add_3cell(row, 'N/A');
1027 add_3cell(row, p.callahans);
1028 add_3cell(row, '+' + p.defensive_soft_plus);
1029 add_3cell(row, '-' + p.defensive_soft_minus);
1030 row.dataset.player = q;
1033 defenses += p.defenses;
1035 oob_pulls += p.oob_pulls;
1036 sum_sum_time += sum_time;
1037 callahans += p.callahans;
1041 let ci_oob = make_binomial_ci(oob_pulls, pulls, z);
1042 ci_oob.format = 'percentage';
1043 ci_oob.desired = 0.2; // Arbitrary.
1044 ci_oob.inverted = true;
1046 let avg_time = 1e-3 * sum_sum_time / (pulls - oob_pulls);
1047 let oob_pct = 100 * oob_pulls / pulls;
1049 let row = document.createElement('tr');
1051 add_3cell(row, defenses);
1052 add_3cell(row, pulls);
1053 add_3cell(row, oob_pulls);
1054 add_3cell_ci(row, ci_oob);
1055 if (pulls > oob_pulls) {
1056 add_3cell(row, avg_time.toFixed(1) + ' sec');
1058 add_3cell(row, 'N/A');
1060 add_3cell(row, callahans);
1068 function make_table_playing_time(players) {
1071 let header = document.createElement('tr');
1072 add_th(header, 'Player');
1073 add_th(header, 'Points played');
1074 add_th(header, 'Time played');
1075 add_th(header, 'O time');
1076 add_th(header, 'D time');
1077 add_th(header, 'Time on field');
1078 add_th(header, 'O points');
1079 add_th(header, 'D points');
1083 for (const [q,p] of get_sorted_players(players)) {
1084 if (q === 'globals') continue;
1085 let row = document.createElement('tr');
1086 add_3cell(row, p.name, 'name'); // TODO: number?
1087 add_3cell(row, p.points_played);
1088 add_3cell(row, Math.floor(p.playing_time_ms / 60000) + ' min');
1089 add_3cell(row, Math.floor(p.offensive_playing_time_ms / 60000) + ' min');
1090 add_3cell(row, Math.floor(p.defensive_playing_time_ms / 60000) + ' min');
1091 add_3cell(row, Math.floor(p.field_time_ms / 60000) + ' min');
1092 add_3cell(row, p.offensive_points_completed);
1093 add_3cell(row, p.defensive_points_completed);
1094 row.dataset.player = q;
1099 let globals = players['globals'];
1100 let row = document.createElement('tr');
1102 add_3cell(row, globals.points_played);
1103 add_3cell(row, Math.floor(globals.playing_time_ms / 60000) + ' min');
1104 add_3cell(row, Math.floor(globals.offensive_playing_time_ms / 60000) + ' min');
1105 add_3cell(row, Math.floor(globals.defensive_playing_time_ms / 60000) + ' min');
1106 add_3cell(row, Math.floor(globals.field_time_ms / 60000) + ' min');
1107 add_3cell(row, globals.offensive_points_completed);
1108 add_3cell(row, globals.defensive_points_completed);
1114 function make_table_per_point(players) {
1117 let header = document.createElement('tr');
1118 add_th(header, 'Player');
1119 add_th(header, 'Goals');
1120 add_th(header, 'Assists');
1121 add_th(header, 'Hockey assists');
1122 add_th(header, 'Ds');
1123 add_th(header, 'Throwaways');
1124 add_th(header, 'Recv. errors');
1125 add_th(header, 'Touches');
1131 let hockey_assists = 0;
1134 let receiver_errors = 0;
1136 for (const [q,p] of get_sorted_players(players)) {
1137 if (q === 'globals') continue;
1139 // Can only happen once per point, so these are binomials.
1140 let ci_goals = make_binomial_ci(p.goals, p.points_played, z);
1141 let ci_assists = make_binomial_ci(p.assists, p.points_played, z);
1142 let ci_hockey_assists = make_binomial_ci(p.hockey_assists, p.points_played, z);
1143 // Arbitrarily desire at least 10% (not everybody can score or assist).
1144 ci_goals.desired = 0.1;
1145 ci_assists.desired = 0.1;
1146 ci_hockey_assists.desired = 0.1;
1148 let row = document.createElement('tr');
1149 add_3cell(row, p.name, 'name'); // TODO: number?
1150 add_3cell_ci(row, ci_goals);
1151 add_3cell_ci(row, ci_assists);
1152 add_3cell_ci(row, ci_hockey_assists);
1153 add_3cell_ci(row, make_poisson_ci(p.defenses, p.points_played, z));
1154 add_3cell_ci(row, make_poisson_ci(p.throwaways, p.points_played, z, true));
1155 add_3cell_ci(row, make_poisson_ci(p.drops + p.was_ds, p.points_played, z, true));
1156 if (p.points_played > 0) {
1157 add_3cell(row, p.touches == 0 ? 0 : (p.touches / p.points_played).toFixed(2));
1159 add_3cell(row, 'N/A');
1161 row.dataset.player = q;
1165 assists += p.assists;
1166 hockey_assists += p.hockey_assists;
1167 defenses += p.defenses;
1168 throwaways += p.throwaways;
1169 receiver_errors += p.drops + p.was_ds;
1170 touches += p.touches;
1174 let globals = players['globals'];
1175 let row = document.createElement('tr');
1177 if (globals.points_played > 0) {
1178 let ci_goals = make_binomial_ci(goals, globals.points_played, z);
1179 let ci_assists = make_binomial_ci(assists, globals.points_played, z);
1180 let ci_hockey_assists = make_binomial_ci(hockey_assists, globals.points_played, z);
1181 ci_goals.desired = 0.5;
1182 ci_assists.desired = 0.5;
1183 ci_hockey_assists.desired = 0.5;
1185 add_3cell_ci(row, ci_goals);
1186 add_3cell_ci(row, ci_assists);
1187 add_3cell_ci(row, ci_hockey_assists);
1189 add_3cell_ci(row, make_poisson_ci(defenses, globals.points_played, z));
1190 add_3cell_ci(row, make_poisson_ci(throwaways, globals.points_played, z, true));
1191 add_3cell_ci(row, make_poisson_ci(receiver_errors, globals.points_played, z, true));
1193 add_3cell(row, touches == 0 ? 0 : (touches / globals.points_played).toFixed(2));
1195 add_3cell_with_filler_ci(row, 'N/A');
1196 add_3cell_with_filler_ci(row, 'N/A');
1197 add_3cell_with_filler_ci(row, 'N/A');
1198 add_3cell_with_filler_ci(row, 'N/A');
1199 add_3cell_with_filler_ci(row, 'N/A');
1200 add_3cell_with_filler_ci(row, 'N/A');
1201 add_3cell(row, 'N/A');
1208 function open_filter_menu(click_to_add_div) {
1209 document.getElementById('filter-submenu').style.display = 'none';
1210 let filter_div = click_to_add_div.parentElement;
1211 let filter_id = filter_div.dataset.filterId;
1213 let menu = document.getElementById('filter-add-menu');
1214 menu.parentElement.removeChild(menu);
1215 filter_div.appendChild(menu);
1216 menu.style.display = 'block';
1217 menu.replaceChildren();
1219 // Place the menu directly under the “click to add” label;
1220 // we don't anchor it since that label will move around
1221 // and the menu shouldn't.
1222 let rect = click_to_add_div.getBoundingClientRect();
1223 menu.style.left = rect.left + 'px';
1224 menu.style.top = (rect.bottom + 10) + 'px';
1226 let filterset = global_filters[filter_id];
1227 if (filterset === undefined) {
1228 global_filters[filter_id] = filterset = [];
1231 add_menu_item(filter_div, filterset, menu, 0, 'match', 'Match (any)');
1232 add_menu_item(filter_div, filterset, menu, 1, 'player_any', 'Player on field (any)');
1233 add_menu_item(filter_div, filterset, menu, 2, 'player_all', 'Player on field (all)');
1234 add_menu_item(filter_div, filterset, menu, 3, 'formation_offense', 'Offense played (any)');
1235 add_menu_item(filter_div, filterset, menu, 4, 'formation_defense', 'Defense played (any)');
1236 add_menu_item(filter_div, filterset, menu, 5, 'starting_on', 'Starting on');
1237 add_menu_item(filter_div, filterset, menu, 6, 'gender_ratio', 'Gender ratio');
1240 function add_menu_item(filter_div, filterset, menu, menu_idx, filter_type, title) {
1241 let item = document.createElement('div');
1242 item.classList.add('option');
1243 item.appendChild(document.createTextNode(title));
1245 let arrow = document.createElement('div');
1246 arrow.classList.add('arrow');
1247 arrow.textContent = '▸';
1248 item.appendChild(arrow);
1250 menu.appendChild(item);
1252 item.addEventListener('click', (e) => { show_submenu(filter_div, filterset, menu_idx, null, filter_type); });
1255 function find_all_ratios(json)
1259 for (const player of json['players']) {
1260 players[player['player_id']] = {
1261 'gender': player['gender'],
1262 'last_point_seen': null,
1265 for (const match of json['matches']) {
1266 for (const [q,p] of Object.entries(players)) {
1267 p.on_field_since = null;
1269 for (const e of match['events']) {
1270 let p = players[e['player']];
1271 let type = e['type'];
1272 if (type === 'in') {
1273 p.on_field_since = 1;
1274 } else if (type === 'out') {
1275 p.on_field_since = null;
1276 } else if (type === 'pull' || type == 'their_pull') { // We assume no cross-gender subs for now.
1277 let code = find_gender_ratio_code(players);
1278 if (ratios[code] === undefined) {
1279 ratios[code] = code;
1280 if (code !== '4 F, 3 M' && code !== '4 M, 3 F' && code !== '3 M, 2 F' && code !== '3 F, 2 M') {
1281 console.log('Unexpected gender ratio ' + code + ' first seen at: ' +
1282 match['description'] + ', ' + format_time(e['t']));
1291 function show_submenu(filter_div, filterset, menu_idx, pill, filter_type) {
1292 let submenu = document.getElementById('filter-submenu');
1294 // Move to the same place as the top-level menu.
1295 submenu.parentElement.removeChild(submenu);
1296 document.getElementById('filter-add-menu').parentElement.appendChild(submenu);
1299 const filter = find_filter(filterset, filter_type);
1302 if (filter_type === 'match') {
1303 for (const match of global_json['matches']) {
1305 'title': match['description'],
1306 'id': match['match_id']
1309 } else if (filter_type === 'player_any' || filter_type === 'player_all') {
1310 for (const player of global_json['players']) {
1312 'title': player['name'],
1313 'id': player['player_id']
1316 } else if (filter_type === 'formation_offense') {
1318 'title': '(None/unknown)',
1321 for (const formation of global_json['formations']) {
1322 if (formation['offense']) {
1324 'title': formation['name'],
1325 'id': formation['formation_id']
1329 } else if (filter_type === 'formation_defense') {
1331 'title': '(None/unknown)',
1334 for (const formation of global_json['formations']) {
1335 if (!formation['offense']) {
1337 'title': formation['name'],
1338 'id': formation['formation_id']
1342 } else if (filter_type === 'starting_on') {
1345 'id': false, // last_pull_was_ours
1349 'id': true, // last_pull_was_ours
1351 } else if (filter_type === 'gender_ratio') {
1352 for (const [title, id] of Object.entries(find_all_ratios(global_json)).sort()) {
1360 for (const choice of choices) {
1361 let label = document.createElement('label');
1363 let subitem = document.createElement('div');
1364 subitem.classList.add('option');
1366 let check = document.createElement('input');
1367 check.setAttribute('type', 'checkbox');
1368 check.setAttribute('id', 'choice' + choice.id);
1369 if (filter !== null && filter.elements.has(choice.id)) {
1370 check.setAttribute('checked', 'checked');
1372 check.addEventListener('change', (e) => { checkbox_changed(filter_div, filterset, e, filter_type, choice.id); });
1374 subitem.appendChild(check);
1375 subitem.appendChild(document.createTextNode(choice.title));
1377 label.appendChild(subitem);
1378 subitems.push(label);
1380 submenu.replaceChildren(...subitems);
1381 submenu.style.display = 'block';
1383 if (pill !== null) {
1384 let rect = pill.getBoundingClientRect();
1385 submenu.style.top = (rect.bottom + 10) + 'px';
1386 submenu.style.left = rect.left + 'px';
1388 // Position just outside the selected menu.
1389 let rect = document.getElementById('filter-add-menu').getBoundingClientRect();
1390 submenu.style.top = (rect.top + menu_idx * 35) + 'px';
1391 submenu.style.left = (rect.right - 1) + 'px';
1395 // Find the right filter, if it exists.
1396 function find_filter(filterset, filter_type) {
1397 for (let f of filterset) {
1398 if (f.type === filter_type) {
1405 // Equivalent to Array.prototype.filter, but in-place.
1406 function inplace_filter(arr, cond) {
1408 for (let i = 0; i < arr.length; ++i) {
1416 function add_new_filterset() {
1417 let template = document.querySelector('.filter'); // First one is fine.
1418 let div = template.cloneNode(true);
1419 let add_menu = div.querySelector('#filter-add-menu');
1420 if (add_menu !== null) {
1421 div.removeChild(add_menu);
1423 let submenu = div.querySelector('#filter-submenu');
1424 if (submenu !== null) {
1425 div.removeChild(submenu);
1427 div.querySelector('.filters').replaceChildren();
1428 div.dataset.filterId = next_filterset_id++;
1429 template.parentElement.appendChild(div);
1432 function try_gc_filter_menus(cheap) {
1435 for (const filter_div of document.querySelectorAll('.filter')) {
1436 let id = filter_div.dataset.filterId;
1437 divs.push(filter_div);
1438 empties.push(global_filters[id] === undefined || global_filters[id].length === 0);
1440 let last_div = divs[empties.length - 1];
1442 // See if we have two empty filter lists at the bottom of the list;
1443 // if so, we can remove one without it looking funny in the UI.
1444 // If not, we'll have to wait until closing the menu.
1445 // (The menu is positioned at the second-last one, so we can
1446 // remove the last one without issue.)
1447 if (empties.length >= 2 && empties[empties.length - 2] && empties[empties.length - 1]) {
1448 delete global_filters[last_div.dataset.filterId];
1449 last_div.parentElement.removeChild(last_div);
1452 // This is a different situation from try_cheap_gc(), where we should
1453 // remove the one _not_ last. We might need to move the menu to the last one,
1454 // though, so it doesn't get lost.
1455 for (let i = 0; i < empties.length - 1; ++i) {
1460 delete global_filters[div.dataset.filterId];
1461 let add_menu = div.querySelector('#filter-add-menu');
1462 if (add_menu !== null) {
1463 div.removeChild(add_menu);
1464 last_div.appendChild(add_menu);
1466 let submenu = div.querySelector('#filter-submenu');
1467 if (submenu !== null) {
1468 div.removeChild(submenu);
1469 last_div.appendChild(submenu);
1471 div.parentElement.removeChild(div);
1476 function checkbox_changed(filter_div, filterset, e, filter_type, id) {
1477 let filter = find_filter(filterset, filter_type);
1478 let pills_div = filter_div.querySelector('.filters');
1479 if (e.target.checked) {
1480 // See if we must create a new empty filterset (since this went from
1481 // empty to non-empty).
1482 if (filterset.length === 0) {
1483 add_new_filterset();
1486 // See if we must add a new filter to the list.
1487 if (filter === null) {
1489 'type': filter_type,
1490 'elements': new Set([ id ]),
1492 filter.pill = make_filter_pill(filter_div, filterset, filter);
1493 filterset.push(filter);
1494 pills_div.appendChild(filter.pill);
1496 filter.elements.add(id);
1497 let new_pill = make_filter_pill(filter_div, filterset, filter);
1498 pills_div.replaceChild(new_pill, filter.pill);
1499 filter.pill = new_pill;
1502 filter.elements.delete(id);
1503 if (filter.elements.size === 0) {
1504 pills_div.removeChild(filter.pill);
1505 inplace_filter(filterset, f => f !== filter);
1507 if (filterset.length == 0) {
1508 try_gc_filter_menus(true);
1511 let new_pill = make_filter_pill(filter_div, filterset, filter);
1512 pills_div.replaceChild(new_pill, filter.pill);
1513 filter.pill = new_pill;
1517 process_matches(global_json, global_filters);
1520 function make_filter_pill(filter_div, filterset, filter) {
1521 let pill = document.createElement('div');
1522 pill.classList.add('filter-pill');
1524 if (filter.type === 'match') {
1528 for (const match_id of filter.elements) {
1529 all_names.push(find_match(match_id)['description']);
1531 let common_prefix = find_common_prefix_of_all(all_names);
1532 if (common_prefix !== null) {
1533 text += common_prefix + '(';
1537 let sorted_match_id = Array.from(filter.elements).sort((a, b) => a - b);
1538 for (const match_id of sorted_match_id) {
1542 let desc = find_match(match_id)['description'];
1543 if (common_prefix === null) {
1546 text += desc.substr(common_prefix.length);
1551 if (common_prefix !== null) {
1554 } else if (filter.type === 'player_any') {
1555 text = 'Player (any): ';
1556 let sorted_players = Array.from(filter.elements).sort((a, b) => player_pos(a) - player_pos(b));
1558 for (const player_id of sorted_players) {
1562 text += find_player(player_id)['name'];
1565 } else if (filter.type === 'player_all') {
1567 let sorted_players = Array.from(filter.elements).sort((a, b) => player_pos(a) - player_pos(b));
1569 for (const player_id of sorted_players) {
1573 text += find_player(player_id)['name'];
1576 } else if (filter.type === 'formation_offense' || filter.type === 'formation_defense') {
1577 const offense = (filter.type === 'formation_offense');
1585 for (const formation_id of filter.elements) {
1586 all_names.push(find_formation(formation_id)['name']);
1588 let common_prefix = find_common_prefix_of_all(all_names);
1589 if (common_prefix !== null) {
1590 text += common_prefix + '(';
1594 let sorted_formation_id = Array.from(filter.elements).sort((a, b) => a - b);
1595 for (const formation_id of sorted_formation_id) {
1599 let desc = find_formation(formation_id)['name'];
1600 if (common_prefix === null) {
1603 text += desc.substr(common_prefix.length);
1608 if (common_prefix !== null) {
1611 } else if (filter.type === 'starting_on') {
1612 text = 'Starting on: ';
1614 if (filter.elements.has(false) && filter.elements.has(true)) {
1616 } else if (filter.elements.has(false)) {
1621 } else if (filter.type === 'gender_ratio') {
1625 for (const name of Array.from(filter.elements).sort()) {
1629 text += name; // FIXME
1634 let text_node = document.createElement('span');
1635 text_node.innerText = text;
1636 text_node.addEventListener('click', (e) => show_submenu(filter_div, filterset, null, pill, filter.type));
1637 pill.appendChild(text_node);
1639 pill.appendChild(document.createTextNode(' '));
1641 let delete_node = document.createElement('span');
1642 delete_node.innerText = '✖';
1643 delete_node.addEventListener('click', (e) => {
1644 // Delete this filter entirely.
1645 pill.parentElement.removeChild(pill);
1646 inplace_filter(filterset, f => f !== filter);
1647 if (filterset.length == 0) {
1648 try_gc_filter_menus(false);
1650 process_matches(global_json, global_filters);
1652 let add_menu = document.getElementById('filter-add-menu');
1653 let add_submenu = document.getElementById('filter-submenu');
1654 add_menu.style.display = 'none';
1655 add_submenu.style.display = 'none';
1657 pill.appendChild(delete_node);
1658 pill.style.cursor = 'pointer';
1663 function make_filter_marker(filterset) {
1665 for (const filter of filterset) {
1669 if (filter.type === 'match') {
1671 for (const match of global_json['matches']) {
1672 all_names.push(match['description']);
1674 let common_prefix = find_common_prefix_of_all(all_names);
1676 let sorted_match_id = Array.from(filter.elements).sort((a, b) => a - b);
1677 for (const match_id of sorted_match_id) {
1678 let desc = find_match(match_id)['description'];
1679 if (common_prefix === null) {
1680 text += desc.substr(0, 3);
1682 text += desc.substr(common_prefix.length, 3);
1685 } else if (filter.type === 'player_any' || filter.type === 'player_all') {
1686 let sorted_players = Array.from(filter.elements).sort((a, b) => player_pos(a) - player_pos(b));
1687 for (const player_id of sorted_players) {
1688 text += find_player(player_id)['name'].substr(0, 3);
1690 } else if (filter.type === 'formation_offense' || filter.type === 'formation_defense') {
1691 let sorted_formation_id = Array.from(filter.elements).sort((a, b) => a - b);
1692 for (const formation_id of sorted_formation_id) {
1693 text += find_formation(formation_id)['name'].substr(0, 3);
1695 } else if (filter.type === 'starting_on') {
1696 if (filter.elements.has(false) && filter.elements.has(true)) {
1698 } else if (filter.elements.has(false)) {
1703 } else if (filter.type === 'gender_ratio') {
1705 for (const name of Array.from(filter.elements).sort()) {
1709 text += name.replaceAll(' ', '').substr(0, 2); // 4 F, 3M -> 4 F.
1717 function find_common_prefix(a, b) {
1719 for (let i = 0; i < Math.min(a.length, b.length); ++i) {
1720 if (a[i] === b[i]) {
1729 function find_common_prefix_of_all(values) {
1730 if (values.length < 2) {
1733 let common_prefix = null;
1734 for (const desc of values) {
1735 if (common_prefix === null) {
1736 common_prefix = desc;
1738 common_prefix = find_common_prefix(common_prefix, desc);
1741 if (common_prefix.length >= 3) {
1742 return common_prefix;
1748 function find_match(match_id) {
1749 for (const match of global_json['matches']) {
1750 if (match['match_id'] === match_id) {
1757 function find_formation(formation_id) {
1758 for (const formation of global_json['formations']) {
1759 if (formation['formation_id'] === formation_id) {
1766 function find_player(player_id) {
1767 for (const player of global_json['players']) {
1768 if (player['player_id'] === player_id) {
1775 function player_pos(player_id) {
1777 for (const player of global_json['players']) {
1778 if (player['player_id'] === player_id) {
1786 function keep_match(match_id, filters) {
1787 for (const filter of filters) {
1788 if (filter.type === 'match') {
1789 return filter.elements.has(match_id);
1795 // Returns a map of e.g. F => 4, M => 3.
1796 function find_gender_ratio(players) {
1798 for (const [q,p] of Object.entries(players)) {
1799 if (p.on_field_since === null) {
1802 let gender = p.gender;
1803 if (gender === '' || gender === undefined || gender === null) {
1806 if (map[gender] === undefined) {
1815 function find_gender_ratio_code(players) {
1816 let map = find_gender_ratio(players);
1817 let all_genders = Array.from(Object.keys(map)).sort(
1819 if (map[a] !== map[b]) {
1820 return map[b] - map[a]; // Reverse numeric.
1830 for (const g of all_genders) {
1841 // null if none (e.g., if playing 3–3).
1842 function find_predominant_gender(players) {
1844 let predominant_gender = null;
1845 for (const [gender, num] of Object.entries(find_gender_ratio(players))) {
1848 predominant_gender = gender;
1849 } else if (num == max) {
1850 predominant_gender = null; // At least two have the same.
1853 return predominant_gender;
1856 function find_num_players_on_field(players) {
1858 for (const [q,p] of Object.entries(players)) {
1859 if (p.on_field_since !== null) {
1866 function filter_passes(players, formations_used_this_point, last_pull_was_ours, filter) {
1867 if (filter.type === 'player_any') {
1868 for (const p of Array.from(filter.elements)) {
1869 if (players[p].on_field_since !== null) {
1874 } else if (filter.type === 'player_all') {
1875 for (const p of Array.from(filter.elements)) {
1876 if (players[p].on_field_since === null) {
1881 } else if (filter.type === 'formation_offense' || filter.type === 'formation_defense') {
1882 for (const f of Array.from(filter.elements)) {
1883 if (formations_used_this_point.has(f)) {
1888 } else if (filter.type === 'starting_on') {
1889 return filter.elements.has(last_pull_was_ours);
1890 } else if (filter.type === 'gender_ratio') {
1891 return filter.elements.has(find_gender_ratio_code(players));
1896 function keep_event(players, formations_used_this_point, last_pull_was_ours, filters) {
1897 for (const filter of filters) {
1898 if (!filter_passes(players, formations_used_this_point, last_pull_was_ours, filter)) {
1905 // Heuristic: If we go at least ten seconds without the possession changing
1906 // or the operator specifying some other formation, we probably play the
1907 // same formation as the last point.
1908 function should_reuse_last_formation(events, t) {
1909 for (const e of events) {
1913 if (e.t > t + 10000) {
1916 const type = e.type;
1917 if (type === 'their_goal' || type === 'goal' ||
1918 type === 'set_defense' || type === 'set_offense' ||
1919 type === 'throwaway' || type === 'their_throwaway' ||
1920 type === 'drop' || type === 'was_d' || type === 'stallout' || type === 'defense' || type === 'interception' ||
1921 type === 'pull' || type === 'pull_landed' || type === 'pull_oob' || type === 'their_pull' ||
1922 type === 'formation_offense' || type === 'formation_defense') {
1929 function possibly_close_menu(e) {
1930 if (e.target.closest('.filter-click-to-add') === null &&
1931 e.target.closest('#filter-add-menu') === null &&
1932 e.target.closest('#filter-submenu') === null &&
1933 e.target.closest('.filter-pill') === null) {
1934 let add_menu = document.getElementById('filter-add-menu');
1935 let add_submenu = document.getElementById('filter-submenu');
1936 add_menu.style.display = 'none';
1937 add_submenu.style.display = 'none';
1938 try_gc_filter_menus(false);
1942 let global_sort = {};
1944 function sort_by(th) {
1945 let tr = th.parentElement;
1947 for (let column_idx = 0; column_idx < tr.children.length; ++column_idx) {
1948 let element = tr.children[column_idx];
1949 if (element === th) {
1950 ++child_idx; // Pad.
1953 if (element.hasAttribute('colspan')) {
1954 child_idx += parseInt(element.getAttribute('colspan'));
1961 let table = tr.parentElement;
1962 for (let row_idx = 1; row_idx < table.children.length - 1; ++row_idx) { // Skip header and globals.
1963 let row = table.children[row_idx];
1964 let player = parseInt(row.dataset.player);
1965 let value = row.children[child_idx].textContent;
1966 global_sort[player] = value;
1970 function get_sorted_players(players)
1972 let p = Object.entries(players);
1973 if (global_sort.length !== 0) {
1975 let ai = parseFloat(global_sort[a[0]]);
1976 let bi = parseFloat(global_sort[b[0]]);
1977 if (ai == ai && bi == bi) {
1978 return bi - ai; // Reverse numeric.
1979 } else if (global_sort[a[0]] < global_sort[b[0]]) {
1981 } else if (global_sort[a[0]] > global_sort[b[0]]) {