Skip to content

Commit

Permalink
fix(players): ignore TF2 in-game hours verification failure when the …
Browse files Browse the repository at this point in the history
…verification is not needed (#477)
  • Loading branch information
garrappachc authored Jun 30, 2020
1 parent 18324a2 commit ae2ffc4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
22 changes: 22 additions & 0 deletions src/players/services/players.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ import { SteamApiService } from './steam-api.service';
import { DiscordService } from '@/discord/services/discord.service';
import { ObjectId } from 'mongodb';

jest.mock('@configs/players', () => ({
minimumTf2InGameHours: 500,
requireEtf2lAccount: true,
}));
import { minimumTf2InGameHours } from '@configs/players';

jest.mock('@/discord/services/discord.service');

class EnvironmentStub {
Expand Down Expand Up @@ -259,6 +265,22 @@ describe('PlayersService', () => {
it('should deny', async () => {
await expect(service.createPlayer(mockSteamProfile)).rejects.toThrowError('cannot verify in-game hours for TF2');
});

describe('and nobody cares', () => {
beforeEach(() => {
// @ts-ignore
minimumTf2InGameHours = 0;
});

afterEach(() => {
// @ts-ignore
minimumTf2InGameHours = 500;
});

it('should pass', async () => {
await service.createPlayer(mockSteamProfile);
});
});
});
});

Expand Down
20 changes: 16 additions & 4 deletions src/players/services/players.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,7 @@ export class PlayersService {
}

async createPlayer(steamProfile: SteamProfile): Promise<DocumentType<Player>> {
const hoursInTf2 = await this.steamApiService.getTf2InGameHours(steamProfile.id);
if (hoursInTf2 < minimumTf2InGameHours) {
throw new Error('not enough tf2 hours');
}
await this.verifyTf2InGameHours(steamProfile.id);

let etf2lProfile: Etf2lProfile;
let name = steamProfile.displayName;
Expand Down Expand Up @@ -178,4 +175,19 @@ export class PlayersService {
return { player: playerId, gamesPlayed, classesPlayed };
}

private async verifyTf2InGameHours(steamId: string) {
try {
const hoursInTf2 = await this.steamApiService.getTf2InGameHours(steamId);
if (hoursInTf2 < minimumTf2InGameHours) {
throw new Error('not enough tf2 hours');
}
} catch (e) {
if (e.message === 'cannot verify in-game hours for TF2' && minimumTf2InGameHours <= 0) {
return;
} else {
throw e;
}
}
}

}

0 comments on commit ae2ffc4

Please sign in to comment.