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 tr.appendChild(element);
39 function process_matches(json) {
41 for (const player of json['players']) {
42 players[player['player_id']] = {
43 'name': player['name'],
44 'number': player['number'],
62 'offensive_points_completed': 0,
63 'offensive_points_won': 0,
64 'defensive_points_completed': 0,
65 'defensive_points_won': 0,
67 'offensive_soft_plus': 0,
68 'offensive_soft_minus': 0,
69 'defensive_soft_plus': 0,
70 'defensive_soft_minus': 0,
77 'last_point_seen': null,
78 'on_field_since': null,
83 players['globals'] = {
88 'offensive_points_completed': 0,
89 'offensive_points_won': 0,
90 'defensive_points_completed': 0,
91 'defensive_points_won': 0,
93 let globals = players['globals'];
95 for (const match of json['matches']) {
99 let prev_handler = null;
100 let live_since = null;
103 let pull_started = null;
104 let last_pull_was_ours = null; // Effectively whether we're playing an O or D point (not affected by turnovers).
106 let game_started = null;
107 let last_goal = null;
108 for (const [q,p] of Object.entries(players)) {
109 p.on_field_since = null;
110 p.last_point_seen = null;
112 for (const e of match['events']) {
114 let type = e['type'];
115 let p = players[e['player']];
118 if (type === 'in' && p.on_field_since === null) {
119 p.on_field_since = t;
120 } else if (type === 'out') {
121 take_off_field(p, t, live_since);
124 // Liveness management
125 if (type === 'pull' || type === 'their_pull' || type === 'restart') {
127 } else if (type === 'catch' && last_pull_was_ours === null) {
128 // Someone forgot to add the pull, so we'll need to wing it.
129 console.log('Missing pull on ' + our_score + '\u2013' + their_score + ' in ' + match['description'] + '; pretending to have one.');
131 last_pull_was_ours = !offense;
132 } else if (type === 'goal' || type === 'their_goal' || type === 'stoppage') {
133 for (const [q,p] of Object.entries(players)) {
134 if (p.on_field_since === null) {
137 if (type !== 'stoppage' && p.last_point_seen !== point_num) {
138 // In case the player did nothing this point,
139 // not even subbing in.
140 p.last_point_seen = point_num;
143 attribute_player_time(p, t, live_since);
145 if (type !== 'stoppage') {
146 if (last_pull_was_ours === true) { // D point.
147 ++p.defensive_points_completed;
148 if (type === 'goal') {
149 ++p.defensive_points_won;
151 } else if (last_pull_was_ours === false) { // O point.
152 ++p.offensive_points_completed;
153 if (type === 'goal') {
154 ++p.offensive_points_won;
160 if (type !== 'stoppage') {
162 ++globals.points_played;
163 if (last_pull_was_ours === true) { // D point.
164 ++globals.defensive_points_completed;
165 if (type === 'goal') {
166 ++globals.defensive_points_won;
168 } else if (last_pull_was_ours === false) { // O point.
169 ++globals.offensive_points_completed;
170 if (type === 'goal') {
171 ++globals.offensive_points_won;
175 if (live_since !== null) {
176 globals.playing_time_ms += t - live_since;
183 if (type === 'goal') {
185 } else if (type === 'their_goal') {
189 // Point count management
190 if (p !== undefined && type !== 'out' && p.last_point_seen !== point_num) {
191 p.last_point_seen = point_num;
194 if (type === 'goal' || type === 'their_goal') {
198 if (type !== 'out' && game_started === null) {
203 if (type === 'pull') {
204 puller = e['player'];
207 } else if (type === 'in' || type === 'out' || type === 'stoppage' || type === 'restart' || type === 'unknown' || type === 'set_defense' || type === 'set_offense') {
208 // No effect on pull.
209 } else if (type === 'pull_landed' && puller !== null) {
210 players[puller].pull_times.push(t - pull_started);
211 } else if (type === 'pull_oob' && puller !== null) {
212 ++players[puller].oob_pulls;
214 // Not pulling (if there was one, we never recorded its outcome, but still count it).
215 puller = pull_started = null;
218 // Offense/defense management (TODO: use it for actual counting)
219 if (type === 'set_defense' || type === 'goal' || type === 'throwaway' || type === 'drop') {
221 } else if (type === 'set_offense' || type === 'their_goal' || type === 'their_throwaway' || type === 'defense' || type === 'interception') {
224 if (type === 'pull') {
225 last_pull_was_ours = true;
226 } else if (type === 'their_pull') {
227 last_pull_was_ours = false;
228 } else if (type === 'set_offense' && last_pull_was_ours === null) {
229 // set_offense could either be “changed to offense for some reason
230 // we could not express”, or “we started in the middle of a point,
231 // and we are offense”. We assume that if we already saw the pull,
232 // it's the former, and if not, it's the latter; thus, the === null
233 // test above. (It could also be “we set offense before the pull,
234 // so that we get the right button enabled”, in which case it will
235 // be overwritten by the next pull/their_pull event anyway.)
236 last_pull_was_ours = false;
237 } else if (type === 'set_defense' && last_pull_was_ours === null) {
239 last_pull_was_ours = true;
240 } else if (type === 'goal' || type === 'their_goal') {
241 last_pull_was_ours = null;
245 if (type === 'catch' || type === 'goal') {
246 if (handler !== null) {
247 ++players[handler].num_throws;
252 if (type === 'goal') {
253 if (prev_handler !== null) {
254 ++players[prev_handler].hockey_assists;
256 if (handler !== null) {
257 ++players[handler].assists;
260 handler = prev_handler = null;
262 // Update hold history.
263 prev_handler = handler;
264 handler = e['player'];
266 } else if (type === 'throwaway') {
269 handler = prev_handler = null;
270 } else if (type === 'drop') {
272 handler = prev_handler = null;
273 } else if (type === 'defense') {
275 } else if (type === 'interception') {
280 handler = e['player'];
281 } else if (type === 'offensive_soft_plus' || type === 'offensive_soft_minus' || type === 'defensive_soft_plus' || type === 'defensive_soft_minus') {
283 } else if (type !== 'in' && type !== 'out' && type !== 'pull' &&
284 type !== 'their_goal' && type !== 'stoppage' && type !== 'restart' && type !== 'unknown' &&
285 type !== 'set_defense' && type !== 'goal' && type !== 'throwaway' &&
286 type !== 'drop' && type !== 'set_offense' && type !== 'their_goal' &&
287 type !== 'pull' && type !== 'pull_landed' && type !== 'pull_oob' && type !== 'their_pull' &&
288 type !== 'their_throwaway' && type !== 'defense' && type !== 'interception') {
289 console.log("Unknown event:", e);
293 // Add field time for all players still left at match end.
294 for (const [q,p] of Object.entries(players)) {
295 if (p.on_field_since !== null && last_goal !== null) {
296 p.field_time_ms += last_goal - p.on_field_since;
299 if (game_started !== null && last_goal !== null) {
300 globals.field_time_ms += last_goal - game_started;
302 if (live_since !== null && last_goal !== null) {
303 globals.playing_time_ms += last_goal - live_since;
307 let chosen_category = get_chosen_category();
308 write_main_menu(chosen_category);
311 if (chosen_category === 'general') {
312 rows = make_table_general(players);
313 } else if (chosen_category === 'offense') {
314 rows = make_table_offense(players);
315 } else if (chosen_category === 'defense') {
316 rows = make_table_defense(players);
317 } else if (chosen_category === 'playing_time') {
318 rows = make_table_playing_time(players);
319 } else if (chosen_category === 'per_point') {
320 rows = make_table_per_point(players);
322 document.getElementById('stats').replaceChildren(...rows);
325 function get_chosen_category() {
326 if (window.location.hash === '#offense') {
328 } else if (window.location.hash === '#defense') {
330 } else if (window.location.hash === '#playing_time') {
331 return 'playing_time';
332 } else if (window.location.hash === '#per_point') {
339 function write_main_menu(chosen_category) {
341 if (chosen_category === 'general') {
342 elems.push(document.createTextNode('General'));
344 let a = document.createElement('a');
345 a.appendChild(document.createTextNode('General'));
346 a.setAttribute('href', '#general');
350 elems.push(document.createTextNode(' | '));
351 if (chosen_category === 'offense') {
352 elems.push(document.createTextNode('Offense'));
354 let a = document.createElement('a');
355 a.appendChild(document.createTextNode('Offense'));
356 a.setAttribute('href', '#offense');
360 elems.push(document.createTextNode(' | '));
361 if (chosen_category === 'defense') {
362 elems.push(document.createTextNode('Defense'));
364 let a = document.createElement('a');
365 a.appendChild(document.createTextNode('Defense'));
366 a.setAttribute('href', '#defense');
370 elems.push(document.createTextNode(' | '));
371 if (chosen_category === 'playing_time') {
372 elems.push(document.createTextNode('Playing time'));
374 let a = document.createElement('a');
375 a.appendChild(document.createTextNode('Playing time'));
376 a.setAttribute('href', '#playing_time');
380 elems.push(document.createTextNode(' | '));
381 if (chosen_category === 'per_point') {
382 elems.push(document.createTextNode('Per point'));
384 let a = document.createElement('a');
385 a.appendChild(document.createTextNode('Per point'));
386 a.setAttribute('href', '#per_point');
390 document.getElementById('mainmenu').replaceChildren(...elems);
393 function make_table_general(players) {
396 let header = document.createElement('tr');
397 add_cell(header, 'th', 'Player');
398 add_cell(header, 'th', '+/-');
399 add_cell(header, 'th', 'Soft +/-');
400 add_cell(header, 'th', 'O efficiency');
401 add_cell(header, 'th', 'D efficiency');
402 add_cell(header, 'th', 'Points played');
406 for (const [q,p] of Object.entries(players)) {
407 if (q === 'globals') continue;
408 let row = document.createElement('tr');
409 let pm = p.goals + p.assists + p.hockey_assists + p.defenses - p.throwaways - p.drops;
410 let soft_pm = p.offensive_soft_plus + p.defensive_soft_plus - p.offensive_soft_minus - p.defensive_soft_minus;
411 let o_efficiency = (p.offensive_points_won / p.offensive_points_completed) * 2 - 1;
412 let d_efficiency = (p.defensive_points_won / p.defensive_points_completed) * 2 - 1;
413 add_cell(row, 'td', p.name); // TODO: number?
414 add_cell(row, 'td', pm > 0 ? ('+' + pm) : pm);
415 add_cell(row, 'td', soft_pm > 0 ? ('+' + soft_pm) : soft_pm);
416 add_cell(row, 'td', p.offensive_points_completed > 0 ? o_efficiency.toFixed(2) : 'N/A');
417 add_cell(row, 'td', p.defensive_points_completed > 0 ? d_efficiency.toFixed(2) : 'N/A');
418 add_cell(row, 'td', p.points_played);
423 let globals = players['globals'];
424 let o_efficiency = (globals.offensive_points_won / globals.offensive_points_completed) * 2 - 1;
425 let d_efficiency = (globals.defensive_points_won / globals.defensive_points_completed) * 2 - 1;
426 let row = document.createElement('tr');
427 add_cell(row, 'td', '');
428 add_cell(row, 'td', '');
429 add_cell(row, 'td', '');
430 add_cell(row, 'td', globals.offensive_points_completed > 0 ? o_efficiency.toFixed(2) : 'N/A');
431 add_cell(row, 'td', globals.defensive_points_completed > 0 ? d_efficiency.toFixed(2) : 'N/A');
432 add_cell(row, 'td', globals.points_played);
438 function make_table_offense(players) {
441 let header = document.createElement('tr');
442 add_cell(header, 'th', 'Player');
443 add_cell(header, 'th', 'Goals');
444 add_cell(header, 'th', 'Assists');
445 add_cell(header, 'th', 'Hockey assists');
446 add_cell(header, 'th', 'Throws');
447 add_cell(header, 'th', 'Throwaways');
448 add_cell(header, 'th', '%OK');
449 add_cell(header, 'th', 'Catches');
450 add_cell(header, 'th', 'Drops');
451 add_cell(header, 'th', '%OK');
452 add_cell(header, 'th', 'Soft +/-');
460 for (const [q,p] of Object.entries(players)) {
461 if (q === 'globals') continue;
462 let throw_ok = 100 * (1 - p.throwaways / p.num_throws);
463 let catch_ok = 100 * (p.catches / (p.catches + p.drops));
465 let row = document.createElement('tr');
466 add_cell(row, 'td', p.name); // TODO: number?
467 add_cell(row, 'td', p.goals);
468 add_cell(row, 'td', p.assists);
469 add_cell(row, 'td', p.hockey_assists);
470 add_cell(row, 'td', p.num_throws);
471 add_cell(row, 'td', p.throwaways);
472 if (p.num_throws > 0) {
473 add_cell(row, 'td', throw_ok.toFixed(0) + '%');
475 add_cell(row, 'td', 'N/A');
477 add_cell(row, 'td', p.catches);
478 add_cell(row, 'td', p.drops);
479 if (p.catches + p.drops > 0) {
480 add_cell(row, 'td', catch_ok.toFixed(0) + '%');
482 add_cell(row, 'td', 'N/A');
484 add_cell(row, 'td', '+' + p.offensive_soft_plus);
485 add_cell(row, 'td', '-' + p.offensive_soft_minus);
488 num_throws += p.num_throws;
489 throwaways += p.throwaways;
490 catches += p.catches;
495 let throw_ok = 100 * (1 - throwaways / num_throws);
496 let catch_ok = 100 * (catches / (catches + drops));
498 let row = document.createElement('tr');
499 add_cell(row, 'td', '');
500 add_cell(row, 'td', '');
501 add_cell(row, 'td', '');
502 add_cell(row, 'td', '');
503 add_cell(row, 'td', num_throws);
504 add_cell(row, 'td', throwaways);
505 add_cell(row, 'td', throw_ok.toFixed(0) + '%');
506 add_cell(row, 'td', catches);
507 add_cell(row, 'td', drops);
508 add_cell(row, 'td', catch_ok.toFixed(0) + '%');
509 add_cell(row, 'td', '');
510 add_cell(row, 'td', '');
516 function make_table_defense(players) {
519 let header = document.createElement('tr');
520 add_cell(header, 'th', 'Player');
521 add_cell(header, 'th', 'Ds');
522 add_cell(header, 'th', 'Pulls');
523 add_cell(header, 'th', 'OOB pulls');
524 add_cell(header, 'th', 'Avg. hang time (IB)');
525 add_cell(header, 'th', 'Soft +/-');
528 for (const [q,p] of Object.entries(players)) {
529 if (q === 'globals') continue;
531 for (const t of p.pull_times) {
534 let avg_time = 1e-3 * sum_time / p.pulls;
535 let oob_pct = 100 * p.oob_pulls / p.pulls;
537 let row = document.createElement('tr');
538 add_cell(row, 'td', p.name); // TODO: number?
539 add_cell(row, 'td', p.defenses);
540 add_cell(row, 'td', p.pulls);
542 add_cell(row, 'td', 'N/A');
544 add_cell(row, 'td', p.oob_pulls + ' (' + oob_pct.toFixed(0) + '%)');
546 if (p.pulls > p.oob_pulls) {
547 add_cell(row, 'td', avg_time.toFixed(1) + ' sec');
549 add_cell(row, 'td', 'N/A');
551 add_cell(row, 'td', '+' + p.defensive_soft_plus);
552 add_cell(row, 'td', '-' + p.defensive_soft_minus);
558 function make_table_playing_time(players) {
561 let header = document.createElement('tr');
562 add_cell(header, 'th', 'Player');
563 add_cell(header, 'th', 'Points played');
564 add_cell(header, 'th', 'Time played');
565 add_cell(header, 'th', 'Time on field');
566 add_cell(header, 'th', 'O points');
567 add_cell(header, 'th', 'D points');
571 for (const [q,p] of Object.entries(players)) {
572 if (q === 'globals') continue;
573 let row = document.createElement('tr');
574 add_cell(row, 'td', p.name); // TODO: number?
575 add_cell(row, 'td', p.points_played);
576 add_cell(row, 'td', Math.floor(p.playing_time_ms / 60000) + ' min');
577 add_cell(row, 'td', Math.floor(p.field_time_ms / 60000) + ' min');
578 add_cell(row, 'td', p.offensive_points_completed);
579 add_cell(row, 'td', p.defensive_points_completed);
584 let globals = players['globals'];
585 let row = document.createElement('tr');
586 add_cell(row, 'td', '');
587 add_cell(row, 'td', globals.points_played);
588 add_cell(row, 'td', Math.floor(globals.playing_time_ms / 60000) + ' min');
589 add_cell(row, 'td', Math.floor(globals.field_time_ms / 60000) + ' min');
590 add_cell(row, 'td', globals.offensive_points_completed);
591 add_cell(row, 'td', globals.defensive_points_completed);
597 function make_table_per_point(players) {
600 let header = document.createElement('tr');
601 add_cell(header, 'th', 'Player');
602 add_cell(header, 'th', 'Goals');
603 add_cell(header, 'th', 'Assists');
604 add_cell(header, 'th', 'Hockey assists');
605 add_cell(header, 'th', 'Ds');
606 add_cell(header, 'th', 'Throwaways');
607 add_cell(header, 'th', 'Drops');
613 let hockey_assists = 0;
617 for (const [q,p] of Object.entries(players)) {
618 if (q === 'globals') continue;
619 let row = document.createElement('tr');
620 add_cell(row, 'td', p.name); // TODO: number?
621 if (p.points_played > 0) {
622 add_cell(row, 'td', p.goals == 0 ? 0 : (p.goals / p.points_played).toFixed(2));
623 add_cell(row, 'td', p.assists == 0 ? 0 : (p.assists / p.points_played).toFixed(2));
624 add_cell(row, 'td', p.hockey_assists == 0 ? 0 : (p.hockey_assists / p.points_played).toFixed(2));
625 add_cell(row, 'td', p.defenses == 0 ? 0 : (p.defenses / p.points_played).toFixed(2));
626 add_cell(row, 'td', p.throwaways == 0 ? 0 : (p.throwaways / p.points_played).toFixed(2));
627 add_cell(row, 'td', p.drops == 0 ? 0 : (p.drops / p.points_played).toFixed(2));
629 add_cell(row, 'td', 'N/A');
630 add_cell(row, 'td', 'N/A');
631 add_cell(row, 'td', 'N/A');
632 add_cell(row, 'td', 'N/A');
633 add_cell(row, 'td', 'N/A');
634 add_cell(row, 'td', 'N/A');
639 assists += p.assists;
640 hockey_assists += p.hockey_assists;
641 defenses += p.defenses;
642 throwaways += p.throwaways;
647 let globals = players['globals'];
648 let row = document.createElement('tr');
649 add_cell(row, 'td', '');
650 if (globals.points_played > 0) {
651 add_cell(row, 'td', goals == 0 ? 0 : (goals / globals.points_played).toFixed(2));
652 add_cell(row, 'td', assists == 0 ? 0 : (assists / globals.points_played).toFixed(2));
653 add_cell(row, 'td', hockey_assists == 0 ? 0 : (hockey_assists / globals.points_played).toFixed(2));
654 add_cell(row, 'td', defenses == 0 ? 0 : (defenses / globals.points_played).toFixed(2));
655 add_cell(row, 'td', throwaways == 0 ? 0 : (throwaways / globals.points_played).toFixed(2));
656 add_cell(row, 'td', drops == 0 ? 0 : (drops / globals.points_played).toFixed(2));
658 add_cell(row, 'td', 'N/A');
659 add_cell(row, 'td', 'N/A');
660 add_cell(row, 'td', 'N/A');
661 add_cell(row, 'td', 'N/A');
662 add_cell(row, 'td', 'N/A');
663 add_cell(row, 'td', 'N/A');