Skip to content
This repository has been archived by the owner on Feb 12, 2023. It is now read-only.

Commit

Permalink
feat: add getUserPoints() function
Browse files Browse the repository at this point in the history
  • Loading branch information
wescopeland committed Jan 19, 2023
1 parent 64d9c36 commit 1e8a329
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 25 deletions.
6 changes: 4 additions & 2 deletions src/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ export * from './php-responses/api-achievement.model';
export * from './php-responses/api-console-id.model';
export * from './php-responses/api-dated-achievement.model';
export * from './php-responses/api-game-info-and-user-progress.model';
export * from './php-responses/api-game-info.model';
export * from './php-responses/api-game-info-extended.model';
export * from './php-responses/api-game-info.model';
export * from './php-responses/api-game-list-entity.model';
export * from './php-responses/api-top-ten-user.model';
export * from './php-responses/api-user-game-completion.model';
export * from './php-responses/api-user-points.model';
export * from './php-responses/api-user-progress-for-game.model';
export * from './php-responses/api-user-rank-and-score.model';
export * from './php-responses/api-user-recently-played-game.model';
Expand All @@ -16,11 +17,12 @@ export * from './sanitized/achievement.model';
export * from './sanitized/console-id.model';
export * from './sanitized/dated-achievement.model';
export * from './sanitized/game-info-and-user-progress.model';
export * from './sanitized/game-info.model';
export * from './sanitized/game-info-extended.model';
export * from './sanitized/game-info.model';
export * from './sanitized/game-list-entity.model';
export * from './sanitized/top-ten-user.model';
export * from './sanitized/user-game-completion.model';
export * from './sanitized/user-points.model';
export * from './sanitized/user-progress-for-game.model';
export * from './sanitized/user-rank-and-score.model';
export * from './sanitized/user-recently-played-game.model';
Expand Down
8 changes: 3 additions & 5 deletions src/models/php-responses/api-user-game-completion.model.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
export interface ApiUserGameCompletion {
GameID: string;
ConsoleName: string;
ConsoleID: string;
ConsoleName: string;
ImageIcon: string;
Title: string;
MaxPossible: string;
'MAX(aw.HardcoreMode)': string;
NumAwarded: string;
NumAwardedHC: string;
MaxPossible: string;
PctWon: string;
PctWonHC: string;
HardcoreMode: string;
}
4 changes: 4 additions & 0 deletions src/models/php-responses/api-user-points.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface ApiUserPoints {
Points: number;
SoftcorePoints: number;
}
1 change: 0 additions & 1 deletion src/models/php-responses/api-user-summary.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ export interface ApiUserSummary {
TotalTruePoints: string;
Permissions: string;
Untracked: string;
ID: string;
UserWallActive: string;
Motto: string;
Rank: string;
Expand Down
4 changes: 1 addition & 3 deletions src/models/sanitized/user-game-completion.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ export interface UserGameCompletion {
imageIcon: string;
title: string;
maxPossible: number;
'max(awHardcoreMode)': number;
numAwarded: number;
numAwardedHc: number;
pctWon: number;
pctWonHc: number;
hardcoreMode: number;
}
4 changes: 4 additions & 0 deletions src/models/sanitized/user-points.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface UserPoints {
points: number;
softcorePoints: number;
}
1 change: 0 additions & 1 deletion src/models/sanitized/user-summary.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ export interface UserSummary {
totalTruePoints: number;
permissions: number;
untracked: number;
id: number;
userWallActive: number;
motto: string;
rank: number;
Expand Down
21 changes: 21 additions & 0 deletions src/retro-achievements-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,27 @@ export class RetroAchievementsClient {
}
}

async getUserPoints(userName: string): Promise<fromModels.UserPoints> {
const requestUrl = urlcat(this.baseUrl, 'API_GetUserPoints.php', {
...this.buildAuthParameters(),
u: userName
});

try {
const responseBody = await this.loadResponseBody<
fromModels.ApiUserPoints
>(requestUrl);

return camelcaseKeys(
sanitizeProps(responseBody)
) as fromModels.UserPoints;
} catch (err) {
throw new Error(
`RetroAchievements API: There was a problem retrieving the points stats for user ${userName}. ${err}`
);
}
}

async getAchievementDistributionForGameId(
gameId: number,
isHardcoreOnly: boolean
Expand Down
45 changes: 32 additions & 13 deletions test/retro-achievements-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -648,11 +648,9 @@ describe('RetroAchievementsClient', () => {
ImageIcon: '/Images/042133.png',
Title: 'Mortal Kombat 4',
MaxPossible: '131',
'MAX(aw.HardcoreMode)': '1',
NumAwarded: '131',
NumAwardedHC: '131',
PctWon: '1.0000',
PctWonHC: '1.0000'
HardcoreMode: '1'
},
{
GameID: '10116',
Expand All @@ -661,11 +659,9 @@ describe('RetroAchievementsClient', () => {
ImageIcon: '/Images/042459.png',
Title: 'Mortal Kombat 4',
MaxPossible: '130',
'MAX(aw.HardcoreMode)': '1',
NumAwarded: '130',
NumAwardedHC: '130',
PctWon: '1.0000',
PctWonHC: '1.0000'
HardcoreMode: '0'
}
];

Expand Down Expand Up @@ -695,11 +691,9 @@ describe('RetroAchievementsClient', () => {
imageIcon: '/Images/042133.png',
title: 'Mortal Kombat 4',
maxPossible: 131,
'max(awHardcoreMode)': 1,
numAwarded: 131,
numAwardedHc: 131,
pctWon: 1,
pctWonHc: 1
hardcoreMode: 1
},
{
gameId: 10116,
Expand All @@ -708,11 +702,9 @@ describe('RetroAchievementsClient', () => {
imageIcon: '/Images/042459.png',
title: 'Mortal Kombat 4',
maxPossible: 130,
'max(awHardcoreMode)': 1,
numAwarded: 130,
numAwardedHc: 130,
pctWon: 1,
pctWonHc: 1
hardcoreMode: 0
}
]);
});
Expand Down Expand Up @@ -1086,7 +1078,6 @@ describe('RetroAchievementsClient', () => {
TotalTruePoints: '31058',
Permissions: '1',
Untracked: '0',
ID: '117089',
UserWallActive: '1',
Motto: '',
Rank: '4674',
Expand Down Expand Up @@ -1208,4 +1199,32 @@ describe('RetroAchievementsClient', () => {
]);
});
});

describe('getUserPoints', () => {
it('returns points stats for the given username', async () => {
// ARRANGE
const mockUserPoints: fromModels.ApiUserPoints = {
Points: 10_000,
SoftcorePoints: 25
};

server = setupServer(
rest.get(
'https://retroachievements.org/API/API_GetUserPoints.php',
(_, res, ctx) => {
return res(ctx.json(mockUserPoints));
}
)
);

server.listen();

// ACT
const userPoints = await client.getUserPoints('xelnia');

// ASSERT
expect(userPoints.points).toEqual(10_000);
expect(userPoints.softcorePoints).toEqual(25);
});
});
});

0 comments on commit 1e8a329

Please sign in to comment.