From 95f7f5064a57663918a18c0694f655b9b599c238 Mon Sep 17 00:00:00 2001 From: Geoffrey Allott Date: Thu, 8 Jun 2023 09:19:24 +0100 Subject: [PATCH] turn over pegging cards --- site/modules/cribbage.js | 34 +++++++++++++++++++++++----------- site/modules/gamechat.js | 4 +++- 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/site/modules/cribbage.js b/site/modules/cribbage.js index 004abf4..c6aa469 100644 --- a/site/modules/cribbage.js +++ b/site/modules/cribbage.js @@ -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; diff --git a/site/modules/gamechat.js b/site/modules/gamechat.js index 14eb32c..f37c764 100644 --- a/site/modules/gamechat.js +++ b/site/modules/gamechat.js @@ -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 = ""; } -- 2.34.1