aboutsummaryrefslogtreecommitdiff
path: root/poker/models.py
diff options
context:
space:
mode:
authorPaul Harrison <paul@harrison.sh>2022-11-18 19:30:19 +0000
committerPaul Harrison <paul@harrison.sh>2022-12-15 16:02:14 +0000
commit6be02c447dd19a3a103c9735600604f5be58de75 (patch)
tree9f046f3c702165285fb0a25b7ec8c0c19766cc51 /poker/models.py
parentc337984965c38b87319befbe97547c0420371aef (diff)
feat: Ensure hand comprises unique cards
Diffstat (limited to 'poker/models.py')
-rw-r--r--poker/models.py18
1 files changed, 16 insertions, 2 deletions
diff --git a/poker/models.py b/poker/models.py
index e62df17..5ecda90 100644
--- a/poker/models.py
+++ b/poker/models.py
@@ -1,4 +1,4 @@
-from pydantic import BaseModel, Field
+from pydantic import BaseModel, validator
from poker.constants import Suit, Value
@@ -9,8 +9,22 @@ class Card(BaseModel):
suit: Suit
value: Value
+ def __hash__(self) -> int:
+ """Hash function."""
+ return hash(f"{self.value} {self.suit}")
+
+
+HandType = tuple[Card, Card, Card, Card, Card]
+
class Hand(BaseModel):
"""Hand domain model class."""
- cards: list[Card] = Field(..., min_length=5, max_length=5)
+ cards: HandType
+
+ @validator("cards")
+ def validate_unique(cls, cards: HandType) -> HandType:
+ """Validate hand comprises unique cards."""
+ if len(cards) != len(set(cards)):
+ raise ValueError("Hand contains duplicate cards.")
+ return cards