UNPKG

card-factory

Version:

A comprehensive library for card manipulation

88 lines (87 loc) 2.95 kB
import { CardElement } from "../card/cardElement"; import Pile from "../pile/pile"; import { createDefaultOptions, pileElement } from "../pile/pileElement"; /** * A deck is all of the cards to be used in your game. * * Most Standard Playing Card games will only have one deck (52 cards) * * Players will have cards from this deck in their piles... draw piles, hands, discard piles, etc. Those are Piles. * * In a more advanced game, each player could be playing from separate decks, ex) Magic The Gathering * * Pass an argument of an array of Cards to build deck with cards added already. Or initiate a deck and use function addCards to populate cards. */ export default class Deck { _cards; _piles; _graveyard; _cardBuilder; pileElements; constructor(cards, cardBuilder = (card) => CardElement(card)) { this._cards = cards; this._piles = []; this._graveyard = new Pile("graveyard"); this._cardBuilder = cardBuilder; this.pileElements = []; } get cards() { return this._cards; } /** * @param cards must be a singular item of class Card or an Array of class Card * @returns card will be added into the deck */ addCards = (cards) => { if (Array.isArray(cards)) { cards.forEach((card) => { this._cards.push(card); }); } else { this.cards.push(cards); } return; }; /** * This will create a pile for draw piles, discard piles, hands. Anywhere cards can go! * @returns Piles */ createPile = (name, cards = []) => { const pile = new Pile(name, cards); this._piles.push(pile); return pile; }; createPileElement = (name, cards = [], options = {}) => { const mergedOptions = { ...createDefaultOptions(), ...{ cardElements: cards.map((card) => this._cardBuilder(card)) }, ...options, }; const pile = this.createPile(name, cards); const pileElem = pileElement(pile, this, mergedOptions); this.pileElements.push(pileElem); return pileElem; }; // just totally eliminates a card from existence /** * * @param card A singular item of class Card * @returns true if card was removed * @returns false if card was not found in deck */ removeCard = (card) => { this.cards.forEach((item) => { if (JSON.stringify(item) === JSON.stringify(card)) { const removalCard = this._cards.splice(this.cards.findIndex((index) => index === card), 1)[0]; this._piles.forEach((pile) => { if (pile.cards.includes(removalCard)) { pile.passCard(this._graveyard, removalCard); } }); return true; } }); return false; }; }