diff --git a/public/elotracker/Tracker.php b/public/elotracker/Tracker.php index c98675e..08acb65 100644 --- a/public/elotracker/Tracker.php +++ b/public/elotracker/Tracker.php @@ -208,10 +208,20 @@ class Account { $currentElo->rank = $fetchedElo->rank; $currentElo->points = $fetchedElo->leaguePoints; - $date = (new DateTime("now"))->format("Y-m-d H:i:s"); - $eloEntries[$date] = $currentElo; + $latestMatchTimestamp = $this->getLatestMatchTimestamp(); $conn = new MySQLConnection(); + $isMatchUnknown = $conn->query(" + SELECT COUNT(date) = 0 AS match_unknown + FROM updates + WHERE date > FROM_UNIXTIME($latestMatchTimestamp) AND status = 'COMPLETE'; + ")->fetch_assoc()["match_unknown"]; + + $date = new DateTime(); + if ($isMatchUnknown) + $date->setTimestamp($latestMatchTimestamp); + + $eloEntries[$date->getTimestamp()] = $currentElo; $sqlAccountId = $conn->query(" SELECT id @@ -224,7 +234,7 @@ class Account { (accountId, date, tier, `rank`, points) VALUES (?, ?, ?, ?, ?) ")->execute([ - $sqlAccountId, $date, $currentElo->tier, $currentElo->rank, $currentElo->points + $sqlAccountId, $date->format("Y-m-d H:i:s"), $currentElo->tier, $currentElo->rank, $currentElo->points ]); } } @@ -245,6 +255,20 @@ class Account { WHERE `puuid` = ? ")->execute([$this->gameName, $this->tagLine, $this->puuid]); } + private function getLatestMatchTimestamp(): int { + $matchIdsRequest = new RiotRequest("lol/match/v5/matches/by-puuid/$this->puuid/ids"); + $matchIdsRequest->useRouting(); + $matchIdsRequest->setQueries([ + "count" => 1, + "queue" => 420 // 5x5 solo queue + ]); + $latestMatchId = $matchIdsRequest->run()[0]; + + $latestMatchRequest = new RiotRequest("lol/match/v5/matches/$latestMatchId"); + $latestMatchRequest->useRouting(); + + return intval($latestMatchRequest->run()->info->gameEndTimestamp / 1000); + } } class Elo { diff --git a/public/elotracker/update.php b/public/elotracker/update.php index 5d6c6a9..38fb169 100755 --- a/public/elotracker/update.php +++ b/public/elotracker/update.php @@ -50,4 +50,10 @@ $tracker->update(function($value){ sendEvent('progress', $value); }); +$conn->query(" + UPDATE updates + SET status = 'COMPLETE' + WHERE status = 'RUNNING' +"); + sendEvent("done", "");