-
Notifications
You must be signed in to change notification settings - Fork 16
feat: add getGameLeaderboards() function #133
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
stiefeljackal
wants to merge
3
commits into
RetroAchievements:main
Choose a base branch
from
stiefeljackal:feat/leaderboard/by-game-id
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,152 @@ | ||
| import { http, HttpResponse } from "msw"; | ||
| import { setupServer } from "msw/node"; | ||
|
|
||
| import { apiBaseUrl } from "../utils/internal"; | ||
| import { buildAuthorization } from "../utils/public"; | ||
| import { getGameLeaderboards } from "./getGameLeaderboards"; | ||
| import type { GameLeaderboards, GetGameLeaderboardsResponse } from "./models"; | ||
|
|
||
| const server = setupServer(); | ||
|
|
||
| describe("Function: getGameLeaderboards", () => { | ||
| // MSW Setup | ||
| beforeAll(() => server.listen()); | ||
| afterEach(() => server.resetHandlers()); | ||
| afterAll(() => server.close()); | ||
|
|
||
| it("is defined #sanity", () => { | ||
| // ASSERT | ||
| expect(getGameLeaderboards).toBeDefined(); | ||
| }); | ||
|
|
||
| it("using defaults, retrieves the list of game leaderboards for the given game id", async () => { | ||
| // ARRANGE | ||
| const authorization = buildAuthorization({ | ||
| username: "mockUserName", | ||
| webApiKey: "mockWebApiKey", | ||
| }); | ||
|
|
||
| const mockResponse = mockGetGameLeaderboardsResponse; | ||
|
|
||
| server.use( | ||
| http.get(`${apiBaseUrl}/API_GetGameLeaderboards.php`, (info) => { | ||
| const url = new URL(info.request.url); | ||
| expect(url.searchParams.get("i")).toBe(mockGameId); | ||
| expect(url.searchParams.has("c")).toBeFalsy(); | ||
| expect(url.searchParams.has("o")).toBeFalsy(); | ||
| return HttpResponse.json(mockResponse); | ||
| }) | ||
| ); | ||
|
|
||
| // ACT | ||
| const response = await getGameLeaderboards(authorization, { | ||
| gameId: mockGameId, | ||
| }); | ||
| expect(response).toEqual(mockGameLeaderboardsValue); | ||
| }); | ||
|
|
||
| it.each([{ offset: 1, count: 1 }, { offset: 5 }, { count: 20 }])( | ||
| "calls the 'Game Leaderboards' endpoint with a given offset ($offset) and/or count ($count)", | ||
| async ({ offset: mockOffset, count: mockCount }) => { | ||
| // ARRANGE | ||
| const authorization = buildAuthorization({ | ||
| username: "mockUserName", | ||
| webApiKey: "mockWebApiKey", | ||
| }); | ||
|
|
||
| server.use( | ||
| http.get(`${apiBaseUrl}/API_GetGameLeaderboards.php`, (info) => { | ||
| const url = new URL(info.request.url); | ||
| const c = url.searchParams.get("c"); | ||
| const o = url.searchParams.get("o"); | ||
| expect(url.searchParams.get("i")).toBe(mockGameId); | ||
| expect(String(c)).toEqual(String(mockCount ?? null)); | ||
| expect(String(o)).toEqual(String(mockOffset ?? null)); | ||
| return HttpResponse.json(mockGetGameLeaderboardsResponse); | ||
| }) | ||
| ); | ||
|
|
||
| // ACT | ||
| await getGameLeaderboards(authorization, { | ||
| gameId: mockGameId, | ||
| offset: mockOffset, | ||
| count: mockCount, | ||
| }); | ||
| } | ||
| ); | ||
|
|
||
| it.each([ | ||
| { status: 503, statusText: "The API is currently down" }, | ||
| { status: 422, statusText: "HTTP Error: Status 422 Unprocessable Entity" }, | ||
| ])( | ||
| "given the API returns a $status, throws an error", | ||
| async ({ status, statusText }) => { | ||
| // ARRANGE | ||
| const authorization = buildAuthorization({ | ||
| username: "mockUserName", | ||
| webApiKey: "mockWebApiKey", | ||
| }); | ||
|
|
||
| const mockResponse = `<html><body>${statusText}</body></html>`; | ||
|
|
||
| server.use( | ||
| http.get(`${apiBaseUrl}/API_GetGameLeaderboards.php`, () => | ||
| HttpResponse.json(mockResponse, { status, statusText }) | ||
| ) | ||
| ); | ||
|
|
||
| // ASSERT | ||
| await expect( | ||
| getGameLeaderboards(authorization, { gameId: mockGameId }) | ||
| ).rejects.toThrow(); | ||
| } | ||
| ); | ||
| }); | ||
|
|
||
| const mockGameId = "14402"; | ||
|
|
||
| const mockGetGameLeaderboardsResponse: GetGameLeaderboardsResponse = { | ||
| Count: 1, | ||
| Total: 1, | ||
| Results: [ | ||
| { | ||
| ID: 1234, | ||
| RankAsc: false, | ||
| Title: "South Island Conqueror", | ||
| Description: "Complete the game with the highest score possible.", | ||
| Format: "VALUE", | ||
| Author: "Example", | ||
| AuthorULID: "0123456789ABCDEFGHIJKLMNO", | ||
| State: "active", | ||
| TopEntry: { | ||
| User: "TopExample", | ||
| ULID: "ONMLKJIHGFEDCBA9876543210", | ||
| Score: 98_765, | ||
| FormattedScore: "98,765", | ||
| }, | ||
| }, | ||
| ], | ||
| }; | ||
|
|
||
| const mockGameLeaderboardsValue: GameLeaderboards = { | ||
| count: 1, | ||
| total: 1, | ||
| results: [ | ||
| { | ||
| id: 1234, | ||
| rankAsc: false, | ||
| title: "South Island Conqueror", | ||
| description: "Complete the game with the highest score possible.", | ||
| format: "VALUE", | ||
| author: "Example", | ||
| authorUlid: "0123456789ABCDEFGHIJKLMNO", | ||
| state: "active", | ||
| topEntry: { | ||
| user: "TopExample", | ||
| ulid: "ONMLKJIHGFEDCBA9876543210", | ||
| score: 98_765, | ||
| formattedScore: "98,765", | ||
| }, | ||
| }, | ||
| ], | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| import type { ID } from "../utils/internal"; | ||
| import { | ||
| apiBaseUrl, | ||
| buildRequestUrl, | ||
| call, | ||
| serializeProperties, | ||
| } from "../utils/internal"; | ||
| import type { AuthObject } from "../utils/public"; | ||
| import type { GameLeaderboards, GetGameLeaderboardsResponse } from "./models"; | ||
|
|
||
| /** | ||
| * A call to this function will retrieve a list of leaderboards for a | ||
| * given game ID. | ||
| * | ||
| * @param authorization An object containing your username and webApiKey. | ||
| * This can be constructed with `buildAuthorization()`. | ||
| * | ||
| * @param payload.gameId The ID of the game to retrieve leaderboards for. | ||
| * | ||
| * @param payload.offset The number of entries to skip. The API will default | ||
| * to 0 if the parameter is not specified. | ||
| * | ||
| * @param payload.count The number of entries to return. The API will | ||
| * default to 100 if the parameter is not specified. The max number | ||
| * of entries that can be returned is 500. | ||
| * | ||
| * @example | ||
| * ``` | ||
| * const gameLeaderboards = await getGameLeaderboards( | ||
| * authorization, | ||
| * { gameId: 14402 } | ||
| * ); | ||
| * ``` | ||
| * | ||
| * @returns An object containing a list of leaderboards that were created | ||
| * for the specified game. | ||
| * ```json | ||
| * { | ||
| * "count": 1, | ||
| * "total": 1, | ||
| * "results": [ | ||
| * { | ||
| * "id": 1234, | ||
| * "rankAsc": false, | ||
| * "title": "South Island Conqueror", | ||
| * "description": "Complete the game with the highest score possible.", | ||
| * "format": "VALUE", | ||
| * "author": "Example", | ||
| * "authorUlid": "0123456789ABCDEFGHIJKLMNO", | ||
| * "state": "active", | ||
| * "topEntry" : { | ||
| * "user": "TopExample", | ||
| * "ulid": "ONMLKJIHGFEDCBA9876543210", | ||
| * "score": 98765, | ||
| * "formattedScore": "98,765" | ||
| * } | ||
| * } | ||
| * ] | ||
| * } | ||
| * ``` | ||
| * | ||
| * @throws If the API was given invalid parameters (422) or if the | ||
| * API is currently down (503). | ||
| */ | ||
| export const getGameLeaderboards = async ( | ||
| authorization: AuthObject, | ||
| payload: { gameId: ID; offset?: number; count?: number } | ||
| ): Promise<GameLeaderboards> => { | ||
| const queryParams: Record<string, string | number> = {}; | ||
| queryParams.i = payload.gameId; | ||
| if (payload.offset !== null && payload.offset !== undefined) { | ||
| queryParams.o = payload.offset; | ||
| } | ||
| if (payload.count !== null && payload.count !== undefined) { | ||
| queryParams.c = payload.count; | ||
| } | ||
|
|
||
| const url = buildRequestUrl( | ||
| apiBaseUrl, | ||
| "/API_GetGameLeaderboards.php", | ||
| authorization, | ||
| queryParams | ||
| ); | ||
|
|
||
| const rawResponse = await call<GetGameLeaderboardsResponse>({ url }); | ||
|
|
||
| return serializeProperties(rawResponse, { | ||
| shouldCastToNumbers: ["ID", "Score"], | ||
| shouldMapToBooleans: ["RankAsc"], | ||
| }); | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| export * from "./getGameLeaderboards"; | ||
| export * from "./getLeaderboardEntries"; | ||
| export * from "./getUserGameLeaderboards"; | ||
| export * from "./models"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| export interface GameLeaderboards { | ||
| count: number; | ||
| total: number; | ||
| results: Array<{ | ||
| id: number; | ||
| rankAsc: boolean; | ||
| title: string; | ||
| description: string; | ||
| format: string; | ||
| author: string; | ||
| authorUlid: string; | ||
| state: "active" | "disabled" | "unpublished"; | ||
| topEntry?: { | ||
| user: string; | ||
| ulid: string; | ||
| score: number; | ||
| formattedScore: string; | ||
| }; | ||
| }>; | ||
| } |
20 changes: 20 additions & 0 deletions
20
src/leaderboard/models/get-game-leaderboards-response.model.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| export interface GetGameLeaderboardsResponse { | ||
| Count: number; | ||
| Total: number; | ||
| Results: Array<{ | ||
| ID: number; | ||
| RankAsc: boolean; | ||
| Title: string; | ||
| Description: string; | ||
| Format: string; | ||
| Author: string; | ||
| AuthorULID: string; | ||
| State: "active" | "disabled" | "unpublished"; | ||
| TopEntry?: { | ||
| User: string; | ||
| ULID: string; | ||
| Score: number; | ||
| FormattedScore: string; | ||
| }; | ||
| }>; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.