-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTournament.php
More file actions
351 lines (306 loc) · 12.2 KB
/
Tournament.php
File metadata and controls
351 lines (306 loc) · 12.2 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
<?php
require_once 'TournamentRip.php';
require_once 'PlayersRip.php';
require_once 'NameRip.php';
require_once 'arrayExtract.php';
require_once 'Player.php';
require_once 'Match.php';
class Tournament
{
// Tournament Specific Data;
private $completed_at;
private $id;
private $name;
private $participants_count;
private $progress_meter;
private $started_at;
private $state;
private $tournament_type;
private $url;
private $host;
private $full_challonge_url;
// Touanment Aggregated Data
private $matches; //Numbered array of match objects;
private $participants; //Numbered array of player objects;
// constructor declaration
function __construct($url){
//Verify that it is a propery Challonge URL
$url = $this->validateUrl($url);
if ($url == false)
throw new InvalidArgumentException("URL is not a valid Challonge URL!");
$this->saveUrlData($url);
$tRip = getTournamentData($url);
$pRip = getPlayersData($url."/standings");
//Save Tournament Data
//This also makes sure that the tournament is of correct format
$this->name = getName($url);
$this->populateTouranmentData($tRip);
//Create Player List
$this->participants = $this->createParticipantList($pRip);
//Create Matches
$this->matches = $this->createMatchList($tRip);
$this->aggregatePlayerData();
$this->calculateTimes();
}
/**
* Checks to make sure that the URL is valid from Challonge
* Precondition: $url is validated
* @param string $url a validated url
* @return boolean|mixed the filtered url if valid, false otherwise
*/
private function validateUrl(string $url){
//Check if input string is a valid Challonge URL
//If so, return the last part of it.
$url = filter_var($url, FILTER_SANITIZE_URL);
if($url == false)
return false;
$components = parse_url($url);
if($components == false)
return false;
//Make sure all necessary parts are there
//print_r($components);
if (!(array_key_exists("host", $components) &&
array_key_exists("path", $components) &&
array_key_exists("scheme", $components)))
return false;
$host = $components["host"];
$path = $components["path"];
$scheme = $components["scheme"];
$pattern = "/challonge\.com$/i";
if (filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED) &&
preg_match($pattern, $host) === 1 &&
$scheme == "http" &&
substr_count($path, '/') == 1){
return $url;
}
else
return false;
}
/**
* Saves the data from the url
* @param string $url
*/
private function saveUrlData(string $url)
{
$components = parse_url($url);
$host = $components["host"];
$path = $components["path"];
$scheme = $components["scheme"];
//Full URl
$this->full_challonge_url = $url;
//Extract Host
$pattern = "/([a-zA-Z]+)\.challonge\.com$/i";
$result = preg_match($pattern, $host, $matches);
if (sizeof($matches) > 0)
$this->host = $matches[1];
else
$this->host = NULL;
//Extract path
$this->url = substr($path, 1);
}
/**
* Updataes the Tournament-Object Specific Data using a Tournament Rip
* @param array $tournamentRip
* @throws InvalidDataException if tournament is not a complete double elimination bracket
*/
private function populateTouranmentData(array $tournamentRip){
//Validate Tournament
$tournamentType = $tournamentRip["requested_plotter"];
if ($tournamentType != "DoubleEliminationBracketPlotter")
throw new InvalidDataException("tournamentType must be a DoubleEliminationBracketPlotter!");
$tournamentData = $tournamentRip["tournament"];
//Extract Data
$this->id = arrayExtract($tournamentData, "id", "int");
$this->progress_meter = arrayExtract($tournamentData, "progress_meter", "int");
$this->state = arrayExtract($tournamentData, "state", "string");
$this->tournament_type = arrayExtract($tournamentData, "tournament_type", "string");
//Validate other variables
if ($this->state != "complete")
throw new InvalidDataException("The tournament is not finished yet!");
}
/**
* Creates a participants list using data from the playerrip
* Player id is not set
* @param array $playerRip
* @throws InvalidDataException if the playerRip contains non-array members
*/
public function createParticipantList(array $playerRip)
{
$participants = array();
foreach($playerRip as $p)
{
//Validate the list
if(gettype($p) != "array")
throw new InvalidDataException("playerRip should only contain arrays: ".gettype($p)." found");
array_push($participants, new Player($p));
}
return $participants;
}
/**
* Creates a match list using data from the tournamentRip
* @param array $tournamentRip
* @throws InvalidDataException if the tournamentRip does not have propery array inclusion
* @return array
*/
public function createMatchList(array $tournamentRip){
$matchList = arrayExtract($tournamentRip, "matches_by_round", "array");
$roundList = arrayExtract($tournamentRip, "rounds", "array");
$matches = array();
$matchNames = array();
//Populate matchnames
foreach ($roundList as $round)
{
//Validate the list
if(gettype($round) != "array")
throw new InvalidDataException("roundList should only contain arrays: ".gettype($round)." found");
$matchNames[arrayExtract($round, "number", "int")] = arrayExtract($round, "title", "string");
}
//Populate matches
foreach($matchList as $matchesByRound){
//Validate the list
if(gettype($matchesByRound) != "array")
throw new InvalidDataException("matchList should only contain arrays: ".gettype($matchesByRound)." found");
foreach($matchesByRound as $match)
{
//Validate the list
if(gettype($match) != "array")
throw new InvalidDataException("matchList should only contain arrays: ".gettype($match)." found");
$m = new Match($match);
$m->setTitle($matchNames[$m->index("round")]);
array_push($matches, $m);
}
}
return $matches;
}
/**
* Finishes the data of the participant list using the matches list
* Precondition: matches and participants are successfully created
*
*/
public function aggregatePlayerData(){
foreach($this->participants as $participant)
{
$participant->setTournamentId($this->id);
$success = $this->completePlayerByMatches($participant);
/*if($success == false)
echo "NOOOOOOO";*/
}
$this->participants_count = sizeof($this->participants);
}
/**
* Uses the data from matches to get the id and seed of the player
* Precondition: matches is sucessfully created
* @param Player $participant
* @return boolean true if successful, false if the data is not found
*/
public function completePlayerByMatches(Player &$participant){
$name = $participant->index("name");
foreach ($this->matches as $match)
{
$player1 = $match->index("player1");
$player2 = $match->index("player2");
$player1_name = arrayExtract($player1, "display_name", "string");
$player2_name = arrayExtract($player2, "display_name", "string");
if ($player1_name == $name)
{
$participant->setId(arrayExtract($player1, "id", "int"));
$participant->setSeed(arrayExtract($player1, "seed", "int"));
return true;
}
elseif ($player2_name == $name)
{
$participant->setId(arrayExtract($player2, "id", "int"));
$participant->setSeed(arrayExtract($player2, "seed", "int"));
return true;
}
}
return false;
}
/**
* Uses the underway_at variable of matches to determine start and end times
* Precondition: Matches have been populated
*/
private function calculateTimes(){
//Get first round
$firstIdentifier = 1;
$lastIdentifier = sizeof($this->matches);
foreach ($this->matches as $match)
{
$identifier = $match->index("identifier");
if ($identifier == $firstIdentifier)
$this->started_at = $match->index("underway_at");
if ($identifier == $lastIdentifier)
$this->completed_at = $match->index("underway_at");
}
}
/**
* Returns the value of a specific index, much like JSON
* @param string $indexString The value which is indexed
* @throws OutOfBoundsException if index is invalid
*/
public function index($indexString) {
switch ($indexString) {
case "completed_at":
return $this->completed_at;
break;
case "id":
return $this->id;
break;
case "name":
return $this->name;
break;
case "participants_count":
return $this->participants_count;
break;
case "progress_meter":
return $this->progress_meter;
break;
case "started_at":
return $this->started_at;
break;
case "state":
return $this->state;
break;
case "tournament_type":
return $this->tournament_type;
break;
case "host":
return $this->host;
break;
case "url":
return $this->url;
break;
case "full_challonge_url":
return $this->full_challonge_url;
break;
default:
throw new OutOfBoundsException($indexString." is not a valid index of Tournament!");
break;
}
}
public function toArray()
{
$matchArray = array();
$participantArray = array();
foreach ($this->matches as $match)
array_push($matchArray, $match->toArray());
foreach ($this->participants as $participant)
array_push($participantArray, $participant->toArray());
return array(
"completed_at" => $this->completed_at,
"id" => $this->id,
"name" => $this->name,
"participants_count" => $this->participants_count,
"progress_meter" => $this->progress_meter,
"started_at" => $this->started_at,
"state" => $this->state,
"tournament_type" => $this->tournament_type,
"url" => $this->url,
"host" => $this->host,
"full_challonge_url" => $this->full_challonge_url,
"matches" => $matchArray,
"participants" => $participantArray
);
}
}
?>