From 4a4b5385a7fdaf292a6a3638854deb074b184b0f Mon Sep 17 00:00:00 2001 From: Benjamin Kraft Date: Sun, 2 Jun 2024 19:27:34 +0200 Subject: [PATCH] add, remove --- public/elotracker/RiotRequest.php | 20 +++++++--- public/elotracker/Tracker.php | 61 ++++++++++++++++++++++++++++++- public/elotracker/add.php | 8 ++++ public/elotracker/index.php | 11 +++--- public/elotracker/remove.php | 8 ++++ 5 files changed, 94 insertions(+), 14 deletions(-) create mode 100644 public/elotracker/add.php create mode 100644 public/elotracker/remove.php diff --git a/public/elotracker/RiotRequest.php b/public/elotracker/RiotRequest.php index 30b8784..915d46c 100644 --- a/public/elotracker/RiotRequest.php +++ b/public/elotracker/RiotRequest.php @@ -8,14 +8,20 @@ class RiotRequest { protected string $host = "https://euw1.api.riotgames.com"; public array $responseHeader = array(); public int $responseCode; - private bool $triedEUNE = false; + private bool $isEUNE; + private bool $usesRouting = false; - public function __construct($endpoint) { + public function __construct($endpoint, $isEUNE=false) { $this->endpoint = $endpoint; + $this->isEUNE = $isEUNE; + if ($isEUNE){ + $this->host = "https://eun1.api.riotgames.com"; + } } public function useRouting(): void { $this->host = "https://europe.api.riotgames.com"; + $this->usesRouting = true; } public function setHeaders($headers): void { @@ -44,7 +50,7 @@ class RiotRequest { return str_replace(" ", "", $url); } - public function run() { + public function run(&$successWithEUNE=false) { $opts = [ "http" => [ "method" => "GET", @@ -58,12 +64,14 @@ class RiotRequest { preg_match_all("/HTTP\/\d\.\d\s+(\d+)\s+/", $this->responseHeader[0], $matches); $this->responseCode = intval($matches[1][0]); - if ($this->responseCode == 404 && !$this->triedEUNE) { + if ($this->responseCode == 404 && !$this->isEUNE && !$this->usesRouting) { + $this->isEUNE = true; $this->host = "https://eun1.api.riotgames.com"; - $this->triedEUNE = true; - return $this->run(); + return $this->run($successWithEUNE); } + $successWithEUNE = $this->isEUNE; + return json_decode($result); } diff --git a/public/elotracker/Tracker.php b/public/elotracker/Tracker.php index cfc6da6..8a75413 100644 --- a/public/elotracker/Tracker.php +++ b/public/elotracker/Tracker.php @@ -76,6 +76,63 @@ class Tracker { } } + + public function add($gameName, $tagLine, &$message): bool { + $request = new RiotRequest("riot/account/v1/accounts/by-riot-id/$gameName/$tagLine"); + $request->useRouting(); + $result = $request->run(); + + if ($request->responseCode === 200){ + if (!key_exists($result->puuid, $this->accounts)){ + $account = new Account(); + $account->puuid = $result->puuid; + $account->gameName = $result->gameName; + $account->tagLine = $result->tagLine; + $this->accounts[$account->puuid] = $account; + + $this->conn->prepare(" + INSERT INTO `accounts` + (`puuid`, `gameName`, `tagLine`) + VALUES (?, ?, ?) + ")->execute([$account->puuid, $account->gameName, $account->tagLine]); + + $message = "Success."; + return true; + } else { + $message = "Riot ID already added."; + } + } else { + $message = "Riot ID not found."; + } + + return false; + } + public function remove($gameName, $tagLine, &$message): bool { + $request = new RiotRequest("riot/account/v1/accounts/by-riot-id/$gameName/$tagLine"); + $request->useRouting(); + $result = $request->run(); + + if ($request->responseCode === 200){ + if (key_exists($result->puuid, $this->accounts)){ + unset($this->accounts[$result->puuid]); + unset($this->entries[$result->puuid]); + + $this->conn->query(" + DELETE FROM `accounts` + WHERE puuid = '$result->puuid'; + "); + + $message = "Success."; + return true; + } else { + $message = "Riot ID was not added."; + } + } else { + $message = "Riot ID not found."; + } + + return false; + } } class Account { @@ -87,8 +144,8 @@ class Account { * @param Elo[] $eloEntries */ public function addNewEloEntry(array &$eloEntries): void { - $summonerId = (new RiotRequest("lol/summoner/v4/summoners/by-puuid/$this->puuid"))->run()->id; - $fetchedLeagues = (new RiotRequest("lol/league/v4/entries/by-summoner/$summonerId"))->run(); + $summonerId = (new RiotRequest("lol/summoner/v4/summoners/by-puuid/$this->puuid"))->run($isEUNE)->id; + $fetchedLeagues = (new RiotRequest("lol/league/v4/entries/by-summoner/$summonerId", $isEUNE))->run(); $currentElo = end($eloEntries); diff --git a/public/elotracker/add.php b/public/elotracker/add.php new file mode 100644 index 0000000..016ff0f --- /dev/null +++ b/public/elotracker/add.php @@ -0,0 +1,8 @@ +add($_GET["gameName"], $_GET["tagLine"], $message)){ + echo "Failure: ".$message; +} \ No newline at end of file diff --git a/public/elotracker/index.php b/public/elotracker/index.php index 6707cea..0f83038 100644 --- a/public/elotracker/index.php +++ b/public/elotracker/index.php @@ -15,13 +15,12 @@ $tracker = new Tracker(); foreach ($tracker->accounts as $puuid => $account){ $entries = $tracker->entries[$puuid]; - if (sizeof($entries) > 0){ - echo "

$account->gameName#$account->tagLine

"; - foreach ($entries as $date => $elo){ - $eloValue = $elo->value(); - echo "$date = $eloValue
"; - } + echo "

$account->gameName#$account->tagLine

"; + foreach ($entries as $date => $elo){ + $eloValue = $elo->value(); + echo "$date = $eloValue
"; } + } ?> diff --git a/public/elotracker/remove.php b/public/elotracker/remove.php new file mode 100644 index 0000000..d55a8d1 --- /dev/null +++ b/public/elotracker/remove.php @@ -0,0 +1,8 @@ +remove($_GET["gameName"], $_GET["tagLine"], $message)){ + echo "Failure: ".$message; +}