diff --git a/public/elotracker/RiotRequest.php b/public/elotracker/RiotRequest.php new file mode 100644 index 0000000..30b8784 --- /dev/null +++ b/public/elotracker/RiotRequest.php @@ -0,0 +1,70 @@ +endpoint = $endpoint; + } + + public function useRouting(): void { + $this->host = "https://europe.api.riotgames.com"; + } + + public function setHeaders($headers): void { + $this->headers = $headers; + } + + public function setQueries($queries): void { + $this->queries = $queries; + } + + protected function getHeaderString(): string { + $api_key = getenv("RIOT_API_KEY"); + $header = "X-Riot-Token: $api_key\r\n"; + foreach ($this->headers as $key => $value) { + $header .= "$key: $value\r\n"; + } + return $header; + } + + private function getFinalURL(): string { + $queryString = "?"; + foreach ($this->queries as $key => $value) { + $queryString .= "$key=$value&"; + } + $url = $this->host . "/" . $this->endpoint . $queryString; + return str_replace(" ", "", $url); + } + + public function run() { + $opts = [ + "http" => [ + "method" => "GET", + "header" => $this->getHeaderString() + ] + ]; + $context = stream_context_create($opts); + $url = $this->getFinalURL(); + $result = @file_get_contents($url, false, $context); + $this->responseHeader = $http_response_header; + 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) { + $this->host = "https://eun1.api.riotgames.com"; + $this->triedEUNE = true; + return $this->run(); + } + + return json_decode($result); + } + +} \ No newline at end of file diff --git a/public/elotracker/Tracker.php b/public/elotracker/Tracker.php new file mode 100644 index 0000000..cfc6da6 --- /dev/null +++ b/public/elotracker/Tracker.php @@ -0,0 +1,180 @@ +conn = new MySQLConnection(); + $this->readAccounts(); + $this->readEntries(); + } + + private function readAccounts(): void { + $accounts = []; + + $sql = $this->conn->query("SELECT * FROM `accounts`"); + while ($row = $sql->fetch_assoc()) { + $account = new Account(); + $account->puuid = $row["puuid"]; + $account->gameName = $row["gameName"]; + $account->tagLine = $row["tagLine"]; + + $accounts[$account->puuid] = $account; + } + + $this->accounts = $accounts; + } + + private function readEntries(): void { + $sql = $this->conn->query(" + SELECT accounts.puuid AS puuid, date, tier, `rank`, points + FROM accounts LEFT JOIN elo_entries ON id = accountId + ORDER BY accounts.puuid, date + "); + + $result = []; + + while ($row = $sql->fetch_assoc()) { + $puuid = $row["puuid"]; + $date = $row["date"]; + + if (!isset($result[$puuid])) + $result[$puuid] = []; + + if (!$date) + continue; + + $elo = new Elo(); + $elo->tier = $row["tier"]; + $elo->rank = $row["rank"]; + $elo->points = $row["points"]; + + $result[$puuid][$date] = $elo; + } + + $this->entries = $result; + } + + public function update(): void { + + foreach ($this->accounts as $puuid => $account) { + $account->updateRiotID(); + $account->addNewEloEntry($this->entries[$puuid]); + } + + } +} + +class Account { + public string $puuid; + public string $gameName; + public string $tagLine; + + /** + * @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(); + + $currentElo = end($eloEntries); + + foreach ($fetchedLeagues as $fetchedElo) { + if ($fetchedElo->queueType === "RANKED_SOLO_5x5" && ($fetchedElo->wins + $fetchedElo->losses >= 5)) { + if (!$currentElo || + $currentElo->tier !== $fetchedElo->tier || + $currentElo->rank !== $fetchedElo->rank || + $currentElo->points !== $fetchedElo->leaguePoints){ + + $currentElo = new Elo(); + $currentElo->tier = $fetchedElo->tier; + $currentElo->rank = $fetchedElo->rank; + $currentElo->points = $fetchedElo->leaguePoints; + + $date = (new DateTime("now"))->format("Y-m-d H:i:s"); + $eloEntries[$date] = $currentElo; + + $conn = new MySQLConnection(); + + $sqlAccountId = $conn->query(" + SELECT id + FROM `accounts` + WHERE puuid = '$this->puuid' + ")->fetch_assoc()["id"]; + + $conn->prepare(" + INSERT INTO `elo_entries` + (accountId, date, tier, `rank`, points) + VALUES (?, ?, ?, ?, ?) + ")->execute([ + $sqlAccountId, $date, $currentElo->tier, $currentElo->rank, $currentElo->points + ]); + } + } + } + } + public function updateRiotID(): void { + $request = new RiotRequest("riot/account/v1/accounts/by-puuid/$this->puuid"); + $request->useRouting(); + $result = $request->run(); + if ($request->responseCode == 200) { + $this->gameName = $result->gameName; + $this->tagLine = $result->tagLine; + + $conn = new MySQLConnection(); + $conn->query(" + UPDATE `accounts` + SET `gameName` = '$this->gameName', `tagLine` = '$this->tagLine' + WHERE `puuid` = '$this->puuid' + "); + + } + } +} + +class Elo { + public string $tier; + public string $rank; + public int $points; + public function value() : int { + $tiers = [ + "CHALLENGER" => 2800, + "GRANDMASTER" => 2800, + "MASTER" => 2800, + "DIAMOND" => 2400, + "EMERALD" => 2000, + "PLATINUM" => 1600, + "GOLD" => 1200, + "SILVER" => 800, + "BRONZE" => 400, + "IRON" => 0, + ]; + + $ranks = [ + "I" => 300, + "II" => 200, + "III" => 100, + "IV" => 0, + ]; + + $tier = $this->tier; + + if ($tier === "MASTER" || $tier === "GRANDMASTER" || $tier === "CHALLENGER") + return $tiers[$tier] + $this->points; + + return $tiers[$tier] + $ranks[$this->rank] + $this->points; + } +} diff --git a/public/elotracker/index.php b/public/elotracker/index.php index 744956e..6707cea 100644 --- a/public/elotracker/index.php +++ b/public/elotracker/index.php @@ -7,8 +7,22 @@ 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
"; + } + } +} ?> diff --git a/public/elotracker/update.php b/public/elotracker/update.php new file mode 100644 index 0000000..36eace0 --- /dev/null +++ b/public/elotracker/update.php @@ -0,0 +1,6 @@ +update(); \ No newline at end of file diff --git a/public/favicon.ico b/public/favicon.ico new file mode 100644 index 0000000..abf2c62 Binary files /dev/null and b/public/favicon.ico differ diff --git a/public/util/dotenv.php b/public/util/dotenv.php index d435df8..d5e8fea 100644 --- a/public/util/dotenv.php +++ b/public/util/dotenv.php @@ -2,3 +2,4 @@ foreach (parse_ini_file($_SERVER['DOCUMENT_ROOT']."/../.env") as $key => $value) putenv("$key=$value"); +unset($key, $value);