-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpredictEvent.js
More file actions
414 lines (390 loc) · 15.1 KB
/
predictEvent.js
File metadata and controls
414 lines (390 loc) · 15.1 KB
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
const fs = require("fs").promises;
const readline = require("readline");
const axios = require("axios");
const TBA_API_KEY = process.env.TBA_API_KEY;
const BASE_URL = "https://www.thebluealliance.com/api/v3";
const teamMatchStats = [];
const subDataPoints = [
"autoCoralPoints",
"algaePoints",
"endGameBargePoints",
"foulPoints",
"teleopCoralPoints",
//"totalPoints",
];
let fullTeamData;
let trainedNumbers;
let allMatchesInYear;
const oppTeamOppMatchStats = [];
for (let i = 0; i < 20000; i++) {
oppTeamOppMatchStats.push([]);
}
/**
* Asynchronously reads and parses JSON data from a file.
*
* This function attempts to read the contents of a file specified by the filename,
* parse it as JSON, and return the resulting JavaScript object. If an error occurs
* during file reading or JSON parsing, it logs an appropriate error message and
* throws the error.
*
* @async
* @param {string} filename - The name or path of the file to read.
* @returns {Promise<Object>} A promise that resolves with the parsed JSON data as a JavaScript object.
* @throws {Error} If the file is not found or cannot be read, or if the content cannot be parsed as JSON.
*/
async function getDataFromFile(filename) {
try {
const data = await fs.readFile(filename, "utf8");
return JSON.parse(data);
} catch (error) {
if (error.code === "ENOENT") {
console.error(`File not found: ${filename}`);
} else {
console.error(`Error reading file ${filename}: ${error.message}`);
}
throw error;
}
}
/**
* Prompts the user with a question and returns their answer asynchronously.
*
* This function creates a readline interface to ask the user a question via
* the command line. It returns a Promise that resolves with the user's trimmed answer.
*
* @async
* @param {string} question - The question to ask the user.
* @returns {Promise<string>} A promise that resolves with the user's trimmed answer.
*/
async function askUserQuestion(question) {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
return new Promise((resolve) => {
rl.question(question + " ", (answer) => {
rl.close();
resolve(answer.trim());
});
});
}
/**
* Fetches data from The Blue Alliance API for a given endpoint.
*
* This function makes an asynchronous GET request to The Blue Alliance API
* using the provided endpoint. It includes the necessary authentication
* header for API access.
*
* @async
* @param {string} endpoint - The API endpoint to fetch data from, excluding the base URL.
* @returns {Promise<Object>} A promise that resolves with the data returned from the API.
* @throws {Error} If there's an error fetching data from the API, the error is logged and re-thrown.
*/
async function getTBAData(endpoint) {
try {
const response = await axios.get(`${BASE_URL}${endpoint}`, {
headers: {
"X-TBA-Auth-Key": TBA_API_KEY,
},
});
return response.data;
} catch (error) {
console.error(`Error fetching data from TBA: ${error.message}`);
throw error;
}
}
/**
* Fetches and processes match data for a given team and their opponents.
*
* This function retrieves match data for the specified team and their opponents
* from The Blue Alliance API for a given year. It then sorts and filters the data,
* and populates the `teamMatchStats` and `oppTeamMatchStats` arrays with the
* relevant information.
*
* @param {number} teamNumber - The number of the team for which to fetch match data.
* @param {Array<number>} oppTeams - An array of the numbers of the opponent teams.
* @returns {Promise<void>} - A promise that resolves when the data fetching and processing is complete.
* @throws {Error} - If there's an error fetching data from the API, the error is logged and re-thrown.
*/
async function getTeamData(teamNumber, oppTeams, year) {
var teamKey = String(teamNumber);
if (!teamKey.includes("frc")) {
teamKey = "frc" + teamNumber;
}
let fullTeamData = await allMatchesInYear.filter(
(val) =>
val.alliances.blue.team_keys.includes(teamKey) ||
val.alliances.red.team_keys.includes(teamKey)
);
fullTeamData = await fullTeamData.sort(
(a, b) => a.actual_time < b.actual_time
);
const teamData = await fullTeamData.filter(
(val) => val.actual_time < Date.now()
);
for (let i = 0; i < teamData.length; i++) {
let contd = false;
if (!teamData[i].score_breakdown) {
continue;
}
for (let j = 0; j < subDataPoints.length; j++) {
if (
isNaN(teamData[i].score_breakdown.red[subDataPoints[j]]) ||
isNaN(teamData[i].score_breakdown.blue[subDataPoints[j]])
) {
contd = true;
break;
}
}
if (contd) {
continue;
}
if (teamData[i].alliances.red.team_keys.includes(teamKey)) {
teamMatchStats.push({
teamStats: teamData[i].score_breakdown.red,
oppTeams: teamData[i].alliances.blue.team_keys,
comp: teamData[i].key,
time: teamData[i].actual_time,
});
} else {
teamMatchStats.push({
teamStats: teamData[i].score_breakdown.blue,
oppTeams: teamData[i].alliances.red.team_keys,
comp: teamData[i].key,
time: teamData[i].actual_time,
});
}
}
for (let i = 0; i < oppTeams.length; i++) {
let oppTeamData = await getTBAData(`/team/${teamKey}/matches/${year}`);
oppTeamData = await oppTeamData
.sort((a, b) => a.actual_time < b.actual_time)
.filter((val) => val.actual_time < Date.now());
for (let j = 0; j < oppTeamData.length; j++) {
let contd = false;
if (!oppTeamData[j].score_breakdown) {
continue;
}
for (let k = 0; k < subDataPoints.length; k++) {
if (
isNaN(oppTeamData[j].score_breakdown.red[subDataPoints[k]]) ||
isNaN(oppTeamData[j].score_breakdown.blue[subDataPoints[k]])
) {
contd = true;
break;
}
}
if (contd) {
continue;
}
if (oppTeamData[j].alliances.red.team_keys.includes(oppTeams[i])) {
oppTeamOppMatchStats[oppTeams[i]].push({
oppStats: oppTeamData[j].score_breakdown.blue,
comp: oppTeamData[j].key,
time: oppTeamData[j].actual_time,
});
} else {
oppTeamOppMatchStats[oppTeams[i]].push({
oppStats: oppTeamData[j].score_breakdown.red,
comp: oppTeamData[j].key,
time: oppTeamData[j].actual_time,
});
}
}
}
}
async function predictData(dataPoint, teamNumber, oppTeams) {
if (!subDataPoints.includes(dataPoint)) {
let ret = 0;
if (dataPoint.includes("auto")) {
for (let i = 0; i < subDataPoints.length; i++) {
if (subDataPoints[i].includes("auto") && typeof ret == "number") {
ret += await predictData(subDataPoints[i], teamNumber, oppTeams);
}
}
} else if (dataPoint.includes("teleop")) {
for (let i = 0; i < subDataPoints.length; i++) {
if (subDataPoints[i].includes("teleop") && typeof ret == "number") {
ret += await predictData(subDataPoints[i], teamNumber, oppTeams);
}
}
} else if (dataPoint.includes("endgame")) {
for (let i = 0; i < subDataPoints.length; i++) {
if (subDataPoints[i].includes("endgame") && typeof ret == "number") {
ret += await predictData(subDataPoints[i], teamNumber, oppTeams);
}
}
} else {
for (let i = 0; i < subDataPoints.length; i++) {
if (typeof ret == "number" && subDataPoints[i] != "totalPoints") {
ret += await predictData(subDataPoints[i], teamNumber, oppTeams);
}
}
}
console.log(`Predicted ${dataPoint} for team ${teamNumber}: ${ret}`);
return ret;
}
if (teamMatchStats.length == 0) {
console.log(`No matches found for team ${teamNumber}!`);
return `
\n
,@@@@@@@@@@@, \n
@@@@@@@@@@@ @- @@@@ \n
@@@@= @ @@ +@ =@@@ \n
@@@ @@- @@@@* @ @@ . @ \n
@@+ : @@ @@@@%+@:@@@@ @: @@@@@@@@@@ \n
@@@% .:.:. @@ @: :@ @@@ @ \n
@@@- ::::.--: @. .:-. +@@@@.@@ @@@@@ @ \n
@@@+ .::...... ..@ -: =#@@@ @ @@@@@@@ @ \n
@@@#. ::::::.::::.::. -. @@@ @ @@@@@ @. @ \n
@@@. ---:.:....::::..: # @@@@@ @@@@@@ @@ @@@@@ @@@@ @ \n
@@@= .:::......:::.:....- % @ .@@@@@@@ @@ @@@@@@@ @@@@ \n
@@@: :::::....:.::::::: . @ @@ @@@ @@@ @@@@ @@@@+ @@@ @ \n
@=- ::...:..:::::.::::::-. @ . @ @@@@@@@@@@@@@@ @@@ *@@% @ \n
@@= .. ..:..:............:. @@+ @@@ @@ -: @@@@+ :: @ \n
*@- .: = @@. __ : @@@@@@@@@@@@.: .:.:::+ @ = :. .@ \n
@ := @ @@+.#+-*++%%@@@@@=,__ ...:::::::::.: _,@@@ \n
@- :- @ @@ -*@@@@@@@@=,__ _,#@@@- #@ \n
@ :: := @ :--:*@@@@@@@-,_ -@@@@@@@@@@@@@@@@@@@@@= : . -@@ \n
@.: @. @ -::: --#%@@@@@@*.: . @@ \n
@ . @ . @= . . :@@@@@@@@@@@@@#,___ . .__,@@@@@@ \n
@ : @@@@@@@@@@@@@@@@:+,__ . . -==**@@@@++= @ \n
+@ @ :-..: . @@@@@@*+-:,___ ::= .#@@@ \n
@ #. .:.:.:::............::: . :*%@@@@@@@@@@@@@@@@@@@@@@@ \n
@+:@ @# - :..:::::::::::::...:---:::::. -@@@ \n
@::+*=@@ .@ ..:::..::::::::::::::...::.:.. #@@@@@ \n
@:===-:+= @@ @=,_ __.,--:@@@@ \n
@:=======-:.:-:%@@@@@%=:--=#@@@@@@%%@%+ @@@@@ \n
@:============-= .-*#*#%***%@@@@@@@@@@@@@@@*%*--+-#@@@ \n
@:======================-=----:... .:-==========.%@@ \n
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ \n
+ @ \n
`;
//sorry
//i had fun tho
}
let matchesInComp = teamMatchStats.reduce((sum, val) => {
if (val.comp == teamMatchStats[teamMatchStats.length - 1].comp) {
return sum + 1;
}
return sum;
}, 0);
var matchesInCompOpp = 0;
if (matchesInComp === 0) {
console.log(
"No matches found in the current competition!\n ERROR: this really shouldnt happen by the way i wrote the code, probably big bug, line 187"
);
return `womp womp`;
}
let oppTeamsThatHaveData = 3;
let oppTeamsThatHaveDataComp = 3;
var inputs = [
teamMatchStats.reduce((sum, val) => sum + val.teamStats[dataPoint], 0) /
teamMatchStats.length,
teamMatchStats.reduce((sum, val) => {
if (val.comp === teamMatchStats[teamMatchStats.length - 1].comp) {
return sum + val.teamStats[dataPoint];
}
return sum;
}, 0) / matchesInComp,
teamMatchStats
.slice(-3)
.reduce(
(last, val) =>
last > val.teamStats[dataPoint] ? last : val.teamStats[dataPoint],
0
),
teamMatchStats[teamMatchStats.length - 1].teamStats[dataPoint],
(oppTeams.reduce((sum, val) => {
if (!oppTeamOppMatchStats[val] || val == 0) {
oppTeamsThatHaveData--;
if (oppTeamsThatHaveData === 0) {
oppTeamsThatHaveData = 9;
return (
teamMatchStats.reduce(
(sum, val) => sum + val.teamStats[dataPoint],
0
) / teamMatchStats.length
);
}
return sum;
}
return (
sum +
oppTeamOppMatchStats[+val].reduce(
(sum, val) => sum + val.oppStats[dataPoint],
0
) /
oppTeamOppMatchStats[+val].length
);
}, 0) *
oppTeamsThatHaveData) /
3,
(oppTeams.reduce((sum, val) => {
oppTeamsThatHaveDataComp = oppTeamsThatHaveData;
if (oppTeamsThatHaveData === 9) {
return (
teamMatchStats.reduce((sum, val) => {
if (val.comp === teamMatchStats[teamMatchStats.length - 1].comp) {
return sum + val.teamStats[dataPoint];
}
return sum;
}, 0) / matchesInComp
);
}
const ret =
oppTeamOppMatchStats[+val] && val != 0
? oppTeamOppMatchStats[+val].reduce((sum, val) => {
if (val.comp === teamMatchStats[teamMatchStats.length - 1].comp) {
matchesInCompOpp++;
return sum + val.oppStats[dataPoint];
}
return sum;
}, 0)
: 0;
if (!oppTeamOppMatchStats[+val] || val == 0 || matchesInCompOpp == 0) {
oppTeamsThatHaveDataComp--;
return sum;
}
return sum + ret / matchesInCompOpp;
}, 0) *
oppTeamsThatHaveDataComp) /
3,
];
function prediction(dataPoint) {
let ret = 0;
for (let i = 0; i < inputs.length; i++) {
ret += trainedNumbers[dataPoint].a[i] * inputs[i] ** 3;
ret += trainedNumbers[dataPoint].b[i] * inputs[i] ** 2;
ret += trainedNumbers[dataPoint].c[i] * inputs[i];
}
ret += trainedNumbers[dataPoint].d;
return ret;
}
console.log(
`Predicted ${dataPoint} for team ${teamNumber}: ${prediction(
dataPoint
).toFixed(5)}`
);
return prediction(dataPoint);
}
async function main() {
const teamNumber = await askUserQuestion("Enter your team number:");
const oppTeams = [
await askUserQuestion("Enter opposing team 1 (enter to skip):"),
await askUserQuestion("Enter opposing team 2:"),
await askUserQuestion("Enter opposing team 3:"),
];
for (let i = 0; i < oppTeams.length; i++) {
oppTeams[i] = +oppTeams[i];
if (!Number.isInteger(oppTeams[i]) || oppTeams[i] < 1) {
oppTeams[i] = 0;
}
}
const year = await askUserQuestion("Enter the year to predict for:");
const dataPoint = await askUserQuestion("Enter the data point to predict:");
allMatchesInYear = await getDataFromFile(`matches${year}.json`);
trainedNumbers = await getDataFromFile(`trainedNumbers${year}.json`);
const teamData = await getTeamData(teamNumber, oppTeams, year);
await predictData(dataPoint, teamNumber, oppTeams, teamData);
}
main();