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