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);
57 function process_matches(json) {
59 for (const player of json['players']) {
60 players[player['player_id']] = {
61 'name': player['name'],
62 'number': player['number'],
80 'offensive_points_completed': 0,
81 'offensive_points_won': 0,
82 'defensive_points_completed': 0,
83 'defensive_points_won': 0,
85 'offensive_soft_plus': 0,
86 'offensive_soft_minus': 0,
87 'defensive_soft_plus': 0,
88 'defensive_soft_minus': 0,
95 'last_point_seen': null,
96 'on_field_since': null,
101 players['globals'] = {
103 'playing_time_ms': 0,
106 'offensive_points_completed': 0,
107 'offensive_points_won': 0,
108 'defensive_points_completed': 0,
109 'defensive_points_won': 0,
111 let globals = players['globals'];
113 for (const match of json['matches']) {
117 let prev_handler = null;
118 let live_since = null;
121 let pull_started = null;
122 let last_pull_was_ours = null; // Effectively whether we're playing an O or D point (not affected by turnovers).
124 let game_started = null;
125 let last_goal = null;
126 for (const [q,p] of Object.entries(players)) {
127 p.on_field_since = null;
128 p.last_point_seen = null;
130 for (const e of match['events']) {
132 let type = e['type'];
133 let p = players[e['player']];
136 if (type === 'in' && p.on_field_since === null) {
137 p.on_field_since = t;
138 } else if (type === 'out') {
139 take_off_field(p, t, live_since);
142 // Liveness management
143 if (type === 'pull' || type === 'their_pull' || type === 'restart') {
145 } else if (type === 'catch' && last_pull_was_ours === null) {
146 // Someone forgot to add the pull, so we'll need to wing it.
147 console.log('Missing pull on ' + our_score + '\u2013' + their_score + ' in ' + match['description'] + '; pretending to have one.');
149 last_pull_was_ours = !offense;
150 } else if (type === 'goal' || type === 'their_goal' || type === 'stoppage') {
151 for (const [q,p] of Object.entries(players)) {
152 if (p.on_field_since === null) {
155 if (type !== 'stoppage' && p.last_point_seen !== point_num) {
156 // In case the player did nothing this point,
157 // not even subbing in.
158 p.last_point_seen = point_num;
161 attribute_player_time(p, t, live_since);
163 if (type !== 'stoppage') {
164 if (last_pull_was_ours === true) { // D point.
165 ++p.defensive_points_completed;
166 if (type === 'goal') {
167 ++p.defensive_points_won;
169 } else if (last_pull_was_ours === false) { // O point.
170 ++p.offensive_points_completed;
171 if (type === 'goal') {
172 ++p.offensive_points_won;
178 if (type !== 'stoppage') {
180 ++globals.points_played;
181 if (last_pull_was_ours === true) { // D point.
182 ++globals.defensive_points_completed;
183 if (type === 'goal') {
184 ++globals.defensive_points_won;
186 } else if (last_pull_was_ours === false) { // O point.
187 ++globals.offensive_points_completed;
188 if (type === 'goal') {
189 ++globals.offensive_points_won;
193 if (live_since !== null) {
194 globals.playing_time_ms += t - live_since;
201 if (type === 'goal') {
203 } else if (type === 'their_goal') {
207 // Point count management
208 if (p !== undefined && type !== 'out' && p.last_point_seen !== point_num) {
209 p.last_point_seen = point_num;
212 if (type === 'goal' || type === 'their_goal') {
216 if (type !== 'out' && game_started === null) {
221 if (type === 'pull') {
222 puller = e['player'];
225 } else if (type === 'in' || type === 'out' || type === 'stoppage' || type === 'restart' || type === 'unknown' || type === 'set_defense' || type === 'set_offense') {
226 // No effect on pull.
227 } else if (type === 'pull_landed' && puller !== null) {
228 players[puller].pull_times.push(t - pull_started);
229 } else if (type === 'pull_oob' && puller !== null) {
230 ++players[puller].oob_pulls;
232 // Not pulling (if there was one, we never recorded its outcome, but still count it).
233 puller = pull_started = null;
236 // Offense/defense management (TODO: use it for actual counting)
237 if (type === 'set_defense' || type === 'goal' || type === 'throwaway' || type === 'drop') {
239 } else if (type === 'set_offense' || type === 'their_goal' || type === 'their_throwaway' || type === 'defense' || type === 'interception') {
242 if (type === 'pull') {
243 last_pull_was_ours = true;
244 } else if (type === 'their_pull') {
245 last_pull_was_ours = false;
246 } else if (type === 'set_offense' && last_pull_was_ours === null) {
247 // set_offense could either be “changed to offense for some reason
248 // we could not express”, or “we started in the middle of a point,
249 // and we are offense”. We assume that if we already saw the pull,
250 // it's the former, and if not, it's the latter; thus, the === null
251 // test above. (It could also be “we set offense before the pull,
252 // so that we get the right button enabled”, in which case it will
253 // be overwritten by the next pull/their_pull event anyway.)
254 last_pull_was_ours = false;
255 } else if (type === 'set_defense' && last_pull_was_ours === null) {
257 last_pull_was_ours = true;
258 } else if (type === 'goal' || type === 'their_goal') {
259 last_pull_was_ours = null;
263 if (type === 'catch' || type === 'goal') {
264 if (handler !== null) {
265 ++players[handler].num_throws;
270 if (type === 'goal') {
271 if (prev_handler !== null) {
272 ++players[prev_handler].hockey_assists;
274 if (handler !== null) {
275 ++players[handler].assists;
278 handler = prev_handler = null;
280 // Update hold history.
281 prev_handler = handler;
282 handler = e['player'];
284 } else if (type === 'throwaway') {
287 handler = prev_handler = null;
288 } else if (type === 'drop') {
290 handler = prev_handler = null;
291 } else if (type === 'defense') {
293 } else if (type === 'interception') {
298 handler = e['player'];
299 } else if (type === 'offensive_soft_plus' || type === 'offensive_soft_minus' || type === 'defensive_soft_plus' || type === 'defensive_soft_minus') {
301 } else if (type !== 'in' && type !== 'out' && type !== 'pull' &&
302 type !== 'their_goal' && type !== 'stoppage' && type !== 'restart' && type !== 'unknown' &&
303 type !== 'set_defense' && type !== 'goal' && type !== 'throwaway' &&
304 type !== 'drop' && type !== 'set_offense' && type !== 'their_goal' &&
305 type !== 'pull' && type !== 'pull_landed' && type !== 'pull_oob' && type !== 'their_pull' &&
306 type !== 'their_throwaway' && type !== 'defense' && type !== 'interception') {
307 console.log("Unknown event:", e);
311 // Add field time for all players still left at match end.
312 for (const [q,p] of Object.entries(players)) {
313 if (p.on_field_since !== null && last_goal !== null) {
314 p.field_time_ms += last_goal - p.on_field_since;
317 if (game_started !== null && last_goal !== null) {
318 globals.field_time_ms += last_goal - game_started;
320 if (live_since !== null && last_goal !== null) {
321 globals.playing_time_ms += last_goal - live_since;
325 let chosen_category = get_chosen_category();
326 write_main_menu(chosen_category);
329 if (chosen_category === 'general') {
330 rows = make_table_general(players);
331 } else if (chosen_category === 'offense') {
332 rows = make_table_offense(players);
333 } else if (chosen_category === 'defense') {
334 rows = make_table_defense(players);
335 } else if (chosen_category === 'playing_time') {
336 rows = make_table_playing_time(players);
337 } else if (chosen_category === 'per_point') {
338 rows = make_table_per_point(players);
340 document.getElementById('stats').replaceChildren(...rows);
343 function get_chosen_category() {
344 if (window.location.hash === '#offense') {
346 } else if (window.location.hash === '#defense') {
348 } else if (window.location.hash === '#playing_time') {
349 return 'playing_time';
350 } else if (window.location.hash === '#per_point') {
357 function write_main_menu(chosen_category) {
359 if (chosen_category === 'general') {
360 let span = document.createElement('span');
361 span.innerText = 'General';
364 let a = document.createElement('a');
365 a.appendChild(document.createTextNode('General'));
366 a.setAttribute('href', '#general');
370 if (chosen_category === 'offense') {
371 let span = document.createElement('span');
372 span.innerText = 'Offense';
375 let a = document.createElement('a');
376 a.appendChild(document.createTextNode('Offense'));
377 a.setAttribute('href', '#offense');
381 if (chosen_category === 'defense') {
382 let span = document.createElement('span');
383 span.innerText = 'Defense';
386 let a = document.createElement('a');
387 a.appendChild(document.createTextNode('Defense'));
388 a.setAttribute('href', '#defense');
392 if (chosen_category === 'playing_time') {
393 let span = document.createElement('span');
394 span.innerText = 'Playing time';
397 let a = document.createElement('a');
398 a.appendChild(document.createTextNode('Playing time'));
399 a.setAttribute('href', '#playing_time');
403 if (chosen_category === 'per_point') {
404 let span = document.createElement('span');
405 span.innerText = 'Per point';
408 let a = document.createElement('a');
409 a.appendChild(document.createTextNode('Per point'));
410 a.setAttribute('href', '#per_point');
414 document.getElementById('mainmenu').replaceChildren(...elems);
417 function make_table_general(players) {
420 let header = document.createElement('tr');
421 add_cell(header, 'th', 'Player');
422 add_cell(header, 'th', '+/-');
423 add_cell(header, 'th', 'Soft +/-');
424 add_cell(header, 'th', 'O efficiency');
425 add_cell(header, 'th', 'D efficiency');
426 add_cell(header, 'th', 'Points played');
430 for (const [q,p] of Object.entries(players)) {
431 if (q === 'globals') continue;
432 let row = document.createElement('tr');
433 let pm = p.goals + p.assists + p.hockey_assists + p.defenses - p.throwaways - p.drops;
434 let soft_pm = p.offensive_soft_plus + p.defensive_soft_plus - p.offensive_soft_minus - p.defensive_soft_minus;
435 let o_efficiency = (p.offensive_points_won / p.offensive_points_completed) * 2 - 1;
436 let d_efficiency = (p.defensive_points_won / p.defensive_points_completed) * 2 - 1;
437 add_3cell(row, p.name, 'name'); // TODO: number?
438 add_3cell(row, pm > 0 ? ('+' + pm) : pm);
439 add_3cell(row, soft_pm > 0 ? ('+' + soft_pm) : soft_pm);
440 add_3cell(row, p.offensive_points_completed > 0 ? o_efficiency.toFixed(2) : 'N/A');
441 add_3cell(row, p.defensive_points_completed > 0 ? d_efficiency.toFixed(2) : 'N/A');
442 add_3cell(row, p.points_played);
447 let globals = players['globals'];
448 let o_efficiency = (globals.offensive_points_won / globals.offensive_points_completed) * 2 - 1;
449 let d_efficiency = (globals.defensive_points_won / globals.defensive_points_completed) * 2 - 1;
450 let row = document.createElement('tr');
454 add_3cell(row, globals.offensive_points_completed > 0 ? o_efficiency.toFixed(2) : 'N/A');
455 add_3cell(row, globals.defensive_points_completed > 0 ? d_efficiency.toFixed(2) : 'N/A');
456 add_3cell(row, globals.points_played);
462 function make_table_offense(players) {
465 let header = document.createElement('tr');
466 add_cell(header, 'th', 'Player');
467 add_cell(header, 'th', 'Goals');
468 add_cell(header, 'th', 'Assists');
469 add_cell(header, 'th', 'Hockey assists');
470 add_cell(header, 'th', 'Throws');
471 add_cell(header, 'th', 'Throwaways');
472 add_cell(header, 'th', '%OK');
473 add_cell(header, 'th', 'Catches');
474 add_cell(header, 'th', 'Drops');
475 add_cell(header, 'th', '%OK');
476 add_cell(header, 'th', 'Soft +/-');
484 for (const [q,p] of Object.entries(players)) {
485 if (q === 'globals') continue;
486 let throw_ok = 100 * (1 - p.throwaways / p.num_throws);
487 let catch_ok = 100 * (p.catches / (p.catches + p.drops));
489 let row = document.createElement('tr');
490 add_3cell(row, p.name, 'name'); // TODO: number?
491 add_3cell(row, p.goals);
492 add_3cell(row, p.assists);
493 add_3cell(row, p.hockey_assists);
494 add_3cell(row, p.num_throws);
495 add_3cell(row, p.throwaways);
496 if (p.num_throws > 0) {
497 add_3cell(row, throw_ok.toFixed(0) + '%');
499 add_3cell(row, 'N/A');
501 add_3cell(row, p.catches);
502 add_3cell(row, p.drops);
503 if (p.catches + p.drops > 0) {
504 add_3cell(row, catch_ok.toFixed(0) + '%');
506 add_3cell(row, 'N/A');
508 add_3cell(row, '+' + p.offensive_soft_plus);
509 add_3cell(row, '-' + p.offensive_soft_minus);
512 num_throws += p.num_throws;
513 throwaways += p.throwaways;
514 catches += p.catches;
519 let throw_ok = 100 * (1 - throwaways / num_throws);
520 let catch_ok = 100 * (catches / (catches + drops));
522 let row = document.createElement('tr');
527 add_3cell(row, num_throws);
528 add_3cell(row, throwaways);
529 add_3cell(row, throw_ok.toFixed(0) + '%');
530 add_3cell(row, catches);
531 add_3cell(row, drops);
532 add_3cell(row, catch_ok.toFixed(0) + '%');
540 function make_table_defense(players) {
543 let header = document.createElement('tr');
544 add_cell(header, 'th', 'Player');
545 add_cell(header, 'th', 'Ds');
546 add_cell(header, 'th', 'Pulls');
547 add_cell(header, 'th', 'OOB pulls');
548 add_cell(header, 'th', 'Avg. hang time (IB)');
549 add_cell(header, 'th', 'Soft +/-');
552 for (const [q,p] of Object.entries(players)) {
553 if (q === 'globals') continue;
555 for (const t of p.pull_times) {
558 let avg_time = 1e-3 * sum_time / p.pulls;
559 let oob_pct = 100 * p.oob_pulls / p.pulls;
561 let row = document.createElement('tr');
562 add_3cell(row, p.name, 'name'); // TODO: number?
563 add_3cell(row, p.defenses);
564 add_3cell(row, p.pulls);
566 add_3cell(row, 'N/A');
568 add_3cell(row, p.oob_pulls + ' (' + oob_pct.toFixed(0) + '%)');
570 if (p.pulls > p.oob_pulls) {
571 add_3cell(row, avg_time.toFixed(1) + ' sec');
573 add_3cell(row, 'N/A');
575 add_3cell(row, '+' + p.defensive_soft_plus);
576 add_3cell(row, '-' + p.defensive_soft_minus);
582 function make_table_playing_time(players) {
585 let header = document.createElement('tr');
586 add_cell(header, 'th', 'Player');
587 add_cell(header, 'th', 'Points played');
588 add_cell(header, 'th', 'Time played');
589 add_cell(header, 'th', 'Time on field');
590 add_cell(header, 'th', 'O points');
591 add_cell(header, 'th', 'D points');
595 for (const [q,p] of Object.entries(players)) {
596 if (q === 'globals') continue;
597 let row = document.createElement('tr');
598 add_3cell(row, p.name, 'name'); // TODO: number?
599 add_3cell(row, p.points_played);
600 add_3cell(row, Math.floor(p.playing_time_ms / 60000) + ' min');
601 add_3cell(row, Math.floor(p.field_time_ms / 60000) + ' min');
602 add_3cell(row, p.offensive_points_completed);
603 add_3cell(row, p.defensive_points_completed);
608 let globals = players['globals'];
609 let row = document.createElement('tr');
611 add_3cell(row, globals.points_played);
612 add_3cell(row, Math.floor(globals.playing_time_ms / 60000) + ' min');
613 add_3cell(row, Math.floor(globals.field_time_ms / 60000) + ' min');
614 add_3cell(row, globals.offensive_points_completed);
615 add_3cell(row, globals.defensive_points_completed);
621 function make_table_per_point(players) {
624 let header = document.createElement('tr');
625 add_cell(header, 'th', 'Player');
626 add_cell(header, 'th', 'Goals');
627 add_cell(header, 'th', 'Assists');
628 add_cell(header, 'th', 'Hockey assists');
629 add_cell(header, 'th', 'Ds');
630 add_cell(header, 'th', 'Throwaways');
631 add_cell(header, 'th', 'Drops');
632 add_cell(header, 'th', 'Touches');
638 let hockey_assists = 0;
643 for (const [q,p] of Object.entries(players)) {
644 if (q === 'globals') continue;
645 let row = document.createElement('tr');
646 add_3cell(row, p.name, 'name'); // TODO: number?
647 if (p.points_played > 0) {
648 add_3cell(row, p.goals == 0 ? 0 : (p.goals / p.points_played).toFixed(2));
649 add_3cell(row, p.assists == 0 ? 0 : (p.assists / p.points_played).toFixed(2));
650 add_3cell(row, p.hockey_assists == 0 ? 0 : (p.hockey_assists / p.points_played).toFixed(2));
651 add_3cell(row, p.defenses == 0 ? 0 : (p.defenses / p.points_played).toFixed(2));
652 add_3cell(row, p.throwaways == 0 ? 0 : (p.throwaways / p.points_played).toFixed(2));
653 add_3cell(row, p.drops == 0 ? 0 : (p.drops / p.points_played).toFixed(2));
654 add_3cell(row, p.touches == 0 ? 0 : (p.touches / p.points_played).toFixed(2));
656 add_3cell(row, 'N/A');
657 add_3cell(row, 'N/A');
658 add_3cell(row, 'N/A');
659 add_3cell(row, 'N/A');
660 add_3cell(row, 'N/A');
661 add_3cell(row, 'N/A');
662 add_3cell(row, 'N/A');
667 assists += p.assists;
668 hockey_assists += p.hockey_assists;
669 defenses += p.defenses;
670 throwaways += p.throwaways;
672 touches += p.touches;
676 let globals = players['globals'];
677 let row = document.createElement('tr');
679 if (globals.points_played > 0) {
680 add_3cell(row, goals == 0 ? 0 : (goals / globals.points_played).toFixed(2));
681 add_3cell(row, assists == 0 ? 0 : (assists / globals.points_played).toFixed(2));
682 add_3cell(row, hockey_assists == 0 ? 0 : (hockey_assists / globals.points_played).toFixed(2));
683 add_3cell(row, defenses == 0 ? 0 : (defenses / globals.points_played).toFixed(2));
684 add_3cell(row, throwaways == 0 ? 0 : (throwaways / globals.points_played).toFixed(2));
685 add_3cell(row, drops == 0 ? 0 : (drops / globals.points_played).toFixed(2));
686 add_3cell(row, touches == 0 ? 0 : (touches / globals.points_played).toFixed(2));
688 add_3cell(row, 'N/A');
689 add_3cell(row, 'N/A');
690 add_3cell(row, 'N/A');
691 add_3cell(row, 'N/A');
692 add_3cell(row, 'N/A');
693 add_3cell(row, 'N/A');
694 add_3cell(row, 'N/A');