Skip to content

Commit

Permalink
Add results fetch to match abstract
Browse files Browse the repository at this point in the history
  • Loading branch information
williamgrosset committed Feb 11, 2024
1 parent fd199ce commit f0986fb
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 3 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,9 @@ new Duel(options?: Options)
#### Methods

```ts
addPlayer(player: Player)
addPlayer(player: Player): void
calculate(playerId: string): Results
getResults(): Results
```

#### Description
Expand All @@ -210,8 +211,9 @@ new FreeForAll(options?: Options)
#### Methods

```ts
addPlayer(player: Player)
addPlayer(player: Player): void
calculate(playerIds: string[]): Results
getResults(): Results
```

#### Description
Expand All @@ -233,8 +235,9 @@ new TeamDuel(options?: Options)
#### Methods

```ts
addTeam(team: Team)
addTeam(team: Team): void
calculate(teamId: string): Results
getResults(): Results
```

#### Description
Expand Down
1 change: 1 addition & 0 deletions src/lib/match/error/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
37 changes: 37 additions & 0 deletions src/lib/match/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { Match } from '.'
import { Player } from '../../main/contestants/player'
import { ErrorType } from './error'

class TestMatch extends Match {
constructor() {
super()
}

calculate(playerId: string) {
this.completed = true
return []
}
}
Expand All @@ -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)
})
})
8 changes: 8 additions & 0 deletions src/lib/match/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit f0986fb

Please sign in to comment.