buckshot_roulette_gameplay_engine/
game_players.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
use thiserror::Error;

use crate::{
    multiplayer_count::MultiplayerCount, player::Player, player_number::PlayerNumber,
    round_number::RoundNumber,
};

#[derive(Debug, Clone)]
struct ExtraPlayers {
    player_3: Player,
    player_4: Option<Player>,
}

#[derive(Debug, Clone)]
pub struct GamePlayers {
    player_1: Player,
    player_2: Player,
    extra_players: Option<ExtraPlayers>,
    pub multiplayer_count: MultiplayerCount,
}

#[derive(Error, Debug, Clone, Copy)]
pub enum MissingPlayerError {
    #[error("Requested player is not registered!")]
    MissingPlayer,
}

impl GamePlayers {
    pub fn new(multiplayer_count: MultiplayerCount) -> Self {
        let player_1 = Player::new(PlayerNumber::One);
        let player_2 = Player::new(PlayerNumber::Two);
        let extra_players = match multiplayer_count {
            MultiplayerCount::Two => None,
            MultiplayerCount::Three | MultiplayerCount::Four => {
                let player_4 = if multiplayer_count == MultiplayerCount::Four {
                    Some(Player::new(PlayerNumber::Four))
                } else {
                    None
                };

                Some(ExtraPlayers {
                    player_3: Player::new(PlayerNumber::Three),
                    player_4,
                })
            }
        };

        GamePlayers {
            player_1,
            player_2,
            extra_players,
            multiplayer_count,
        }
    }

    pub fn as_vec(&self) -> Vec<&Player> {
        let mut vec = Vec::with_capacity(4);
        vec.push(&self.player_1);
        vec.push(&self.player_2);

        if let Some(extra_players) = &self.extra_players {
            vec.push(&extra_players.player_3);
            if let Some(player_4) = &extra_players.player_4 {
                vec.push(player_4);
            }
        }

        vec
    }

    pub fn register_win(
        &mut self,
        player_number: PlayerNumber,
        round_number: RoundNumber,
    ) -> Result<(), MissingPlayerError> {
        match player_number {
            PlayerNumber::One => self.player_1.register_win(round_number),
            PlayerNumber::Two => self.player_2.register_win(round_number),
            PlayerNumber::Three | PlayerNumber::Four => match &mut self.extra_players {
                Some(extra_players) => {
                    if player_number == PlayerNumber::Three {
                        extra_players.player_3.register_win(round_number);
                    } else if let Some(player_4) = &mut extra_players.player_4 {
                        player_4.register_win(round_number);
                    } else {
                        return Err(MissingPlayerError::MissingPlayer);
                    }
                }
                None => return Err(MissingPlayerError::MissingPlayer),
            },
        };

        Ok(())
    }
}