3 // No frameworks, no compilers, no npm, just JavaScript. :-)
7 addEventListener('hashchange', () => { process_matches(global_json); });
9 .then(response => response.json())
10 .then(response => { global_json = response; process_matches(global_json); });
12 function attribute_player_time(player, to, from) {
13 if (player.on_field_since > from) {
14 // Player came in while play happened (without a stoppage!?).
15 player.playing_time_ms += to - player.on_field_since;
17 player.playing_time_ms += to - from;
21 function take_off_field(player, t, live_since) {
22 if (live_since === null) {
23 // Play isn't live, so nothing to do.
25 attribute_player_time(player, t, live_since);
27 if (player.on_field_since !== null) { // Just a safeguard; out without in should never happen.
28 player.field_time_ms += t - player.on_field_since;
30 player.on_field_since = null;
33 function add_cell(tr, element_type, text) {
34 let element = document.createElement(element_type);
35 element.textContent = text;
36 if (element_type === 'th') {
37 element.setAttribute('colspan', '3');
39 tr.appendChild(element);
43 function add_3cell(tr, text, cls) {
44 let p1 = add_cell(tr, 'td', '');
45 let element = add_cell(tr, 'td', text);
46 let p2 = add_cell(tr, 'td', '');
48 p1.classList.add('pad');
49 p2.classList.add('pad');
50 if (cls === undefined) {
51 element.classList.add('num');
53 element.classList.add(cls);
58 function add_3cell_ci(tr, ci) {
61 return; // FIXME: some SVG padding needed
65 if (ci.format === 'percentage') {
66 text = (100 * ci.val).toFixed(0) + '%';
68 text = ci.val.toFixed(2);
70 let element = add_3cell(tr, text);
71 let to_x = (val) => { return width * (val - ci.min) / (ci.max - ci.min); };
76 let svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
77 svg.classList.add('ci');
78 svg.setAttribute('width', width);
79 svg.setAttribute('height', height);
81 // The good (green) and red (bad) ranges.
82 let s0 = document.createElementNS('http://www.w3.org/2000/svg', 'rect');
83 s0.classList.add('range');
84 s0.classList.add('s0');
85 s0.setAttribute('width', to_x(ci.desired));
86 s0.setAttribute('height', height);
87 s0.setAttribute('x', '0');
89 let s1 = document.createElementNS('http://www.w3.org/2000/svg', 'rect');
90 s1.classList.add('range');
91 s1.classList.add('s1');
92 s1.setAttribute('width', width - to_x(ci.desired));
93 s1.setAttribute('height', height);
94 s1.setAttribute('x', to_x(ci.desired));
97 let bar = document.createElementNS('http://www.w3.org/2000/svg', 'rect');
98 bar.classList.add('bar');
99 bar.setAttribute('width', to_x(ci.upper_ci) - to_x(ci.lower_ci));
100 bar.setAttribute('height', height / 3);
101 bar.setAttribute('x', to_x(ci.lower_ci));
102 bar.setAttribute('y', height / 3);
104 // Marker line for average.
105 let marker = document.createElementNS('http://www.w3.org/2000/svg', 'line');
106 marker.classList.add('marker');
107 marker.setAttribute('x1', to_x(ci.val));
108 marker.setAttribute('x2', to_x(ci.val));
109 marker.setAttribute('y1', height / 6);
110 marker.setAttribute('y2', height * 5 / 6);
114 svg.appendChild(bar);
115 svg.appendChild(marker);
117 element.appendChild(svg);
120 function process_matches(json) {
122 for (const player of json['players']) {
123 players[player['player_id']] = {
124 'name': player['name'],
125 'number': player['number'],
139 'playing_time_ms': 0,
143 'offensive_points_completed': 0,
144 'offensive_points_won': 0,
145 'defensive_points_completed': 0,
146 'defensive_points_won': 0,
148 'offensive_soft_plus': 0,
149 'offensive_soft_minus': 0,
150 'defensive_soft_plus': 0,
151 'defensive_soft_minus': 0,
158 'last_point_seen': null,
159 'on_field_since': null,
164 players['globals'] = {
166 'playing_time_ms': 0,
169 'offensive_points_completed': 0,
170 'offensive_points_won': 0,
171 'defensive_points_completed': 0,
172 'defensive_points_won': 0,
174 let globals = players['globals'];
176 for (const match of json['matches']) {
180 let prev_handler = null;
181 let live_since = null;
184 let pull_started = null;
185 let last_pull_was_ours = null; // Effectively whether we're playing an O or D point (not affected by turnovers).
187 let game_started = null;
188 let last_goal = null;
189 for (const [q,p] of Object.entries(players)) {
190 p.on_field_since = null;
191 p.last_point_seen = null;
193 for (const e of match['events']) {
195 let type = e['type'];
196 let p = players[e['player']];
199 if (type === 'in' && p.on_field_since === null) {
200 p.on_field_since = t;
201 } else if (type === 'out') {
202 take_off_field(p, t, live_since);
205 // Liveness management
206 if (type === 'pull' || type === 'their_pull' || type === 'restart') {
208 } else if (type === 'catch' && last_pull_was_ours === null) {
209 // Someone forgot to add the pull, so we'll need to wing it.
210 console.log('Missing pull on ' + our_score + '\u2013' + their_score + ' in ' + match['description'] + '; pretending to have one.');
212 last_pull_was_ours = !offense;
213 } else if (type === 'goal' || type === 'their_goal' || type === 'stoppage') {
214 for (const [q,p] of Object.entries(players)) {
215 if (p.on_field_since === null) {
218 if (type !== 'stoppage' && p.last_point_seen !== point_num) {
219 // In case the player did nothing this point,
220 // not even subbing in.
221 p.last_point_seen = point_num;
224 attribute_player_time(p, t, live_since);
226 if (type !== 'stoppage') {
227 if (last_pull_was_ours === true) { // D point.
228 ++p.defensive_points_completed;
229 if (type === 'goal') {
230 ++p.defensive_points_won;
232 } else if (last_pull_was_ours === false) { // O point.
233 ++p.offensive_points_completed;
234 if (type === 'goal') {
235 ++p.offensive_points_won;
241 if (type !== 'stoppage') {
243 ++globals.points_played;
244 if (last_pull_was_ours === true) { // D point.
245 ++globals.defensive_points_completed;
246 if (type === 'goal') {
247 ++globals.defensive_points_won;
249 } else if (last_pull_was_ours === false) { // O point.
250 ++globals.offensive_points_completed;
251 if (type === 'goal') {
252 ++globals.offensive_points_won;
256 if (live_since !== null) {
257 globals.playing_time_ms += t - live_since;
264 if (type === 'goal') {
266 } else if (type === 'their_goal') {
270 // Point count management
271 if (p !== undefined && type !== 'out' && p.last_point_seen !== point_num) {
272 p.last_point_seen = point_num;
275 if (type === 'goal' || type === 'their_goal') {
279 if (type !== 'out' && game_started === null) {
284 if (type === 'pull') {
285 puller = e['player'];
288 } else if (type === 'in' || type === 'out' || type === 'stoppage' || type === 'restart' || type === 'unknown' || type === 'set_defense' || type === 'set_offense') {
289 // No effect on pull.
290 } else if (type === 'pull_landed' && puller !== null) {
291 players[puller].pull_times.push(t - pull_started);
292 } else if (type === 'pull_oob' && puller !== null) {
293 ++players[puller].oob_pulls;
295 // Not pulling (if there was one, we never recorded its outcome, but still count it).
296 puller = pull_started = null;
299 // Offense/defense management (TODO: use it for actual counting)
300 if (type === 'set_defense' || type === 'goal' || type === 'throwaway' || type === 'drop') {
302 } else if (type === 'set_offense' || type === 'their_goal' || type === 'their_throwaway' || type === 'defense' || type === 'interception') {
305 if (type === 'pull') {
306 last_pull_was_ours = true;
307 } else if (type === 'their_pull') {
308 last_pull_was_ours = false;
309 } else if (type === 'set_offense' && last_pull_was_ours === null) {
310 // set_offense could either be “changed to offense for some reason
311 // we could not express”, or “we started in the middle of a point,
312 // and we are offense”. We assume that if we already saw the pull,
313 // it's the former, and if not, it's the latter; thus, the === null
314 // test above. (It could also be “we set offense before the pull,
315 // so that we get the right button enabled”, in which case it will
316 // be overwritten by the next pull/their_pull event anyway.)
317 last_pull_was_ours = false;
318 } else if (type === 'set_defense' && last_pull_was_ours === null) {
320 last_pull_was_ours = true;
321 } else if (type === 'goal' || type === 'their_goal') {
322 last_pull_was_ours = null;
326 if (type === 'catch' || type === 'goal') {
327 if (handler !== null) {
328 ++players[handler].num_throws;
333 if (type === 'goal') {
334 if (prev_handler !== null) {
335 ++players[prev_handler].hockey_assists;
337 if (handler !== null) {
338 ++players[handler].assists;
341 handler = prev_handler = null;
343 // Update hold history.
344 prev_handler = handler;
345 handler = e['player'];
347 } else if (type === 'throwaway') {
350 handler = prev_handler = null;
351 } else if (type === 'drop') {
353 handler = prev_handler = null;
354 } else if (type === 'defense') {
356 } else if (type === 'interception') {
361 handler = e['player'];
362 } else if (type === 'offensive_soft_plus' || type === 'offensive_soft_minus' || type === 'defensive_soft_plus' || type === 'defensive_soft_minus') {
364 } else if (type !== 'in' && type !== 'out' && type !== 'pull' &&
365 type !== 'their_goal' && type !== 'stoppage' && type !== 'restart' && type !== 'unknown' &&
366 type !== 'set_defense' && type !== 'goal' && type !== 'throwaway' &&
367 type !== 'drop' && type !== 'set_offense' && type !== 'their_goal' &&
368 type !== 'pull' && type !== 'pull_landed' && type !== 'pull_oob' && type !== 'their_pull' &&
369 type !== 'their_throwaway' && type !== 'defense' && type !== 'interception') {
370 console.log("Unknown event:", e);
374 // Add field time for all players still left at match end.
375 for (const [q,p] of Object.entries(players)) {
376 if (p.on_field_since !== null && last_goal !== null) {
377 p.field_time_ms += last_goal - p.on_field_since;
380 if (game_started !== null && last_goal !== null) {
381 globals.field_time_ms += last_goal - game_started;
383 if (live_since !== null && last_goal !== null) {
384 globals.playing_time_ms += last_goal - live_since;
388 let chosen_category = get_chosen_category();
389 write_main_menu(chosen_category);
392 if (chosen_category === 'general') {
393 rows = make_table_general(players);
394 } else if (chosen_category === 'offense') {
395 rows = make_table_offense(players);
396 } else if (chosen_category === 'defense') {
397 rows = make_table_defense(players);
398 } else if (chosen_category === 'playing_time') {
399 rows = make_table_playing_time(players);
400 } else if (chosen_category === 'per_point') {
401 rows = make_table_per_point(players);
403 document.getElementById('stats').replaceChildren(...rows);
406 function get_chosen_category() {
407 if (window.location.hash === '#offense') {
409 } else if (window.location.hash === '#defense') {
411 } else if (window.location.hash === '#playing_time') {
412 return 'playing_time';
413 } else if (window.location.hash === '#per_point') {
420 function write_main_menu(chosen_category) {
422 if (chosen_category === 'general') {
423 let span = document.createElement('span');
424 span.innerText = 'General';
427 let a = document.createElement('a');
428 a.appendChild(document.createTextNode('General'));
429 a.setAttribute('href', '#general');
433 if (chosen_category === 'offense') {
434 let span = document.createElement('span');
435 span.innerText = 'Offense';
438 let a = document.createElement('a');
439 a.appendChild(document.createTextNode('Offense'));
440 a.setAttribute('href', '#offense');
444 if (chosen_category === 'defense') {
445 let span = document.createElement('span');
446 span.innerText = 'Defense';
449 let a = document.createElement('a');
450 a.appendChild(document.createTextNode('Defense'));
451 a.setAttribute('href', '#defense');
455 if (chosen_category === 'playing_time') {
456 let span = document.createElement('span');
457 span.innerText = 'Playing time';
460 let a = document.createElement('a');
461 a.appendChild(document.createTextNode('Playing time'));
462 a.setAttribute('href', '#playing_time');
466 if (chosen_category === 'per_point') {
467 let span = document.createElement('span');
468 span.innerText = 'Per point';
471 let a = document.createElement('a');
472 a.appendChild(document.createTextNode('Per point'));
473 a.setAttribute('href', '#per_point');
477 document.getElementById('mainmenu').replaceChildren(...elems);
480 // https://en.wikipedia.org/wiki/1.96#History
483 function make_binomial_ci(val, num, z) {
486 // https://en.wikipedia.org/wiki/Binomial_proportion_confidence_interval#Wilson_score_interval
487 let low = (avg + z*z/(2*num) - z * Math.sqrt(avg * (1.0 - avg) / num + z*z/(4*num*num))) / (1 + z*z/num);
488 let high = (avg + z*z/(2*num) + z * Math.sqrt(avg * (1.0 - avg) / num + z*z/(4*num*num))) / (1 + z*z/num);
490 // Fix the signs so that we don't get -0.00.
491 low = Math.max(low, 0.0);
501 // These can only happen once per point, but you get -1 and +1
502 // instead of 0 and +1. After we rewrite to 0 and 1, it's a binomial,
503 // and then we can rewrite back.
504 function make_efficiency_ci(points_won, points_completed, z)
506 let ci = make_binomial_ci(points_won, points_completed, z);
507 ci.val = 2.0 * ci.val - 1.0;
508 ci.lower_ci = 2.0 * ci.lower_ci - 1.0;
509 ci.upper_ci = 2.0 * ci.upper_ci - 1.0;
512 ci.desired = 0.0; // Desired = positive efficiency.
516 function make_table_general(players) {
519 let header = document.createElement('tr');
520 add_cell(header, 'th', 'Player');
521 add_cell(header, 'th', '+/-');
522 add_cell(header, 'th', 'Soft +/-');
523 add_cell(header, 'th', 'O efficiency');
524 add_cell(header, 'th', 'D efficiency');
525 add_cell(header, 'th', 'Points played');
529 for (const [q,p] of Object.entries(players)) {
530 if (q === 'globals') continue;
531 let row = document.createElement('tr');
532 let pm = p.goals + p.assists + p.hockey_assists + p.defenses - p.throwaways - p.drops;
533 let soft_pm = p.offensive_soft_plus + p.defensive_soft_plus - p.offensive_soft_minus - p.defensive_soft_minus;
534 let o_efficiency = make_efficiency_ci(p.offensive_points_won, p.offensive_points_completed, z);
535 let d_efficiency = make_efficiency_ci(p.defensive_points_won, p.defensive_points_completed, z);
536 add_3cell(row, p.name, 'name'); // TODO: number?
537 add_3cell(row, pm > 0 ? ('+' + pm) : pm);
538 add_3cell(row, soft_pm > 0 ? ('+' + soft_pm) : soft_pm);
539 add_3cell_ci(row, o_efficiency);
540 add_3cell_ci(row, d_efficiency);
541 add_3cell(row, p.points_played);
546 let globals = players['globals'];
547 let o_efficiency = make_efficiency_ci(globals.offensive_points_won, globals.offensive_points_completed, z);
548 let d_efficiency = make_efficiency_ci(globals.defensive_points_won, globals.defensive_points_completed, z);
549 let row = document.createElement('tr');
553 add_3cell_ci(row, o_efficiency);
554 add_3cell_ci(row, d_efficiency);
555 add_3cell(row, globals.points_played);
561 function make_table_offense(players) {
564 let header = document.createElement('tr');
565 add_cell(header, 'th', 'Player');
566 add_cell(header, 'th', 'Goals');
567 add_cell(header, 'th', 'Assists');
568 add_cell(header, 'th', 'Hockey assists');
569 add_cell(header, 'th', 'Throws');
570 add_cell(header, 'th', 'Throwaways');
571 add_cell(header, 'th', '%OK');
572 add_cell(header, 'th', 'Catches');
573 add_cell(header, 'th', 'Drops');
574 add_cell(header, 'th', '%OK');
575 add_cell(header, 'th', 'Soft +/-');
583 for (const [q,p] of Object.entries(players)) {
584 if (q === 'globals') continue;
585 let throw_ok = make_binomial_ci(p.num_throws - p.throwaways, p.num_throws, z);
586 let catch_ok = make_binomial_ci(p.catches, p.catches + p.drops, z);
588 throw_ok.format = 'percentage';
589 catch_ok.format = 'percentage';
591 // Desire at least 90% percentage. Fairly arbitrary.
592 throw_ok.desired = 0.9;
593 catch_ok.desired = 0.9;
595 let row = document.createElement('tr');
596 add_3cell(row, p.name, 'name'); // TODO: number?
597 add_3cell(row, p.goals);
598 add_3cell(row, p.assists);
599 add_3cell(row, p.hockey_assists);
600 add_3cell(row, p.num_throws);
601 add_3cell(row, p.throwaways);
602 add_3cell_ci(row, throw_ok);
603 add_3cell(row, p.catches);
604 add_3cell(row, p.drops);
605 add_3cell_ci(row, catch_ok);
606 add_3cell(row, '+' + p.offensive_soft_plus);
607 add_3cell(row, '-' + p.offensive_soft_minus);
610 num_throws += p.num_throws;
611 throwaways += p.throwaways;
612 catches += p.catches;
617 let throw_ok = make_binomial_ci(num_throws - throwaways, num_throws, z);
618 let catch_ok = make_binomial_ci(catches, catches + drops, z);
619 throw_ok.format = 'percentage';
620 catch_ok.format = 'percentage';
621 throw_ok.desired = 0.9;
622 catch_ok.desired = 0.9;
624 let row = document.createElement('tr');
629 add_3cell(row, num_throws);
630 add_3cell(row, throwaways);
631 add_3cell_ci(row, throw_ok);
632 add_3cell(row, catches);
633 add_3cell(row, drops);
634 add_3cell_ci(row, catch_ok);
642 function make_table_defense(players) {
645 let header = document.createElement('tr');
646 add_cell(header, 'th', 'Player');
647 add_cell(header, 'th', 'Ds');
648 add_cell(header, 'th', 'Pulls');
649 add_cell(header, 'th', 'OOB pulls');
650 add_cell(header, 'th', 'Avg. hang time (IB)');
651 add_cell(header, 'th', 'Soft +/-');
654 for (const [q,p] of Object.entries(players)) {
655 if (q === 'globals') continue;
657 for (const t of p.pull_times) {
660 let avg_time = 1e-3 * sum_time / p.pulls;
661 let oob_pct = 100 * p.oob_pulls / p.pulls;
663 let row = document.createElement('tr');
664 add_3cell(row, p.name, 'name'); // TODO: number?
665 add_3cell(row, p.defenses);
666 add_3cell(row, p.pulls);
668 add_3cell(row, 'N/A');
670 add_3cell(row, p.oob_pulls + ' (' + oob_pct.toFixed(0) + '%)');
672 if (p.pulls > p.oob_pulls) {
673 add_3cell(row, avg_time.toFixed(1) + ' sec');
675 add_3cell(row, 'N/A');
677 add_3cell(row, '+' + p.defensive_soft_plus);
678 add_3cell(row, '-' + p.defensive_soft_minus);
684 function make_table_playing_time(players) {
687 let header = document.createElement('tr');
688 add_cell(header, 'th', 'Player');
689 add_cell(header, 'th', 'Points played');
690 add_cell(header, 'th', 'Time played');
691 add_cell(header, 'th', 'Time on field');
692 add_cell(header, 'th', 'O points');
693 add_cell(header, 'th', 'D points');
697 for (const [q,p] of Object.entries(players)) {
698 if (q === 'globals') continue;
699 let row = document.createElement('tr');
700 add_3cell(row, p.name, 'name'); // TODO: number?
701 add_3cell(row, p.points_played);
702 add_3cell(row, Math.floor(p.playing_time_ms / 60000) + ' min');
703 add_3cell(row, Math.floor(p.field_time_ms / 60000) + ' min');
704 add_3cell(row, p.offensive_points_completed);
705 add_3cell(row, p.defensive_points_completed);
710 let globals = players['globals'];
711 let row = document.createElement('tr');
713 add_3cell(row, globals.points_played);
714 add_3cell(row, Math.floor(globals.playing_time_ms / 60000) + ' min');
715 add_3cell(row, Math.floor(globals.field_time_ms / 60000) + ' min');
716 add_3cell(row, globals.offensive_points_completed);
717 add_3cell(row, globals.defensive_points_completed);
723 function make_table_per_point(players) {
726 let header = document.createElement('tr');
727 add_cell(header, 'th', 'Player');
728 add_cell(header, 'th', 'Goals');
729 add_cell(header, 'th', 'Assists');
730 add_cell(header, 'th', 'Hockey assists');
731 add_cell(header, 'th', 'Ds');
732 add_cell(header, 'th', 'Throwaways');
733 add_cell(header, 'th', 'Drops');
734 add_cell(header, 'th', 'Touches');
740 let hockey_assists = 0;
745 for (const [q,p] of Object.entries(players)) {
746 if (q === 'globals') continue;
747 let row = document.createElement('tr');
748 add_3cell(row, p.name, 'name'); // TODO: number?
749 if (p.points_played > 0) {
750 add_3cell(row, p.goals == 0 ? 0 : (p.goals / p.points_played).toFixed(2));
751 add_3cell(row, p.assists == 0 ? 0 : (p.assists / p.points_played).toFixed(2));
752 add_3cell(row, p.hockey_assists == 0 ? 0 : (p.hockey_assists / p.points_played).toFixed(2));
753 add_3cell(row, p.defenses == 0 ? 0 : (p.defenses / p.points_played).toFixed(2));
754 add_3cell(row, p.throwaways == 0 ? 0 : (p.throwaways / p.points_played).toFixed(2));
755 add_3cell(row, p.drops == 0 ? 0 : (p.drops / p.points_played).toFixed(2));
756 add_3cell(row, p.touches == 0 ? 0 : (p.touches / p.points_played).toFixed(2));
758 add_3cell(row, 'N/A');
759 add_3cell(row, 'N/A');
760 add_3cell(row, 'N/A');
761 add_3cell(row, 'N/A');
762 add_3cell(row, 'N/A');
763 add_3cell(row, 'N/A');
764 add_3cell(row, 'N/A');
769 assists += p.assists;
770 hockey_assists += p.hockey_assists;
771 defenses += p.defenses;
772 throwaways += p.throwaways;
774 touches += p.touches;
778 let globals = players['globals'];
779 let row = document.createElement('tr');
781 if (globals.points_played > 0) {
782 add_3cell(row, goals == 0 ? 0 : (goals / globals.points_played).toFixed(2));
783 add_3cell(row, assists == 0 ? 0 : (assists / globals.points_played).toFixed(2));
784 add_3cell(row, hockey_assists == 0 ? 0 : (hockey_assists / globals.points_played).toFixed(2));
785 add_3cell(row, defenses == 0 ? 0 : (defenses / globals.points_played).toFixed(2));
786 add_3cell(row, throwaways == 0 ? 0 : (throwaways / globals.points_played).toFixed(2));
787 add_3cell(row, drops == 0 ? 0 : (drops / globals.points_played).toFixed(2));
788 add_3cell(row, touches == 0 ? 0 : (touches / globals.points_played).toFixed(2));
790 add_3cell(row, 'N/A');
791 add_3cell(row, 'N/A');
792 add_3cell(row, 'N/A');
793 add_3cell(row, 'N/A');
794 add_3cell(row, 'N/A');
795 add_3cell(row, 'N/A');
796 add_3cell(row, 'N/A');