]> git.sesse.net Git - pkanalytics/blob - ultimate.js
Add some styling.
[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         if (element_type === 'th') {
37                 element.setAttribute('colspan', '3');
38         }
39         tr.appendChild(element);
40         return element;
41 }
42
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', '');
47
48         p1.classList.add('pad');
49         p2.classList.add('pad');
50         if (cls === undefined) {
51                 element.classList.add('num');
52         } else {
53                 element.classList.add(cls);
54         }
55 }
56
57 function process_matches(json) {
58         let players = {};
59         for (const player of json['players']) {
60                 players[player['player_id']] = {
61                         'name': player['name'],
62                         'number': player['number'],
63
64                         'goals': 0,
65                         'assists': 0,
66                         'hockey_assists': 0,
67                         'catches': 0,
68                         'touches': 0,
69                         'num_throws': 0,
70                         'throwaways': 0,
71                         'drops': 0,
72
73                         'defenses': 0,
74                         'interceptions': 0,
75                         'points_played': 0,
76                         'playing_time_ms': 0,
77                         'field_time_ms': 0,
78
79                         // For efficiency.
80                         'offensive_points_completed': 0,
81                         'offensive_points_won': 0,
82                         'defensive_points_completed': 0,
83                         'defensive_points_won': 0,
84
85                         'offensive_soft_plus': 0,
86                         'offensive_soft_minus': 0,
87                         'defensive_soft_plus': 0,
88                         'defensive_soft_minus': 0,
89
90                         'pulls': 0,
91                         'pull_times': [],
92                         'oob_pulls': 0,
93
94                         // Internal.
95                         'last_point_seen': null,
96                         'on_field_since': null,
97                 };
98         }
99
100         // Globals.
101         players['globals'] = {
102                 'points_played': 0,
103                 'playing_time_ms': 0,
104                 'field_time_ms': 0,
105
106                 'offensive_points_completed': 0,
107                 'offensive_points_won': 0,
108                 'defensive_points_completed': 0,
109                 'defensive_points_won': 0,
110         };
111         let globals = players['globals'];
112
113         for (const match of json['matches']) {
114                 let our_score = 0;
115                 let their_score = 0;
116                 let handler = null;
117                 let prev_handler = null;
118                 let live_since = null;
119                 let offense = null;
120                 let puller = 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).
123                 let point_num = 0;
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;
129                 }
130                 for (const e of match['events']) {
131                         let t = e['t'];
132                         let type = e['type'];
133                         let p = players[e['player']];
134
135                         // Sub management
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);
140                         }
141
142                         // Liveness management
143                         if (type === 'pull' || type === 'their_pull' || type === 'restart') {
144                                 live_since = t;
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.');
148                                 live_since = t;
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) {
153                                                 continue;
154                                         }
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;
159                                                 ++p.points_played;
160                                         }
161                                         attribute_player_time(p, t, live_since);
162
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;
168                                                         }
169                                                 } else if (last_pull_was_ours === false) {  // O point.
170                                                         ++p.offensive_points_completed;
171                                                         if (type === 'goal') {
172                                                                 ++p.offensive_points_won;
173                                                         }
174                                                 }
175                                         }
176                                 }
177
178                                 if (type !== 'stoppage') {
179                                         // Update globals.
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;
185                                                 }
186                                         } else if (last_pull_was_ours === false) {  // O point.
187                                                 ++globals.offensive_points_completed;
188                                                 if (type === 'goal') {
189                                                         ++globals.offensive_points_won;
190                                                 }
191                                         }
192                                 }
193                                 if (live_since !== null) {
194                                         globals.playing_time_ms += t - live_since;
195                                 }
196
197                                 live_since = null;
198                         }
199
200                         // Score management
201                         if (type === 'goal') {
202                                 ++our_score;
203                         } else if (type === 'their_goal') {
204                                 ++their_score;
205                         }
206
207                         // Point count management
208                         if (p !== undefined && type !== 'out' && p.last_point_seen !== point_num) {
209                                 p.last_point_seen = point_num;
210                                 ++p.points_played;
211                         }
212                         if (type === 'goal' || type === 'their_goal') {
213                                 ++point_num;
214                                 last_goal = t;
215                         }
216                         if (type !== 'out' && game_started === null) {
217                                 game_started = t;
218                         }
219
220                         // Pull management
221                         if (type === 'pull') {
222                                 puller = e['player'];
223                                 pull_started = t;
224                                 ++p.pulls;
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;
231                         } else {
232                                 // Not pulling (if there was one, we never recorded its outcome, but still count it).
233                                 puller = pull_started = null;
234                         }
235
236                         // Offense/defense management (TODO: use it for actual counting)
237                         if (type === 'set_defense' || type === 'goal' || type === 'throwaway' || type === 'drop') {
238                                 offense = false;
239                         } else if (type === 'set_offense' || type === 'their_goal' || type === 'their_throwaway' || type === 'defense' || type === 'interception') {
240                                 offense = true;
241                         }
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) {
256                                 // Similar.
257                                 last_pull_was_ours = true;
258                         } else if (type === 'goal' || type === 'their_goal') {
259                                 last_pull_was_ours = null;
260                         }
261
262                         // Event management
263                         if (type === 'catch' || type === 'goal') {
264                                 if (handler !== null) {
265                                         ++players[handler].num_throws;
266                                         ++p.catches;
267                                 }
268
269                                 ++p.touches;
270                                 if (type === 'goal') {
271                                         if (prev_handler !== null) {
272                                                 ++players[prev_handler].hockey_assists;
273                                         }
274                                         if (handler !== null) {
275                                                 ++players[handler].assists;
276                                         }
277                                         ++p.goals;
278                                         handler = prev_handler = null;
279                                 } else {
280                                         // Update hold history.
281                                         prev_handler = handler;
282                                         handler = e['player'];
283                                 }
284                         } else if (type === 'throwaway') {
285                                 ++p.num_throws;
286                                 ++p.throwaways;
287                                 handler = prev_handler = null;
288                         } else if (type === 'drop') {
289                                 ++p.drops;
290                                 handler = prev_handler = null;
291                         } else if (type === 'defense') {
292                                 ++p.defenses;
293                         } else if (type === 'interception') {
294                                 ++p.interceptions;
295                                 ++p.defenses;
296                                 ++p.touches;
297                                 prev_handler = null;
298                                 handler = e['player'];
299                         } else if (type === 'offensive_soft_plus' || type === 'offensive_soft_minus' || type === 'defensive_soft_plus' || type === 'defensive_soft_minus') {
300                                 ++p[type];
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);
308                         }
309                 }
310
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;
315                         }
316                 }
317                 if (game_started !== null && last_goal !== null) {
318                         globals.field_time_ms += last_goal - game_started;
319                 }
320                 if (live_since !== null && last_goal !== null) {
321                         globals.playing_time_ms += last_goal - live_since;
322                 }
323         }
324
325         let chosen_category = get_chosen_category();
326         write_main_menu(chosen_category);
327
328         let rows = [];
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);
339         }
340         document.getElementById('stats').replaceChildren(...rows);
341 }
342
343 function get_chosen_category() {
344         if (window.location.hash === '#offense') {
345                 return 'offense';
346         } else if (window.location.hash === '#defense') {
347                 return 'defense';
348         } else if (window.location.hash === '#playing_time') {
349                 return 'playing_time';
350         } else if (window.location.hash === '#per_point') {
351                 return 'per_point';
352         } else {
353                 return 'general';
354         }
355 }
356
357 function write_main_menu(chosen_category) {
358         let elems = [];
359         if (chosen_category === 'general') {
360                 let span = document.createElement('span');
361                 span.innerText = 'General';
362                 elems.push(span);
363         } else {
364                 let a = document.createElement('a');
365                 a.appendChild(document.createTextNode('General'));
366                 a.setAttribute('href', '#general');
367                 elems.push(a);
368         }
369
370         if (chosen_category === 'offense') {
371                 let span = document.createElement('span');
372                 span.innerText = 'Offense';
373                 elems.push(span);
374         } else {
375                 let a = document.createElement('a');
376                 a.appendChild(document.createTextNode('Offense'));
377                 a.setAttribute('href', '#offense');
378                 elems.push(a);
379         }
380
381         if (chosen_category === 'defense') {
382                 let span = document.createElement('span');
383                 span.innerText = 'Defense';
384                 elems.push(span);
385         } else {
386                 let a = document.createElement('a');
387                 a.appendChild(document.createTextNode('Defense'));
388                 a.setAttribute('href', '#defense');
389                 elems.push(a);
390         }
391
392         if (chosen_category === 'playing_time') {
393                 let span = document.createElement('span');
394                 span.innerText = 'Playing time';
395                 elems.push(span);
396         } else {
397                 let a = document.createElement('a');
398                 a.appendChild(document.createTextNode('Playing time'));
399                 a.setAttribute('href', '#playing_time');
400                 elems.push(a);
401         }
402
403         if (chosen_category === 'per_point') {
404                 let span = document.createElement('span');
405                 span.innerText = 'Per point';
406                 elems.push(span);
407         } else {
408                 let a = document.createElement('a');
409                 a.appendChild(document.createTextNode('Per point'));
410                 a.setAttribute('href', '#per_point');
411                 elems.push(a);
412         }
413
414         document.getElementById('mainmenu').replaceChildren(...elems);
415 }
416
417 function make_table_general(players) {
418         let rows = [];
419         {
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');
427                 rows.push(header);
428         }
429
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);
443                 rows.push(row);
444         }
445
446         // Globals.
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');
451         add_3cell(row, '');
452         add_3cell(row, '');
453         add_3cell(row, '');
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);
457         rows.push(row);
458
459         return rows;
460 }
461
462 function make_table_offense(players) {
463         let rows = [];
464         {
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 +/-');
477                 rows.push(header);
478         }
479
480         let num_throws = 0;
481         let throwaways = 0;
482         let catches = 0;
483         let drops = 0;
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));
488
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) + '%');
498                 } else {
499                         add_3cell(row, 'N/A');
500                 }
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) + '%');
505                 } else {
506                         add_3cell(row, 'N/A');
507                 }
508                 add_3cell(row, '+' + p.offensive_soft_plus);
509                 add_3cell(row, '-' + p.offensive_soft_minus);
510                 rows.push(row);
511
512                 num_throws += p.num_throws;
513                 throwaways += p.throwaways;
514                 catches += p.catches;
515                 drops += p.drops;
516         }
517
518         // Globals.
519         let throw_ok = 100 * (1 - throwaways / num_throws);
520         let catch_ok = 100 * (catches / (catches + drops));
521
522         let row = document.createElement('tr');
523         add_3cell(row, '');
524         add_3cell(row, '');
525         add_3cell(row, '');
526         add_3cell(row, '');
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) + '%');
533         add_3cell(row, '');
534         add_3cell(row, '');
535         rows.push(row);
536
537         return rows;
538 }
539
540 function make_table_defense(players) {
541         let rows = [];
542         {
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 +/-');
550                 rows.push(header);
551         }
552         for (const [q,p] of Object.entries(players)) {
553                 if (q === 'globals') continue;
554                 let sum_time = 0;
555                 for (const t of p.pull_times) {
556                         sum_time += t;
557                 }
558                 let avg_time = 1e-3 * sum_time / p.pulls;
559                 let oob_pct = 100 * p.oob_pulls / p.pulls;
560
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);
565                 if (p.pulls === 0) {
566                         add_3cell(row, 'N/A');
567                 } else {
568                         add_3cell(row, p.oob_pulls + ' (' + oob_pct.toFixed(0) + '%)');
569                 }
570                 if (p.pulls > p.oob_pulls) {
571                         add_3cell(row, avg_time.toFixed(1) + ' sec');
572                 } else {
573                         add_3cell(row, 'N/A');
574                 }
575                 add_3cell(row, '+' + p.defensive_soft_plus);
576                 add_3cell(row, '-' + p.defensive_soft_minus);
577                 rows.push(row);
578         }
579         return rows;
580 }
581
582 function make_table_playing_time(players) {
583         let rows = [];
584         {
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');
592                 rows.push(header);
593         }
594
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);
604                 rows.push(row);
605         }
606
607         // Globals.
608         let globals = players['globals'];
609         let row = document.createElement('tr');
610         add_3cell(row, '');
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);
616         rows.push(row);
617
618         return rows;
619 }
620
621 function make_table_per_point(players) {
622         let rows = [];
623         {
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');
633                 rows.push(header);
634         }
635
636         let goals = 0;
637         let assists = 0;
638         let hockey_assists = 0;
639         let defenses = 0;
640         let throwaways = 0;
641         let drops = 0;
642         let touches = 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));
655                 } else {
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');
663                 }
664                 rows.push(row);
665
666                 goals += p.goals;
667                 assists += p.assists;
668                 hockey_assists += p.hockey_assists;
669                 defenses += p.defenses;
670                 throwaways += p.throwaways;
671                 drops += p.drops;
672                 touches += p.touches;
673         }
674
675         // Globals.
676         let globals = players['globals'];
677         let row = document.createElement('tr');
678         add_3cell(row, '');
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));
687         } else {
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');
695         }
696         rows.push(row);
697
698         return rows;
699 }