schnapsen_rs/models/
mod.rs1use std::hash::Hash;
2
3use num_enum::FromPrimitive;
4use serde::{Deserialize, Serialize};
5
6#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
7pub struct Card {
8 pub value: CardVal,
9 pub suit: CardSuit,
10}
11
12impl PartialOrd for Card {
13 fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
14 Some(self.value.cmp(&other.value))
15 }
16}
17
18impl Ord for Card {
19 fn cmp(&self, other: &Self) -> std::cmp::Ordering {
20 self.value.cmp(&other.value)
21 }
22}
23
24#[derive(Debug, Serialize, Deserialize, Clone, FromPrimitive, PartialEq, Eq, PartialOrd, Ord)]
25#[repr(u8)]
26pub enum CardVal {
27 Ten = 10,
28 Jack = 2,
29 Queen = 3,
30 King = 4,
31 #[default]
32 Ace = 11,
33}
34
35#[derive(Debug, Serialize, Deserialize, Clone, FromPrimitive, PartialEq, Eq)]
36#[repr(u8)]
37pub enum CardSuit {
38 #[default]
39 Hearts = 0,
40 Diamonds = 1,
41 Clubs = 2,
42 Spades = 3,
43}
44
45#[derive(Debug, Clone)]
46pub struct Player {
47 pub id: String,
48 pub cards: Vec<Card>,
49 pub playable_cards: Vec<Card>,
50 pub tricks: Vec<[Card; 2]>,
51 pub announcements: Vec<Announcement>,
52 pub announcable: Vec<Announcement>,
53 pub possible_trump_swap: Option<Card>,
54 pub points: u8,
55}
56
57impl Player {
58 pub fn reset(&mut self) {
59 self.tricks.clear();
60 }
61
62 pub fn new(id: String) -> Self {
63 Player {
64 id,
65 cards: Vec::new(),
66 playable_cards: Vec::new(),
67 tricks: Vec::new(),
68 announcements: Vec::new(),
69 announcable: Vec::new(),
70 points: 0,
71 possible_trump_swap: None,
72 }
73 }
74
75 pub fn has_announced(&self, mut cards: [Card; 2]) -> bool {
76 cards.sort();
77 let announced = self
78 .announcements
79 .iter()
80 .map(|x| {
81 let mut a = x.cards.clone();
82 a.sort();
83 a
84 })
85 .collect::<Vec<_>>();
86 announced.into_iter().any(|x| x == cards)
87 }
88
89 pub fn has_announcable(&self, announcement: &Announcement) -> bool {
90 has_announcable(&self.announcable, announcement)
91 }
92}
93
94pub fn has_announcable(data: &[Announcement], check: &Announcement) -> bool {
95 data.iter().any(|proposed|
96 proposed.announce_type == check.announce_type
97 && proposed.cards.first().unwrap().suit == check.cards.first().unwrap().suit
98 )
99}
100
101pub fn contains_card_comb(data: &[[Card; 2]], mut check: [Card; 2]) -> bool {
102 check.sort();
103 let announced = data
104 .iter()
105 .map(|x| {
106 let mut clone = x.clone();
107 clone.sort();
108 clone
109 })
110 .collect::<Vec<_>>();
111 announced.into_iter().any(|x| x == check)
112}
113
114impl Hash for Player {
115 fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
116 state.write(self.id.as_bytes());
117 }
118}
119
120
121#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
122pub struct Announcement {
123 pub cards: [Card; 2],
124 pub announce_type: AnnounceType,
125}
126
127#[derive(Debug, Serialize, Deserialize, Clone, FromPrimitive, PartialEq, Eq)]
128#[repr(u8)]
129pub enum AnnounceType {
130 Forty = 40,
131 #[default]
132 Twenty = 20,
133}
134