anonymise card dealt to box, even for the dealer
authorGeoffrey Allott <geoffrey@allott.email>
Mon, 12 Jun 2023 20:54:01 +0000 (21:54 +0100)
committerGeoffrey Allott <geoffrey@allott.email>
Mon, 12 Jun 2023 20:54:01 +0000 (21:54 +0100)
site/modules/chatroom.js
site/modules/cribbage.js
src/game/action.rs
src/game/cribbage/mod.rs

index 7911d01b1f0227162fd8c63d618376f8702ccd2e..1a75f0363372b19c89157f5e8938817b48234623 100644 (file)
@@ -137,6 +137,13 @@ export class Chatroom {
                     this.chat.append(this.info_element(user_action.username, "Puts " + card_name(user_action.action.card) + " in the box"));
                 }
                 break;
+            case "DealBox":
+                if (user_action.action.card === null) {
+                    this.chat.append(this.info_element(user_action.username, "Deals a card into the box"));
+                } else {
+                    this.chat.append(this.info_element(user_action.username, "Deals " + card_name(user_action.action.card) + " into the box"));
+                }
+                break;
             case "WinHand":
                 this.chat.append(this.info_element(
                     user_action.username,
index c6aa4698468900b295ab04ab37c92732548da8b5..cfc631634dfbfab00f9df40645d404078c970e3b 100644 (file)
@@ -297,15 +297,17 @@ export class Cribbage extends GameWithChat {
                 this.redraw_cards();
                 break;
             case "PutInBox":
-                if (!this.dealing) {
-                    const removed = this.remove_card(user_action.username, user_action.action.card);
-                    if (removed !== undefined) {
-                        this.svg.removeChild(removed.image);
-                    }
+                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 "DealBox":
+                this.box.push({card: null, image: this.card_image(null, null)});
+                this.redraw_cards();
+                break;
             case "CommunityCard":
                 const turnup = {
                     card: user_action.action.card,
index 23c2effbded56406ed8783b726a023bc7841e8cb..68da1c4a715c29c5f0c9417914ffad1385119197 100644 (file)
@@ -21,7 +21,12 @@ impl ValidatedUserAction {
         UserAction {
             timestamp: self.0.timestamp,
             username: self.0.username,
-            action: if username == self.0.username { self.0.action.clone() } else { self.0.action.anonymise() },
+            action: match &self.0.action {
+                Action::ReceiveCard { .. } if username != self.0.username => Action::ReceiveCard { card: None },
+                Action::PutInBox { .. } if username != self.0.username => Action::PutInBox { card: None },
+                Action::DealBox { .. } => Action::DealBox { card: None },
+                action => action.clone(),
+            }
         }
     }
 }
@@ -47,6 +52,7 @@ pub enum Action {
     CutCard { card: Card },
     PlayCard { card: Card },
     PutInBox { card: Option<Card> },
+    DealBox { card: Option<Card> },
     Pass,
     ChooseTrumps { suit: Suit },
     Fold,
@@ -64,16 +70,6 @@ pub enum Action {
     KnockedOut,
 }
 
-impl Action {
-    pub fn anonymise(&self) -> Self {
-        match self {
-            Action::ReceiveCard { .. } => Action::ReceiveCard { card: None },
-            Action::PutInBox { .. } => Action::PutInBox { card: None },
-            action => action.clone(),
-        }
-    }
-}
-
 #[derive(Debug, Copy, Clone, Serialize)]
 pub enum ActionError {
     NotAuthorised,
index d8138805c431ac07f21f6ea4e1b31e7d07449113..fc7dcee6936fc7f064a20ec3a2d1a92e4338d9f7 100644 (file)
@@ -290,7 +290,7 @@ impl Game for Cribbage {
                 self.state = State::Choosing;
                 Ok(())
             }
-            (State::Dealing, Action::PutInBox { card: Some(card) }) => {
+            (State::Dealing, Action::DealBox { card: Some(card) }) => {
                 self.deck.remove(&card);
                 self.box_cards.insert(card);
                 Ok(())
@@ -430,7 +430,7 @@ impl Game for Cribbage {
                 } else if let Some(username) = self.dealer {
                     if self.seats.players_len() == 3 && self.box_cards.is_empty() {
                         if let Some(card) = rng.choose_from(self.deck) {
-                            DealerAction::TakeAction(ValidatedUserAction(UserAction { timestamp, username, action: Action::PutInBox { card: Some(card) } }))
+                            DealerAction::TakeAction(ValidatedUserAction(UserAction { timestamp, username, action: Action::DealBox { card: Some(card) } }))
                         } else {
                             error!("Expected to deal a card but none were left in deck");
                             DealerAction::Leave
@@ -545,7 +545,7 @@ mod tests {
         let mut game = Cribbage::new(0, settings, seed);
         for action in actions {
             match action.action {
-                Action::Join { .. } | Action::PutInBox { .. } | Action::PlayCard { .. } | Action::Pass if game.state != State::Dealing => {
+                Action::Join { .. } | Action::PutInBox { .. } | Action::PlayCard { .. } | Action::Pass => {
                     let validated = game.validate_action(action.clone()).unwrap();
                     assert_eq!(ValidatedUserAction(action), validated);
                     game.take_action(validated).unwrap();
@@ -2295,7 +2295,7 @@ mod tests {
             {"timestamp":1686202533323,"username":"Peter","action":{"action":"ReceiveCard","card":{"rank":"Ten","suit":"Clubs"}}},
             {"timestamp":1686202533323,"username":"Geoff","action":{"action":"ReceiveCard","card":{"rank":"Nine","suit":"Diamonds"}}},
             {"timestamp":1686202533324,"username":"Aga","action":{"action":"ReceiveCard","card":{"rank":"Ace","suit":"Hearts"}}},
-            {"timestamp":1686202533324,"username":"Aga","action":{"action":"PutInBox","card":{"rank":"Nine","suit":"Clubs"}}},
+            {"timestamp":1686202533324,"username":"Aga","action":{"action":"DealBox","card":{"rank":"Nine","suit":"Clubs"}}},
             {"timestamp":1686202533324,"username":"Aga","action":{"action":"EndDeal"}}
         ]"#;