implement ghost logic for site as well
authorGeoffrey Allott <geoffrey@allott.email>
Sat, 6 Mar 2021 10:44:30 +0000 (10:44 +0000)
committerGeoffrey Allott <geoffrey@allott.email>
Sat, 6 Mar 2021 13:32:12 +0000 (13:32 +0000)
site/modules/poker.js

index 860647001fe4e5879e706e0f75e5ace8d5396401..7696e26c78786af4ca3a63915321f501ba8ccb5a 100644 (file)
@@ -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);