import { create_svg_element } from "./svg.js";
-import { card_href } from "./card.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 {
this.user_icons = new Map();
this.hands = new Map();
this.played = new Map();
+ this.passed = new Set();
+ this.count = 0;
this.scores = new Map();
this.community = [];
this.box = [];
this.take_initial_actions(actions);
}
+ able_to_pass(username) {
+ return this.active === this.username &&
+ this.community.length !== 0 &&
+ this.hands.has(this.username) &&
+ this.hands.get(this.username).length !== 0 &&
+ this.hands.get(this.username).every(card => card.card !== null && this.count + value_ace_low(card.card.rank) > 31);
+ }
+
redraw_players() {
const active_player = this.active;
- this.pass_control.classList.toggle("active", active_player === this.username && this.community.length !== 0 && this.hands.has(this.username) && this.hands.get(this.username).length !== 0);
+ this.pass_control.classList.toggle("active", this.able_to_pass());
for (const [username, [icon, score, active]] of this.user_icons) {
if (!this.seats.has(username)) {
this.svg.removeChild(icon);
}
}
+ next_active_player(username) {
+ console.log('Finding player after', username);
+ let active = username;
+ do {
+ active = this.player_after(active);
+ console.log('.. got ', active);
+ } while (active !== username && (this.passed.has(active) || this.hands.has(active) && this.hands.get(active).length === 0));
+ return active;
+ }
+
take_action(user_action) {
super.take_action(user_action);
switch (user_action.action.action) {
for (const card of this.box) {
this.svg.removeChild(card.image);
}
+ this.count = 0;
this.community = [];
this.box = [];
for (const [username, hand] of this.hands) {
}
}
this.hands.clear();
+ this.played.clear();
+ this.passed.clear();
this.active = this.player_after(user_action.username);
this.redraw_players();
break;
if (!this.scores.has(user_action.username)) {
this.scores.set(user_action.username, 0);
}
- this.set_info_text(user_action.username + " scores " + user_action.action.points + ": " + user_action.action.reason);
+ 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.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;
this.redraw_players();
break;
case "PlayCard":
+ this.count += value_ace_low(user_action.action.card.rank);
+ if (this.count === 31) {
+ this.passed.clear();
+ this.count = 0;
+ }
const played = this.remove_card(user_action.username, user_action.action.card);
if (played !== undefined) {
this.svg.removeChild(played.image);
image:this.card_image(user_action.username, user_action.action.card),
});
}
- /* fallthrough */
+ this.active = this.next_active_player(user_action.username);
+ this.redraw_cards();
+ this.redraw_players();
+ break;
case "Pass":
- this.active = user_action.username;
- do {
- this.active = this.player_after(this.active);
- } while (this.active !== user_action.username && this.hands.has(this.active) && this.hands.get(this.active).length === 0);
+ this.passed.add(user_action.username);
+ if (this.passed.size === this.hands.size) {
+ this.count = 0;
+ this.passed.clear();
+ }
+ this.active = this.next_active_player(user_action.username);
this.redraw_cards();
this.redraw_players();
break;