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) {
62 return; // FIXME: some SVG padding needed
66 if (ci.format === 'percentage') {
67 text = (100 * ci.val).toFixed(0) + '%';
69 text = ci.val.toFixed(2);
71 let element = add_3cell(tr, text);
72 let to_x = (val) => { return width * (val - ci.min) / (ci.max - ci.min); };
77 let svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
78 svg.classList.add('ci');
79 svg.setAttribute('width', width);
80 svg.setAttribute('height', height);
82 // The good (green) and red (bad) ranges.
83 let s0 = document.createElementNS('http://www.w3.org/2000/svg', 'rect');
84 s0.classList.add('range');
85 s0.classList.add('s0');
86 s0.setAttribute('width', to_x(ci.desired));
87 s0.setAttribute('height', height);
88 s0.setAttribute('x', '0');
90 let s1 = document.createElementNS('http://www.w3.org/2000/svg', 'rect');
91 s1.classList.add('range');
92 s1.classList.add('s1');
93 s1.setAttribute('width', width - to_x(ci.desired));
94 s1.setAttribute('height', height);
95 s1.setAttribute('x', to_x(ci.desired));
98 let bar = document.createElementNS('http://www.w3.org/2000/svg', 'rect');
99 bar.classList.add('bar');
100 bar.setAttribute('width', to_x(ci.upper_ci) - to_x(ci.lower_ci));
101 bar.setAttribute('height', height / 3);
102 bar.setAttribute('x', to_x(ci.lower_ci));
103 bar.setAttribute('y', height / 3);
105 // Marker line for average.
106 let marker = document.createElementNS('http://www.w3.org/2000/svg', 'line');
107 marker.classList.add('marker');
108 marker.setAttribute('x1', to_x(ci.val));
109 marker.setAttribute('x2', to_x(ci.val));
110 marker.setAttribute('y1', height / 6);
111 marker.setAttribute('y2', height * 5 / 6);
115 svg.appendChild(bar);
116 svg.appendChild(marker);
118 element.appendChild(svg);
121 function process_matches(json) {
123 for (const player of json['players']) {
124 players[player['player_id']] = {
125 'name': player['name'],
126 'number': player['number'],
140 'playing_time_ms': 0,
144 'offensive_points_completed': 0,
145 'offensive_points_won': 0,
146 'defensive_points_completed': 0,
147 'defensive_points_won': 0,
149 'offensive_soft_plus': 0,
150 'offensive_soft_minus': 0,
151 'defensive_soft_plus': 0,
152 'defensive_soft_minus': 0,
159 'last_point_seen': null,
160 'on_field_since': null,
165 players['globals'] = {
167 'playing_time_ms': 0,
170 'offensive_points_completed': 0,
171 'offensive_points_won': 0,
172 'defensive_points_completed': 0,
173 'defensive_points_won': 0,
175 let globals = players['globals'];
177 for (const match of json['matches']) {
181 let prev_handler = null;
182 let live_since = null;
185 let pull_started = null;
186 let last_pull_was_ours = null; // Effectively whether we're playing an O or D point (not affected by turnovers).
188 let game_started = null;
189 let last_goal = null;
190 for (const [q,p] of Object.entries(players)) {
191 p.on_field_since = null;
192 p.last_point_seen = null;
194 for (const e of match['events']) {
196 let type = e['type'];
197 let p = players[e['player']];
200 if (type === 'in' && p.on_field_since === null) {
201 p.on_field_since = t;
202 } else if (type === 'out') {
203 take_off_field(p, t, live_since);
206 // Liveness management
207 if (type === 'pull' || type === 'their_pull' || type === 'restart') {
209 } else if (type === 'catch' && last_pull_was_ours === null) {
210 // Someone forgot to add the pull, so we'll need to wing it.
211 console.log('Missing pull on ' + our_score + '\u2013' + their_score + ' in ' + match['description'] + '; pretending to have one.');
213 last_pull_was_ours = !offense;
214 } else if (type === 'goal' || type === 'their_goal' || type === 'stoppage') {
215 for (const [q,p] of Object.entries(players)) {
216 if (p.on_field_since === null) {
219 if (type !== 'stoppage' && p.last_point_seen !== point_num) {
220 // In case the player did nothing this point,
221 // not even subbing in.
222 p.last_point_seen = point_num;
225 attribute_player_time(p, t, live_since);
227 if (type !== 'stoppage') {
228 if (last_pull_was_ours === true) { // D point.
229 ++p.defensive_points_completed;
230 if (type === 'goal') {
231 ++p.defensive_points_won;
233 } else if (last_pull_was_ours === false) { // O point.
234 ++p.offensive_points_completed;
235 if (type === 'goal') {
236 ++p.offensive_points_won;
242 if (type !== 'stoppage') {
244 ++globals.points_played;
245 if (last_pull_was_ours === true) { // D point.
246 ++globals.defensive_points_completed;
247 if (type === 'goal') {
248 ++globals.defensive_points_won;
250 } else if (last_pull_was_ours === false) { // O point.
251 ++globals.offensive_points_completed;
252 if (type === 'goal') {
253 ++globals.offensive_points_won;
257 if (live_since !== null) {
258 globals.playing_time_ms += t - live_since;
265 if (type === 'goal') {
267 } else if (type === 'their_goal') {
271 // Point count management
272 if (p !== undefined && type !== 'out' && p.last_point_seen !== point_num) {
273 p.last_point_seen = point_num;
276 if (type === 'goal' || type === 'their_goal') {
280 if (type !== 'out' && game_started === null) {
285 if (type === 'pull') {
286 puller = e['player'];
289 } else if (type === 'in' || type === 'out' || type === 'stoppage' || type === 'restart' || type === 'unknown' || type === 'set_defense' || type === 'set_offense') {
290 // No effect on pull.
291 } else if (type === 'pull_landed' && puller !== null) {
292 players[puller].pull_times.push(t - pull_started);
293 } else if (type === 'pull_oob' && puller !== null) {
294 ++players[puller].oob_pulls;
296 // Not pulling (if there was one, we never recorded its outcome, but still count it).
297 puller = pull_started = null;
300 // Offense/defense management (TODO: use it for actual counting)
301 if (type === 'set_defense' || type === 'goal' || type === 'throwaway' || type === 'drop') {
303 } else if (type === 'set_offense' || type === 'their_goal' || type === 'their_throwaway' || type === 'defense' || type === 'interception') {
306 if (type === 'pull') {
307 last_pull_was_ours = true;
308 } else if (type === 'their_pull') {
309 last_pull_was_ours = false;
310 } else if (type === 'set_offense' && last_pull_was_ours === null) {
311 // set_offense could either be “changed to offense for some reason
312 // we could not express”, or “we started in the middle of a point,
313 // and we are offense”. We assume that if we already saw the pull,
314 // it's the former, and if not, it's the latter; thus, the === null
315 // test above. (It could also be “we set offense before the pull,
316 // so that we get the right button enabled”, in which case it will
317 // be overwritten by the next pull/their_pull event anyway.)
318 last_pull_was_ours = false;
319 } else if (type === 'set_defense' && last_pull_was_ours === null) {
321 last_pull_was_ours = true;
322 } else if (type === 'goal' || type === 'their_goal') {
323 last_pull_was_ours = null;
327 if (type === 'catch' || type === 'goal') {
328 if (handler !== null) {
329 ++players[handler].num_throws;
334 if (type === 'goal') {
335 if (prev_handler !== null) {
336 ++players[prev_handler].hockey_assists;
338 if (handler !== null) {
339 ++players[handler].assists;
342 handler = prev_handler = null;
344 // Update hold history.
345 prev_handler = handler;
346 handler = e['player'];
348 } else if (type === 'throwaway') {
351 handler = prev_handler = null;
352 } else if (type === 'drop') {
354 handler = prev_handler = null;
355 } else if (type === 'defense') {
357 } else if (type === 'interception') {
362 handler = e['player'];
363 } else if (type === 'offensive_soft_plus' || type === 'offensive_soft_minus' || type === 'defensive_soft_plus' || type === 'defensive_soft_minus') {
365 } else if (type !== 'in' && type !== 'out' && type !== 'pull' &&
366 type !== 'their_goal' && type !== 'stoppage' && type !== 'restart' && type !== 'unknown' &&
367 type !== 'set_defense' && type !== 'goal' && type !== 'throwaway' &&
368 type !== 'drop' && type !== 'set_offense' && type !== 'their_goal' &&
369 type !== 'pull' && type !== 'pull_landed' && type !== 'pull_oob' && type !== 'their_pull' &&
370 type !== 'their_throwaway' && type !== 'defense' && type !== 'interception') {
371 console.log("Unknown event:", e);
375 // Add field time for all players still left at match end.
376 for (const [q,p] of Object.entries(players)) {
377 if (p.on_field_since !== null && last_goal !== null) {
378 p.field_time_ms += last_goal - p.on_field_since;
381 if (game_started !== null && last_goal !== null) {
382 globals.field_time_ms += last_goal - game_started;
384 if (live_since !== null && last_goal !== null) {
385 globals.playing_time_ms += last_goal - live_since;
389 let chosen_category = get_chosen_category();
390 write_main_menu(chosen_category);
393 if (chosen_category === 'general') {
394 rows = make_table_general(players);
395 } else if (chosen_category === 'offense') {
396 rows = make_table_offense(players);
397 } else if (chosen_category === 'defense') {
398 rows = make_table_defense(players);
399 } else if (chosen_category === 'playing_time') {
400 rows = make_table_playing_time(players);
401 } else if (chosen_category === 'per_point') {
402 rows = make_table_per_point(players);
404 document.getElementById('stats').replaceChildren(...rows);
407 function get_chosen_category() {
408 if (window.location.hash === '#offense') {
410 } else if (window.location.hash === '#defense') {
412 } else if (window.location.hash === '#playing_time') {
413 return 'playing_time';
414 } else if (window.location.hash === '#per_point') {
421 function write_main_menu(chosen_category) {
423 if (chosen_category === 'general') {
424 let span = document.createElement('span');
425 span.innerText = 'General';
428 let a = document.createElement('a');
429 a.appendChild(document.createTextNode('General'));
430 a.setAttribute('href', '#general');
434 if (chosen_category === 'offense') {
435 let span = document.createElement('span');
436 span.innerText = 'Offense';
439 let a = document.createElement('a');
440 a.appendChild(document.createTextNode('Offense'));
441 a.setAttribute('href', '#offense');
445 if (chosen_category === 'defense') {
446 let span = document.createElement('span');
447 span.innerText = 'Defense';
450 let a = document.createElement('a');
451 a.appendChild(document.createTextNode('Defense'));
452 a.setAttribute('href', '#defense');
456 if (chosen_category === 'playing_time') {
457 let span = document.createElement('span');
458 span.innerText = 'Playing time';
461 let a = document.createElement('a');
462 a.appendChild(document.createTextNode('Playing time'));
463 a.setAttribute('href', '#playing_time');
467 if (chosen_category === 'per_point') {
468 let span = document.createElement('span');
469 span.innerText = 'Per point';
472 let a = document.createElement('a');
473 a.appendChild(document.createTextNode('Per point'));
474 a.setAttribute('href', '#per_point');
478 document.getElementById('mainmenu').replaceChildren(...elems);
481 // https://en.wikipedia.org/wiki/1.96#History
484 function make_binomial_ci(val, num, z) {
487 // https://en.wikipedia.org/wiki/Binomial_proportion_confidence_interval#Wilson_score_interval
488 let low = (avg + z*z/(2*num) - z * Math.sqrt(avg * (1.0 - avg) / num + z*z/(4*num*num))) / (1 + z*z/num);
489 let high = (avg + z*z/(2*num) + z * Math.sqrt(avg * (1.0 - avg) / num + z*z/(4*num*num))) / (1 + z*z/num);
491 // Fix the signs so that we don't get -0.00.
492 low = Math.max(low, 0.0);
502 // These can only happen once per point, but you get -1 and +1
503 // instead of 0 and +1. After we rewrite to 0 and 1, it's a binomial,
504 // and then we can rewrite back.
505 function make_efficiency_ci(points_won, points_completed, z)
507 let ci = make_binomial_ci(points_won, points_completed, z);
508 ci.val = 2.0 * ci.val - 1.0;
509 ci.lower_ci = 2.0 * ci.lower_ci - 1.0;
510 ci.upper_ci = 2.0 * ci.upper_ci - 1.0;
513 ci.desired = 0.0; // Desired = positive efficiency.
517 function make_table_general(players) {
520 let header = document.createElement('tr');
521 add_cell(header, 'th', 'Player');
522 add_cell(header, 'th', '+/-');
523 add_cell(header, 'th', 'Soft +/-');
524 add_cell(header, 'th', 'O efficiency');
525 add_cell(header, 'th', 'D efficiency');
526 add_cell(header, 'th', 'Points played');
530 for (const [q,p] of Object.entries(players)) {
531 if (q === 'globals') continue;
532 let row = document.createElement('tr');
533 let pm = p.goals + p.assists + p.hockey_assists + p.defenses - p.throwaways - p.drops;
534 let soft_pm = p.offensive_soft_plus + p.defensive_soft_plus - p.offensive_soft_minus - p.defensive_soft_minus;
535 let o_efficiency = make_efficiency_ci(p.offensive_points_won, p.offensive_points_completed, z);
536 let d_efficiency = make_efficiency_ci(p.defensive_points_won, p.defensive_points_completed, z);
537 add_3cell(row, p.name, 'name'); // TODO: number?
538 add_3cell(row, pm > 0 ? ('+' + pm) : pm);
539 add_3cell(row, soft_pm > 0 ? ('+' + soft_pm) : soft_pm);
540 add_3cell_ci(row, o_efficiency);
541 add_3cell_ci(row, d_efficiency);
542 add_3cell(row, p.points_played);
547 let globals = players['globals'];
548 let o_efficiency = make_efficiency_ci(globals.offensive_points_won, globals.offensive_points_completed, z);
549 let d_efficiency = make_efficiency_ci(globals.defensive_points_won, globals.defensive_points_completed, z);
550 let row = document.createElement('tr');
554 add_3cell_ci(row, o_efficiency);
555 add_3cell_ci(row, d_efficiency);
556 add_3cell(row, globals.points_played);
562 function make_table_offense(players) {
565 let header = document.createElement('tr');
566 add_cell(header, 'th', 'Player');
567 add_cell(header, 'th', 'Goals');
568 add_cell(header, 'th', 'Assists');
569 add_cell(header, 'th', 'Hockey assists');
570 add_cell(header, 'th', 'Throws');
571 add_cell(header, 'th', 'Throwaways');
572 add_cell(header, 'th', '%OK');
573 add_cell(header, 'th', 'Catches');
574 add_cell(header, 'th', 'Drops');
575 add_cell(header, 'th', '%OK');
576 add_cell(header, 'th', 'Soft +/-');
584 for (const [q,p] of Object.entries(players)) {
585 if (q === 'globals') continue;
586 let throw_ok = make_binomial_ci(p.num_throws - p.throwaways, p.num_throws, z);
587 let catch_ok = make_binomial_ci(p.catches, p.catches + p.drops, z);
589 throw_ok.format = 'percentage';
590 catch_ok.format = 'percentage';
592 // Desire at least 90% percentage. Fairly arbitrary.
593 throw_ok.desired = 0.9;
594 catch_ok.desired = 0.9;
596 let row = document.createElement('tr');
597 add_3cell(row, p.name, 'name'); // TODO: number?
598 add_3cell(row, p.goals);
599 add_3cell(row, p.assists);
600 add_3cell(row, p.hockey_assists);
601 add_3cell(row, p.num_throws);
602 add_3cell(row, p.throwaways);
603 add_3cell_ci(row, throw_ok);
604 add_3cell(row, p.catches);
605 add_3cell(row, p.drops);
606 add_3cell_ci(row, catch_ok);
607 add_3cell(row, '+' + p.offensive_soft_plus);
608 add_3cell(row, '-' + p.offensive_soft_minus);
611 num_throws += p.num_throws;
612 throwaways += p.throwaways;
613 catches += p.catches;
618 let throw_ok = make_binomial_ci(num_throws - throwaways, num_throws, z);
619 let catch_ok = make_binomial_ci(catches, catches + drops, z);
620 throw_ok.format = 'percentage';
621 catch_ok.format = 'percentage';
622 throw_ok.desired = 0.9;
623 catch_ok.desired = 0.9;
625 let row = document.createElement('tr');
630 add_3cell(row, num_throws);
631 add_3cell(row, throwaways);
632 add_3cell_ci(row, throw_ok);
633 add_3cell(row, catches);
634 add_3cell(row, drops);
635 add_3cell_ci(row, catch_ok);
643 function make_table_defense(players) {
646 let header = document.createElement('tr');
647 add_cell(header, 'th', 'Player');
648 add_cell(header, 'th', 'Ds');
649 add_cell(header, 'th', 'Pulls');
650 add_cell(header, 'th', 'OOB pulls');
651 add_cell(header, 'th', 'Avg. hang time (IB)');
652 add_cell(header, 'th', 'Soft +/-');
655 for (const [q,p] of Object.entries(players)) {
656 if (q === 'globals') continue;
658 for (const t of p.pull_times) {
661 let avg_time = 1e-3 * sum_time / p.pulls;
662 let oob_pct = 100 * p.oob_pulls / p.pulls;
664 let row = document.createElement('tr');
665 add_3cell(row, p.name, 'name'); // TODO: number?
666 add_3cell(row, p.defenses);
667 add_3cell(row, p.pulls);
669 add_3cell(row, 'N/A');
671 add_3cell(row, p.oob_pulls + ' (' + oob_pct.toFixed(0) + '%)');
673 if (p.pulls > p.oob_pulls) {
674 add_3cell(row, avg_time.toFixed(1) + ' sec');
676 add_3cell(row, 'N/A');
678 add_3cell(row, '+' + p.defensive_soft_plus);
679 add_3cell(row, '-' + p.defensive_soft_minus);
685 function make_table_playing_time(players) {
688 let header = document.createElement('tr');
689 add_cell(header, 'th', 'Player');
690 add_cell(header, 'th', 'Points played');
691 add_cell(header, 'th', 'Time played');
692 add_cell(header, 'th', 'Time on field');
693 add_cell(header, 'th', 'O points');
694 add_cell(header, 'th', 'D points');
698 for (const [q,p] of Object.entries(players)) {
699 if (q === 'globals') continue;
700 let row = document.createElement('tr');
701 add_3cell(row, p.name, 'name'); // TODO: number?
702 add_3cell(row, p.points_played);
703 add_3cell(row, Math.floor(p.playing_time_ms / 60000) + ' min');
704 add_3cell(row, Math.floor(p.field_time_ms / 60000) + ' min');
705 add_3cell(row, p.offensive_points_completed);
706 add_3cell(row, p.defensive_points_completed);
711 let globals = players['globals'];
712 let row = document.createElement('tr');
714 add_3cell(row, globals.points_played);
715 add_3cell(row, Math.floor(globals.playing_time_ms / 60000) + ' min');
716 add_3cell(row, Math.floor(globals.field_time_ms / 60000) + ' min');
717 add_3cell(row, globals.offensive_points_completed);
718 add_3cell(row, globals.defensive_points_completed);
724 function make_table_per_point(players) {
727 let header = document.createElement('tr');
728 add_cell(header, 'th', 'Player');
729 add_cell(header, 'th', 'Goals');
730 add_cell(header, 'th', 'Assists');
731 add_cell(header, 'th', 'Hockey assists');
732 add_cell(header, 'th', 'Ds');
733 add_cell(header, 'th', 'Throwaways');
734 add_cell(header, 'th', 'Drops');
735 add_cell(header, 'th', 'Touches');
741 let hockey_assists = 0;
746 for (const [q,p] of Object.entries(players)) {
747 if (q === 'globals') continue;
748 let row = document.createElement('tr');
749 add_3cell(row, p.name, 'name'); // TODO: number?
750 if (p.points_played > 0) {
751 add_3cell(row, p.goals == 0 ? 0 : (p.goals / p.points_played).toFixed(2));
752 add_3cell(row, p.assists == 0 ? 0 : (p.assists / p.points_played).toFixed(2));
753 add_3cell(row, p.hockey_assists == 0 ? 0 : (p.hockey_assists / p.points_played).toFixed(2));
754 add_3cell(row, p.defenses == 0 ? 0 : (p.defenses / p.points_played).toFixed(2));
755 add_3cell(row, p.throwaways == 0 ? 0 : (p.throwaways / p.points_played).toFixed(2));
756 add_3cell(row, p.drops == 0 ? 0 : (p.drops / p.points_played).toFixed(2));
757 add_3cell(row, p.touches == 0 ? 0 : (p.touches / p.points_played).toFixed(2));
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');
765 add_3cell(row, 'N/A');
770 assists += p.assists;
771 hockey_assists += p.hockey_assists;
772 defenses += p.defenses;
773 throwaways += p.throwaways;
775 touches += p.touches;
779 let globals = players['globals'];
780 let row = document.createElement('tr');
782 if (globals.points_played > 0) {
783 add_3cell(row, goals == 0 ? 0 : (goals / globals.points_played).toFixed(2));
784 add_3cell(row, assists == 0 ? 0 : (assists / globals.points_played).toFixed(2));
785 add_3cell(row, hockey_assists == 0 ? 0 : (hockey_assists / globals.points_played).toFixed(2));
786 add_3cell(row, defenses == 0 ? 0 : (defenses / globals.points_played).toFixed(2));
787 add_3cell(row, throwaways == 0 ? 0 : (throwaways / globals.points_played).toFixed(2));
788 add_3cell(row, drops == 0 ? 0 : (drops / globals.points_played).toFixed(2));
789 add_3cell(row, touches == 0 ? 0 : (touches / globals.points_played).toFixed(2));
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');
797 add_3cell(row, 'N/A');