Skip to main content

api/dto/
pegging.rs

1use std::collections::HashMap;
2
3use serde::{Deserialize, Serialize};
4
5use crate::dto::{CardIdDTO, PlayerDTO};
6
7/// Enumerates the types of pegging scores in a Cribbage game.
8///
9/// This is used to classify the different scoring categories when tallying points
10/// during the pegging phase.
11#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
12pub enum PeggingKindDTO {
13    #[doc(hidden)]
14    Fifteens,
15
16    #[doc(hidden)]
17    Pairs,
18
19    #[doc(hidden)]
20    Runs,
21
22    #[doc(hidden)]
23    Flush,
24
25    #[doc(hidden)]
26    LastCard,
27
28    #[doc(hidden)]
29    ThirtyOne,
30
31    #[doc(hidden)]
32    HisHeels,
33
34    #[doc(hidden)]
35    Nobs,
36}
37
38impl std::fmt::Display for PeggingKindDTO {
39    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
40        match &self {
41            PeggingKindDTO::Fifteens => "Fifteens",
42            PeggingKindDTO::Pairs => "Pairs",
43            PeggingKindDTO::Runs => "Runs",
44            PeggingKindDTO::Flush => "Flush",
45            PeggingKindDTO::LastCard => "Last Card",
46            PeggingKindDTO::ThirtyOne => "Thirty One",
47            PeggingKindDTO::HisHeels => "His Heels",
48            PeggingKindDTO::Nobs => "Nobs",
49        }
50        .fmt(f)
51    }
52}
53
54/// A summary of pegging points for a particular category.
55#[derive(Clone, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
56pub struct PeggingSummaryDTO {
57    /// Total points scored.
58    pub points: usize,
59
60    /// The combinations of cards that contributed to the score.
61    pub breakdown: Vec<Vec<CardIdDTO>>,
62}
63
64/// Mapping from pegging kinds to the collections of cards that made up were pegged.
65pub type PeggingBreakdownDTO = HashMap<PeggingKindDTO, PeggingSummaryDTO>;
66
67/// Pegging scores broken down by category.
68///
69/// Maps each `PeggingKindDTO` to a `PeggingSummaryDTO`.
70#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
71pub struct PeggingDTO {
72    /// Recipient of points scored.
73    pub recipient: Option<PlayerDTO>,
74
75    /// Breakdown of the points by kind.
76    pub breakdown: PeggingBreakdownDTO,
77}
78
79impl PeggingDTO {
80    /// Create a new pegging for a receipient
81    pub fn new(recipient: PlayerDTO) -> Self {
82        Self {
83            recipient: Some(recipient),
84            breakdown: HashMap::default(),
85        }
86    }
87}