summaryrefslogtreecommitdiffstats
path: root/src/server/games/roulette.rs
blob: 0a583233f8509a757e70e313e48241401a2bcd9b (plain) (blame)
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
use super::traits::{Game, GameError, GamePhase, GameResult, GameState};
use crate::roulette::{color, Bets};
use rand::Rng;
use std::collections::HashMap;

pub struct RouletteGame {
    fixed_number: Option<u8>,
}

impl RouletteGame {
    pub fn new() -> Self {
        Self { fixed_number: None }
    }

    pub fn with_result(number: u8) -> Self {
        Self {
            fixed_number: Some(number),
        }
    }

    fn spin(&self) -> u8 {
        match self.fixed_number {
            Some(number) => number,
            None => rand::thread_rng().gen_range(0..37),
        }
    }
}

impl Game for RouletteGame {
    fn game_type(&self) -> &'static str {
        "roulette"
    }

    fn get_name(&self) -> &'static str {
        "Roulette"
    }

    fn get_description(&self) -> &'static str {
        "European roulette. Bet on red/black, even/odd, or a straight number."
    }

    fn prepare_state(&self, username: &str, bet_amount: u64) -> GameState {
        let mut data = HashMap::new();
        data.insert("username".to_string(), username.to_string());
        data.insert("status".to_string(), "ready".to_string());

        GameState {
            session_id: String::new(),
            game_type: "roulette".to_string(),
            bet_amount,
            phase: GamePhase::WaitingToStart,
            data,
        }
    }

    fn process_action(
        &self,
        state: &GameState,
        action: &str,
        params: &[&str],
    ) -> Result<GameState, GameError> {
        if state.phase == GamePhase::Finished {
            return Err(GameError::GameOver);
        }

        match action {
            "spin" => {
                if state.phase != GamePhase::WaitingToStart && state.phase != GamePhase::Playing {
                    return Err(GameError::InvalidAction(
                        "Cannot spin in current phase".to_string(),
                    ));
                }

                let payload = params.first().ok_or(GameError::InvalidAction(
                    "Missing roulette bets".to_string(),
                ))?;
                let bets: Bets = serde_json::from_str(payload)
                    .map_err(|error| GameError::InvalidAction(format!("Invalid bets: {error}")))?;

                if bets.is_empty() {
                    return Err(GameError::InvalidBet);
                }
                if let Err(message) = bets.validate(state.bet_amount) {
                    return Err(GameError::InvalidAction(message));
                }
                if bets.total() != state.bet_amount {
                    return Err(GameError::InvalidBet);
                }

                let number = self.spin();
                let bets_json = serde_json::to_string(&bets)
                    .map_err(|error| GameError::InternalError(error.to_string()))?;

                let mut new_data = state.data.clone();
                new_data.insert("status".to_string(), "spun".to_string());
                new_data.insert("bets".to_string(), bets_json);
                new_data.insert("number".to_string(), number.to_string());
                new_data.insert("color".to_string(), color(number).to_string());

                Ok(GameState {
                    session_id: state.session_id.clone(),
                    game_type: state.game_type.clone(),
                    bet_amount: state.bet_amount,
                    phase: GamePhase::Finished,
                    data: new_data,
                })
            }
            _ => Err(GameError::InvalidAction(format!(
                "Unknown action: {action}"
            ))),
        }
    }

    fn calculate_result(&self, state: &GameState) -> Result<GameResult, GameError> {
        if state.phase != GamePhase::Finished {
            return Err(GameError::InternalError("Game not finished".to_string()));
        }

        let number: u8 = state
            .data
            .get("number")
            .ok_or(GameError::InternalError("No winning number".to_string()))?
            .parse()
            .map_err(|_| GameError::InternalError("Invalid winning number".to_string()))?;
        let bets_json = state
            .data
            .get("bets")
            .ok_or(GameError::InternalError("No bets".to_string()))?
            .clone();
        let bets: Bets = serde_json::from_str(&bets_json)
            .map_err(|error| GameError::InternalError(format!("Invalid bets: {error}")))?;

        let winnings = bets.winnings(number);

        let mut result_data = HashMap::new();
        result_data.insert("number".to_string(), number.to_string());
        result_data.insert("color".to_string(), color(number).to_string());
        result_data.insert("bets".to_string(), bets_json);

        Ok(GameResult {
            session_id: state.session_id.clone(),
            game_type: state.game_type.clone(),
            bet_amount: state.bet_amount,
            winnings,
            final_balance: 0,
            result_data,
        })
    }
}