]> git.sesse.net Git - pkanalytics/blob - ultimate.js
Count touches per-point.
[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 !== 'restart' && type !== 'unknown' &&
285                                    type !== 'set_defense' && type !== 'goal' && type !== 'throwaway' &&
286                                    type !== 'drop' && type !== 'set_offense' && type !== 'their_goal' &&
287                                    type !== 'pull' && type !== 'pull_landed' && type !== 'pull_oob' && type !== 'their_pull' &&
288                                    type !== 'their_throwaway' && type !== 'defense' && type !== 'interception') {
289                                 console.log("Unknown event:", e);
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         } else if (chosen_category === 'per_point') {
320                 rows = make_table_per_point(players);
321         }
322         document.getElementById('stats').replaceChildren(...rows);
323 }
324
325 function get_chosen_category() {
326         if (window.location.hash === '#offense') {
327                 return 'offense';
328         } else if (window.location.hash === '#defense') {
329                 return 'defense';
330         } else if (window.location.hash === '#playing_time') {
331                 return 'playing_time';
332         } else if (window.location.hash === '#per_point') {
333                 return 'per_point';
334         } else {
335                 return 'general';
336         }
337 }
338
339 function write_main_menu(chosen_category) {
340         let elems = [];
341         if (chosen_category === 'general') {
342                 elems.push(document.createTextNode('General'));
343         } else {
344                 let a = document.createElement('a');
345                 a.appendChild(document.createTextNode('General'));
346                 a.setAttribute('href', '#general');
347                 elems.push(a);
348         }
349
350         elems.push(document.createTextNode(' | '));
351         if (chosen_category === 'offense') {
352                 elems.push(document.createTextNode('Offense'));
353         } else {
354                 let a = document.createElement('a');
355                 a.appendChild(document.createTextNode('Offense'));
356                 a.setAttribute('href', '#offense');
357                 elems.push(a);
358         }
359
360         elems.push(document.createTextNode(' | '));
361         if (chosen_category === 'defense') {
362                 elems.push(document.createTextNode('Defense'));
363         } else {
364                 let a = document.createElement('a');
365                 a.appendChild(document.createTextNode('Defense'));
366                 a.setAttribute('href', '#defense');
367                 elems.push(a);
368         }
369
370         elems.push(document.createTextNode(' | '));
371         if (chosen_category === 'playing_time') {
372                 elems.push(document.createTextNode('Playing time'));
373         } else {
374                 let a = document.createElement('a');
375                 a.appendChild(document.createTextNode('Playing time'));
376                 a.setAttribute('href', '#playing_time');
377                 elems.push(a);
378         }
379
380         elems.push(document.createTextNode(' | '));
381         if (chosen_category === 'per_point') {
382                 elems.push(document.createTextNode('Per point'));
383         } else {
384                 let a = document.createElement('a');
385                 a.appendChild(document.createTextNode('Per point'));
386                 a.setAttribute('href', '#per_point');
387                 elems.push(a);
388         }
389
390         document.getElementById('mainmenu').replaceChildren(...elems);
391 }
392
393 function make_table_general(players) {
394         let rows = [];
395         {
396                 let header = document.createElement('tr');
397                 add_cell(header, 'th', 'Player');
398                 add_cell(header, 'th', '+/-');
399                 add_cell(header, 'th', 'Soft +/-');
400                 add_cell(header, 'th', 'O efficiency');
401                 add_cell(header, 'th', 'D efficiency');
402                 add_cell(header, 'th', 'Points played');
403                 rows.push(header);
404         }
405
406         for (const [q,p] of Object.entries(players)) {
407                 if (q === 'globals') continue;
408                 let row = document.createElement('tr');
409                 let pm = p.goals + p.assists + p.hockey_assists + p.defenses - p.throwaways - p.drops;
410                 let soft_pm = p.offensive_soft_plus + p.defensive_soft_plus - p.offensive_soft_minus - p.defensive_soft_minus;
411                 let o_efficiency = (p.offensive_points_won / p.offensive_points_completed) * 2 - 1;
412                 let d_efficiency = (p.defensive_points_won / p.defensive_points_completed) * 2 - 1;
413                 add_cell(row, 'td', p.name);  // TODO: number?
414                 add_cell(row, 'td', pm > 0 ? ('+' + pm) : pm);
415                 add_cell(row, 'td', soft_pm > 0 ? ('+' + soft_pm) : soft_pm);
416                 add_cell(row, 'td', p.offensive_points_completed > 0 ? o_efficiency.toFixed(2) : 'N/A');
417                 add_cell(row, 'td', p.defensive_points_completed > 0 ? d_efficiency.toFixed(2) : 'N/A');
418                 add_cell(row, 'td', p.points_played);
419                 rows.push(row);
420         }
421
422         // Globals.
423         let globals = players['globals'];
424         let o_efficiency = (globals.offensive_points_won / globals.offensive_points_completed) * 2 - 1;
425         let d_efficiency = (globals.defensive_points_won / globals.defensive_points_completed) * 2 - 1;
426         let row = document.createElement('tr');
427         add_cell(row, 'td', '');
428         add_cell(row, 'td', '');
429         add_cell(row, 'td', '');
430         add_cell(row, 'td', globals.offensive_points_completed > 0 ? o_efficiency.toFixed(2) : 'N/A');
431         add_cell(row, 'td', globals.defensive_points_completed > 0 ? d_efficiency.toFixed(2) : 'N/A');
432         add_cell(row, 'td', globals.points_played);
433         rows.push(row);
434
435         return rows;
436 }
437
438 function make_table_offense(players) {
439         let rows = [];
440         {
441                 let header = document.createElement('tr');
442                 add_cell(header, 'th', 'Player');
443                 add_cell(header, 'th', 'Goals');
444                 add_cell(header, 'th', 'Assists');
445                 add_cell(header, 'th', 'Hockey assists');
446                 add_cell(header, 'th', 'Throws');
447                 add_cell(header, 'th', 'Throwaways');
448                 add_cell(header, 'th', '%OK');
449                 add_cell(header, 'th', 'Catches');
450                 add_cell(header, 'th', 'Drops');
451                 add_cell(header, 'th', '%OK');
452                 add_cell(header, 'th', 'Soft +/-');
453                 rows.push(header);
454         }
455
456         let num_throws = 0;
457         let throwaways = 0;
458         let catches = 0;
459         let drops = 0;
460         for (const [q,p] of Object.entries(players)) {
461                 if (q === 'globals') continue;
462                 let throw_ok = 100 * (1 - p.throwaways / p.num_throws);
463                 let catch_ok = 100 * (p.catches / (p.catches + p.drops));
464
465                 let row = document.createElement('tr');
466                 add_cell(row, 'td', p.name);  // TODO: number?
467                 add_cell(row, 'td', p.goals);
468                 add_cell(row, 'td', p.assists);
469                 add_cell(row, 'td', p.hockey_assists);
470                 add_cell(row, 'td', p.num_throws);
471                 add_cell(row, 'td', p.throwaways);
472                 if (p.num_throws > 0) {
473                         add_cell(row, 'td', throw_ok.toFixed(0) + '%');
474                 } else {
475                         add_cell(row, 'td', 'N/A');
476                 }
477                 add_cell(row, 'td', p.catches);
478                 add_cell(row, 'td', p.drops);
479                 if (p.catches + p.drops > 0) {
480                         add_cell(row, 'td', catch_ok.toFixed(0) + '%');
481                 } else {
482                         add_cell(row, 'td', 'N/A');
483                 }
484                 add_cell(row, 'td', '+' + p.offensive_soft_plus);
485                 add_cell(row, 'td', '-' + p.offensive_soft_minus);
486                 rows.push(row);
487
488                 num_throws += p.num_throws;
489                 throwaways += p.throwaways;
490                 catches += p.catches;
491                 drops += p.drops;
492         }
493
494         // Globals.
495         let throw_ok = 100 * (1 - throwaways / num_throws);
496         let catch_ok = 100 * (catches / (catches + drops));
497
498         let row = document.createElement('tr');
499         add_cell(row, 'td', '');
500         add_cell(row, 'td', '');
501         add_cell(row, 'td', '');
502         add_cell(row, 'td', '');
503         add_cell(row, 'td', num_throws);
504         add_cell(row, 'td', throwaways);
505         add_cell(row, 'td', throw_ok.toFixed(0) + '%');
506         add_cell(row, 'td', catches);
507         add_cell(row, 'td', drops);
508         add_cell(row, 'td', catch_ok.toFixed(0) + '%');
509         add_cell(row, 'td', '');
510         add_cell(row, 'td', '');
511         rows.push(row);
512
513         return rows;
514 }
515
516 function make_table_defense(players) {
517         let rows = [];
518         {
519                 let header = document.createElement('tr');
520                 add_cell(header, 'th', 'Player');
521                 add_cell(header, 'th', 'Ds');
522                 add_cell(header, 'th', 'Pulls');
523                 add_cell(header, 'th', 'OOB pulls');
524                 add_cell(header, 'th', 'Avg. hang time (IB)');
525                 add_cell(header, 'th', 'Soft +/-');
526                 rows.push(header);
527         }
528         for (const [q,p] of Object.entries(players)) {
529                 if (q === 'globals') continue;
530                 let sum_time = 0;
531                 for (const t of p.pull_times) {
532                         sum_time += t;
533                 }
534                 let avg_time = 1e-3 * sum_time / p.pulls;
535                 let oob_pct = 100 * p.oob_pulls / p.pulls;
536
537                 let row = document.createElement('tr');
538                 add_cell(row, 'td', p.name);  // TODO: number?
539                 add_cell(row, 'td', p.defenses);
540                 add_cell(row, 'td', p.pulls);
541                 if (p.pulls === 0) {
542                         add_cell(row, 'td', 'N/A');
543                 } else {
544                         add_cell(row, 'td', p.oob_pulls + ' (' + oob_pct.toFixed(0) + '%)');
545                 }
546                 if (p.pulls > p.oob_pulls) {
547                         add_cell(row, 'td', avg_time.toFixed(1) + ' sec');
548                 } else {
549                         add_cell(row, 'td', 'N/A');
550                 }
551                 add_cell(row, 'td', '+' + p.defensive_soft_plus);
552                 add_cell(row, 'td', '-' + p.defensive_soft_minus);
553                 rows.push(row);
554         }
555         return rows;
556 }
557
558 function make_table_playing_time(players) {
559         let rows = [];
560         {
561                 let header = document.createElement('tr');
562                 add_cell(header, 'th', 'Player');
563                 add_cell(header, 'th', 'Points played');
564                 add_cell(header, 'th', 'Time played');
565                 add_cell(header, 'th', 'Time on field');
566                 add_cell(header, 'th', 'O points');
567                 add_cell(header, 'th', 'D points');
568                 rows.push(header);
569         }
570
571         for (const [q,p] of Object.entries(players)) {
572                 if (q === 'globals') continue;
573                 let row = document.createElement('tr');
574                 add_cell(row, 'td', p.name);  // TODO: number?
575                 add_cell(row, 'td', p.points_played);
576                 add_cell(row, 'td', Math.floor(p.playing_time_ms / 60000) + ' min');
577                 add_cell(row, 'td', Math.floor(p.field_time_ms / 60000) + ' min');
578                 add_cell(row, 'td', p.offensive_points_completed);
579                 add_cell(row, 'td', p.defensive_points_completed);
580                 rows.push(row);
581         }
582
583         // Globals.
584         let globals = players['globals'];
585         let row = document.createElement('tr');
586         add_cell(row, 'td', '');
587         add_cell(row, 'td', globals.points_played);
588         add_cell(row, 'td', Math.floor(globals.playing_time_ms / 60000) + ' min');
589         add_cell(row, 'td', Math.floor(globals.field_time_ms / 60000) + ' min');
590         add_cell(row, 'td', globals.offensive_points_completed);
591         add_cell(row, 'td', globals.defensive_points_completed);
592         rows.push(row);
593
594         return rows;
595 }
596
597 function make_table_per_point(players) {
598         let rows = [];
599         {
600                 let header = document.createElement('tr');
601                 add_cell(header, 'th', 'Player');
602                 add_cell(header, 'th', 'Goals');
603                 add_cell(header, 'th', 'Assists');
604                 add_cell(header, 'th', 'Hockey assists');
605                 add_cell(header, 'th', 'Ds');
606                 add_cell(header, 'th', 'Throwaways');
607                 add_cell(header, 'th', 'Drops');
608                 add_cell(header, 'th', 'Touches');
609                 rows.push(header);
610         }
611
612         let goals = 0;
613         let assists = 0;
614         let hockey_assists = 0;
615         let defenses = 0;
616         let throwaways = 0;
617         let drops = 0;
618         let touches = 0;
619         for (const [q,p] of Object.entries(players)) {
620                 if (q === 'globals') continue;
621                 let row = document.createElement('tr');
622                 add_cell(row, 'td', p.name);  // TODO: number?
623                 if (p.points_played > 0) {
624                         add_cell(row, 'td', p.goals == 0 ? 0 : (p.goals / p.points_played).toFixed(2));
625                         add_cell(row, 'td', p.assists == 0 ? 0 : (p.assists / p.points_played).toFixed(2));
626                         add_cell(row, 'td', p.hockey_assists == 0 ? 0 : (p.hockey_assists / p.points_played).toFixed(2));
627                         add_cell(row, 'td', p.defenses == 0 ? 0 : (p.defenses / p.points_played).toFixed(2));
628                         add_cell(row, 'td', p.throwaways == 0 ? 0 : (p.throwaways / p.points_played).toFixed(2));
629                         add_cell(row, 'td', p.drops == 0 ? 0 : (p.drops / p.points_played).toFixed(2));
630                         add_cell(row, 'td', p.touches == 0 ? 0 : (p.touches / p.points_played).toFixed(2));
631                 } else {
632                         add_cell(row, 'td', 'N/A');
633                         add_cell(row, 'td', 'N/A');
634                         add_cell(row, 'td', 'N/A');
635                         add_cell(row, 'td', 'N/A');
636                         add_cell(row, 'td', 'N/A');
637                         add_cell(row, 'td', 'N/A');
638                         add_cell(row, 'td', 'N/A');
639                 }
640                 rows.push(row);
641
642                 goals += p.goals;
643                 assists += p.assists;
644                 hockey_assists += p.hockey_assists;
645                 defenses += p.defenses;
646                 throwaways += p.throwaways;
647                 drops += p.drops;
648                 touches += p.touches;
649         }
650
651         // Globals.
652         let globals = players['globals'];
653         let row = document.createElement('tr');
654         add_cell(row, 'td', '');
655         if (globals.points_played > 0) {
656                 add_cell(row, 'td', goals == 0 ? 0 : (goals / globals.points_played).toFixed(2));
657                 add_cell(row, 'td', assists == 0 ? 0 : (assists / globals.points_played).toFixed(2));
658                 add_cell(row, 'td', hockey_assists == 0 ? 0 : (hockey_assists / globals.points_played).toFixed(2));
659                 add_cell(row, 'td', defenses == 0 ? 0 : (defenses / globals.points_played).toFixed(2));
660                 add_cell(row, 'td', throwaways == 0 ? 0 : (throwaways / globals.points_played).toFixed(2));
661                 add_cell(row, 'td', drops == 0 ? 0 : (drops / globals.points_played).toFixed(2));
662                 add_cell(row, 'td', touches == 0 ? 0 : (touches / globals.points_played).toFixed(2));
663         } else {
664                 add_cell(row, 'td', 'N/A');
665                 add_cell(row, 'td', 'N/A');
666                 add_cell(row, 'td', 'N/A');
667                 add_cell(row, 'td', 'N/A');
668                 add_cell(row, 'td', 'N/A');
669                 add_cell(row, 'td', 'N/A');
670                 add_cell(row, 'td', 'N/A');
671         }
672         rows.push(row);
673
674         return rows;
675 }