UNPKG

shoehive

Version:

WebSocket-based multiplayer game framework for real-time, event-driven gameplay

141 lines (140 loc) 3.86 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Hand = void 0; /** * A hand of [Card](/api/interfaces/card/)s. * * ✅ Attribute Support * * This class manages a player's hand of cards, including their cards and attributes. * It provides methods for adding and removing cards, clearing the hand, and managing hand attributes. */ class Hand { constructor(id = 'main') { this.cards = []; this.attributes = new Map(); this.id = id; } /** * Adds a card to the hand. * * @param card The card to add. */ addCard({ card }) { this.cards.push(card); } /** * Removes a card from the hand. * * @param index The index of the card to remove. * @returns The removed card or null if the index is out of bounds. */ removeCard({ index }) { if (index < 0 || index >= this.cards.length) { return null; } return this.cards.splice(index, 1)[0]; } /** * Gets all cards in the hand. * * @returns A copy of the cards in the hand. */ getCards() { return [...this.cards]; } /** * Gets all visible cards in the hand. * * @returns A copy of the visible cards in the hand. */ getVisibleCards() { return this.cards.filter((card) => card.isVisible); } /** * Gets all hidden cards in the hand. * * @returns A copy of the hidden cards in the hand. */ getHiddenCards() { return this.cards.filter((card) => !card.isVisible); } /** * Clears the hand. * * @param deck Optional deck to discard cards to when clearing the hand */ clear({ deck } = {}) { if (deck) { // Add all cards to the discard pile for (const card of this.cards) { deck.addToDiscard({ card }); } } this.cards = []; } /** * Gets the ID of the hand. * * @returns The ID of the hand. */ getId() { return this.id; } /** * Sets an attribute on the hand. * * @param key The key of the attribute to set. * @param value The value of the attribute to set. */ setAttribute({ key, value }) { this.attributes.set(key, value); } /** * Gets an attribute from the hand. * * @param key The key of the attribute to get. * @returns The value of the attribute or null if the attribute does not exist. */ getAttribute({ key }) { return this.attributes.get(key); } /** * Checks if the hand has an attribute. * * @param key The key of the attribute to check. * @returns True if the attribute exists, false otherwise. */ hasAttribute({ key }) { return this.attributes.has(key); } /** * Returns a representation of the hand that is safe to send to clients. * Only includes visible cards and attributes. * * @returns A representation of the hand that is safe to send to clients. */ getVisibleState() { return { id: this.id, cards: this.getVisibleCards(), hiddenCardCount: this.getHiddenCards().length, attributes: Object.fromEntries(this.attributes.entries()), }; } /** * Returns a complete representation of the hand, including hidden cards. * This should ONLY be sent to the player who owns this hand. * * @returns A complete representation of the hand. */ getFullState() { return { id: this.id, cards: this.getCards(), hiddenCardCount: this.getHiddenCards().length, attributes: Object.fromEntries(this.attributes.entries()), }; } } exports.Hand = Hand;