Skip to main content

api/dto/
pending.rs

1use serde::{Deserialize, Serialize};
2
3/// Represents which player is currently pending an action in the game.
4#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
5pub enum PendingDTO {
6    /// No player is pending.
7    #[default]
8    Nobody,
9
10    /// The user is pending an action; this is prioritised even
11    /// if the opponent is also pending.
12    User,
13
14    /// The opponent is pending an action.
15    Opponent,
16}
17
18#[cfg(feature = "server")]
19mod server_only {
20    use server::domain::{Pending, Player};
21
22    use super::*;
23
24    impl PendingDTO {
25        /// Creates a `PendingDTO` from the server domain `Pending` state for the given player.
26        ///
27        /// # Parameters
28        /// - `player`: The player for whom to determine pending status.
29        /// - `pending`: The domain `Pending` object representing game state.
30        ///
31        /// # Returns
32        /// - `PendingDTO::User` if the player is waiting.
33        /// - `PendingDTO::Opponent` if the opponent is waiting.
34        /// - `PendingDTO::Nobody` if neither is waiting.
35        pub fn new(player: Player, pending: &Pending) -> Self {
36            let opponent = player.opponent();
37            match (pending.waiting_on(player), pending.waiting_on(opponent)) {
38                (true, _) => PendingDTO::User,
39                (false, true) => PendingDTO::Opponent,
40                _ => PendingDTO::Nobody,
41            }
42        }
43    }
44}