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 !== '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' &&
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);
320 document.getElementById('stats').replaceChildren(...rows);
323 function get_chosen_category() {
324 if (window.location.hash === '#offense') {
326 } else if (window.location.hash === '#defense') {
328 } else if (window.location.hash === '#playing_time') {
329 return 'playing_time';
335 function write_main_menu(chosen_category) {
337 if (chosen_category === 'general') {
338 elems.push(document.createTextNode('General'));
340 let a = document.createElement('a');
341 a.appendChild(document.createTextNode('General'));
342 a.setAttribute('href', '#general');
346 elems.push(document.createTextNode(' | '));
347 if (chosen_category === 'offense') {
348 elems.push(document.createTextNode('Offense'));
350 let a = document.createElement('a');
351 a.appendChild(document.createTextNode('Offense'));
352 a.setAttribute('href', '#offense');
356 elems.push(document.createTextNode(' | '));
357 if (chosen_category === 'defense') {
358 elems.push(document.createTextNode('Defense'));
360 let a = document.createElement('a');
361 a.appendChild(document.createTextNode('Defense'));
362 a.setAttribute('href', '#defense');
366 elems.push(document.createTextNode(' | '));
367 if (chosen_category === 'playing_time') {
368 elems.push(document.createTextNode('Playing time'));
370 let a = document.createElement('a');
371 a.appendChild(document.createTextNode('Playing time'));
372 a.setAttribute('href', '#playing_time');
376 document.getElementById('mainmenu').replaceChildren(...elems);
379 function make_table_general(players) {
382 let header = document.createElement('tr');
383 add_cell(header, 'th', 'Player');
384 add_cell(header, 'th', '+/-');
385 add_cell(header, 'th', 'Soft +/-');
386 add_cell(header, 'th', 'O efficiency');
387 add_cell(header, 'th', 'D efficiency');
388 add_cell(header, 'th', 'Points played');
392 for (const [q,p] of Object.entries(players)) {
393 if (q === 'globals') continue;
394 let row = document.createElement('tr');
395 let pm = p.goals + p.assists + p.hockey_assists + p.defenses - p.throwaways - p.drops;
396 let soft_pm = p.offensive_soft_plus + p.defensive_soft_plus - p.offensive_soft_minus - p.defensive_soft_minus;
397 let o_efficiency = (p.offensive_points_won / p.offensive_points_completed) * 2 - 1;
398 let d_efficiency = (p.defensive_points_won / p.defensive_points_completed) * 2 - 1;
399 add_cell(row, 'td', p.name); // TODO: number?
400 add_cell(row, 'td', pm > 0 ? ('+' + pm) : pm);
401 add_cell(row, 'td', soft_pm > 0 ? ('+' + soft_pm) : soft_pm);
402 add_cell(row, 'td', p.offensive_points_completed > 0 ? o_efficiency.toFixed(2) : 'N/A');
403 add_cell(row, 'td', p.defensive_points_completed > 0 ? d_efficiency.toFixed(2) : 'N/A');
404 add_cell(row, 'td', p.points_played);
409 let globals = players['globals'];
410 let o_efficiency = (globals.offensive_points_won / globals.offensive_points_completed) * 2 - 1;
411 let d_efficiency = (globals.defensive_points_won / globals.defensive_points_completed) * 2 - 1;
412 let row = document.createElement('tr');
413 add_cell(row, 'td', '');
414 add_cell(row, 'td', '');
415 add_cell(row, 'td', '');
416 add_cell(row, 'td', globals.offensive_points_completed > 0 ? o_efficiency.toFixed(2) : 'N/A');
417 add_cell(row, 'td', globals.defensive_points_completed > 0 ? d_efficiency.toFixed(2) : 'N/A');
418 add_cell(row, 'td', globals.points_played);
424 function make_table_offense(players) {
427 let header = document.createElement('tr');
428 add_cell(header, 'th', 'Player');
429 add_cell(header, 'th', 'Goals');
430 add_cell(header, 'th', 'Assists');
431 add_cell(header, 'th', 'Hockey assists');
432 add_cell(header, 'th', 'Throws');
433 add_cell(header, 'th', 'Throwaways');
434 add_cell(header, 'th', '%OK');
435 add_cell(header, 'th', 'Catches');
436 add_cell(header, 'th', 'Drops');
437 add_cell(header, 'th', '%OK');
438 add_cell(header, 'th', 'Soft +/-');
446 for (const [q,p] of Object.entries(players)) {
447 if (q === 'globals') continue;
448 let throw_ok = 100 * (1 - p.throwaways / p.num_throws);
449 let catch_ok = 100 * (p.catches / (p.catches + p.drops));
451 let row = document.createElement('tr');
452 add_cell(row, 'td', p.name); // TODO: number?
453 add_cell(row, 'td', p.goals);
454 add_cell(row, 'td', p.assists);
455 add_cell(row, 'td', p.hockey_assists);
456 add_cell(row, 'td', p.num_throws);
457 add_cell(row, 'td', p.throwaways);
458 add_cell(row, 'td', throw_ok.toFixed(0) + '%');
459 add_cell(row, 'td', p.catches);
460 add_cell(row, 'td', p.drops);
461 add_cell(row, 'td', catch_ok.toFixed(0) + '%');
462 add_cell(row, 'td', '+' + p.offensive_soft_plus);
463 add_cell(row, 'td', '-' + p.offensive_soft_minus);
466 num_throws += p.num_throws;
467 throwaways += p.throwaways;
468 catches += p.catches;
473 let throw_ok = 100 * (1 - throwaways / num_throws);
474 let catch_ok = 100 * (catches / (catches + drops));
476 let row = document.createElement('tr');
477 add_cell(row, 'td', '');
478 add_cell(row, 'td', '');
479 add_cell(row, 'td', '');
480 add_cell(row, 'td', '');
481 add_cell(row, 'td', num_throws);
482 add_cell(row, 'td', throwaways);
483 add_cell(row, 'td', throw_ok.toFixed(0) + '%');
484 add_cell(row, 'td', catches);
485 add_cell(row, 'td', drops);
486 add_cell(row, 'td', catch_ok.toFixed(0) + '%');
487 add_cell(row, 'td', '');
488 add_cell(row, 'td', '');
494 function make_table_defense(players) {
497 let header = document.createElement('tr');
498 add_cell(header, 'th', 'Player');
499 add_cell(header, 'th', 'Ds');
500 add_cell(header, 'th', 'Pulls');
501 add_cell(header, 'th', 'OOB pulls');
502 add_cell(header, 'th', 'Avg. hang time (IB)');
503 add_cell(header, 'th', 'Soft +/-');
506 for (const [q,p] of Object.entries(players)) {
507 if (q === 'globals') continue;
509 for (const t of p.pull_times) {
512 let avg_time = 1e-3 * sum_time / p.pulls;
513 let oob_pct = 100 * p.oob_pulls / p.pulls;
515 let row = document.createElement('tr');
516 add_cell(row, 'td', p.name); // TODO: number?
517 add_cell(row, 'td', p.defenses);
518 add_cell(row, 'td', p.pulls);
520 add_cell(row, 'td', 'N/A');
522 add_cell(row, 'td', p.oob_pulls + ' (' + oob_pct.toFixed(0) + '%)');
524 if (p.pulls > p.oob_pulls) {
525 add_cell(row, 'td', avg_time.toFixed(1) + ' sec');
527 add_cell(row, 'td', 'N/A');
529 add_cell(row, 'td', '+' + p.defensive_soft_plus);
530 add_cell(row, 'td', '-' + p.defensive_soft_minus);
536 function make_table_playing_time(players) {
539 let header = document.createElement('tr');
540 add_cell(header, 'th', 'Player');
541 add_cell(header, 'th', 'Points played');
542 add_cell(header, 'th', 'Time played');
543 add_cell(header, 'th', 'Time on field');
544 add_cell(header, 'th', 'O points');
545 add_cell(header, 'th', 'D points');
549 for (const [q,p] of Object.entries(players)) {
550 if (q === 'globals') continue;
551 let row = document.createElement('tr');
552 add_cell(row, 'td', p.name); // TODO: number?
553 add_cell(row, 'td', p.points_played);
554 add_cell(row, 'td', Math.floor(p.playing_time_ms / 60000) + ' min');
555 add_cell(row, 'td', Math.floor(p.field_time_ms / 60000) + ' min');
556 add_cell(row, 'td', p.offensive_points_completed);
557 add_cell(row, 'td', p.defensive_points_completed);
562 let globals = players['globals'];
563 let row = document.createElement('tr');
564 add_cell(row, 'td', '');
565 add_cell(row, 'td', globals.points_played);
566 add_cell(row, 'td', Math.floor(globals.playing_time_ms / 60000) + ' min');
567 add_cell(row, 'td', Math.floor(globals.field_time_ms / 60000) + ' min');
568 add_cell(row, 'td', globals.offensive_points_completed);
569 add_cell(row, 'td', globals.defensive_points_completed);