handle dealer PutInBox correctly
authorGeoffrey Allott <geoffrey@allott.email>
Thu, 8 Jun 2023 08:18:59 +0000 (09:18 +0100)
committerGeoffrey Allott <geoffrey@allott.email>
Thu, 8 Jun 2023 08:18:59 +0000 (09:18 +0100)
src/game/cribbage/mod.rs

index 1dcd1ced134210fee9c69ab039b2cb27cf6cd491..d8138805c431ac07f21f6ea4e1b31e7d07449113 100644 (file)
@@ -290,6 +290,11 @@ impl Game for Cribbage {
                 self.state = State::Choosing;
                 Ok(())
             }
+            (State::Dealing, Action::PutInBox { card: Some(card) }) => {
+                self.deck.remove(&card);
+                self.box_cards.insert(card);
+                Ok(())
+            }
             (State::Choosing, Action::PutInBox { card: Some(card) }) => {
                 if let Some(hand) = self.hands.get_mut(&username) {
                     hand.remove(&card);
@@ -540,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 => {
+                Action::Join { .. } | Action::PutInBox { .. } | Action::PlayCard { .. } | Action::Pass if game.state != State::Dealing => {
                     let validated = game.validate_action(action.clone()).unwrap();
                     assert_eq!(ValidatedUserAction(action), validated);
                     game.take_action(validated).unwrap();
@@ -2264,4 +2269,43 @@ mod tests {
 
         test_game(actions, settings, seed);
     }
+
+    #[test]
+    fn three_player_cribbage() {
+        let actions = r#"[
+            {"timestamp":1686202473466,"username":"Geoff","action":{"action":"Join","seat":0,"chips":0}},
+            {"timestamp":1686202505067,"username":"Aga","action":{"action":"Join","seat":1,"chips":0}},
+            {"timestamp":1686202533315,"username":"Peter","action":{"action":"Join","seat":2,"chips":0}},
+            {"timestamp":1686202533316,"username":"Aga","action":{"action":"NextToDeal"}},
+            {"timestamp":1686202533316,"username":"Peter","action":{"action":"ReceiveCard","card":{"rank":"Five","suit":"Hearts"}}},
+            {"timestamp":1686202533317,"username":"Geoff","action":{"action":"ReceiveCard","card":{"rank":"Jack","suit":"Diamonds"}}},
+            {"timestamp":1686202533318,"username":"Aga","action":{"action":"ReceiveCard","card":{"rank":"Five","suit":"Spades"}}},
+            {"timestamp":1686202533319,"username":"Peter","action":{"action":"ReceiveCard","card":{"rank":"Jack","suit":"Hearts"}}},
+            {"timestamp":1686202533319,"username":"Geoff","action":{"action":"ReceiveCard","card":{"rank":"Nine","suit":"Spades"}}},
+            {"timestamp":1686202533320,"username":"Aga","action":{"action":"ReceiveCard","card":{"rank":"Three","suit":"Clubs"}}},
+            {"timestamp":1686202533320,"username":"Peter","action":{"action":"ReceiveCard","card":{"rank":"Four","suit":"Hearts"}}},
+            {"timestamp":1686202533320,"username":"Geoff","action":{"action":"ReceiveCard","card":{"rank":"Queen","suit":"Diamonds"}}},
+            {"timestamp":1686202533321,"username":"Aga","action":{"action":"ReceiveCard","card":{"rank":"Seven","suit":"Clubs"}}},
+            {"timestamp":1686202533321,"username":"Peter","action":{"action":"ReceiveCard","card":{"rank":"Eight","suit":"Spades"}}},
+            {"timestamp":1686202533321,"username":"Geoff","action":{"action":"ReceiveCard","card":{"rank":"Five","suit":"Clubs"}}},
+            {"timestamp":1686202533322,"username":"Aga","action":{"action":"ReceiveCard","card":{"rank":"Three","suit":"Diamonds"}}},
+            {"timestamp":1686202533322,"username":"Peter","action":{"action":"ReceiveCard","card":{"rank":"King","suit":"Clubs"}}},
+            {"timestamp":1686202533322,"username":"Geoff","action":{"action":"ReceiveCard","card":{"rank":"Six","suit":"Clubs"}}},
+            {"timestamp":1686202533323,"username":"Aga","action":{"action":"ReceiveCard","card":{"rank":"Four","suit":"Diamonds"}}},
+            {"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":"EndDeal"}}
+        ]"#;
+
+        let actions = serde_json::from_str(actions).unwrap();
+
+        let settings = r#"{"format":"Cribbage","title":"Based Business","max_players":3,"target_score":121,"start_time":null}"#;
+        let settings = serde_json::from_str(settings).unwrap();
+        let seed = r#"{"rng":"ChaCha8","seed":"983c249eac8299ca583f8826898e0e1eb87b70787fb39235bc4953a92c28db41"}"#;
+        let seed = serde_json::from_str(seed).unwrap();
+
+        test_game(actions, settings, seed);
+    }
 }