#![allow(dead_code)] use super::traits::{GameError, GameState}; use std::collections::HashMap; use std::sync::{Arc, Mutex}; use uuid::Uuid; #[derive(Debug, Clone)] pub struct GameSession { pub session_id: String, pub username: String, pub game_type: String, pub bet_amount: u64, pub state: GameState, } pub struct SessionManager { sessions: Arc>>, } impl SessionManager { pub fn new() -> Self { Self { sessions: Arc::new(Mutex::new(HashMap::new())), } } pub fn create_session(&self, username: &str, game_type: String, state: GameState) -> String { let session_id = Uuid::new_v4().to_string(); let session = GameSession { session_id: session_id.clone(), username: username.to_string(), game_type, bet_amount: state.bet_amount, state, }; let mut sessions = self.sessions.lock().unwrap(); sessions.insert(session_id.clone(), session); session_id } pub fn get_session(&self, session_id: &str) -> Result { let sessions = self.sessions.lock().unwrap(); sessions .get(session_id) .cloned() .ok_or(GameError::SessionNotFound) } pub fn update_session(&self, session_id: &str, new_state: GameState) -> Result<(), GameError> { let mut sessions = self.sessions.lock().unwrap(); if let Some(session) = sessions.get_mut(session_id) { session.bet_amount = new_state.bet_amount; session.state = new_state; Ok(()) } else { Err(GameError::SessionNotFound) } } pub fn remove_session(&self, session_id: &str) -> Result<(), GameError> { let mut sessions = self.sessions.lock().unwrap(); sessions .remove(session_id) .ok_or(GameError::SessionNotFound) .map(|_| ()) } pub fn get_user_sessions(&self, username: &str) -> Vec { let sessions = self.sessions.lock().unwrap(); sessions .values() .filter(|s| s.username == username) .cloned() .collect() } pub fn len(&self) -> usize { self.sessions.lock().unwrap().len() } pub fn is_empty(&self) -> bool { self.len() == 0 } }