]> git.sesse.net Git - pkanalytics/blob - ultimate.js
Make a table for the pulls 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.throwaways;
164                                 handler = prev_handler = null;
165                         } else if (type === 'drop') {
166                                 ++p.drops;
167                                 handler = prev_handler = null;
168                         } else if (type === 'defense') {
169                                 ++p.defenses;
170                         } else if (type === 'interception') {
171                                 ++p.interceptions;
172                                 ++p.defenses;
173                                 ++p.touches;
174                                 prev_handler = null;
175                                 handler = e['player'];
176                         } else if (type === 'offensive_soft_plus' || type === 'offensive_soft_minus' || type === 'defensive_soft_plus' || type === 'defensive_soft_minus') {
177                                 ++p[type];
178                         } else if (type !== 'in' && type !== 'out' && type !== 'pull' &&
179                                    type !== 'their_goal' && type !== 'stoppage' && type !== 'unknown' &&
180                                    type !== 'set_defense' && type !== 'goal' && type !== 'throwaway' &&
181                                    type !== 'drop' && type !== 'set_offense' && type !== 'their_goal' &&
182                                    type !== 'pull' && type !== 'pull_landed' && type !== 'pull_oob' &&
183                                    type !== 'their_throwaway' && type !== 'defense' && type !== 'interception') {
184                                 console.log("Unknown event:", e);
185                         }
186                 }
187         }
188
189         let chosen_category = get_chosen_category();
190         write_main_menu(chosen_category);
191
192         let rows = [];
193         if (chosen_category === 'general') {
194                 rows = make_table_general(players);
195         } else if (chosen_category === 'pulls') {
196                 rows = make_table_pulls(players);
197         }
198         document.getElementById('stats').replaceChildren(...rows);
199 }
200
201 function get_chosen_category() {
202         if (window.location.hash === '#pulls') {
203                 return 'pulls';
204         } else {
205                 return 'general';
206         }
207 }
208
209 function write_main_menu(chosen_category) {
210         let elems = [];
211         if (chosen_category === 'general') {
212                 elems.push(document.createTextNode('General'));
213         } else {
214                 let a = document.createElement('a');
215                 a.appendChild(document.createTextNode('General'));
216                 a.setAttribute('href', '#general');
217                 elems.push(a);
218         }
219         elems.push(document.createTextNode(' | '));
220         if (chosen_category === 'pulls') {
221                 elems.push(document.createTextNode('Pulls'));
222         } else {
223                 let a = document.createElement('a');
224                 a.appendChild(document.createTextNode('Pulls'));
225                 a.setAttribute('href', '#pulls');
226                 elems.push(a);
227         }
228         document.getElementById('mainmenu').replaceChildren(...elems);
229 }
230
231 function make_table_general(players) {
232         let rows = [];
233         {
234                 let header = document.createElement('tr');
235                 add_cell(header, 'th', 'Player');
236                 add_cell(header, 'th', '+/-');
237                 add_cell(header, 'th', 'Soft +/-');
238                 add_cell(header, 'th', 'Points played');
239                 add_cell(header, 'th', 'Time played');
240                 rows.push(header);
241         }
242
243         for (const [q,p] of Object.entries(players)) {
244                 let row = document.createElement('tr');
245                 let pm = p.goals + p.assists + p.hockey_assists + p.defenses - p.throwaways - p.drops;
246                 let soft_pm = p.offensive_soft_plus + p.defensive_soft_plus - p.offensive_soft_minus - p.defensive_soft_minus;
247                 add_cell(row, 'td', p.name);  // TODO: number?
248                 add_cell(row, 'td', pm > 0 ? ('+' + pm) : pm);
249                 add_cell(row, 'td', soft_pm > 0 ? ('+' + soft_pm) : soft_pm);
250                 add_cell(row, 'td', p.points_played);
251                 add_cell(row, 'td', Math.floor(p.playing_time_ms / 60000) + ' min');
252                 rows.push(row);
253                 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);
254         }
255         return rows;
256 }
257
258 function make_table_pulls(players) {
259         let rows = [];
260         {
261                 let header = document.createElement('tr');
262                 add_cell(header, 'th', 'Player');
263                 add_cell(header, 'th', 'Pulls');
264                 add_cell(header, 'th', 'OOB pulls');
265                 add_cell(header, 'th', 'Avg. hang time (IB)');
266                 rows.push(header);
267         }
268         for (const [q,p] of Object.entries(players)) {
269                 if (p.pulls === 0) {
270                         continue;
271                 }
272                 let sum_time = 0;
273                 for (const t of p.pull_times) {
274                         sum_time += t;
275                 }
276                 let avg_time = 1e-3 * sum_time / p.pulls;
277                 let oob_pct = 100 * p.oob_pulls / p.pulls;
278
279                 let row = document.createElement('tr');
280                 add_cell(row, 'td', p.name);  // TODO: number?
281                 add_cell(row, 'td', p.pulls);
282                 add_cell(row, 'td', p.oob_pulls + ' (' + oob_pct.toFixed(0) + '%)');
283                 if (p.pulls > p.oob_pulls) {
284                         add_cell(row, 'td', avg_time.toFixed(1) + ' sec');
285                 } else {
286                         add_cell(row, 'td', 'N/A');
287                 }
288                 rows.push(row);
289         }
290         return rows;
291 }