]> git.sesse.net Git - pkanalytics/blob - ultimate.js
Correct counting of global points.
[pkanalytics] / ultimate.js
1 'use strict';
2
3 // No frameworks, no compilers, no npm, just JavaScript. :-)
4
5 let global_json;
6
7 addEventListener('hashchange', () => { process_matches(global_json); });
8 fetch('ultimate.json')
9    .then(response => response.json())
10    .then(response => { global_json = response; process_matches(global_json); });
11
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;
16         } else {
17                 player.playing_time_ms += to - from;
18         }
19 }
20
21 function take_off_field(player, t, live_since) {
22         if (live_since === null) {
23                 // Play isn't live, so nothing to do.
24         } else {
25                 attribute_player_time(player, t, live_since);
26         }
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;
29         }
30         player.on_field_since = null;
31 }
32
33 function add_cell(tr, element_type, text) {
34         let element = document.createElement(element_type);
35         element.textContent = text;
36         tr.appendChild(element);
37 }
38
39 function process_matches(json) {
40         let players = {};
41         for (const player of json['players']) {
42                 players[player['player_id']] = {
43                         'name': player['name'],
44                         'number': player['number'],
45
46                         'goals': 0,
47                         'assists': 0,
48                         'hockey_assists': 0,
49                         'catches': 0,
50                         'touches': 0,
51                         'num_throws': 0,
52                         'throwaways': 0,
53                         'drops': 0,
54
55                         'defenses': 0,
56                         'interceptions': 0,
57                         'points_played': 0,
58                         'playing_time_ms': 0,
59                         'field_time_ms': 0,
60
61                         // For efficiency.
62                         'offensive_points_completed': 0,
63                         'offensive_points_won': 0,
64                         'defensive_points_completed': 0,
65                         'defensive_points_won': 0,
66
67                         'offensive_soft_plus': 0,
68                         'offensive_soft_minus': 0,
69                         'defensive_soft_plus': 0,
70                         'defensive_soft_minus': 0,
71
72                         'pulls': 0,
73                         'pull_times': [],
74                         'oob_pulls': 0,
75
76                         // Internal.
77                         'last_point_seen': null,
78                         'on_field_since': null,
79                 };
80         }
81
82         // Globals.
83         players['globals'] = {
84                 'points_played': 0,
85                 'playing_time_ms': 0,
86                 'field_time_ms': 0,
87
88                 'offensive_points_completed': 0,
89                 'offensive_points_won': 0,
90                 'defensive_points_completed': 0,
91                 'defensive_points_won': 0,
92         };
93         let globals = players['globals'];
94
95         for (const match of json['matches']) {
96                 let our_score = 0;
97                 let their_score = 0;
98                 let handler = null;
99                 let prev_handler = null;
100                 let live_since = null;
101                 let offense = null;
102                 let puller = 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).
105                 let point_num = 0;
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;
111                 }
112                 for (const e of match['events']) {
113                         let t = e['t'];
114                         let type = e['type'];
115                         let p = players[e['player']];
116
117                         // Sub management
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);
122                         }
123
124                         // Liveness management
125                         if (type === 'pull' || type === 'their_pull' || type === 'restart') {
126                                 live_since = t;
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.');
130                                 live_since = t;
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) {
135                                                 continue;
136                                         }
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;
141                                                 ++p.points_played;
142                                         }
143                                         attribute_player_time(p, t, live_since);
144
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;
150                                                         }
151                                                 } else if (last_pull_was_ours === false) {  // O point.
152                                                         ++p.offensive_points_completed;
153                                                         if (type === 'goal') {
154                                                                 ++p.offensive_points_won;
155                                                         }
156                                                 }
157                                         }
158                                 }
159
160                                 if (type !== 'stoppage') {
161                                         // Update globals.
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;
167                                                 }
168                                         } else if (last_pull_was_ours === false) {  // O point.
169                                                 ++globals.offensive_points_completed;
170                                                 if (type === 'goal') {
171                                                         ++globals.offensive_points_won;
172                                                 }
173                                         }
174                                 }
175                                 if (live_since !== null) {
176                                         globals.playing_time_ms += t - live_since;
177                                 }
178
179                                 live_since = null;
180                         }
181
182                         // Score management
183                         if (type === 'goal') {
184                                 ++our_score;
185                         } else if (type === 'their_goal') {
186                                 ++their_score;
187                         }
188
189                         // Point count management
190                         if (p !== undefined && type !== 'out' && p.last_point_seen !== point_num) {
191                                 p.last_point_seen = point_num;
192                                 ++p.points_played;
193                         }
194                         if (type === 'goal' || type === 'their_goal') {
195                                 ++point_num;
196                                 last_goal = t;
197                         }
198                         if (type !== 'out' && game_started === null) {
199                                 game_started = t;
200                         }
201
202                         // Pull management
203                         if (type === 'pull') {
204                                 puller = e['player'];
205                                 pull_started = t;
206                                 ++p.pulls;
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;
213                         } else {
214                                 // Not pulling (if there was one, we never recorded its outcome, but still count it).
215                                 puller = pull_started = null;
216                         }
217
218                         // Offense/defense management (TODO: use it for actual counting)
219                         if (type === 'set_defense' || type === 'goal' || type === 'throwaway' || type === 'drop') {
220                                 offense = false;
221                         } else if (type === 'set_offense' || type === 'their_goal' || type === 'their_throwaway' || type === 'defense' || type === 'interception') {
222                                 offense = true;
223                         }
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) {
238                                 // Similar.
239                                 last_pull_was_ours = true;
240                         } else if (type === 'goal' || type === 'their_goal') {
241                                 last_pull_was_ours = null;
242                         }
243
244                         // Event management
245                         if (type === 'catch' || type === 'goal') {
246                                 if (handler !== null) {
247                                         ++players[handler].num_throws;
248                                         ++p.catches;
249                                 }
250
251                                 ++p.touches;
252                                 if (type === 'goal') {
253                                         if (prev_handler !== null) {
254                                                 ++players[prev_handler].hockey_assists;
255                                         }
256                                         if (handler !== null) {
257                                                 ++players[handler].assists;
258                                         }
259                                         ++p.goals;
260                                         handler = prev_handler = null;
261                                 } else {
262                                         // Update hold history.
263                                         prev_handler = handler;
264                                         handler = e['player'];
265                                 }
266                         } else if (type === 'throwaway') {
267                                 ++p.num_throws;
268                                 ++p.throwaways;
269                                 handler = prev_handler = null;
270                         } else if (type === 'drop') {
271                                 ++p.drops;
272                                 handler = prev_handler = null;
273                         } else if (type === 'defense') {
274                                 ++p.defenses;
275                         } else if (type === 'interception') {
276                                 ++p.interceptions;
277                                 ++p.defenses;
278                                 ++p.touches;
279                                 prev_handler = null;
280                                 handler = e['player'];
281                         } else if (type === 'offensive_soft_plus' || type === 'offensive_soft_minus' || type === 'defensive_soft_plus' || type === 'defensive_soft_minus') {
282                                 ++p[type];
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);
290                         }
291                 }
292
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;
297                         }
298                 }
299                 if (game_started !== null && last_goal !== null) {
300                         globals.field_time_ms += last_goal - game_started;
301                 }
302                 if (live_since !== null && last_goal !== null) {
303                         globals.playing_time_ms += last_goal - live_since;
304                 }
305         }
306
307         let chosen_category = get_chosen_category();
308         write_main_menu(chosen_category);
309
310         let rows = [];
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         }
320         document.getElementById('stats').replaceChildren(...rows);
321 }
322
323 function get_chosen_category() {
324         if (window.location.hash === '#offense') {
325                 return 'offense';
326         } else if (window.location.hash === '#defense') {
327                 return 'defense';
328         } else if (window.location.hash === '#playing_time') {
329                 return 'playing_time';
330         } else {
331                 return 'general';
332         }
333 }
334
335 function write_main_menu(chosen_category) {
336         let elems = [];
337         if (chosen_category === 'general') {
338                 elems.push(document.createTextNode('General'));
339         } else {
340                 let a = document.createElement('a');
341                 a.appendChild(document.createTextNode('General'));
342                 a.setAttribute('href', '#general');
343                 elems.push(a);
344         }
345
346         elems.push(document.createTextNode(' | '));
347         if (chosen_category === 'offense') {
348                 elems.push(document.createTextNode('Offense'));
349         } else {
350                 let a = document.createElement('a');
351                 a.appendChild(document.createTextNode('Offense'));
352                 a.setAttribute('href', '#offense');
353                 elems.push(a);
354         }
355
356         elems.push(document.createTextNode(' | '));
357         if (chosen_category === 'defense') {
358                 elems.push(document.createTextNode('Defense'));
359         } else {
360                 let a = document.createElement('a');
361                 a.appendChild(document.createTextNode('Defense'));
362                 a.setAttribute('href', '#defense');
363                 elems.push(a);
364         }
365
366         elems.push(document.createTextNode(' | '));
367         if (chosen_category === 'playing_time') {
368                 elems.push(document.createTextNode('Playing time'));
369         } else {
370                 let a = document.createElement('a');
371                 a.appendChild(document.createTextNode('Playing time'));
372                 a.setAttribute('href', '#playing_time');
373                 elems.push(a);
374         }
375
376         document.getElementById('mainmenu').replaceChildren(...elems);
377 }
378
379 function make_table_general(players) {
380         let rows = [];
381         {
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');
389                 rows.push(header);
390         }
391
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);
405                 rows.push(row);
406         }
407
408         // Globals.
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);
419         rows.push(row);
420
421         return rows;
422 }
423
424 function make_table_offense(players) {
425         let rows = [];
426         {
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 +/-');
439                 rows.push(header);
440         }
441
442         let num_throws = 0;
443         let throwaways = 0;
444         let catches = 0;
445         let drops = 0;
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));
450
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);
464                 rows.push(row);
465
466                 num_throws += p.num_throws;
467                 throwaways += p.throwaways;
468                 catches += p.catches;
469                 drops += p.drops;
470         }
471
472         // Globals.
473         let throw_ok = 100 * (1 - throwaways / num_throws);
474         let catch_ok = 100 * (catches / (catches + drops));
475
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', '');
489         rows.push(row);
490
491         return rows;
492 }
493
494 function make_table_defense(players) {
495         let rows = [];
496         {
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 +/-');
504                 rows.push(header);
505         }
506         for (const [q,p] of Object.entries(players)) {
507                 if (q === 'globals') continue;
508                 let sum_time = 0;
509                 for (const t of p.pull_times) {
510                         sum_time += t;
511                 }
512                 let avg_time = 1e-3 * sum_time / p.pulls;
513                 let oob_pct = 100 * p.oob_pulls / p.pulls;
514
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);
519                 if (p.pulls === 0) {
520                         add_cell(row, 'td', 'N/A');
521                 } else {
522                         add_cell(row, 'td', p.oob_pulls + ' (' + oob_pct.toFixed(0) + '%)');
523                 }
524                 if (p.pulls > p.oob_pulls) {
525                         add_cell(row, 'td', avg_time.toFixed(1) + ' sec');
526                 } else {
527                         add_cell(row, 'td', 'N/A');
528                 }
529                 add_cell(row, 'td', '+' + p.defensive_soft_plus);
530                 add_cell(row, 'td', '-' + p.defensive_soft_minus);
531                 rows.push(row);
532         }
533         return rows;
534 }
535
536 function make_table_playing_time(players) {
537         let rows = [];
538         {
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');
546                 rows.push(header);
547         }
548
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);
558                 rows.push(row);
559         }
560
561         // Globals.
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);
570         rows.push(row);
571
572         return rows;
573 }