diff options
Diffstat (limited to 'src/bin/server/games/traits.rs')
| -rw-r--r-- | src/bin/server/games/traits.rs | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/src/bin/server/games/traits.rs b/src/bin/server/games/traits.rs new file mode 100644 index 0000000..d56b334 --- /dev/null +++ b/src/bin/server/games/traits.rs | |||
| @@ -0,0 +1,67 @@ | |||
| 1 | use serde::{Deserialize, Serialize}; | ||
| 2 | use std::collections::HashMap; | ||
| 3 | |||
| 4 | #[derive(Debug, Clone, Serialize, Deserialize)] | ||
| 5 | pub enum GameError { | ||
| 6 | InvalidAction(String), | ||
| 7 | InsufficientBalance, | ||
| 8 | InvalidBet, | ||
| 9 | GameOver, | ||
| 10 | SessionNotFound, | ||
| 11 | InternalError(String), | ||
| 12 | } | ||
| 13 | |||
| 14 | #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] | ||
| 15 | pub enum GamePhase { | ||
| 16 | WaitingToStart, | ||
| 17 | Playing, | ||
| 18 | Finished, | ||
| 19 | } | ||
| 20 | |||
| 21 | #[derive(Debug, Clone, Serialize, Deserialize)] | ||
| 22 | pub struct GameState { | ||
| 23 | pub session_id: String, | ||
| 24 | pub game_type: String, | ||
| 25 | pub bet_amount: u64, | ||
| 26 | pub phase: GamePhase, | ||
| 27 | pub data: HashMap<String, String>, | ||
| 28 | } | ||
| 29 | |||
| 30 | #[derive(Debug, Clone, Serialize, Deserialize)] | ||
| 31 | pub struct GameResult { | ||
| 32 | pub session_id: String, | ||
| 33 | pub game_type: String, | ||
| 34 | pub bet_amount: u64, | ||
| 35 | pub winnings: u64, | ||
| 36 | pub final_balance: u64, | ||
| 37 | pub result_data: HashMap<String, String>, | ||
| 38 | } | ||
| 39 | |||
| 40 | pub trait Game: Send + Sync { | ||
| 41 | fn game_type(&self) -> &'static str; | ||
| 42 | |||
| 43 | fn get_name(&self) -> &'static str; | ||
| 44 | |||
| 45 | fn get_description(&self) -> &'static str; | ||
| 46 | |||
| 47 | fn validate_bet(&self, bet_amount: u64, user_balance: u64) -> Result<(), GameError> { | ||
| 48 | if bet_amount == 0 { | ||
| 49 | return Err(GameError::InvalidBet); | ||
| 50 | } | ||
| 51 | if bet_amount > user_balance { | ||
| 52 | return Err(GameError::InsufficientBalance); | ||
| 53 | } | ||
| 54 | Ok(()) | ||
| 55 | } | ||
| 56 | |||
| 57 | fn prepare_state(&self, username: &str, bet_amount: u64) -> GameState; | ||
| 58 | |||
| 59 | fn process_action( | ||
| 60 | &self, | ||
| 61 | state: &GameState, | ||
| 62 | action: &str, | ||
| 63 | params: &[&str], | ||
| 64 | ) -> Result<GameState, GameError>; | ||
| 65 | |||
| 66 | fn calculate_result(&self, state: &GameState) -> Result<GameResult, GameError>; | ||
| 67 | } | ||
