shoehive
Version:
WebSocket-based multiplayer game framework for real-time, event-driven gameplay
170 lines (169 loc) • 4.69 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Seat = void 0;
const card_1 = require("./card");
/**
* Represents a seat at a table.
*
* ✅ Attribute Support
*
* This class manages a player's seat at a table, including their hands and attributes.
* It provides methods for setting and getting player information, adding and removing hands,
* clearing hands, and managing seat attributes.
*/
class Seat {
/**
* Creates a new seat with a main hand by default.
*/
constructor() {
this.player = null;
this.hands = new Map();
this.attributes = new Map();
// Initialize with a main hand by default
this.hands.set('main', new card_1.Hand('main'));
}
/**
* Sets the player for the seat.
*
* @param player The player to set for the seat.
*/
setPlayer({ player }) {
this.player = player;
}
/**
* Gets the player for the seat.
*
* @returns The player for the seat or null if no player is set.
*/
getPlayer() {
return this.player;
}
/**
* Adds a hand to the seat.
*
* @param handId The ID of the hand to add.
* @returns True if the hand was added, false if it already exists.
*/
addHand({ handId }) {
if (this.hands.has(handId)) {
return false;
}
this.hands.set(handId, new card_1.Hand(handId));
return true;
}
/**
* Removes a hand from the seat.
*
* @param handId The ID of the hand to remove.
* @returns True if the hand was removed, false if it does not exist.
*/
removeHand({ handId }) {
if (handId === 'main') {
return false; // Cannot remove the main hand
}
return this.hands.delete(handId);
}
/**
* Gets a hand from the seat.
*
* @param handId The ID of the hand to get.
* @returns The hand or null if it does not exist.
*/
getHand({ handId = 'main' } = {}) {
return this.hands.get(handId) || null;
}
/**
* Gets all hands from the seat.
*
* @returns A map of all hands.
*/
getAllHands() {
return new Map(this.hands);
}
/**
* Clears a hand from the seat.
*
* @param handId The ID of the hand to clear.
* @returns True if the hand was cleared, false if it does not exist.
*/
clearHand({ handId = 'main' } = {}) {
const hand = this.hands.get(handId);
if (!hand)
return false;
hand.clear();
return true;
}
/**
* Clears all hands from the seat.
*/
clearAllHands() {
for (const [_, hand] of this.hands) {
hand.clear();
}
}
/**
* Sets an attribute on the seat.
*
* @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 seat.
*
* @param key The key of the attribute to get.
* @returns The value of the attribute or null if it does not exist.
*/
getAttribute({ key }) {
return this.attributes.get(key);
}
/**
* Checks if the seat 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);
}
/**
* Gets all attributes from the seat.
*
* @returns A record of all attributes.
*/
getAttributes() {
return Object.fromEntries(this.attributes.entries());
}
/**
* Sets multiple attributes on the seat.
*
* @param attributes A record of attributes to set.
*/
setAttributes({ attributes }) {
for (const [key, value] of Object.entries(attributes)) {
this.attributes.set(key, value);
}
}
/**
* Removes an attribute from the seat.
*
* @param key The key of the attribute to remove.
*/
removeAttribute({ key }) {
this.attributes.delete(key);
}
/**
* Checks if a player can modify a seat. This enforces
* that only the player who owns the seat can modify it.
*
* @param playerId The ID of the player to check.
* @returns True if the player can modify the seat, false otherwise.
*/
canPlayerModify({ playerId }) {
// Only the player who owns the seat can modify their hand
return this.player !== null && this.player.id === playerId;
}
}
exports.Seat = Seat;