diff --git a/README.md b/README.md index 772076b..04ca515 100644 --- a/README.md +++ b/README.md @@ -185,8 +185,9 @@ new Duel(options?: Options) #### Methods ```ts -addPlayer(player: Player) +addPlayer(player: Player): void calculate(playerId: string): Results +getResults(): Results ``` #### Description @@ -210,8 +211,9 @@ new FreeForAll(options?: Options) #### Methods ```ts -addPlayer(player: Player) +addPlayer(player: Player): void calculate(playerIds: string[]): Results +getResults(): Results ``` #### Description @@ -233,8 +235,9 @@ new TeamDuel(options?: Options) #### Methods ```ts -addTeam(team: Team) +addTeam(team: Team): void calculate(teamId: string): Results +getResults(): Results ``` #### Description diff --git a/src/lib/match/error/index.ts b/src/lib/match/error/index.ts index f82d6de..6caf8e6 100644 --- a/src/lib/match/error/index.ts +++ b/src/lib/match/error/index.ts @@ -4,6 +4,7 @@ const ERROR_NAME = 'MATCH_ERROR' export const ErrorType = { MATCH_COMPLETE: 'Match completed', + MATCH_INCOMPLETE: 'Match incomplete', MAX_PLAYERS: 'Cannot add more players', MIN_PLAYERS: 'Need more players', MAX_TEAMS: 'Cannot add more teams', diff --git a/src/lib/match/index.test.ts b/src/lib/match/index.test.ts index 6bff360..3c19b51 100644 --- a/src/lib/match/index.test.ts +++ b/src/lib/match/index.test.ts @@ -1,5 +1,6 @@ import { Match } from '.' import { Player } from '../../main/contestants/player' +import { ErrorType } from './error' class TestMatch extends Match { constructor() { @@ -7,6 +8,7 @@ class TestMatch extends Match { } calculate(playerId: string) { + this.completed = true return [] } } @@ -22,4 +24,39 @@ describe('Match', () => { expect(match.contestants.size).toBe(2) }) + + test('gets results of match', () => { + const player1 = new Player('1', 1000) + const player2 = new Player('2', 900) + const match = new TestMatch() + + match.addContestant(player1) + match.addContestant(player2) + match.calculate('1') + + const results = match.getResults() + + expect(results).toStrictEqual([ + { + id: '1', + elo: 1000 + }, + { + id: '2', + elo: 900 + } + ]) + }) + + test('throws error if getting results before match calculation', () => { + expect(() => { + const player1 = new Player('1', 1000) + const player2 = new Player('2', 900) + const match = new TestMatch() + + match.addContestant(player1) + match.addContestant(player2) + match.getResults() + }).toThrow(ErrorType.MATCH_INCOMPLETE) + }) }) diff --git a/src/lib/match/index.ts b/src/lib/match/index.ts index 5e2defd..6de8c56 100644 --- a/src/lib/match/index.ts +++ b/src/lib/match/index.ts @@ -93,6 +93,14 @@ export abstract class Match { return elos } + getResults(): Results { + if (!this._completed) { + throw new MatchError(ErrorType.MATCH_INCOMPLETE) + } + + return this.contestantMapToResults() + } + addContestant(contestant: Contestant) { if (this._completed) { throw new MatchError(ErrorType.MATCH_COMPLETE)