forked from RetroAchievements/api-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetUserGameLeaderboards.ts
More file actions
83 lines (79 loc) · 2.11 KB
/
getUserGameLeaderboards.ts
File metadata and controls
83 lines (79 loc) · 2.11 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
import type { ID } from "../utils/internal";
import {
apiBaseUrl,
buildRequestUrl,
call,
serializeProperties,
} from "../utils/internal";
import type { AuthObject } from "../utils/public";
import type {
GetUserGameLeaderboardsResponse,
UserGameLeaderboards,
} from "./models";
/**
* A call to this endpoint will retrieve a user's list of leaderboards for a given game, targeted by the game's ID.
*
* @param authorization An object containing your username and webApiKey.
* This can be constructed with `buildAuthorization()`.
*
* @param payload.gameId The target game ID.
*
* @param payload.offset Defaults to 0. The number of entries to skip.
*
* @param payload.count Defaults to 100, has a max of 500.
*
* @example
* ```
* const gameLeaderboards = await getUserGameLeaderboards(
* authorization,
* { gameId: 14402 }
* );
* ```
*
* @returns An object containing user game leaderboard's.
* ```json
* {
* "count": 10,
* "total": 64,
* "results": [
* {
* "id": 19062,
* "rankAsc": true,
* "title": "New Zealand One",
* "description": "Complete New Zealand S1 in least time",
* "format": "MILLISECS",
* "userEntry": {
* "user": "zuliman92",
* "ulid": "00003EMFWR7XB8SDPEHB3K56ZQ",
* "score": 12620,
* "formattedScore": "2:06.20",
* "rank": 2,
* "dateUpdated": "2024-12-12T16:40:59+00:00"
* }
* }
* ]
* }
* ```
*/
export const getUserGameLeaderboards = async (
authorization: AuthObject,
payload: { gameId: ID; username: string; offset?: number; count?: number }
): Promise<UserGameLeaderboards> => {
const queryParams: Record<string, any> = {};
queryParams.i = payload.gameId;
queryParams.u = payload.username;
if (payload?.offset) {
queryParams.o = payload.offset;
}
if (payload?.count) {
queryParams.c = payload.count;
}
const url = buildRequestUrl(
apiBaseUrl,
"/API_GetUserGameLeaderboards.php",
authorization,
queryParams
);
const rawResponse = await call<GetUserGameLeaderboardsResponse>({ url });
return serializeProperties(rawResponse);
};