From 261c027198e30595fb0d6d39ba018eb41a9c7d54 Mon Sep 17 00:00:00 2001 From: Geoffrey Allott Date: Sat, 6 Mar 2021 10:44:30 +0000 Subject: [PATCH] implement ghost logic for site as well --- site/modules/poker.js | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/site/modules/poker.js b/site/modules/poker.js index 8606470..7696e26 100644 --- a/site/modules/poker.js +++ b/site/modules/poker.js @@ -13,6 +13,7 @@ export class TexasHoldEm { this.stacks = new Map(); this.hands = new Map(); this.bets = new Map(); + this.ghosts = new Map(); this.pot = 0; this.community = []; this.user_icons = new Map(); @@ -243,9 +244,10 @@ export class TexasHoldEm { chips_to_call() { const max = Math.max(...this.bets.values()); + const stack = this.stacks.get(this.username) || 0; const bet = this.bets.get(this.username) || 0; if (max >= 0) { - return max - bet; + return Math.min(max - bet, stack); } else { return 0; } @@ -259,6 +261,16 @@ export class TexasHoldEm { } } + remove_ghosts() { + for (const [ghost, turns] of this.ghosts) { + if (turns === 0) { + this.seats.delete(ghost); + } else { + this.ghosts.set(ghost, turns - 1); + } + } + } + take_action(user_action) { switch (user_action.action.action) { case "Join": @@ -279,7 +291,6 @@ export class TexasHoldEm { this.redraw_players(); break; case "KnockedOut": - this.seats.delete(user_action.username); this.stacks.delete(user_action.username); if (this.hands.has(user_action.username)) { for (const card of this.hands.get(user_action.username)) { @@ -288,13 +299,24 @@ export class TexasHoldEm { } this.hands.delete(user_action.username); this.bets.delete(user_action.username); - if (this.active === user_action.username) { - this.active = null; + const small_blind = this.player_after(this.dealer, () => true); + const big_blind = this.player_after(small_blind, () => true); + if (this.stacks.length <= 1) { + this.seats.delete(user_action.username); + } else if (user_action.username === this.dealer) { + this.ghosts.set(user_action.username, 0); + } else if (user_action.username === small_blind) { + this.ghosts.set(user_action.username, 1); + } else if (user_action.username === big_blind) { + this.ghosts.set(user_action.username, 2); + } else { + this.seats.delete(user_action.username); } this.redraw_cards(); this.redraw_players(); break; case "NextToDeal": + this.remove_ghosts(); this.dealer = user_action.username; for (const card of this.community) { this.svg.removeChild(card.image); -- 2.34.1