-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtbaHandler.php
380 lines (332 loc) · 11.4 KB
/
tbaHandler.php
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
<?php
/*
TBA Handler
The purpose of this object is to create a function that can deal with The Blue Alliance.
This will locally cache data in a MySQL server to limit TBA load and help with outages (IE a local server).
Any data from the TBA API will come out as follows
{
expiryTime : dateTime,
updated : boolean,
data : {}
}
*/
class tbaHandler {
private $charset = "utf8";
private $alreadyConnected = false;
private $tbaApiKey;
private $tbaTableName;
private $dbConnection;
private $apiURL;
function __construct($tbaApiKey, $tbaTableName, $dbConnection) {
$this->tbaApiKey = $tbaApiKey;
$this->tbaTableName = $tbaTableName;
$this->dbConnection = $dbConnection;
$this->apiURL = "https://www.thebluealliance.com/api/v3";
}
function readURIFromTBA($uri){
$url = $this->apiURL.$uri."?X-TBA-Auth-Key=".$this->tbaApiKey;
$ch = curl_init();
curl_setopt_array($ch,
array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_HEADER => true
)
);
$response = curl_exec($ch);
if(curl_errno($ch)){
throw new Exception(curl_error($ch));
}
curl_close($ch);
list($headers, $content) = explode("\r\n\r\n", $response, 2);
// Parse Header to get max-age
$maxAge = 0;
foreach(explode("\r\n", $headers) as $hdr){
$kv = explode(":", $hdr, 2);
if (sizeof($kv) == 2){
list($key, $value) = $kv;
if ($key == "Cache-Control"){
foreach(explode(",", $value) as $cacheControlKV){
$cacheKV = explode("=", $cacheControlKV, 2);
if (count($cacheKV) == 2 and strcmp($cacheKV[0], "max-age")){
$maxAge = intval($cacheKV[1]);
}
}
}
}
}
$content = str_replace("'", "", $content);
$out = array();
$out["expiryTime"] = time() + $maxAge;
$out["response"] = json_decode($content, true);
return $out;
}
function readURIFromDB($uri){
/*
If uri not in db, then return empty
*/
$sql = "SELECT expiryTime, response from ".$this->tbaTableName." where requestURI='".$uri."'";
$prepared_statement = $this->dbConnection->prepare($sql);
$prepared_statement->execute();
$result = $prepared_statement->fetchAll(PDO::FETCH_ASSOC);
//
$out = array("expiryTime" => 0, "response" => null);
if (count($result) != 0){
$out["expiryTime"] = intval($result[0]["expiryTime"]);
$out["response"] = json_decode($result[0]["response"], true);
}
return $out;
}
function writeResponseToDB($uri, $response){
$replaceValues = array();
$replaceValues["requestURI"] = $uri;
$replaceValues["expiryTime"] = $response["expiryTime"];
$replaceValues["response"] = json_encode($response["response"]);
// echo($replaceValues["response"]);
$sql = "REPLACE INTO ".$this->tbaTableName." (requestURI, expiryTime, response) VALUES (:requestURI, :expiryTime, :response)";
$prepared_statement = $this->dbConnection->prepare($sql);
$prepared_statement->execute($replaceValues);
}
function makeDBCachedCall($uri){
$currTime = time();
$dbResponse = $this->readURIFromDB($uri);
// If expiryTime is after current time, cache is valid
if ($currTime < $dbResponse["expiryTime"]){
return $dbResponse;
}
$apiResponse = $this->readURIFromTBA($uri);
$this->writeResponseToDB($uri, $apiResponse);
return $apiResponse;
}
/* Team List Operations */
function getTeamList($eventCode){
$requestURI = "/event/" . $eventCode . "/teams";
return $this->makeDBCachedCall($requestURI);
}
function getSimpleTeamList($eventCode){
$tl = $this->getTeamList($eventCode);
$out = array();
foreach($tl["response"] as $teamRow){
array_push($out, $teamRow["team_number"]);
}
return $out;
}
function teamListToLookup($teamList){
$out = array();
$i = 0;
foreach($teamList as $team){
$out[$team] = $i;
$out["frc".$team] = $i;
$i += 1;
}
return $out;
}
/* Match Data Operations */
function getMatches($eventCode){
$requestURI = "/event/" . $eventCode . "/matches";
return $this->makeDBCachedCall($requestURI);
}
function getSimpleMatches($eventCode){
$ml = $this->getMatches($eventCode);
return $ml["response"];
}
function removeElimMatches($matchData){
$out = array();
foreach($matchData as $matchRow){
if ($matchRow["comp_level"] == "qm"){
array_push($out, $matchRow);
}
}
return $out;
}
function removeUnplayedMatches($matchData){
$out = array();
foreach($matchData as $matchRow){
if ($matchRow["alliances"]["red"]["score"] != -1){
array_push($out, $matchRow);
}
}
return $out;
}
function getNumericalBreakdownKeys($matchData){
$sampleBreakdown = $matchData[0]["score_breakdown"]["red"];
$out = array();
foreach($sampleBreakdown as $key => $value){
if (is_numeric($value)){
array_push($out, $key);
}
}
return $out;
}
/* COPR Calculation */
function choleskyDecomposition($A){
/*
Args:
$A - Must be square matrix that is symetric and positive definite
Returns:
array("L" => and "Lp" =>) decompositions
*/
$n = sizeof($A);
$L = array_fill(0, $n, array_fill(0, $n, 0));
$Lp = array_fill(0, $n, array_fill(0, $n, 0));
for($i = 0; $i < $n; $i++){
for($j = 0; $j <= $i; $j++){
$sum = 0;
for($k = 0; $k < $j; $k++){
$sum += $L[$i][$k] * $L[$j][$k];
}
if ($i == $j){
$L[$i][$j] = sqrt($A[$i][$j] - $sum);
$Lp[$j][$i] = sqrt($A[$i][$j] - $sum);
}
else {
$L[$i][$j] = (1 / $L[$j][$j]) * ($A[$i][$j] - $sum);
$Lp[$j][$i] = (1 / $L[$j][$j]) * ($A[$i][$j] - $sum);
}
}
}
return array("L" => $L , "Lp" => $Lp);
}
function forwardSubstitution($A, $B){
/*
Args:
$A - Lower Triangular Square Matrix
$B - Vector of Length of a side of $A
Returns:
$X - Solved vector
*/
$n = sizeof($A);
$X = array_fill(0, $n, 0);
for($i = 0; $i != $n; $i++){
$sum = 0;
for($j = 0; $j < $i; $j++){
$sum += $A[$i][$j] * $X[$j];
}
$X[$i] = ($B[$i] - $sum)/$A[$i][$i];
}
return $X;
}
function backwardSubstitution($A, $B){
/*
Args:
$A - Upper Triangular Square Matrix
$B - Vector of Length of a side of $A
Returns:
$X - Solved vector
*/
$n = sizeof($A);
$nm = $n - 1;
$X = array_fill(0, $n, 0);
for($i = 0; $i != $n; $i++){
$sum = 0;
for($j = 0; $j < $i; $j++){
$sum += $A[$nm - $i][$nm - $j] * $X[$nm - $j];
}
$X[$nm - $i] = ($B[$nm - $i] - $sum)/$A[$nm - $i][$nm - $i];
}
return $X;
}
function createABMatricies($teamCount, $teamLookup, $simpleMatchData){
$aMatrix = array_fill(0, $teamCount, array_fill(0, $teamCount, 0)); // Just for who plays in what matchData
$bVectors = array(); // Set of data we want to solve for
$coprKeys = $this->getNumericalBreakdownKeys($simpleMatchData);
// Initialize B Vectors
foreach($coprKeys as $key){
$bVectors[$key] = array_fill(0, $teamCount, 0);
}
// Iterate through matches
foreach ($simpleMatchData as &$match){
foreach (array("red", "blue") as $color){
foreach ($match["alliances"][$color]["team_keys"] as $teamA){
// Modify A Matrix
foreach ($match["alliances"][$color]["team_keys"] as $teamB){
$aMatrix[$teamLookup[$teamA]][$teamLookup[$teamB]] += 1;
}
// Modify B Vectors
foreach($coprKeys as $coprKey){
$bVectors[$coprKey][$teamLookup[$teamA]] += $match["score_breakdown"][$color][$coprKey];
}
}
}
}
return array("A" => $aMatrix, "B" => $bVectors);
}
function getComponentOPRS($eventCode){
$simpleTeamList = $this->getSimpleTeamList($eventCode);
$teamLookup = $this->teamListToLookup($simpleTeamList);
$teamCount = sizeof($simpleTeamList);
$simpleMatchData = $this->getSimpleMatches($eventCode);
$simpleMatchData = $this->removeElimMatches($simpleMatchData);
$simpleMatchData = $this->removeUnplayedMatches($simpleMatchData);
$matchMatricies = $this->createABMatricies($teamCount, $teamLookup, $simpleMatchData);
$A = $matchMatricies["A"];
$Bs = $matchMatricies["B"];
$Xs = array();
$Lmat = $this->choleskyDecomposition($A);
$L = $Lmat["L"];
$Lp = $Lmat["Lp"];
foreach ($Bs as $component => $Ba){
$y = $this->forwardSubstitution($L, $Ba);
$x = $this->backwardSubstitution($Lp, $y);
$Xs[$component] = $x;
}
// Repackage Values into Team Value Dicts
$data = array();
foreach ($simpleTeamList as $team){
$data[$team] = array();
foreach ($Xs as $component => $x){
$data[$team][$component] = round($x[$teamLookup[$team]], 2);
}
}
$out = array("eventCode" => $eventCode, "data" => $data, "keys" => $this->getNumericalBreakdownKeys($simpleMatchData));
return $out;
}
function getAllEventCodes($team){
$year = '2022';
$uri = '/team/frc'.$team.'/events/'.$year.'/keys';
$dataCall = $this->makeDBCachedCall($uri)['response'];
return($dataCall);
}
function getLastEventCode($team){
$year = '2022';
$uri = '/team/frc'.$team.'/events/'.$year.'/statuses';
$dataCall = $this->makeDBCachedCall($uri)['response'];
$lastEventCode = null;
foreach($this->getAllEventCodes($team) as $eventCode){
if ($dataCall[$eventCode] != null && $eventCode != '2022week0'){
$lastEventCode = $eventCode;
}
}
return $lastEventCode;
}
function getLastEventOPRS($teamList){
/*
Iterate through the last event for each team and add OPRs, climb OPRs, and cargo OPRs to dict.
*/
// Create last event dict
$lastEventDict = array();
foreach($teamList as $team){
$lastEventDict[$team] = $this->getLastEventCode($team);
}
// Create Output Dict
$out = array();
$coprLocalOutput = array();
foreach($teamList as $team){
$lastEvent = $lastEventDict[$team];
if (!isset($coprLocalOutput[$lastEvent])){
$coprLocalOutput[$lastEvent] = $this->getComponentOPRS($lastEvent);
}
$out[$team] = array();
$out[$team]["event"] = $lastEvent;
try{
$out[$team]["oprs"] = $coprLocalOutput[$lastEvent]['data'][$team];
} catch (Exception $e) {
$out[$team]["oprs"] = null;
}
}
return $out;
}
}
?>