the-game-domain
Version:
Complete domain layer for 'The Game' card game with rich business entities, automatic persistence, action tracking, and advanced game mechanics. Features Game, Player, Card, Deck, Pile entities with repository pattern, turn management, victory/defeat dete
135 lines • 4.11 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Player = void 0;
const Card_1 = require("./Card");
const uuid_1 = require("../utils/uuid");
class Player {
constructor(name) {
this._id = (0, uuid_1.generateUUID)();
this._cards = [];
this._isCurrentTurn = false;
if (!name || name.trim() === '') {
Player.playerCounter++;
this._name = `Player ${Player.playerCounter}`;
}
else {
this._name = name.trim();
}
this._validatePlayer();
}
get id() {
return this._id;
}
get name() {
return this._name;
}
get cards() {
return [...this._cards];
}
get isCurrentTurn() {
return this._isCurrentTurn;
}
get cardCount() {
return this._cards.length;
}
get isHandFull() {
return this._cards.length >= Player.MAX_CARDS;
}
_validatePlayer() {
}
changeName(newName) {
if (!newName || newName.trim() === '') {
throw new Error('Player name cannot be empty');
}
this._name = newName.trim();
}
addCard(card) {
if (this.isHandFull) {
throw new Error(`Cannot add card: player hand is full (maximum ${Player.MAX_CARDS} cards)`);
}
this._cards.push(card);
}
addCards(cards) {
if (this._cards.length + cards.length > Player.MAX_CARDS) {
throw new Error(`Cannot add cards: would exceed maximum limit of ${Player.MAX_CARDS} cards`);
}
this._cards.push(...cards);
}
removeCard(cardValue) {
const cardIndex = this._cards.findIndex(card => card.value === cardValue);
if (cardIndex === -1) {
return undefined;
}
const [removedCard] = this._cards.splice(cardIndex, 1);
return removedCard;
}
clearCards() {
this._cards = [];
}
hasCard(cardValue) {
return this._cards.some(card => card.value === cardValue);
}
drawCardFromDeck(deck) {
if (this.isHandFull) {
throw new Error(`Cannot draw card: player hand is full (maximum ${Player.MAX_CARDS} cards)`);
}
const drawnCard = deck.drawCard();
if (drawnCard) {
this._cards.push(drawnCard);
}
return drawnCard;
}
setCurrentTurn(isCurrentTurn) {
this._isCurrentTurn = isCurrentTurn;
}
endTurn(players) {
if (!this._isCurrentTurn) {
throw new Error(`Player ${this._id} is not in current turn`);
}
if (players.length === 0) {
throw new Error('No players in the game');
}
this._isCurrentTurn = false;
const currentIndex = players.findIndex(p => p.id === this._id);
if (currentIndex === -1) {
throw new Error(`Player ${this._id} not found in players list`);
}
const nextIndex = (currentIndex + 1) % players.length;
const nextPlayer = players[nextIndex];
if (!nextPlayer) {
throw new Error('Next player not found');
}
nextPlayer.setCurrentTurn(true);
return nextPlayer.id;
}
equals(other) {
return this._id === other._id;
}
toString() {
return `Player(${this._id}, ${this._name}, ${this._cards.length} cards)`;
}
toJSON() {
return {
id: this._id,
name: this._name,
cards: this._cards.map(card => card.toJSON()),
isCurrentTurn: this._isCurrentTurn
};
}
static fromJSON(data) {
const cards = data.cards.map(cardData => Card_1.Card.fromJSON(cardData));
const player = Object.create(Player.prototype);
player._id = data.id;
player._name = data.name;
player._cards = cards;
player._isCurrentTurn = data.isCurrentTurn;
return player;
}
static resetCounter() {
Player.playerCounter = 0;
}
}
exports.Player = Player;
Player.MAX_CARDS = 6;
Player.playerCounter = 0;
//# sourceMappingURL=Player.js.map