@vlad-yakovlev/poker
Version:
Texas Hold'em poker library
69 lines (68 loc) • 1.93 kB
JavaScript
import { Combination } from './Combination.js';
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export class Player {
room;
id;
cards;
balance;
betAmount;
hasFolded;
hasLost;
hasTurned;
payload;
constructor(room, playerData) {
this.room = room;
this.id = playerData.id;
this.cards = playerData.cards;
this.balance = playerData.balance;
this.betAmount = playerData.betAmount;
this.hasFolded = playerData.hasFolded;
this.hasLost = playerData.hasLost;
this.hasTurned = playerData.hasTurned;
this.payload = playerData.payload;
}
/**
* Best combination built from player and room cards
*/
// TODO: memoize
get bestCombination() {
return Combination.getBest([...this.room.cards, ...this.cards]);
}
/**
* Amount that player should pay to call
*/
get callAmount() {
return this.room.requiredBetAmount - this.betAmount;
}
get minRaiseAmount() {
return this.room.baseBetAmount;
}
get maxRaiseAmount() {
return this.balance - this.callAmount;
}
get canFold() {
return !this.hasLost && !this.hasFolded && !!this.balance;
}
get canCheck() {
return (!this.hasLost && !this.hasFolded && !!this.balance && !this.callAmount);
}
get canCall() {
return (!this.hasLost &&
!this.hasFolded &&
!!this.callAmount &&
this.callAmount <= this.balance);
}
get canRaise() {
return (!this.hasLost &&
!this.hasFolded &&
this.minRaiseAmount <= this.maxRaiseAmount);
}
get canAllIn() {
return !this.hasLost && !this.hasFolded && !!this.balance;
}
increaseBet(amount) {
amount = Math.min(amount, this.balance);
this.betAmount += amount;
this.balance -= amount;
}
}