Skip to main content

server/domain/phase/state/
finished.rs

1use serde::{Deserialize, Serialize};
2
3use crate::{
4    display::format_vec,
5    domain::{
6        Crib, Hands, HasCrib, HasHands, HasRoles, HasScoreboard, HasStarterCut, Player, Roles,
7        Scoreboard, StarterCut,
8    },
9};
10
11/// Represents the terminal state of a completed game.
12///
13/// This structure captures all information required to understand the final
14/// outcome, including the winning player, the final scoreboard, the roles
15/// assigned during the round, the hands as they existed at the end of play,
16/// the crib, and the starter card revealed during the play phase.
17#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
18pub struct Finished {
19    winner: Player,
20    scoreboard: Scoreboard,
21    roles: Roles,
22    hands: Hands,
23    crib: Crib,
24    starter_cut: StarterCut,
25}
26
27impl Finished {
28    /// Creates a new `Finished` state capturing the game’s final outcome.
29    ///
30    /// All parameters must reflect valid final-game state. This constructor
31    /// does not perform validation; callers are responsible for ensuring
32    /// coherence (e.g., that `winner` matches the final scoreboard).
33    pub const fn new(
34        winner: Player,
35        scoreboard: Scoreboard,
36        roles: Roles,
37        hands: Hands,
38        crib: Crib,
39        starter_cut: StarterCut,
40    ) -> Self {
41        Self {
42            winner,
43            scoreboard,
44            roles,
45            hands,
46            crib,
47            starter_cut,
48        }
49    }
50
51    /// Returns the player who won the game.
52    #[must_use]
53    pub const fn winner(&self) -> Player {
54        self.winner
55    }
56}
57
58impl HasScoreboard for Finished {
59    fn scoreboard(&self) -> &Scoreboard {
60        &self.scoreboard
61    }
62
63    fn scoreboard_mut(&mut self) -> &mut Scoreboard {
64        &mut self.scoreboard
65    }
66}
67
68impl HasRoles for Finished {
69    fn roles(&self) -> &Roles {
70        &self.roles
71    }
72
73    fn roles_mut(&mut self) -> &mut Roles {
74        &mut self.roles
75    }
76}
77
78impl HasHands for Finished {
79    fn hands(&self) -> &Hands {
80        &self.hands
81    }
82
83    fn hands_mut(&mut self) -> &mut Hands {
84        &mut self.hands
85    }
86}
87
88impl HasCrib for Finished {
89    fn crib(&self) -> &Crib {
90        &self.crib
91    }
92
93    fn crib_mut(&mut self) -> &mut Crib {
94        &mut self.crib
95    }
96}
97
98impl HasStarterCut for Finished {
99    fn starter_cut(&self) -> &StarterCut {
100        &self.starter_cut
101    }
102}
103
104impl std::fmt::Display for Finished {
105    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
106        #[rustfmt::skip]
107        let Self { winner, scoreboard, roles, hands, crib, starter_cut } = self;
108        let hands = format_vec(hands);
109
110        write!(
111            f,
112            r#"Finished(
113    winner: {winner},
114    scoreboard: {scoreboard},
115    roles: {roles},
116    hands: {hands},
117    crib: {crib},
118    cut: {starter_cut}
119)"#
120        )
121    }
122}