34 lines
784 B
TypeScript
34 lines
784 B
TypeScript
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);
|
|
}
|
|
}
|