add, remove

main
Benjamin Kraft 4 months ago
parent 090985b931
commit 4a4b5385a7
  1. 20
      public/elotracker/RiotRequest.php
  2. 61
      public/elotracker/Tracker.php
  3. 8
      public/elotracker/add.php
  4. 11
      public/elotracker/index.php
  5. 8
      public/elotracker/remove.php

@ -8,14 +8,20 @@ class RiotRequest {
protected string $host = "https://euw1.api.riotgames.com"; protected string $host = "https://euw1.api.riotgames.com";
public array $responseHeader = array(); public array $responseHeader = array();
public int $responseCode; 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->endpoint = $endpoint;
$this->isEUNE = $isEUNE;
if ($isEUNE){
$this->host = "https://eun1.api.riotgames.com";
}
} }
public function useRouting(): void { public function useRouting(): void {
$this->host = "https://europe.api.riotgames.com"; $this->host = "https://europe.api.riotgames.com";
$this->usesRouting = true;
} }
public function setHeaders($headers): void { public function setHeaders($headers): void {
@ -44,7 +50,7 @@ class RiotRequest {
return str_replace(" ", "", $url); return str_replace(" ", "", $url);
} }
public function run() { public function run(&$successWithEUNE=false) {
$opts = [ $opts = [
"http" => [ "http" => [
"method" => "GET", "method" => "GET",
@ -58,12 +64,14 @@ class RiotRequest {
preg_match_all("/HTTP\/\d\.\d\s+(\d+)\s+/", $this->responseHeader[0], $matches); preg_match_all("/HTTP\/\d\.\d\s+(\d+)\s+/", $this->responseHeader[0], $matches);
$this->responseCode = intval($matches[1][0]); $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->host = "https://eun1.api.riotgames.com";
$this->triedEUNE = true; return $this->run($successWithEUNE);
return $this->run();
} }
$successWithEUNE = $this->isEUNE;
return json_decode($result); return json_decode($result);
} }

@ -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 { class Account {
@ -87,8 +144,8 @@ class Account {
* @param Elo[] $eloEntries * @param Elo[] $eloEntries
*/ */
public function addNewEloEntry(array &$eloEntries): void { public function addNewEloEntry(array &$eloEntries): void {
$summonerId = (new RiotRequest("lol/summoner/v4/summoners/by-puuid/$this->puuid"))->run()->id; $summonerId = (new RiotRequest("lol/summoner/v4/summoners/by-puuid/$this->puuid"))->run($isEUNE)->id;
$fetchedLeagues = (new RiotRequest("lol/league/v4/entries/by-summoner/$summonerId"))->run(); $fetchedLeagues = (new RiotRequest("lol/league/v4/entries/by-summoner/$summonerId", $isEUNE))->run();
$currentElo = end($eloEntries); $currentElo = end($eloEntries);

@ -0,0 +1,8 @@
<?php
require_once "Tracker.php";
$tracker = new Tracker();
if (!$tracker->add($_GET["gameName"], $_GET["tagLine"], $message)){
echo "Failure: ".$message;
}

@ -15,13 +15,12 @@ $tracker = new Tracker();
foreach ($tracker->accounts as $puuid => $account){ foreach ($tracker->accounts as $puuid => $account){
$entries = $tracker->entries[$puuid]; $entries = $tracker->entries[$puuid];
if (sizeof($entries) > 0){ echo "<h3>$account->gameName#$account->tagLine</h3>";
echo "<h3>$account->gameName#$account->tagLine</h3>"; foreach ($entries as $date => $elo){
foreach ($entries as $date => $elo){ $eloValue = $elo->value();
$eloValue = $elo->value(); echo "$date = $eloValue<br>";
echo "$date = $eloValue<br>";
}
} }
} }
?> ?>

@ -0,0 +1,8 @@
<?php
require_once "Tracker.php";
$tracker = new Tracker();
if (!$tracker->remove($_GET["gameName"], $_GET["tagLine"], $message)){
echo "Failure: ".$message;
}
Loading…
Cancel
Save