schnapsen_rs/models/
mod.rs

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
use std::hash::Hash;

use num_enum::FromPrimitive;
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
pub struct Card {
    pub value: CardVal,
    pub suit: CardSuit,
}

impl PartialOrd for Card {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.value.cmp(&other.value))
    }
}

impl Ord for Card {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        self.value.cmp(&other.value)
    }
}

#[derive(Debug, Serialize, Deserialize, Clone, FromPrimitive, PartialEq, Eq, PartialOrd, Ord)]
#[repr(u8)]
pub enum CardVal {
    Ten = 10,
    Jack = 2,
    Queen = 3,
    King = 4,
    #[default]
    Ace = 11,
}

#[derive(Debug, Serialize, Deserialize, Clone, FromPrimitive, PartialEq, Eq)]
#[repr(u8)]
pub enum CardSuit {
    #[default]
    Hearts = 0,
    Diamonds = 1,
    Clubs = 2,
    Spades = 3,
}

#[derive(Clone)]
pub struct Player {
    pub id: String,
    pub cards: Vec<Card>,
    pub playable_cards: Vec<Card>,
    pub tricks: Vec<[Card; 2]>,
    pub announcements: Vec<Announcement>,
    pub announcable: Vec<Announcement>,
    pub possible_trump_swap: Option<Card>,
    pub points: u8,
}

impl Player {
    pub fn reset(&mut self) {
        self.cards.clear();
        self.tricks.clear();
        self.announcements.clear();
        self.announcable.clear();
    }

    pub fn new(id: String) -> Self {
        Player {
            id,
            cards: Vec::new(),
            playable_cards: Vec::new(),
            tricks: Vec::new(),
            announcements: Vec::new(),
            announcable: Vec::new(),
            points: 0,
            possible_trump_swap: None,
        }
    }

    pub fn has_announced(&self, mut cards: [Card; 2]) -> bool {
        cards.sort();
        let announced = self
            .announcements
            .iter()
            .map(|x| {
                let mut a = x.cards.clone();
                a.sort();
                a
            })
            .collect::<Vec<_>>();
        announced.into_iter().any(|x| x == cards)
    }

    pub fn has_announcable(&self, announcement: &Announcement) -> bool {
        has_announcable(&self.announcable, announcement)
    }
}

pub fn has_announcable(data: &[Announcement], check: &Announcement) -> bool {
    data.iter().any(|proposed| 
        proposed.announce_type == check.announce_type
            && proposed.cards.first().unwrap().suit == check.cards.first().unwrap().suit
    )
}

pub fn contains_card_comb(data: &[[Card; 2]], mut check: [Card; 2]) -> bool {
    check.sort();
    let announced = data
        .iter()
        .map(|x| {
            let mut clone = x.clone();
            clone.sort();
            clone
        })
        .collect::<Vec<_>>();
    announced.into_iter().any(|x| x == check)
}

impl Hash for Player {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        state.write(self.id.as_bytes());
    }
}


#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
pub struct Announcement {
    pub cards: [Card; 2],
    pub announce_type: AnnounceType,
}

#[derive(Debug, Serialize, Deserialize, Clone, FromPrimitive, PartialEq, Eq)]
#[repr(u8)]
pub enum AnnounceType {
    Forty = 40,
    #[default]
    Twenty = 20,
}