aboutsummaryrefslogtreecommitdiff
path: root/poker/models.py
blob: 5ecda90837cef21f4355864fc7b388cfff53169b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
from pydantic import BaseModel, validator

from poker.constants import Suit, Value


class Card(BaseModel):
    """Card domain model class."""

    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: 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