· 2 years ago · Apr 23, 2023, 07:09 AM
1<?php
2require_once "{$_SERVER['DOCUMENT_ROOT']}/Models/Environment.php";
3require_once "{$_SERVER['DOCUMENT_ROOT']}/Models/PDO.php";
4/**
5 * The API which interacts with PUBG API to take the data needed from PUBG Data Center as well as the data model which will be used for data analysis.
6 */
7class PlayerUnknownBattleGrounds
8{
9 /**
10 * The primary key of the player as well as the identifier of the player
11 */
12 private ?string $identifier;
13 /**
14 * The username of the player
15 */
16 private string $playerName;
17 /**
18 * The platform of the player
19 */
20 private string $platform;
21 /**
22 * PUBG API Key
23 */
24 private string $apiKey;
25 /**
26 * PDO which will interact with the database server
27 */
28 protected PHPDataObject $PDO;
29 /**
30 * Client for Uniform Resource Locators
31 */
32 private CurlHandle $Curl;
33 /**
34 * Hypertext Markup Language document parser
35 */
36 private DOMDocument $DOM;
37 /**
38 * PHP DOM selector
39 */
40 private DOMXPath $DOMXPath;
41 /**
42 * Upon instantiation, its dependencies are also instantiated
43 */
44 public function __construct()
45 {
46 $this->setApiKey(Environment::PubgAPIKey);
47 $this->PDO = new PHPDataObject();
48 $this->DOM = new DOMDocument();
49 }
50 public function getPlayerName(): string
51 {
52 return $this->playerName;
53 }
54 public function setPlayerName(string $player_name): void
55 {
56 $this->playerName = $player_name;
57 }
58 public function getPlatform(): string
59 {
60 return $this->platform;
61 }
62 public function setPlatform(string $platform): void
63 {
64 $this->platform = $platform;
65 }
66 public function getIdentifier(): ?string
67 {
68 return $this->identifier;
69 }
70 public function setIdentifier(?string $identifier): void
71 {
72 $this->identifier = $identifier;
73 }
74 public function getApiKey(): string
75 {
76 return $this->apiKey;
77 }
78 public function setApiKey(string $api_key): void
79 {
80 $this->apiKey = $api_key;
81 }
82 /**
83 * Retrieving accounts's data
84 * @param string $player_name Name of the player
85 * @param string $platform Platform at which the player plays the game
86 * @return object
87 */
88 public function getAccount(string $player_name, string $platform): object
89 {
90 $this->setPlayerName($player_name);
91 $this->setPlatform($platform);
92 $request = "https://api.pubg.com/shards/{$this->getPlatform()}/players?filter[playerNames]={$this->getPlayerName()}";
93 $this->Curl = curl_init();
94 curl_setopt_array(
95 $this->Curl,
96 array(
97 CURLOPT_URL => $request,
98 CURLOPT_RETURNTRANSFER => true,
99 CURLOPT_ENCODING => '',
100 CURLOPT_MAXREDIRS => 10,
101 CURLOPT_TIMEOUT => 0,
102 CURLOPT_FOLLOWLOCATION => true,
103 CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
104 CURLOPT_CUSTOMREQUEST => 'GET',
105 CURLOPT_HTTPHEADER => array(
106 'Accept: application/vnd.api+json',
107 "Authorization: Bearer {$this->getApiKey()}"
108 )
109 )
110 );
111 $pubgAccountApiResponse = json_decode(curl_exec($this->Curl));
112 $pubgAccountApiResponseCode = curl_getinfo($this->Curl, CURLINFO_HTTP_CODE);
113 curl_close($this->Curl);
114 if ($pubgAccountApiResponseCode == 200) {
115 $this->setIdentifier($pubgAccountApiResponse->data[0]->id);
116 $response = (object) array(
117 "account" => $pubgAccountApiResponseCode,
118 "identifier" => $this->getIdentifier(),
119 "playerName" => $this->getPlayerName(),
120 "platform" => $this->getPlatform()
121 );
122 } else {
123 $response = (object) array(
124 "account" => $pubgAccountApiResponseCode
125 );
126 }
127 return $response;
128 }
129 /**
130 * Adding Player Unknown Battle Grounds account in the database
131 * @param string $player_name Name of the player
132 * @param string $platform Platform which the player uses to play the game
133 * @return int
134 */
135 public function addAccount(string $player_name, string $platform): int
136 {
137 $Account = $this->getAccount($player_name, $platform);
138 if ($Account->account == 200) {
139 $this->PDO->query("SELECT * FROM PlayerUnknownBattleGrounds WHERE PlayerUnknownBattleGroundsIdentifier = :PlayerUnknownBattleGroundsIdentifier");
140 $this->PDO->bind(":PlayerUnknownBattleGroundsIdentifier", $this->getIdentifier());
141 $this->PDO->execute();
142 if (empty($this->PDO->resultSet())) {
143 $this->PDO->query("INSERT INTO PlayerUnknownBattleGrounds (PlayerUnknownBattleGroundsIdentifier, PlayerUnknownBattleGroundsPlayerName, PlayerUnknownBattleGroundsPlatform) VALUES (:PlayerUnknownBattleGroundsIdentifier, :PlayerUnknownBattleGroundsPlayerName, :PlayerUnknownBattleGroundsPlatform)");
144 $this->PDO->bind(":PlayerUnknownBattleGroundsIdentifier", $this->getIdentifier());
145 $this->PDO->bind(":PlayerUnknownBattleGroundsPlayerName", $this->getPlayerName());
146 $this->PDO->bind(":PlayerUnknownBattleGroundsPlatform", $this->getPlatform());
147 $this->PDO->execute();
148 $playerUnknownBattleGrounds = array(
149 "identifier" => $this->getIdentifier(),
150 "playerName" => $this->getPlayerName(),
151 "platform" => $this->getPlatform()
152 );
153 $_SESSION['PlayerUnknownBattleGrounds'] = $playerUnknownBattleGrounds;
154 }
155 $this->PDO->query("SELECT * FROM PlayerUnknownBattleGrounds");
156 $this->PDO->execute();
157 return 0;
158 } else {
159 return 1;
160 }
161 }
162 /**
163 * Accessing player data
164 * @param string $player_name Name of the player
165 * @param string $platform Platform that the player uses to play the game
166 * @return void
167 */
168 public function getPlayer(string $player_name, string $platform): void
169 {
170 $Account = $this->getAccount($player_name, $platform);
171 if ($Account->account == 200) {
172 $request = "https://api.pubg.com/shards/{$this->getPlatform()}/players/{$this->getIdentifier()}/seasons/lifetime";
173 $this->Curl = curl_init();
174 curl_setopt_array(
175 $this->Curl,
176 array(
177 CURLOPT_URL => $request,
178 CURLOPT_RETURNTRANSFER => true,
179 CURLOPT_ENCODING => '',
180 CURLOPT_MAXREDIRS => 10,
181 CURLOPT_TIMEOUT => 0,
182 CURLOPT_FOLLOWLOCATION => true,
183 CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
184 CURLOPT_CUSTOMREQUEST => 'GET',
185 CURLOPT_HTTPHEADER => array(
186 'Accept: application/vnd.api+json',
187 "Authorization: Bearer {$this->getApiKey()}"
188 )
189 )
190 );
191 $pubgLifetimeStatsApiResponse = json_decode(curl_exec($this->Curl));
192 $pubgLifetimeStatsApiResponseCode = curl_getinfo($this->Curl, CURLINFO_HTTP_CODE);
193 curl_close($this->Curl);
194 if ($pubgLifetimeStatsApiResponseCode == 200) {
195 $kda = 0.0;
196 $headshot = 0.0;
197 $damagePerMatch = 0.0;
198 $kdas = array(
199 (($pubgLifetimeStatsApiResponse->data->attributes->gameModeStats->solo->kills + $pubgLifetimeStatsApiResponse->data->attributes->gameModeStats->solo->assists) / $pubgLifetimeStatsApiResponse->data->attributes->gameModeStats->solo->losses),
200 (($pubgLifetimeStatsApiResponse->data->attributes->gameModeStats->duo->kills + $pubgLifetimeStatsApiResponse->data->attributes->gameModeStats->duo->assists) / $pubgLifetimeStatsApiResponse->data->attributes->gameModeStats->duo->losses),
201 (($pubgLifetimeStatsApiResponse->data->attributes->gameModeStats->squad->kills + $pubgLifetimeStatsApiResponse->data->attributes->gameModeStats->squad->assists) / $pubgLifetimeStatsApiResponse->data->attributes->gameModeStats->squad->losses)
202 );
203 $killStreaks = array(
204 $pubgLifetimeStatsApiResponse->data->attributes->gameModeStats->solo->maxKillStreaks,
205 $pubgLifetimeStatsApiResponse->data->attributes->gameModeStats->duo->maxKillStreaks,
206 $pubgLifetimeStatsApiResponse->data->attributes->gameModeStats->squad->maxKillStreaks
207 );
208 $longestKills = array(
209 $pubgLifetimeStatsApiResponse->data->attributes->gameModeStats->solo->longestKill,
210 $pubgLifetimeStatsApiResponse->data->attributes->gameModeStats->duo->longestKill,
211 $pubgLifetimeStatsApiResponse->data->attributes->gameModeStats->squad->longestKill
212 );
213 $headshots = array(
214 (($pubgLifetimeStatsApiResponse->data->attributes->gameModeStats->solo->headshotKills / $pubgLifetimeStatsApiResponse->data->attributes->gameModeStats->solo->kills) * 100),
215 (($pubgLifetimeStatsApiResponse->data->attributes->gameModeStats->duo->headshotKills / $pubgLifetimeStatsApiResponse->data->attributes->gameModeStats->duo->kills) * 100),
216 (($pubgLifetimeStatsApiResponse->data->attributes->gameModeStats->squad->headshotKills / $pubgLifetimeStatsApiResponse->data->attributes->gameModeStats->squad->kills) * 100)
217 );
218 $damagePerMatchs = array(
219 ($pubgLifetimeStatsApiResponse->data->attributes->gameModeStats->solo->damageDealt / ($pubgLifetimeStatsApiResponse->data->attributes->gameModeStats->solo->wins + $pubgLifetimeStatsApiResponse->data->attributes->gameModeStats->solo->losses)),
220 ($pubgLifetimeStatsApiResponse->data->attributes->gameModeStats->duo->damageDealt / ($pubgLifetimeStatsApiResponse->data->attributes->gameModeStats->duo->wins + $pubgLifetimeStatsApiResponse->data->attributes->gameModeStats->duo->losses)),
221 ($pubgLifetimeStatsApiResponse->data->attributes->gameModeStats->squad->damageDealt / ($pubgLifetimeStatsApiResponse->data->attributes->gameModeStats->squad->wins + $pubgLifetimeStatsApiResponse->data->attributes->gameModeStats->squad->losses))
222 );
223 $lengths = array(
224 count($kdas),
225 count($headshots),
226 count($damagePerMatchs)
227 );
228 for ($index = 0; $index < max($lengths); $index++) {
229 $kda += $kdas[$index];
230 $headshot += $headshots[$index];
231 $damagePerMatch += $damagePerMatchs[$index];
232 }
233 $duo = (object) array(
234 "winrate" => round((($pubgLifetimeStatsApiResponse->data->attributes->gameModeStats->duo->wins / ($pubgLifetimeStatsApiResponse->data->attributes->gameModeStats->duo->wins + $pubgLifetimeStatsApiResponse->data->attributes->gameModeStats->duo->losses + $pubgLifetimeStatsApiResponse->data->attributes->gameModeStats->duo->top10s)) * 100), 2),
235 "top10Probability" => round((($pubgLifetimeStatsApiResponse->data->attributes->gameModeStats->duo->top10s / ($pubgLifetimeStatsApiResponse->data->attributes->gameModeStats->duo->wins + $pubgLifetimeStatsApiResponse->data->attributes->gameModeStats->duo->losses + $pubgLifetimeStatsApiResponse->data->attributes->gameModeStats->duo->top10s)) * 100), 2)
236 );
237 $solo = (object) array(
238 "winrate" => round((($pubgLifetimeStatsApiResponse->data->attributes->gameModeStats->solo->wins / ($pubgLifetimeStatsApiResponse->data->attributes->gameModeStats->solo->wins + $pubgLifetimeStatsApiResponse->data->attributes->gameModeStats->solo->losses + $pubgLifetimeStatsApiResponse->data->attributes->gameModeStats->solo->top10s)) * 100), 2),
239 "top10Probability" => round((($pubgLifetimeStatsApiResponse->data->attributes->gameModeStats->solo->top10s / ($pubgLifetimeStatsApiResponse->data->attributes->gameModeStats->solo->wins + $pubgLifetimeStatsApiResponse->data->attributes->gameModeStats->solo->losses + $pubgLifetimeStatsApiResponse->data->attributes->gameModeStats->solo->top10s)) * 100), 2)
240 );
241 $squad = (object) array(
242 "winrate" => round((($pubgLifetimeStatsApiResponse->data->attributes->gameModeStats->squad->wins / ($pubgLifetimeStatsApiResponse->data->attributes->gameModeStats->squad->wins + $pubgLifetimeStatsApiResponse->data->attributes->gameModeStats->squad->losses + $pubgLifetimeStatsApiResponse->data->attributes->gameModeStats->squad->top10s)) * 100), 2),
243 "top10Probability" => round((($pubgLifetimeStatsApiResponse->data->attributes->gameModeStats->squad->top10s / ($pubgLifetimeStatsApiResponse->data->attributes->gameModeStats->squad->wins + $pubgLifetimeStatsApiResponse->data->attributes->gameModeStats->squad->losses + $pubgLifetimeStatsApiResponse->data->attributes->gameModeStats->squad->top10s)) * 100), 2)
244 );
245 $response = array(
246 "account" => $Account->account,
247 "lifetime" => $pubgLifetimeStatsApiResponseCode,
248 "requestedDate" => date("Y/m/d H:i:s"),
249 "renewOn" => date("Y/m/d H:i:s", strtotime("+1 hours")),
250 "kda" => round(($kda / max($lengths)), 2),
251 "killStreak" => max($killStreaks),
252 "longestKill" => round(max($longestKills), 2),
253 "headshot" => round(($headshot / max($lengths)), 2),
254 "damagePerMatch" => round(($damagePerMatch / max($lengths)), 2),
255 "duo" => $duo,
256 "solo" => $solo,
257 "squad" => $squad
258 );
259 $cacheData = json_encode($response);
260 $cache = fopen("{$_SERVER['DOCUMENT_ROOT']}/Cache/PUBG/Users/Profiles/{$this->getIdentifier()}.json", "w");
261 fwrite($cache, $cacheData);
262 fclose($cache);
263 } else {
264 $response = array(
265 "account" => $Account->account,
266 "lifetime" => $pubgLifetimeStatsApiResponseCode
267 );
268 }
269 } else {
270 $response = array(
271 "account" => $Account->account
272 );
273 }
274 header('Content-Type: application/json', true, 200);
275 echo json_encode($response);
276 }
277 /**
278 * Retrieving the patch notes of the game
279 * @return void
280 */
281 public function getPatchNotes(): void
282 {
283 $request = "https://na.battlegrounds.pubg.com/patch-notes/";
284 $patches = array();
285 $this->Curl = curl_init();
286 curl_setopt_array(
287 $this->Curl,
288 array(
289 CURLOPT_URL => $request,
290 CURLOPT_RETURNTRANSFER => true,
291 CURLOPT_ENCODING => '',
292 CURLOPT_MAXREDIRS => 10,
293 CURLOPT_TIMEOUT => 0,
294 CURLOPT_FOLLOWLOCATION => true,
295 CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
296 CURLOPT_CUSTOMREQUEST => 'GET'
297 )
298 );
299 $page = curl_exec($this->Curl);
300 $pageResponseCode = curl_getinfo($this->Curl, CURLINFO_HTTP_CODE);
301 curl_close($this->Curl);
302 if ($pageResponseCode == 200) {
303 $this->DOM->loadHTML($page);
304 $this->DOMXPath = new DOMXPath($this->DOM);
305 $patchDetails1 = $this->DOMXPath->query("//section[@id='news-list']//div[@class='top-section']//div[@class='news-list-first-column']//div[@class='content']//a[@class='title']//h2");
306 $patchDetails2 = $this->DOMXPath->query("//section[@id='news-list']//div[@class='top-section']//div[@class='news-list-second-column']//div[@class='content']//a[@class='title']//h2");
307 foreach ($patchDetails1 as $node) {
308 array_push($patches, $node->nodeValue);
309 }
310 foreach ($patchDetails2 as $node) {
311 array_push($patches, $node->nodeValue);
312 }
313 for ($index = 0; $index < count($patches); $index++) {
314 $version = "";
315 if (str_contains($patches[$index], "Patch Notes - Update ")) {
316 $version = str_replace("Patch Notes - Update ", "", $patches[$index]);
317 } else if (str_contains($patches[$index], "Patch Notes â Update ")) {
318 $version = str_replace("Patch Notes â Update ", "", $patches[$index]);
319 }
320 $patches[$index] = $version;
321 }
322 rsort($patches);
323 $latestVersion = $patches[0];
324 $latestVersionArray = explode(".", $latestVersion);
325 $response = (object) array(
326 "patchNotes" => $pageResponseCode,
327 "requestedDate" => date("Y/m/d"),
328 "renewOn" => date("Y/m/d", strtotime("+2 weeks")),
329 "major" => (int)$latestVersionArray[0],
330 "minor" => (int)$latestVersionArray[1]
331 );
332 $cacheData = json_encode($response);
333 $cache = fopen("{$_SERVER['DOCUMENT_ROOT']}/Cache/PUBG/Platform/Version.json", "w");
334 fwrite($cache, $cacheData);
335 fclose($cache);
336 } else {
337 $response = (object) array(
338 "patchNotes" => $pageResponseCode
339 );
340 }
341 header('Content-Type: application/json', true, 200);
342 echo json_encode($response);
343 }
344 /**
345 * Retrieving player's current season stats
346 * @param string $player_name Name of the player
347 * @param string $platform Platform that the player uses to play the game
348 * @return void
349 */
350 public function getSeason(string $player_name, string $platform): void
351 {
352 $Account = $this->getAccount($player_name, $platform);
353 if ($Account->account == 200) {
354 $request = "https://api.pubg.com/shards/{$this->getPlatform()}/seasons";
355 $this->Curl = curl_init();
356 curl_setopt_array(
357 $this->Curl,
358 array(
359 CURLOPT_URL => $request,
360 CURLOPT_RETURNTRANSFER => true,
361 CURLOPT_ENCODING => '',
362 CURLOPT_MAXREDIRS => 10,
363 CURLOPT_TIMEOUT => 0,
364 CURLOPT_FOLLOWLOCATION => true,
365 CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
366 CURLOPT_CUSTOMREQUEST => 'GET',
367 CURLOPT_HTTPHEADER => array(
368 'Accept: application/vnd.api+json',
369 "Authorization: Bearer {$this->getApiKey()}"
370 )
371 )
372 );
373 $pubgSeasonsApiResponse1 = json_decode(curl_exec($this->Curl));
374 $pubgSeasonsApiResponseCode1 = curl_getinfo($this->Curl, CURLINFO_HTTP_CODE);
375 if ($pubgSeasonsApiResponseCode1 == 200) {
376 $currentSeason = "";
377 for ($index = 0; $index < count($pubgSeasonsApiResponse1->data); $index++) {
378 if ($pubgSeasonsApiResponse1->data[$index]->attributes->isCurrentSeason == true) {
379 $currentSeason = $pubgSeasonsApiResponse1->data[$index]->id;
380 }
381 }
382 $request = "https://api.pubg.com/shards/{$this->getPlatform()}/players/{$this->getIdentifier()}/seasons/{$currentSeason}/ranked";
383 $this->Curl = curl_init();
384 curl_setopt_array(
385 $this->Curl,
386 array(
387 CURLOPT_URL => $request,
388 CURLOPT_RETURNTRANSFER => true,
389 CURLOPT_ENCODING => '',
390 CURLOPT_MAXREDIRS => 10,
391 CURLOPT_TIMEOUT => 0,
392 CURLOPT_FOLLOWLOCATION => true,
393 CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
394 CURLOPT_CUSTOMREQUEST => 'GET',
395 CURLOPT_HTTPHEADER => array(
396 'Accept: application/vnd.api+json',
397 "Authorization: Bearer {$this->getApiKey()}"
398 )
399 )
400 );
401 $pubgSeasonsApiResponse2 = json_decode(curl_exec($this->Curl));
402 $pubgSeasonsApiResponseCode2 = curl_getinfo($this->Curl, CURLINFO_HTTP_CODE);
403 if ($pubgSeasonsApiResponseCode2 == 200) {
404 if (!empty($pubgSeasonsApiResponse2->data->attributes->rankedGameModeStats)) {
405 $rankedModes = array_keys((array) $pubgSeasonsApiResponse2->data->attributes->rankedGameModeStats);
406 if (count($rankedModes) == 1) {
407 switch ($rankedModes[0]) {
408 case 'solo':
409 $Solo = (object) array(
410 "tier" => $pubgSeasonsApiResponse2->data->attributes->rankedGameModeStats->solo->currentTier->tier,
411 "division" => $pubgSeasonsApiResponse2->data->attributes->rankedGameModeStats->solo->currentTier->subTier,
412 "rankPoint" => $pubgSeasonsApiResponse2->data->attributes->rankedGameModeStats->solo->currentRankPoint,
413 "winRate" => round(($pubgSeasonsApiResponse2->data->attributes->rankedGameModeStats->solo->winRatio * 100), 2),
414 "top10Rate" => round(($pubgSeasonsApiResponse2->data->attributes->rankedGameModeStats->solo->top10Ratio * 100), 2)
415 );
416 break;
417 case 'duo':
418 $Duo = (object) array(
419 "tier" => $pubgSeasonsApiResponse2->data->attributes->rankedGameModeStats->duo->currentTier->tier,
420 "division" => $pubgSeasonsApiResponse2->data->attributes->rankedGameModeStats->duo->currentTier->subTier,
421 "rankPoint" => $pubgSeasonsApiResponse2->data->attributes->rankedGameModeStats->duo->currentRankPoint,
422 "winRate" => round(($pubgSeasonsApiResponse2->data->attributes->rankedGameModeStats->duo->winRatio * 100), 2),
423 "top10Rate" => round(($pubgSeasonsApiResponse2->data->attributes->rankedGameModeStats->duo->top10Ratio * 100), 2)
424 );
425 break;
426 case 'squad':
427 $Squad = (object) array(
428 "tier" => $pubgSeasonsApiResponse2->data->attributes->rankedGameModeStats->squad->currentTier->tier,
429 "division" => $pubgSeasonsApiResponse2->data->attributes->rankedGameModeStats->squad->currentTier->subTier,
430 "rankPoint" => $pubgSeasonsApiResponse2->data->attributes->rankedGameModeStats->squad->currentRankPoint,
431 "winRate" => round(($pubgSeasonsApiResponse2->data->attributes->rankedGameModeStats->squad->winRatio * 100), 2),
432 "top10Rate" => round(($pubgSeasonsApiResponse2->data->attributes->rankedGameModeStats->squad->top10Ratio * 100), 2)
433 );
434 break;
435 }
436 } else {
437 for ($index = 0; $index < count($rankedModes); $index++) {
438 switch ($rankedModes[$index]) {
439 case 'solo':
440 $Solo = (object) array(
441 "tier" => $pubgSeasonsApiResponse2->data->attributes->rankedGameModeStats->solo->currentTier->tier,
442 "division" => $pubgSeasonsApiResponse2->data->attributes->rankedGameModeStats->solo->currentTier->subTier,
443 "rankPoint" => $pubgSeasonsApiResponse2->data->attributes->rankedGameModeStats->solo->currentRankPoint,
444 "winRate" => round(($pubgSeasonsApiResponse2->data->attributes->rankedGameModeStats->solo->winRatio * 100), 2),
445 "top10Rate" => round(($pubgSeasonsApiResponse2->data->attributes->rankedGameModeStats->solo->top10Ratio * 100), 2)
446 );
447 break;
448 case 'duo':
449 $Duo = (object) array(
450 "tier" => $pubgSeasonsApiResponse2->data->attributes->rankedGameModeStats->duo->currentTier->tier,
451 "division" => $pubgSeasonsApiResponse2->data->attributes->rankedGameModeStats->duo->currentTier->subTier,
452 "rankPoint" => $pubgSeasonsApiResponse2->data->attributes->rankedGameModeStats->duo->currentRankPoint,
453 "winRate" => round(($pubgSeasonsApiResponse2->data->attributes->rankedGameModeStats->duo->winRatio * 100), 2),
454 "top10Rate" => round(($pubgSeasonsApiResponse2->data->attributes->rankedGameModeStats->duo->top10Ratio * 100), 2)
455 );
456 break;
457 case 'squad':
458 $Squad = (object) array(
459 "tier" => $pubgSeasonsApiResponse2->data->attributes->rankedGameModeStats->squad->currentTier->tier,
460 "division" => $pubgSeasonsApiResponse2->data->attributes->rankedGameModeStats->squad->currentTier->subTier,
461 "rankPoint" => $pubgSeasonsApiResponse2->data->attributes->rankedGameModeStats->squad->currentRankPoint,
462 "winRate" => round(($pubgSeasonsApiResponse2->data->attributes->rankedGameModeStats->squad->winRatio * 100), 2),
463 "top10Rate" => round(($pubgSeasonsApiResponse2->data->attributes->rankedGameModeStats->squad->top10Ratio * 100), 2)
464 );
465 break;
466 }
467 }
468 }
469 if (is_null($Solo)) {
470 $Solo = (object) array(
471 "tier" => "Unranked",
472 "division" => 0,
473 "rankPoint" => 0,
474 "winRate" => round(0.0, 2),
475 "top10Rate" => round(0.0, 2)
476 );
477 }
478 if (is_null($Duo)) {
479 $Duo = (object) array(
480 "tier" => "Unranked",
481 "division" => 0,
482 "rankPoint" => 0,
483 "winRate" => round(0.0, 2),
484 "top10Rate" => round(0.0, 2)
485 );
486 }
487 if (is_null($Squad)) {
488 $Squad = (object) array(
489 "tier" => "Unranked",
490 "division" => 0,
491 "rankPoint" => 0,
492 "winRate" => round(0.0, 2),
493 "top10Rate" => round(0.0, 2)
494 );
495 }
496 $Season = (object) array(
497 "Solo" => $Solo,
498 "Duo" => $Duo,
499 "Squad" => $Squad
500 );
501 $response = (object) array(
502 "account" => $Account->account,
503 "season1" => $pubgSeasonsApiResponseCode1,
504 "currentSeason" => $currentSeason,
505 "season2" => $pubgSeasonsApiResponseCode2,
506 "Season" => $Season,
507 "requestedDate" => date("Y/m/d H:i:s"),
508 "renewOn" => date("Y/m/d H:i:s", strtotime("+1 hours"))
509 );
510 $cacheData = json_encode($response);
511 $cache = fopen("{$_SERVER['DOCUMENT_ROOT']}/Cache/PUBG/Users/Seasons/{$this->getIdentifier()}.json", "w");
512 fwrite($cache, $cacheData);
513 fclose($cache);
514 } else {
515 $response = (object) array(
516 "account" => $Account->account,
517 "season1" => $pubgSeasonsApiResponseCode1,
518 "currentSeason" => $currentSeason,
519 "season2" => $pubgSeasonsApiResponseCode2,
520 "Season" => (object) array()
521 );
522 }
523 } else {
524 $response = (object) array(
525 "account" => $Account->account,
526 "season1" => $pubgSeasonsApiResponseCode1,
527 "currentSeason" => $currentSeason,
528 "season2" => $pubgSeasonsApiResponseCode2,
529 );
530 }
531 } else {
532 $response = (object) array(
533 "account" => $Account->account,
534 "season" => $pubgSeasonsApiResponseCode1
535 );
536 }
537 } else {
538 $response = (object) array(
539 "account" => $Account->account
540 );
541 }
542 header('Content-Type: application/json', true, 200);
543 echo json_encode($response);
544 }
545 /**
546 * Accessing Match History
547 * @param string $player_name The name of the player
548 * @param string $platform The platform on which the player plays the game
549 * @return void
550 */
551 public function getMatchHistory(string $player_name, string $platform): void
552 {
553 $Account = $this->getAccount($player_name, $platform);
554 if ($Account->account == 200) {
555 $request = "https://api.pubg.com/shards/{$this->getPlatform()}/seasons";
556 $this->Curl = curl_init();
557 curl_setopt_array(
558 $this->Curl,
559 array(
560 CURLOPT_URL => $request,
561 CURLOPT_RETURNTRANSFER => true,
562 CURLOPT_ENCODING => '',
563 CURLOPT_MAXREDIRS => 10,
564 CURLOPT_TIMEOUT => 0,
565 CURLOPT_FOLLOWLOCATION => true,
566 CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
567 CURLOPT_CUSTOMREQUEST => 'GET',
568 CURLOPT_HTTPHEADER => array(
569 'Accept: application/vnd.api+json',
570 "Authorization: Bearer {$this->getApiKey()}"
571 )
572 )
573 );
574 $pubgSeasonsApiResponse1 = json_decode(curl_exec($this->Curl));
575 $pubgSeasonsApiResponseCode1 = curl_getinfo($this->Curl, CURLINFO_HTTP_CODE);
576 if ($pubgSeasonsApiResponseCode1 == 200) {
577 $currentSeason = "";
578 for ($index = 0; $index < count($pubgSeasonsApiResponse1->data); $index++) {
579 if ($pubgSeasonsApiResponse1->data[$index]->attributes->isCurrentSeason == true) {
580 $currentSeason = $pubgSeasonsApiResponse1->data[$index]->id;
581 }
582 }
583 $request = "https://api.pubg.com/shards/{$this->getPlatform()}/players/{$this->getIdentifier()}/seasons/{$currentSeason}";
584 $this->Curl = curl_init();
585 curl_setopt_array(
586 $this->Curl,
587 array(
588 CURLOPT_URL => $request,
589 CURLOPT_RETURNTRANSFER => true,
590 CURLOPT_ENCODING => '',
591 CURLOPT_MAXREDIRS => 10,
592 CURLOPT_TIMEOUT => 0,
593 CURLOPT_FOLLOWLOCATION => true,
594 CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
595 CURLOPT_CUSTOMREQUEST => 'GET',
596 CURLOPT_HTTPHEADER => array(
597 'Accept: application/vnd.api+json',
598 "Authorization: Bearer {$this->getApiKey()}"
599 )
600 )
601 );
602 $pubgSeasonsApiResponse2 = json_decode(curl_exec($this->Curl));
603 $pubgSeasonsApiResponseCode2 = curl_getinfo($this->Curl, CURLINFO_HTTP_CODE);
604 if ($pubgSeasonsApiResponseCode2 == 200) {
605 $matchIdentifiers = array();
606 if (!empty($pubgSeasonsApiResponse2->data->relationships->matchesSolo->data)) {
607 for ($index = 0; $index < count($pubgSeasonsApiResponse2->data->relationships->matchesSolo->data); $index++) {
608 array_push($matchIdentifiers, $pubgSeasonsApiResponse2->data->relationships->matchesSolo->data[$index]);
609 }
610 }
611 if (!empty($pubgSeasonsApiResponse2->data->relationships->matchesDuo->data)) {
612 for ($index = 0; $index < count($pubgSeasonsApiResponse2->data->relationships->matchesDuo->data); $index++) {
613 array_push($matchIdentifiers, $pubgSeasonsApiResponse2->data->relationships->matchesDuo->data[$index]);
614 }
615 }
616 if (!empty($pubgSeasonsApiResponse2->data->relationships->matchesSquad->data)) {
617 for ($index = 0; $index < count($pubgSeasonsApiResponse2->data->relationships->matchesSquad->data); $index++) {
618 array_push($matchIdentifiers, $pubgSeasonsApiResponse2->data->relationships->matchesSquad->data[$index]);
619 }
620 }
621 if (!empty($matchIdentifiers)) {
622 $matches = array();
623 for ($firstIndex = 0; $firstIndex < count($matchIdentifiers); $firstIndex++) {
624 $request = "https://api.pubg.com/shards/{$this->getPlatform()}/matches/{$matchIdentifiers[$firstIndex]->id}";
625 $this->Curl = curl_init();
626 curl_setopt_array(
627 $this->Curl,
628 array(
629 CURLOPT_URL => $request,
630 CURLOPT_RETURNTRANSFER => true,
631 CURLOPT_ENCODING => '',
632 CURLOPT_MAXREDIRS => 10,
633 CURLOPT_TIMEOUT => 0,
634 CURLOPT_FOLLOWLOCATION => true,
635 CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
636 CURLOPT_CUSTOMREQUEST => 'GET',
637 CURLOPT_HTTPHEADER => array(
638 'Accept: application/vnd.api+json',
639 "Authorization: Bearer {$this->getApiKey()}"
640 )
641 )
642 );
643 $pubgMatchesApiResponse = json_decode(curl_exec($this->Curl));
644 $match = array();
645 for ($secondIndex = 0; $secondIndex < count($pubgMatchesApiResponse->included); $secondIndex++) {
646 if ($pubgMatchesApiResponse->included[$secondIndex]->type == "participant" && $pubgMatchesApiResponse->included[$secondIndex]->attributes->stats->playerId == $this->getIdentifier()) {
647 $distance = ($pubgMatchesApiResponse->included[$secondIndex]->attributes->stats->rideDistance + $pubgMatchesApiResponse->included[$secondIndex]->attributes->stats->swimDistance + $pubgMatchesApiResponse->included[$secondIndex]->attributes->stats->walkDistance) / 1000;
648 $data = $pubgMatchesApiResponse->included[$secondIndex]->attributes->stats;
649 $data = (object) array(
650 "rank" => $pubgMatchesApiResponse->included[$secondIndex]->attributes->stats->winPlace,
651 "kill" => $pubgMatchesApiResponse->included[$secondIndex]->attributes->stats->kills,
652 "damage" => round($pubgMatchesApiResponse->included[$secondIndex]->attributes->stats->damageDealt, 2),
653 "distance" => round($distance, 2)
654 );
655 }
656 $match = $data;
657 }
658 array_push($matches, $match);
659 }
660 $response = (object) array(
661 "account" => $Account->account,
662 "season1" => $pubgSeasonsApiResponseCode1,
663 "season2" => $pubgSeasonsApiResponseCode2,
664 "matches" => $matches,
665 "requestedDate" => date("Y/m/d H:i:s"),
666 "renewOn" => date("Y/m/d H:i:s", strtotime("+1 hours"))
667 );
668 $cacheData = json_encode($response);
669 $cache = fopen("{$_SERVER['DOCUMENT_ROOT']}/Cache/PUBG/Users/Match Histories/{$this->getIdentifier()}.json", "w");
670 fwrite($cache, $cacheData);
671 fclose($cache);
672 } else {
673 $response = (object) array(
674 "account" => $Account->account,
675 "season1" => $pubgSeasonsApiResponseCode1,
676 "season2" => $pubgSeasonsApiResponseCode2,
677 "matches" => array()
678 );
679 }
680 } else {
681 $response = (object) array(
682 "account" => $Account->account,
683 "season1" => $pubgSeasonsApiResponseCode1,
684 "season2" => $pubgSeasonsApiResponseCode2
685 );
686 }
687 } else {
688 $response = (object) array(
689 "account" => $Account->account,
690 "season" => $pubgSeasonsApiResponseCode1
691 );
692 }
693 } else {
694 $response = (object) array(
695 "account" => $Account->account
696 );
697 }
698 header('Content-Type: application/json', true, 200);
699 echo json_encode($response);
700 }
701 /**
702 * Searching for a player
703 * @param string $player_name The username of the player
704 * @param string $platform The platform that the player use to play the game
705 * @return void
706 */
707 public function search(string $player_name, string $platform): void
708 {
709 $PlayerData = $this->getAccount($player_name, $platform);
710 $response = (object) array(
711 "account" => $PlayerData->account,
712 "identifier" => $PlayerData->identifier,
713 "playerName" => $PlayerData->playerName,
714 "platform" => $PlayerData->platform,
715 "url" => "/PlayerUnknownBattleGrounds/Profile/$PlayerData->playerName"
716 );
717 $searchPUBG = $response;
718 $search = (object) array(
719 "PlayerUnknownBattleGrounds" => $searchPUBG
720 );
721 $session = array(
722 "Client" => $_SESSION['Client'],
723 "User" => $_SESSION['User'],
724 "Account" => $_SESSION['Account'],
725 "Search" => $search
726 );
727 $cacheData = json_encode($session);
728 $cache = fopen("{$_SERVER['DOCUMENT_ROOT']}/Cache/Session/Users/{$_SESSION['User']['username']}.json", "w");
729 fwrite($cache, $cacheData);
730 fclose($cache);
731 header('Content-Type: application/json; X-XSS-Protection: 1; mode=block', true, 200);
732 echo json_encode($response);
733 }
734}
735