]> git.sesse.net Git - pkanalytics/blob - ultimate.js
7de103e86b385c6243508b307f1b4277182a95a1
[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                 console.log("PULL STATS");
197                 for (const [q,p] of Object.entries(players)) {
198                         if (p.pulls === 0) {
199                                 continue;
200                         }
201                         let sum_time = 0;
202                         for (const t of p.pull_times) {
203                                 sum_time += t;
204                         }
205                         let avg_time = 1e-3 * sum_time / p.pulls;
206                         let msg = p.name + ' did ' + p.pulls + ' pull(s), ' + p.oob_pulls + ' OOB';
207                         if (p.oob_pulls < p.pulls) {
208                                 msg += ', avg. hangtime ' + avg_time.toFixed(1) + ' sec for others';
209                         }
210                         console.log(msg, p.pull_times);
211                 }
212         }
213         document.getElementById('stats').replaceChildren(...rows);
214 }
215
216 function get_chosen_category() {
217         if (window.location.hash === '#pulls') {
218                 return 'pulls';
219         } else {
220                 return 'general';
221         }
222 }
223
224 function write_main_menu(chosen_category) {
225         let elems = [];
226         if (chosen_category === 'general') {
227                 elems.push(document.createTextNode('General'));
228         } else {
229                 let a = document.createElement('a');
230                 a.appendChild(document.createTextNode('General'));
231                 a.setAttribute('href', '#general');
232                 elems.push(a);
233         }
234         elems.push(document.createTextNode(' | '));
235         if (chosen_category === 'pulls') {
236                 elems.push(document.createTextNode('Pulls'));
237         } else {
238                 let a = document.createElement('a');
239                 a.appendChild(document.createTextNode('Pulls'));
240                 a.setAttribute('href', '#pulls');
241                 elems.push(a);
242         }
243         document.getElementById('mainmenu').replaceChildren(...elems);
244 }
245
246 function make_table_general(players) {
247         let rows = [];
248         {
249                 let header = document.createElement('tr');
250                 add_cell(header, 'th', 'Player');
251                 add_cell(header, 'th', '+/-');
252                 add_cell(header, 'th', 'Soft +/-');
253                 add_cell(header, 'th', 'Points played');
254                 add_cell(header, 'th', 'Time played');
255                 rows.push(header);
256         }
257
258         for (const [q,p] of Object.entries(players)) {
259                 let row = document.createElement('tr');
260                 let pm = p.goals + p.assists + p.hockey_assists + p.defenses - p.throwaways - p.drops;
261                 let soft_pm = p.offensive_soft_plus + p.defensive_soft_plus - p.offensive_soft_minus - p.defensive_soft_minus;
262                 add_cell(row, 'td', p.name);  // TODO: number?
263                 add_cell(row, 'td', pm > 0 ? ('+' + pm) : pm);
264                 add_cell(row, 'td', soft_pm > 0 ? ('+' + soft_pm) : soft_pm);
265                 add_cell(row, 'td', p.points_played);
266                 add_cell(row, 'td', Math.floor(p.playing_time_ms / 60000) + ' min');
267                 rows.push(row);
268                 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);
269         }
270         return rows;
271 }