turn over pegging cards
authorGeoffrey Allott <geoffrey@allott.email>
Thu, 8 Jun 2023 08:19:24 +0000 (09:19 +0100)
committerGeoffrey Allott <geoffrey@allott.email>
Thu, 8 Jun 2023 08:19:24 +0000 (09:19 +0100)
site/modules/cribbage.js
site/modules/gamechat.js

index 004abf4b638ca7c88fc6ef568ab7d3d2f6fa5e2a..c6aa4698468900b295ab04ab37c92732548da8b5 100644 (file)
@@ -1,7 +1,6 @@
 import { create_svg_element } from "./svg.js";
 import { card_href, value_ace_low } from "./card.js";
 import { GameWithChat } from "./gamechat.js";
-import { break_lines } from "./words.js";
 import { CongratulateWinner } from "./winner.js";
 
 export class Cribbage extends GameWithChat {
@@ -20,6 +19,7 @@ export class Cribbage extends GameWithChat {
 
         this.active = null;
         this.dealer = null;
+        this.dealing = false;
 
         const table = create_svg_element(this.svg, "ellipse", [], [["cx", "250"], ["cy", "253"], ["rx", "250"], ["ry", "110"], ["fill", "#604010"]]);
         const felt = create_svg_element(this.svg, "ellipse", [], [["cx", "250"], ["cy", "250"], ["rx", "240"], ["ry", "100"], ["fill", "green"]]);
@@ -183,6 +183,17 @@ export class Cribbage extends GameWithChat {
         return active;
     }
 
+    clear_pegging() {
+        this.count = 0;
+        this.passed.clear();
+        for (const [username, hand] of this.played) {
+            for (const card of hand) {
+                this.svg.removeChild(card.image);
+                card.image = this.card_image(username, null);
+            }
+        }
+    }
+
     take_action(user_action) {
         super.take_action(user_action);
         switch (user_action.action.action) {
@@ -199,6 +210,7 @@ export class Cribbage extends GameWithChat {
                 this.redraw_players();
                 break;
             case "NextToDeal":
+                this.dealing = true;
                 this.dealer = user_action.username;
                 for (const card of this.community) {
                     this.svg.removeChild(card.image);
@@ -235,8 +247,7 @@ export class Cribbage extends GameWithChat {
                 if (!this.scores.has(user_action.username)) {
                     this.scores.set(user_action.username, 0);
                 }
-                const info_text = user_action.username + " scores " + user_action.action.points + ": " + user_action.action.reason;
-                this.set_info_text(break_lines(info_text, 65));
+                this.set_info_text(user_action.username + " scores " + user_action.action.points + ": " + user_action.action.reason);
                 this.scores.set(user_action.username, this.scores.get(user_action.username) + user_action.action.points);
                 if ([...this.hands.values()].every(hand => hand.length === 0)) {
                     this.hands = this.played;
@@ -255,8 +266,7 @@ export class Cribbage extends GameWithChat {
             case "PlayCard":
                 this.count += value_ace_low(user_action.action.card.rank);
                 if (this.count === 31) {
-                    this.passed.clear();
-                    this.count = 0;
+                    this.clear_pegging();
                 }
                 const played = this.remove_card(user_action.username, user_action.action.card);
                 if (played !== undefined) {
@@ -276,8 +286,7 @@ export class Cribbage extends GameWithChat {
             case "Pass":
                 this.passed.add(user_action.username);
                 if (this.passed.size === this.hands.size) {
-                    this.count = 0;
-                    this.passed.clear();
+                    this.clear_pegging();
                 }
                 this.active = this.next_active_player(user_action.username);
                 this.redraw_cards();
@@ -288,11 +297,13 @@ export class Cribbage extends GameWithChat {
                 this.redraw_cards();
                 break;
             case "PutInBox":
-                const removed = this.remove_card(user_action.username, user_action.action.card);
-                if (removed !== undefined) {
-                    this.svg.removeChild(removed.image);
-                    this.box.push({card: null, image: this.card_image(null, null)});
+                if (!this.dealing) {
+                    const removed = this.remove_card(user_action.username, user_action.action.card);
+                    if (removed !== undefined) {
+                        this.svg.removeChild(removed.image);
+                    }
                 }
+                this.box.push({card: null, image: this.card_image(null, null)});
                 this.redraw_cards();
                 break;
             case "CommunityCard":
@@ -306,6 +317,7 @@ export class Cribbage extends GameWithChat {
                 this.redraw_players();
                 break;
             case "EndDeal":
+                this.dealing = false;
                 this.active = user_action.username;
                 this.redraw_players();
                 break;
index 14eb32c1fe8ccc7d6cc2171cdbcb1b39b4cf8b44..f37c764cc7d9c633e78c931df132da53af01b9ca 100644 (file)
@@ -1,6 +1,7 @@
 import { create_svg_element } from "./svg.js";
 import { Game } from "./game.js";
 import { Chatroom } from "./chatroom.js";
+import { break_lines } from "./words.js";
 
 export class GameWithChat extends Game {
     constructor(container, summary, actions, username, send, close) {
@@ -43,7 +44,8 @@ export class GameWithChat extends Game {
 
     set_text(style, text) {
         this.error.setAttribute("class", style);
-        const lines = text.split("\n", this.error_text.length);
+        const MAX_LINE_LENGTH = 65;
+        const lines = break_lines(text, MAX_LINE_LENGTH).split("\n", this.error_text.length);
         for (const text of this.error_text) {
             text.textContent = "";
         }