]> git.sesse.net Git - pkanalytics/blob - ultimate.js
Make a table for the offense category.
[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', () => { console.log('heei'); 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         player.on_field_since = null;
28 }
29
30 function add_cell(tr, element_type, text) {
31         let element = document.createElement(element_type);
32         element.textContent = text;
33         tr.appendChild(element);
34 }
35
36 function process_matches(json) {
37         let players = {};
38         for (const player of json['players']) {
39                 players[player['player_id']] = {
40                         'name': player['name'],
41                         'number': player['number'],
42
43                         'goals': 0,
44                         'assists': 0,
45                         'hockey_assists': 0,
46                         'catches': 0,
47                         'touches': 0,
48                         'num_throws': 0,
49                         'throwaways': 0,
50                         'drops': 0,
51
52                         'defenses': 0,
53                         'interceptions': 0,
54                         'points_played': 0,
55                         'playing_time_ms': 0,
56
57                         'offensive_soft_plus': 0,
58                         'offensive_soft_minus': 0,
59                         'defensive_soft_plus': 0,
60                         'defensive_soft_minus': 0,
61
62                         'pulls': 0,
63                         'pull_times': [],
64                         'oob_pulls': 0,
65
66                         // Internal.
67                         'last_point_seen': null,
68                         'on_field_since': null,
69                 };
70         }
71
72         for (const match of json['matches']) {
73                 let handler = null;
74                 let prev_handler = null;
75                 let live_since = null;
76                 let offense = null;
77                 let puller = null;
78                 let pull_started = null;
79                 let point_num = 0;
80                 for (const [q,p] of Object.entries(players)) {
81                         p.on_field_since = null;
82                         p.last_point_seen = null;
83                 }
84                 for (const e of match['events']) {
85                         let t = e['t'];
86                         let type = e['type'];
87                         let p = players[e['player']];
88
89                         // Point count management
90                         if (p !== undefined && type !== 'out' && p.last_point_seen !== point_num) {
91                                 p.last_point_seen = point_num;
92                                 ++p.points_played;
93                         }
94                         if (type === 'goal' || type === 'their_goal') {
95                                 ++point_num;
96                         }
97
98                         // Sub management
99                         if (type === 'in' && p.on_field_since === null) {
100                                 p.on_field_since = t;
101                         } else if (type === 'out') {
102                                 take_off_field(p, t, live_since);
103                         }
104
105                         // Liveness management
106                         if (type === 'pull' || type === 'their_pull' || type === 'restart') {
107                                 live_since = t;
108                         } else if (type === 'goal' || type === 'their_goal' || type === 'stoppage') {
109                                 for (const [q,p] of Object.entries(players)) {
110                                         if (p.on_field_since !== null) {
111                                                 attribute_player_time(p, t, live_since);
112                                         }
113                                 }
114                                 live_since = null;
115                         }
116
117                         // Pull management
118                         if (type === 'pull') {
119                                 puller = e['player'];
120                                 pull_started = t;
121                                 ++p.pulls;
122                         } else if (type === 'in' || type === 'out' || type === 'stoppage' || type === 'restart' || type === 'unknown' || type === 'set_defense' || type === 'set_offense') {
123                                 // No effect on pull.
124                         } else if (type === 'pull_landed' && puller !== null) {
125                                 players[puller].pull_times.push(t - pull_started);
126                         } else if (type === 'pull_oob' && puller !== null) {
127                                 ++players[puller].oob_pulls;
128                         } else {
129                                 // Not pulling (if there was one, we never recorded its outcome, but still count it).
130                                 puller = pull_started = null;
131                         }
132
133                         // Offense/defense management (TODO: use it for actual counting)
134                         if (type === 'set_defense' || type === 'goal' || type === 'throwaway' || type === 'drop') {
135                                 offense = false;
136                         } else if (type === 'set_offense' || type === 'their_goal' || type === 'their_throwaway' || type === 'defense' || type === 'interception') {
137                                 offense = true;
138                         }
139
140                         // Event management
141                         if (type === 'catch' || type === 'goal') {
142                                 if (handler !== null) {
143                                         ++players[handler].num_throws;
144                                         ++p.catches;
145                                 }
146
147                                 ++p.touches;
148                                 if (type === 'goal') {
149                                         if (prev_handler !== null) {
150                                                 ++players[prev_handler].hockey_assists;
151                                         }
152                                         if (handler !== null) {
153                                                 ++players[handler].assists;
154                                         }
155                                         ++p.goals;
156                                         handler = prev_handler = null;
157                                 } else {
158                                         // Update hold history.
159                                         prev_handler = handler;
160                                         handler = e['player'];
161                                 }
162                         } else if (type === 'throwaway') {
163                                 ++p.num_throws;
164                                 ++p.throwaways;
165                                 handler = prev_handler = null;
166                         } else if (type === 'drop') {
167                                 ++p.drops;
168                                 handler = prev_handler = null;
169                         } else if (type === 'defense') {
170                                 ++p.defenses;
171                         } else if (type === 'interception') {
172                                 ++p.interceptions;
173                                 ++p.defenses;
174                                 ++p.touches;
175                                 prev_handler = null;
176                                 handler = e['player'];
177                         } else if (type === 'offensive_soft_plus' || type === 'offensive_soft_minus' || type === 'defensive_soft_plus' || type === 'defensive_soft_minus') {
178                                 ++p[type];
179                         } else if (type !== 'in' && type !== 'out' && type !== 'pull' &&
180                                    type !== 'their_goal' && type !== 'stoppage' && type !== 'unknown' &&
181                                    type !== 'set_defense' && type !== 'goal' && type !== 'throwaway' &&
182                                    type !== 'drop' && type !== 'set_offense' && type !== 'their_goal' &&
183                                    type !== 'pull' && type !== 'pull_landed' && type !== 'pull_oob' &&
184                                    type !== 'their_throwaway' && type !== 'defense' && type !== 'interception') {
185                                 console.log("Unknown event:", e);
186                         }
187                 }
188         }
189
190         let chosen_category = get_chosen_category();
191         write_main_menu(chosen_category);
192
193         let rows = [];
194         if (chosen_category === 'general') {
195                 rows = make_table_general(players);
196         } else if (chosen_category === 'offense') {
197                 rows = make_table_offense(players);
198         } else if (chosen_category === 'pulls') {
199                 rows = make_table_pulls(players);
200         }
201         document.getElementById('stats').replaceChildren(...rows);
202 }
203
204 function get_chosen_category() {
205         if (window.location.hash === '#pulls') {
206                 return 'pulls';
207         } else if (window.location.hash === '#offense') {
208                 return 'offense';
209         } else {
210                 return 'general';
211         }
212 }
213
214 function write_main_menu(chosen_category) {
215         let elems = [];
216         if (chosen_category === 'general') {
217                 elems.push(document.createTextNode('General'));
218         } else {
219                 let a = document.createElement('a');
220                 a.appendChild(document.createTextNode('General'));
221                 a.setAttribute('href', '#general');
222                 elems.push(a);
223         }
224
225         elems.push(document.createTextNode(' | '));
226         if (chosen_category === 'pulls') {
227                 elems.push(document.createTextNode('Pulls'));
228         } else {
229                 let a = document.createElement('a');
230                 a.appendChild(document.createTextNode('Pulls'));
231                 a.setAttribute('href', '#pulls');
232                 elems.push(a);
233         }
234
235         elems.push(document.createTextNode(' | '));
236         if (chosen_category === 'offense') {
237                 elems.push(document.createTextNode('Offense'));
238         } else {
239                 let a = document.createElement('a');
240                 a.appendChild(document.createTextNode('Offense'));
241                 a.setAttribute('href', '#offense');
242                 elems.push(a);
243         }
244
245         document.getElementById('mainmenu').replaceChildren(...elems);
246 }
247
248 function make_table_general(players) {
249         let rows = [];
250         {
251                 let header = document.createElement('tr');
252                 add_cell(header, 'th', 'Player');
253                 add_cell(header, 'th', '+/-');
254                 add_cell(header, 'th', 'Soft +/-');
255                 add_cell(header, 'th', 'Points played');
256                 add_cell(header, 'th', 'Time played');
257                 rows.push(header);
258         }
259
260         for (const [q,p] of Object.entries(players)) {
261                 let row = document.createElement('tr');
262                 let pm = p.goals + p.assists + p.hockey_assists + p.defenses - p.throwaways - p.drops;
263                 let soft_pm = p.offensive_soft_plus + p.defensive_soft_plus - p.offensive_soft_minus - p.defensive_soft_minus;
264                 add_cell(row, 'td', p.name);  // TODO: number?
265                 add_cell(row, 'td', pm > 0 ? ('+' + pm) : pm);
266                 add_cell(row, 'td', soft_pm > 0 ? ('+' + soft_pm) : soft_pm);
267                 add_cell(row, 'td', p.points_played);
268                 add_cell(row, 'td', Math.floor(p.playing_time_ms / 60000) + ' min');
269                 rows.push(row);
270                 console.log(p.name + " played " + p.points_played + " points (" + Math.floor(p.playing_time_ms / 60000) + " min), " + p.goals + " goals, " + p.assists + " assists, plus/minus: " + pm);
271         }
272         return rows;
273 }
274
275 function make_table_offense(players) {
276         let rows = [];
277         {
278                 let header = document.createElement('tr');
279                 add_cell(header, 'th', 'Player');
280                 add_cell(header, 'th', 'Goals');
281                 add_cell(header, 'th', 'Assists');
282                 add_cell(header, 'th', 'Hockey assists');
283                 add_cell(header, 'th', 'Throws');
284                 add_cell(header, 'th', 'Throwaways');
285                 add_cell(header, 'th', '%OK');
286                 add_cell(header, 'th', 'Catches');
287                 add_cell(header, 'th', 'Drops');
288                 add_cell(header, 'th', '%OK');
289                 add_cell(header, 'th', 'Soft +/-');
290                 rows.push(header);
291         }
292
293         for (const [q,p] of Object.entries(players)) {
294                 let throw_ok = 100 * (1 - p.throwaways / p.num_throws);
295                 let catch_ok = 100 * (p.catches / (p.catches + p.drops));
296
297                 let row = document.createElement('tr');
298                 add_cell(row, 'td', p.name);  // TODO: number?
299                 add_cell(row, 'td', p.goals);
300                 add_cell(row, 'td', p.assists);
301                 add_cell(row, 'td', p.hockey_assists);
302                 add_cell(row, 'td', p.num_throws);
303                 add_cell(row, 'td', p.throwaways);
304                 add_cell(row, 'td', throw_ok.toFixed(0) + '%');
305                 add_cell(row, 'td', p.catches);
306                 add_cell(row, 'td', p.drops);
307                 add_cell(row, 'td', catch_ok.toFixed(0) + '%');
308                 add_cell(row, 'td', '+' + p.offensive_soft_plus);
309                 add_cell(row, 'td', '-' + p.offensive_soft_minus);
310                 rows.push(row);
311         }
312         return rows;
313 }
314
315 function make_table_pulls(players) {
316         let rows = [];
317         {
318                 let header = document.createElement('tr');
319                 add_cell(header, 'th', 'Player');
320                 add_cell(header, 'th', 'Pulls');
321                 add_cell(header, 'th', 'OOB pulls');
322                 add_cell(header, 'th', 'Avg. hang time (IB)');
323                 rows.push(header);
324         }
325         for (const [q,p] of Object.entries(players)) {
326                 if (p.pulls === 0) {
327                         continue;
328                 }
329                 let sum_time = 0;
330                 for (const t of p.pull_times) {
331                         sum_time += t;
332                 }
333                 let avg_time = 1e-3 * sum_time / p.pulls;
334                 let oob_pct = 100 * p.oob_pulls / p.pulls;
335
336                 let row = document.createElement('tr');
337                 add_cell(row, 'td', p.name);  // TODO: number?
338                 add_cell(row, 'td', p.pulls);
339                 add_cell(row, 'td', p.oob_pulls + ' (' + oob_pct.toFixed(0) + '%)');
340                 if (p.pulls > p.oob_pulls) {
341                         add_cell(row, 'td', avg_time.toFixed(1) + ' sec');
342                 } else {
343                         add_cell(row, 'td', 'N/A');
344                 }
345                 rows.push(row);
346         }
347         return rows;
348 }