-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
345 lines (307 loc) · 17.7 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
// Argv order:
// 0. node
// 1. String, file to run
// 2. String, shootingNumbers file name
// 3. Boolean, true to use a gameStats file that already exists, false to create a new one
// 4. String, filename of gameStats file to use or gameId to fetch
// 5. Boolean, calculate other teams stats
// 6. String, filename of other teams stats file to use or gameId to fetch
// 7. String Season years to save as
// Example - using a gameId input from the command line
// node index.js jazzShootingNumbers false none 19646f6e-9812-4218-9289-f0340aab4168 > results/ 2021-11-07-jazz-magic.txt 2021-2022
// Example - using a preloaded gameStats file
// ode index.js jazzShootingNumbers true 2021-11-11-jazz-pacers > results/2021-11-11-jazz-pacers.txt
const fs = require('fs');
require('dotenv')
const main = async () => {
// Setting up the work
const GetGameInfo = require('./getGameInfo');
const gameFile = process.argv[4];
let allEvents
const getAllEvents = async () => {
if (process.argv[3] === 'true') {
allEvents = require(`./setupInfo/gameStats/2022-2023/${gameFile}`)
}
else {
allEvents = await GetGameInfo.getGameInfo(gameFile)
}
}
// Either get game events from the file or make the API call to get events
await getAllEvents()
// Initialize the variables
const seasonYears = process.argv[7] || '2022-2023'
const calculateOtherTeam = process.argv[5] === 'true' ? true : false;
const shootingFile = process.argv[2] || 'jazzShootingNumbers2020-2021'
const otherShootingFile = process.argv[6] || null
const otherTeamShootingPercentages = otherShootingFile ? require(`./setupInfo/shootingNumbers/2022-2023/${otherShootingFile}`) : null
const playerShootingPercentages = require(`./setupInfo/shootingNumbers/2022-2023/${shootingFile}`);
const home = allEvents.home.name
const away = allEvents.away.name
const opponent = allEvents.home.name === "Jazz" ? allEvents.away.name : allEvents.home.name
const date = allEvents.scheduled.slice(0, 10)
const allEventsList = []
let actualScore = {}
// Get events from each period and put them into a single array
allEvents.periods.forEach(period => allEventsList.push(...period.events))
// Find game events with statistics and then put only the statistics into an array
const eventsWithStats = allEventsList.filter(event => event?.statistics?.length > 0);
const allStats = []
eventsWithStats.forEach(e => allStats.push(...e.statistics));
// Filter out all statistics that aren't fieldgoal or freethrow
const filteredStats = allStats.filter(stat => ['fieldgoal', 'freethrow'].includes(stat.type))
// Sort stats by player into a list for each player
const playerStats = sortStatsByPlayer(filteredStats)
const expectedScore = {}
const playerScoring = {}
let twoPointersTaken = {}
let threePointersTaken = {}
let freeThrowsTaken = {}
let totalShotsTaken = {}
for (let team in playerStats) {
playerScoring[team] = {}
actualScore[team] = 0
twoPointersTaken[team] = 0
threePointersTaken[team] = 0
freeThrowsTaken[team] = 0
totalShotsTaken[team] = 0
if (team === 'Jazz' || calculateOtherTeam) {
const currentTeamShootingPercentages = team === 'Jazz' ? playerShootingPercentages : otherTeamShootingPercentages
expectedScore[team] = 0
for (let player in playerStats[team]) {
playerScoring[team][player] = {}
playerScoring[team][player]['expected'] = 0
playerScoring[team][player]['actual'] = 0
for (let shot of playerStats[team][player]) {
// console.log({ player: shot.player, player, shotMade: shot.shotMade, distance: shot.distance })
totalShotsTaken[team]++
let shotValue = 0
if (shot.three_point_shot) {
threePointersTaken[team]++
shotValue = 3
} else if (shot.type === 'freethrow') {
freeThrowsTaken[team]++
shotValue = 1
} else {
twoPointersTaken[team]++
shotValue = 2
}
if (shot.type === 'freethrow') {
expectedPoints = (currentTeamShootingPercentages[team][shot.player.full_name]['ft'] / 100) * shotValue
expectedScore[team] += expectedPoints
if (!playerScoring[team][shot.player.full_name]) {
playerScoring[team][shot.player.full_name] = {}
}
if (playerScoring[team][shot.player.full_name].expected) {
playerScoring[team][shot.player.full_name].expected += expectedPoints
} else {
playerScoring[team][shot.player.full_name]['expected'] = expectedPoints
}
if (shot.made) {
actualScore[team] += shotValue
if (!playerScoring[team][shot.player.full_name]) {
playerScoring[team][shot.player.full_name] = {}
}
if (!playerScoring[team][shot.player.full_name].actual) {
playerScoring[team][shot.player.full_name]['actual'] = shotValue
} else {
playerScoring[team][shot.player.full_name].actual += shotValue
}
}
} else {
if (shot.shot_distance > 0 && shot.shot_distance <= 4) {
const expectedPoints = (currentTeamShootingPercentages[team][shot.player.full_name]['_0_4'] / 100) * shotValue
expectedScore[team] += expectedPoints
if (!playerScoring[team][shot.player.full_name]) {
playerScoring[team][shot.player.full_name] = {}
}
if (playerScoring[team][shot.player.full_name].expected) {
playerScoring[team][shot.player.full_name].expected += expectedPoints
} else {
playerScoring[team][shot.player.full_name]['expected'] = expectedPoints
}
if (shot.made) {
actualScore[team] += shotValue
if (!playerScoring[team][shot.player.full_name]) {
playerScoring[team][shot.player.full_name] = {}
}
if (!playerScoring[team][shot.player.full_name].actual) {
playerScoring[team][shot.player.full_name]['actual'] = shotValue
} else {
playerScoring[team][shot.player.full_name].actual += shotValue
}
}
} else if (shot.shot_distance > 4 && shot.shot_distance <= 9) {
const expectedPoints = (currentTeamShootingPercentages[team][shot.player.full_name]['_5_9'] / 100) * shotValue
expectedScore[team] += expectedPoints
if (!playerScoring[team][shot.player.full_name]) {
playerScoring[team][shot.player.full_name] = {}
}
if (playerScoring[team][shot.player.full_name].expected) {
playerScoring[team][shot.player.full_name].expected += expectedPoints
} else {
playerScoring[team][shot.player.full_name]['expected'] = expectedPoints
}
if (shot.made) {
actualScore[team] += shotValue
if (!playerScoring[team][shot.player.full_name]) {
playerScoring[team][shot.player.full_name] = {}
}
if (!playerScoring[team][shot.player.full_name].actual) {
playerScoring[team][shot.player.full_name]['actual'] = shotValue
} else {
playerScoring[team][shot.player.full_name].actual += shotValue
}
}
} else if (shot.shot_distance > 9 && shot.shot_distance <= 14) {
const expectedPoints = (currentTeamShootingPercentages[team][shot.player.full_name]['_10_14'] / 100) * shotValue
expectedScore[team] += expectedPoints
if (!playerScoring[team][shot.player.full_name]) {
playerScoring[team][shot.player.full_name] = {}
}
if (playerScoring[team][shot.player.full_name].expected) {
playerScoring[team][shot.player.full_name].expected += expectedPoints
} else {
playerScoring[team][shot.player.full_name]['expected'] = expectedPoints
}
if (shot.made) {
actualScore[team] += shotValue
if (!playerScoring[team][shot.player.full_name]) {
playerScoring[team][shot.player.full_name] = {}
}
if (!playerScoring[team][shot.player.full_name].actual) {
playerScoring[team][shot.player.full_name]['actual'] = shotValue
} else {
playerScoring[team][shot.player.full_name].actual += shotValue
}
}
} else if (shot.shot_distance > 14 && shot.shot_distance <= 19) {
const expectedPoints = (currentTeamShootingPercentages[team][shot.player.full_name]['_15_19'] / 100) * shotValue
expectedScore[team] += expectedPoints
if (!playerScoring[team][shot.player.full_name]) {
playerScoring[team][shot.player.full_name] = {}
}
if (playerScoring[team][shot.player.full_name].expected) {
playerScoring[team][shot.player.full_name].expected += expectedPoints
} else {
playerScoring[team][shot.player.full_name]['expected'] = expectedPoints
}
if (shot.made) {
actualScore[team] += shotValue
if (!playerScoring[team][shot.player.full_name]) {
playerScoring[team][shot.player.full_name] = {}
}
if (!playerScoring[team][shot.player.full_name].actual) {
playerScoring[team][shot.player.full_name]['actual'] = shotValue
} else {
playerScoring[team][shot.player.full_name].actual += shotValue
}
}
} else if (shot.shot_distance > 19 && shot.shot_distance <= 24) {
const expectedPoints = (currentTeamShootingPercentages[team][shot.player.full_name]['_20_24'] / 100) * shotValue
expectedScore[team] += expectedPoints
if (!playerScoring[team][shot.player.full_name]) {
playerScoring[team][shot.player.full_name] = {}
}
if (playerScoring[team][shot.player.full_name].expected) {
playerScoring[team][shot.player.full_name].expected += expectedPoints
} else {
playerScoring[team][shot.player.full_name]['expected'] = expectedPoints
}
if (shot.made) {
actualScore[team] += shotValue
if (!playerScoring[team][shot.player.full_name]) {
playerScoring[team][shot.player.full_name] = {}
}
if (!playerScoring[team][shot.player.full_name].actual) {
playerScoring[team][shot.player.full_name]['actual'] = shotValue
} else {
playerScoring[team][shot.player.full_name].actual += shotValue
}
}
} else {
const expectedPoints = (currentTeamShootingPercentages[team][shot.player.full_name]['_25_29'] / 100) * shotValue
expectedScore[team] += expectedPoints
if (!playerScoring[team][shot.player.full_name]) {
playerScoring[team][shot.player.full_name] = {}
}
if (playerScoring[team][shot.player.full_name].expected) {
playerScoring[team][shot.player.full_name].expected += expectedPoints
} else {
playerScoring[team][shot.player.full_name]['expected'] = expectedPoints
}
if (shot.made) {
actualScore[team] += shotValue
if (!playerScoring[team][shot.player.full_name]) {
playerScoring[team][shot.player.full_name] = {}
}
if (!playerScoring[team][shot.player.full_name].actual) {
playerScoring[team][shot.player.full_name]['actual'] = shotValue
} else {
playerScoring[team][shot.player.full_name].actual += shotValue
}
}
}
}
// console.log({
// expectedScore, player: shot.player.full_name, pstats: playerScoring[shot.player.full_name], shotValue, shotmade: shot.made
// })
}
}
}
}
let contentByTeam = {}
for (let team in expectedScore) {
let writeContent = ''
const oponentScore = allEvents.home.name !== team ? allEvents.home.points : allEvents.away.points
writeContent += `Summary for ${team}\n\n`
writeContent += 'Expected Score: ' + expectedScore[team] + '\n'
writeContent += 'Actual Score: ' + actualScore[team] + '\n'
writeContent += 'Difference: ' + (actualScore[team] - expectedScore[team]) + '\n'
writeContent += 'Opponent Score: ' + (oponentScore) + '\n'
writeContent += 'Game Outcome: ' + (oponentScore > actualScore[team] ? 'Loss' : "Win") + '\n'
writeContent += 'Two Pointers Taken: ' + twoPointersTaken[team] + '\n'
writeContent += 'Three Pointers Taken: ' + threePointersTaken[team] + '\n'
writeContent += 'Free Throws Taken: ' + freeThrowsTaken[team] + '\n'
writeContent += 'Total Shots Taken: ' + totalShotsTaken[team] + '\n'
let playerScoringContent = ''
for (let player in playerScoring[team]) {
playerScoringContent += '\t' + player + ': \n'
playerScoringContent += '\t\tShots Taken: ' + playerStats[team][player].length + '\n'
playerScoringContent += '\t\t' + 'Expected: ' + playerScoring[team][player].expected + '\n'
playerScoringContent += '\t\t' + 'Actual: ' + playerScoring[team][player].actual + '\n'
playerScoringContent += '\t\t' + 'Difference: ' + (playerScoring[team][player].actual - playerScoring[team][player].expected) + '\n'
}
writeContent += 'Player Scoring: \n' + playerScoringContent + '\n\n\n'
contentByTeam[team] = writeContent
}
const totalContent = contentByTeam["Jazz"] + contentByTeam[opponent] || ''
fs.writeFileSync(`./results/${seasonYears}-midseason-dec-shooting-numbers/${date}-${home}-${away}-${shootingFile}.txt`, totalContent)
}
function sortStatsByPlayer(filteredStats) {
// Create an object for each team with player names as the key and an array of events for that player as the value
/*
{
Jazz: {
"Mike Conley" : [
event1,
event2,
etc...
]
}
}
*/
const playerStats = {}
filteredStats.forEach(event => {
if (!playerStats[event.team.name]) {
playerStats[event.team.name] = {}
}
if (!playerStats[event.team.name][event.player.full_name]) {
playerStats[event.team.name][event.player.full_name] = [event]
} else {
playerStats[event.team.name][event.player.full_name].push(event)
}
})
return playerStats
}
main()
module.exports = { main }