]> git.sesse.net Git - pkanalytics/blob - ultimate.js
Add a rudimentary start of the viewer.
[pkanalytics] / ultimate.js
1 'use strict';
2
3 // No frameworks, no compilers, no npm, just JavaScript. :-)
4
5 let json;
6
7 fetch('ultimate.json')
8    .then(response => response.json())
9    .then(response => { process_matches(response); });
10
11 function attribute_player_time(player, to, from) {
12         if (player.on_field_since > from) {
13                 // Player came in while play happened (without a stoppage!?).
14                 player.playing_time_ms += to - player.on_field_since;
15         } else {
16                 player.playing_time_ms += to - from;
17         }
18 }
19
20 function take_off_field(player, t, live_since) {
21         if (live_since === null) {
22                 // Play isn't live, so nothing to do.
23         } else {
24                 attribute_player_time(player, t, live_since);
25         }
26         player.on_field_since = null;
27 }
28
29 function add_cell(tr, element_type, text) {
30         let element = document.createElement(element_type);
31         element.textContent = text;
32         tr.appendChild(element);
33 }
34
35 function process_matches(json) {
36         let players = {};
37         for (const player of json['players']) {
38                 players[player['player_id']] = {
39                         'name': player['name'],
40                         'number': player['number'],
41
42                         'goals': 0,
43                         'assists': 0,
44                         'hockey_assists': 0,
45                         'catches': 0,
46                         'touches': 0,
47                         'num_throws': 0,
48                         'throwaways': 0,
49                         'drops': 0,
50
51                         'defenses': 0,
52                         'interceptions': 0,
53                         'points_played': 0,
54                         'playing_time_ms': 0,
55
56                         'offensive_soft_plus': 0,
57                         'offensive_soft_minus': 0,
58                         'defensive_soft_plus': 0,
59                         'defensive_soft_minus': 0,
60
61                         'pulls': 0,
62                         'pull_times': [],
63                         'oob_pulls': 0,
64
65                         // Internal.
66                         'last_point_seen': null,
67                         'on_field_since': null,
68                 };
69         }
70
71         for (const match of json['matches']) {
72                 let handler = null;
73                 let prev_handler = null;
74                 let live_since = null;
75                 let offense = null;
76                 let puller = null;
77                 let pull_started = null;
78                 let point_num = 0;
79                 for (const [q,p] of Object.entries(players)) {
80                         p.on_field_since = null;
81                         p.last_point_seen = null;
82                 }
83                 for (const e of match['events']) {
84                         let t = e['t'];
85                         let type = e['type'];
86                         let p = players[e['player']];
87
88                         // Point count management
89                         if (p !== undefined && type !== 'out' && p.last_point_seen !== point_num) {
90                                 p.last_point_seen = point_num;
91                                 ++p.points_played;
92                         }
93                         if (type === 'goal' || type === 'their_goal') {
94                                 ++point_num;
95                         }
96
97                         // Sub management
98                         if (type === 'in' && p.on_field_since === null) {
99                                 p.on_field_since = t;
100                         } else if (type === 'out') {
101                                 take_off_field(p, t, live_since);
102                         }
103
104                         // Liveness management
105                         if (type === 'pull' || type === 'their_pull' || type === 'restart') {
106                                 live_since = t;
107                         } else if (type === 'goal' || type === 'their_goal' || type === 'stoppage') {
108                                 for (const [q,p] of Object.entries(players)) {
109                                         if (p.on_field_since !== null) {
110                                                 attribute_player_time(p, t, live_since);
111                                         }
112                                 }
113                                 live_since = null;
114                         }
115
116                         // Pull management
117                         if (type === 'pull') {
118                                 puller = e['player'];
119                                 pull_started = t;
120                                 ++p.pulls;
121                         } else if (type === 'in' || type === 'out' || type === 'stoppage' || type === 'restart' || type === 'unknown' || type === 'set_defense' || type === 'set_offense') {
122                                 // No effect on pull.
123                         } else if (type === 'pull_landed' && puller !== null) {
124                                 players[puller].pull_times.push(t - pull_started);
125                         } else if (type === 'pull_oob' && puller !== null) {
126                                 ++players[puller].oob_pulls;
127                         } else {
128                                 // Not pulling (if there was one, we never recorded its outcome, but still count it).
129                                 puller = pull_started = null;
130                         }
131
132                         // Offense/defense management (TODO: use it for actual counting)
133                         if (type === 'set_defense' || type === 'goal' || type === 'throwaway' || type === 'drop') {
134                                 offense = false;
135                         } else if (type === 'set_offense' || type === 'their_goal' || type === 'their_throwaway' || type === 'defense' || type === 'interception') {
136                                 offense = true;
137                         }
138
139                         // Event management
140                         if (type === 'catch' || type === 'goal') {
141                                 if (handler !== null) {
142                                         ++players[handler].num_throws;
143                                         ++p.catches;
144                                 }
145
146                                 ++p.touches;
147                                 if (type === 'goal') {
148                                         if (prev_handler !== null) {
149                                                 ++players[prev_handler].hockey_assists;
150                                         }
151                                         if (handler !== null) {
152                                                 ++players[handler].assists;
153                                         }
154                                         ++p.goals;
155                                         handler = prev_handler = null;
156                                 } else {
157                                         // Update hold history.
158                                         prev_handler = handler;
159                                         handler = e['player'];
160                                 }
161                         } else if (type === 'throwaway') {
162                                 ++p.throwaways;
163                                 handler = prev_handler = null;
164                         } else if (type === 'drop') {
165                                 ++p.drops;
166                                 handler = prev_handler = null;
167                         } else if (type === 'defense') {
168                                 ++p.defenses;
169                         } else if (type === 'interception') {
170                                 ++p.interceptions;
171                                 ++p.defenses;
172                                 ++p.touches;
173                                 prev_handler = null;
174                                 handler = e['player'];
175                         } else if (type === 'offensive_soft_plus' || type === 'offensive_soft_minus' || type === 'defensive_soft_plus' || type === 'defensive_soft_minus') {
176                                 ++p[type];
177                         } else if (type !== 'in' && type !== 'out' && type !== 'pull' &&
178                                    type !== 'their_pull' && type !== 'restart' && type !== 'goal' &&
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 rows = [];
190
191         {
192                 let header = document.createElement('tr');
193                 add_cell(header, 'th', 'Player');
194                 add_cell(header, 'th', '+/-');
195                 add_cell(header, 'th', 'Soft +/-');
196                 add_cell(header, 'th', 'Points played');
197                 add_cell(header, 'th', 'Time played');
198                 rows.push(header);
199         }
200
201         for (const [q,p] of Object.entries(players)) {
202                 let row = document.createElement('tr');
203                 let pm = p.goals + p.assists + p.hockey_assists + p.defenses - p.throwaways - p.drops;
204                 let soft_pm = p.offensive_soft_plus + p.defensive_soft_plus - p.offensive_soft_minus - p.defensive_soft_minus;
205                 add_cell(row, 'td', p.name);  // TODO: number?
206                 add_cell(row, 'td', pm > 0 ? ('+' + pm) : pm);
207                 add_cell(row, 'td', soft_pm > 0 ? ('+' + soft_pm) : soft_pm);
208                 add_cell(row, 'td', p.points_played);
209                 add_cell(row, 'td', Math.floor(p.playing_time_ms / 60000) + ' min');
210                 rows.push(row);
211                 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);
212         }
213         document.getElementById('stats').replaceChildren(...rows);
214
215         console.log("PULL STATS");
216         for (const [q,p] of Object.entries(players)) {
217                 if (p.pulls === 0) {
218                         continue;
219                 }
220                 let sum_time = 0;
221                 for (const t of p.pull_times) {
222                         sum_time += t;
223                 }
224                 let avg_time = 1e-3 * sum_time / p.pulls;
225                 let msg = p.name + ' did ' + p.pulls + ' pull(s), ' + p.oob_pulls + ' OOB';
226                 if (p.oob_pulls < p.pulls) {
227                         msg += ', avg. hangtime ' + avg_time.toFixed(1) + ' sec for others';
228                 }
229                 console.log(msg, p.pull_times);
230         }
231 }