diff options
Diffstat (limited to '')
| -rw-r--r-- | src/bin/server/games/session.rs | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/src/bin/server/games/session.rs b/src/bin/server/games/session.rs new file mode 100644 index 0000000..595a745 --- /dev/null +++ b/src/bin/server/games/session.rs | |||
| @@ -0,0 +1,77 @@ | |||
| 1 | #![allow(dead_code)] | ||
| 2 | |||
| 3 | use crate::games::traits::{GameError, GameState}; | ||
| 4 | use std::collections::HashMap; | ||
| 5 | use std::sync::{Arc, Mutex}; | ||
| 6 | use uuid::Uuid; | ||
| 7 | |||
| 8 | #[derive(Debug, Clone)] | ||
| 9 | pub struct GameSession { | ||
| 10 | pub session_id: String, | ||
| 11 | pub username: String, | ||
| 12 | pub game_type: String, | ||
| 13 | pub bet_amount: u64, | ||
| 14 | pub state: GameState, | ||
| 15 | } | ||
| 16 | |||
| 17 | pub struct SessionManager { | ||
| 18 | sessions: Arc<Mutex<HashMap<String, GameSession>>>, | ||
| 19 | } | ||
| 20 | |||
| 21 | impl SessionManager { | ||
| 22 | pub fn new() -> Self { | ||
| 23 | Self { | ||
| 24 | sessions: Arc::new(Mutex::new(HashMap::new())), | ||
| 25 | } | ||
| 26 | } | ||
| 27 | |||
| 28 | pub fn create_session(&self, username: &str, game_type: String, state: GameState) -> String { | ||
| 29 | let session_id = Uuid::new_v4().to_string(); | ||
| 30 | let session = GameSession { | ||
| 31 | session_id: session_id.clone(), | ||
| 32 | username: username.to_string(), | ||
| 33 | game_type, | ||
| 34 | bet_amount: state.bet_amount, | ||
| 35 | state, | ||
| 36 | }; | ||
| 37 | |||
| 38 | let mut sessions = self.sessions.lock().unwrap(); | ||
| 39 | sessions.insert(session_id.clone(), session); | ||
| 40 | session_id | ||
| 41 | } | ||
| 42 | |||
| 43 | pub fn get_session(&self, session_id: &str) -> Result<GameSession, GameError> { | ||
| 44 | let sessions = self.sessions.lock().unwrap(); | ||
| 45 | sessions | ||
| 46 | .get(session_id) | ||
| 47 | .cloned() | ||
| 48 | .ok_or(GameError::SessionNotFound) | ||
| 49 | } | ||
| 50 | |||
| 51 | pub fn update_session(&self, session_id: &str, new_state: GameState) -> Result<(), GameError> { | ||
| 52 | let mut sessions = self.sessions.lock().unwrap(); | ||
| 53 | if let Some(session) = sessions.get_mut(session_id) { | ||
| 54 | session.state = new_state; | ||
| 55 | Ok(()) | ||
| 56 | } else { | ||
| 57 | Err(GameError::SessionNotFound) | ||
| 58 | } | ||
| 59 | } | ||
| 60 | |||
| 61 | pub fn remove_session(&self, session_id: &str) -> Result<(), GameError> { | ||
| 62 | let mut sessions = self.sessions.lock().unwrap(); | ||
| 63 | sessions | ||
| 64 | .remove(session_id) | ||
| 65 | .ok_or(GameError::SessionNotFound) | ||
| 66 | .map(|_| ()) | ||
| 67 | } | ||
| 68 | |||
| 69 | pub fn get_user_sessions(&self, username: &str) -> Vec<GameSession> { | ||
| 70 | let sessions = self.sessions.lock().unwrap(); | ||
| 71 | sessions | ||
| 72 | .values() | ||
| 73 | .filter(|s| s.username == username) | ||
| 74 | .cloned() | ||
| 75 | .collect() | ||
| 76 | } | ||
| 77 | } | ||
