diff options
Diffstat (limited to '')
| -rw-r--r-- | src/server/games/registry.rs | 22 | ||||
| -rw-r--r-- | src/server/games/session.rs | 8 | ||||
| -rw-r--r-- | src/server/games/slots.rs | 23 |
3 files changed, 35 insertions, 18 deletions
diff --git a/src/server/games/registry.rs b/src/server/games/registry.rs index 3b8c391..b7ed9d3 100644 --- a/src/server/games/registry.rs +++ b/src/server/games/registry.rs | |||
| @@ -1,5 +1,6 @@ | |||
| 1 | #![allow(dead_code)] | 1 | #![allow(dead_code)] |
| 2 | 2 | ||
| 3 | use super::slots::SlotsGame; | ||
| 3 | use super::traits::Game; | 4 | use super::traits::Game; |
| 4 | use std::collections::HashMap; | 5 | use std::collections::HashMap; |
| 5 | use std::sync::{Arc, Mutex}; | 6 | use std::sync::{Arc, Mutex}; |
| @@ -17,6 +18,12 @@ impl GameRegistry { | |||
| 17 | } | 18 | } |
| 18 | } | 19 | } |
| 19 | 20 | ||
| 21 | pub fn with_defaults() -> Self { | ||
| 22 | let registry = Self::new(); | ||
| 23 | registry.register_game(SlotsGame::new()); | ||
| 24 | registry | ||
| 25 | } | ||
| 26 | |||
| 20 | pub fn register_game<G: Game + Send + Sync + 'static>(&self, game: G) { | 27 | pub fn register_game<G: Game + Send + Sync + 'static>(&self, game: G) { |
| 21 | let game_type = game.game_type().to_string(); | 28 | let game_type = game.game_type().to_string(); |
| 22 | let mut games = self.games.lock().unwrap(); | 29 | let mut games = self.games.lock().unwrap(); |
| @@ -38,18 +45,3 @@ impl GameRegistry { | |||
| 38 | games.contains_key(game_type) | 45 | games.contains_key(game_type) |
| 39 | } | 46 | } |
| 40 | } | 47 | } |
| 41 | |||
| 42 | // Global registry instance | ||
| 43 | use super::slots::SlotsGame; | ||
| 44 | use once_cell::sync::Lazy; | ||
| 45 | |||
| 46 | static GAME_REGISTRY: Lazy<GameRegistry> = Lazy::new(|| { | ||
| 47 | let registry = GameRegistry::new(); | ||
| 48 | // Register games here | ||
| 49 | registry.register_game(SlotsGame::new()); | ||
| 50 | registry | ||
| 51 | }); | ||
| 52 | |||
| 53 | pub fn get_game_registry() -> &'static GameRegistry { | ||
| 54 | &GAME_REGISTRY | ||
| 55 | } | ||
diff --git a/src/server/games/session.rs b/src/server/games/session.rs index 97ceaf3..a8410a9 100644 --- a/src/server/games/session.rs +++ b/src/server/games/session.rs | |||
| @@ -74,4 +74,12 @@ impl SessionManager { | |||
| 74 | .cloned() | 74 | .cloned() |
| 75 | .collect() | 75 | .collect() |
| 76 | } | 76 | } |
| 77 | |||
| 78 | pub fn len(&self) -> usize { | ||
| 79 | self.sessions.lock().unwrap().len() | ||
| 80 | } | ||
| 81 | |||
| 82 | pub fn is_empty(&self) -> bool { | ||
| 83 | self.len() == 0 | ||
| 84 | } | ||
| 77 | } | 85 | } |
diff --git a/src/server/games/slots.rs b/src/server/games/slots.rs index 0a9797d..02595b1 100644 --- a/src/server/games/slots.rs +++ b/src/server/games/slots.rs | |||
| @@ -5,6 +5,8 @@ use std::collections::HashMap; | |||
| 5 | pub struct SlotsGame { | 5 | pub struct SlotsGame { |
| 6 | symbols: Vec<char>, | 6 | symbols: Vec<char>, |
| 7 | payouts: HashMap<String, u64>, | 7 | payouts: HashMap<String, u64>, |
| 8 | /// Deterministic reels for tests and demos. | ||
| 9 | fixed_reels: Option<Vec<char>>, | ||
| 8 | } | 10 | } |
| 9 | 11 | ||
| 10 | impl SlotsGame { | 12 | impl SlotsGame { |
| @@ -27,10 +29,25 @@ impl SlotsGame { | |||
| 27 | payouts.insert("DD".to_string(), 2); | 29 | payouts.insert("DD".to_string(), 2); |
| 28 | payouts.insert("SS".to_string(), 3); | 30 | payouts.insert("SS".to_string(), 3); |
| 29 | 31 | ||
| 30 | Self { symbols, payouts } | 32 | Self { |
| 33 | symbols, | ||
| 34 | payouts, | ||
| 35 | fixed_reels: None, | ||
| 36 | } | ||
| 37 | } | ||
| 38 | |||
| 39 | /// Build a slots game that always spins the given three symbols. | ||
| 40 | pub fn with_reels(reels: &str) -> Self { | ||
| 41 | let mut game = Self::new(); | ||
| 42 | game.fixed_reels = Some(reels.chars().take(3).collect()); | ||
| 43 | game | ||
| 31 | } | 44 | } |
| 32 | 45 | ||
| 33 | fn spin_reels(&self) -> Vec<char> { | 46 | fn spin_reels(&self) -> Vec<char> { |
| 47 | if let Some(reels) = &self.fixed_reels { | ||
| 48 | return reels.clone(); | ||
| 49 | } | ||
| 50 | |||
| 34 | let mut rng = rand::thread_rng(); | 51 | let mut rng = rand::thread_rng(); |
| 35 | (0..3) | 52 | (0..3) |
| 36 | .map(|_| self.symbols[rng.gen_range(0..self.symbols.len())]) | 53 | .map(|_| self.symbols[rng.gen_range(0..self.symbols.len())]) |
| @@ -41,7 +58,7 @@ impl SlotsGame { | |||
| 41 | let reel_string: String = reels.iter().collect(); | 58 | let reel_string: String = reels.iter().collect(); |
| 42 | 59 | ||
| 43 | if let Some(&multiplier) = self.payouts.get(&reel_string) { | 60 | if let Some(&multiplier) = self.payouts.get(&reel_string) { |
| 44 | return bet * multiplier; | 61 | return bet.saturating_mul(multiplier); |
| 45 | } | 62 | } |
| 46 | 63 | ||
| 47 | // Check all three pairs including non-adjacent (e.g. [C,D,C]) | 64 | // Check all three pairs including non-adjacent (e.g. [C,D,C]) |
| @@ -51,7 +68,7 @@ impl SlotsGame { | |||
| 51 | if reels[a] == reels[b] { | 68 | if reels[a] == reels[b] { |
| 52 | let two_of_kind = format!("{}{}", reels[a], reels[a]); | 69 | let two_of_kind = format!("{}{}", reels[a], reels[a]); |
| 53 | if let Some(&multiplier) = self.payouts.get(&two_of_kind) { | 70 | if let Some(&multiplier) = self.payouts.get(&two_of_kind) { |
| 54 | best = best.max(bet * multiplier); | 71 | best = best.max(bet.saturating_mul(multiplier)); |
| 55 | } | 72 | } |
| 56 | } | 73 | } |
| 57 | } | 74 | } |
