From ea5e7d336719571e51ef14478ab603e71126e3d8 Mon Sep 17 00:00:00 2001 From: salmonik Date: Wed, 28 Oct 2026 22:28:05 +0100 Subject: v0.2.2: split client/server logic into the library --- src/server/games/traits.rs | 67 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 src/server/games/traits.rs (limited to 'src/server/games/traits.rs') diff --git a/src/server/games/traits.rs b/src/server/games/traits.rs new file mode 100644 index 0000000..d56b334 --- /dev/null +++ b/src/server/games/traits.rs @@ -0,0 +1,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, +} + +#[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, +} + +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; + + fn calculate_result(&self, state: &GameState) -> Result; +} -- cgit v1.2.3