summaryrefslogtreecommitdiffstats
path: root/src/bin/server/games/traits.rs
blob: d56b3341b0dea2cf80b6fec96f174299a2fb447f (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
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum GameError {
    InvalidAction(String),
    InsufficientBalance,
    InvalidBet,
    GameOver,
    SessionNotFound,
    InternalError(String),
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum GamePhase {
    WaitingToStart,
    Playing,
    Finished,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GameState {
    pub session_id: String,
    pub game_type: String,
    pub bet_amount: u64,
    pub phase: GamePhase,
    pub data: HashMap<String, String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GameResult {
    pub session_id: String,
    pub game_type: String,
    pub bet_amount: u64,
    pub winnings: u64,
    pub final_balance: u64,
    pub result_data: HashMap<String, String>,
}

pub trait Game: Send + Sync {
    fn game_type(&self) -> &'static str;

    fn get_name(&self) -> &'static str;

    fn get_description(&self) -> &'static str;

    fn validate_bet(&self, bet_amount: u64, user_balance: u64) -> Result<(), GameError> {
        if bet_amount == 0 {
            return Err(GameError::InvalidBet);
        }
        if bet_amount > user_balance {
            return Err(GameError::InsufficientBalance);
        }
        Ok(())
    }

    fn prepare_state(&self, username: &str, bet_amount: u64) -> GameState;

    fn process_action(
        &self,
        state: &GameState,
        action: &str,
        params: &[&str],
    ) -> Result<GameState, GameError>;

    fn calculate_result(&self, state: &GameState) -> Result<GameResult, GameError>;
}