summaryrefslogtreecommitdiffstats
path: root/src/bin/server/games/session.rs
diff options
context:
space:
mode:
authorsalmonik <salmonik@tuta.io>2026-10-28 22:28:05 +0100
committersalmonik <salmonik@tuta.io>2026-10-28 22:28:05 +0100
commitea5e7d336719571e51ef14478ab603e71126e3d8 (patch)
tree4fb59337501f798e6591701e52b9e785307efd63 /src/bin/server/games/session.rs
parentFix TUI: render_lines 6 for correct MoveUp, drain buffered events before clea... (diff)
downloadzagan-bet-ea5e7d336719571e51ef14478ab603e71126e3d8.tar.gz
zagan-bet-ea5e7d336719571e51ef14478ab603e71126e3d8.zip
v0.2.2: split client/server logic into the library
Diffstat (limited to 'src/bin/server/games/session.rs')
-rw-r--r--src/bin/server/games/session.rs77
1 files changed, 0 insertions, 77 deletions
diff --git a/src/bin/server/games/session.rs b/src/bin/server/games/session.rs
deleted file mode 100644
index 595a745..0000000
--- a/src/bin/server/games/session.rs
+++ /dev/null
@@ -1,77 +0,0 @@
1#![allow(dead_code)]
2
3use crate::games::traits::{GameError, GameState};
4use std::collections::HashMap;
5use std::sync::{Arc, Mutex};
6use uuid::Uuid;
7
8#[derive(Debug, Clone)]
9pub 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
17pub struct SessionManager {
18 sessions: Arc<Mutex<HashMap<String, GameSession>>>,
19}
20
21impl 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}