|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace RetroAchievements\Api; |
| 6 | + |
| 7 | +use GuzzleHttp\Client; |
| 8 | + |
| 9 | +class RetroAchievements |
| 10 | +{ |
| 11 | + use MakesHttpRequests; |
| 12 | + |
| 13 | + public const API_URL = 'https://retroachievements.org/API/'; |
| 14 | + |
| 15 | + public const API_VERSION = 1; |
| 16 | + |
| 17 | + protected Client $client; |
| 18 | + |
| 19 | + public function __construct(public string $username, public string $apiKey, string $baseUri = self::API_URL) |
| 20 | + { |
| 21 | + $this->client = new Client([ |
| 22 | + 'base_uri' => $baseUri, |
| 23 | + 'http_errors' => false, |
| 24 | + 'headers' => [ |
| 25 | + 'Accept' => 'application/json', |
| 26 | + 'Content-Type' => 'application/json', |
| 27 | + ], |
| 28 | + ]); |
| 29 | + } |
| 30 | + |
| 31 | + public function setClient(Client $client): self |
| 32 | + { |
| 33 | + $this->client = $client; |
| 34 | + |
| 35 | + return $this; |
| 36 | + } |
| 37 | + |
| 38 | + public function getTopTenUsers(): mixed |
| 39 | + { |
| 40 | + return $this->getApiUrl('API_GetTopTenUsers.php'); |
| 41 | + } |
| 42 | + |
| 43 | + public function getGameInfo(int $gameID): mixed |
| 44 | + { |
| 45 | + return $this->getApiUrl('API_GetGame.php', [ |
| 46 | + 'i' => $gameID, |
| 47 | + ]); |
| 48 | + } |
| 49 | + |
| 50 | + public function getGameInfoExtended(int $gameID): mixed |
| 51 | + { |
| 52 | + return $this->getApiUrl('API_GetGameExtended.php', [ |
| 53 | + 'i' => $gameID, |
| 54 | + ]); |
| 55 | + } |
| 56 | + |
| 57 | + public function getConsoleIDs(): mixed |
| 58 | + { |
| 59 | + return $this->getApiUrl('API_GetConsoleIDs.php'); |
| 60 | + } |
| 61 | + |
| 62 | + public function getGameList(int $consoleID): mixed |
| 63 | + { |
| 64 | + return $this->getApiUrl('API_GetGameList.php', [ |
| 65 | + 'i' => $consoleID, |
| 66 | + ]); |
| 67 | + } |
| 68 | + |
| 69 | + public function getFeedFor(string $user, int $count, int $offset = 0): mixed |
| 70 | + { |
| 71 | + return $this->getApiUrl('API_GetFeed.php', [ |
| 72 | + 'u' => $user, |
| 73 | + 'c' => $count, |
| 74 | + 'o' => $offset, |
| 75 | + ]); |
| 76 | + } |
| 77 | + |
| 78 | + public function getUserRankAndScore(string $user): mixed |
| 79 | + { |
| 80 | + return $this->getApiUrl('API_GetUserRankAndScore.php', [ |
| 81 | + 'u' => $user, |
| 82 | + ]); |
| 83 | + } |
| 84 | + |
| 85 | + public function getUserProgress(string $user, string $gameIDCSV): mixed |
| 86 | + { |
| 87 | + $gameIDCSV = preg_replace('/\s+/', '', $gameIDCSV); // Remove all whitespace |
| 88 | + |
| 89 | + return $this->getApiUrl('API_GetUserProgress.php', [ |
| 90 | + 'u' => $user, |
| 91 | + 'i' => $gameIDCSV, |
| 92 | + ]); |
| 93 | + } |
| 94 | + |
| 95 | + public function getUserRecentlyPlayedGames(string $user, int $count, int $offset = 0): mixed |
| 96 | + { |
| 97 | + return $this->getApiUrl('API_GetUserRecentlyPlayedGames.php', [ |
| 98 | + 'u' => $user, |
| 99 | + 'c' => $count, |
| 100 | + 'o' => $offset, |
| 101 | + ]); |
| 102 | + } |
| 103 | + |
| 104 | + public function getUserSummary(string $user, int $numRecentGames): mixed |
| 105 | + { |
| 106 | + return $this->getApiUrl('API_GetUserSummary.php', [ |
| 107 | + 'u' => $user, |
| 108 | + 'g' => $numRecentGames, |
| 109 | + 'a' => 5, |
| 110 | + ]); |
| 111 | + } |
| 112 | + |
| 113 | + public function getGameInfoAndUserProgress(string $user, int $gameID): mixed |
| 114 | + { |
| 115 | + return $this->getApiUrl('API_GetGameInfoAndUserProgress.php', [ |
| 116 | + 'u' => $user, |
| 117 | + 'g' => $gameID, |
| 118 | + ]); |
| 119 | + } |
| 120 | + |
| 121 | + public function getAchievementsEarnedOnDay(string $user, string $date): mixed |
| 122 | + { |
| 123 | + return $this->getApiUrl('API_GetAchievementsEarnedOnDay.php', [ |
| 124 | + 'u' => $user, |
| 125 | + 'd' => $date, |
| 126 | + ]); |
| 127 | + } |
| 128 | + |
| 129 | + public function getAchievementsEarnedBetween(string $user, string $startDate, string $endDate): mixed |
| 130 | + { |
| 131 | + return $this->getApiUrl('API_GetAchievementsEarnedBetween.php', [ |
| 132 | + 'u' => $user, |
| 133 | + 'f' => strtotime($startDate), |
| 134 | + 't' => strtotime($endDate), |
| 135 | + ]); |
| 136 | + } |
| 137 | + |
| 138 | + protected function getApiUrl(string $endpoint, array $parameters = []): mixed |
| 139 | + { |
| 140 | + return $this->get( |
| 141 | + sprintf('%s?%s', $endpoint, http_build_query([ |
| 142 | + 'z' => $this->username, |
| 143 | + 'y' => $this->apiKey, |
| 144 | + ] + $parameters)) |
| 145 | + ); |
| 146 | + } |
| 147 | +} |
0 commit comments