You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
33 lines
784 B
33 lines
784 B
import { Game } from "./game.js"; |
|
import { GameError } from "./errors/game_error.js"; |
|
import { NotParticipatingError } from "./errors/not_participating_error.js"; |
|
|
|
export class Player { |
|
public games: Game[] = []; |
|
private rollCount = 0; |
|
|
|
constructor(public playerId: number) {} |
|
|
|
get rolls() { |
|
return this.rollCount; |
|
} |
|
|
|
join(game: Game) { |
|
if (this.games.includes(game)) { |
|
throw new GameError( |
|
`Player: ${this.playerId} attempted to join the same game multiple ` + |
|
`times` |
|
); |
|
} |
|
this.games.push(game); |
|
} |
|
|
|
rollDice(game: Game, roll: number) { |
|
if (!this.games.includes(game)) { |
|
throw new NotParticipatingError(game.gameId, this.playerId, "roll dice"); |
|
} |
|
|
|
this.rollCount++; |
|
game.rollDice(this, roll); |
|
} |
|
}
|
|
|