summaryrefslogtreecommitdiffstats
path: root/src/bin/client/games
diff options
context:
space:
mode:
Diffstat (limited to 'src/bin/client/games')
-rw-r--r--src/bin/client/games/manager.rs134
-rw-r--r--src/bin/client/games/mod.rs3
-rw-r--r--src/bin/client/games/slots.rs61
-rw-r--r--src/bin/client/games/traits.rs32
4 files changed, 0 insertions, 230 deletions
diff --git a/src/bin/client/games/manager.rs b/src/bin/client/games/manager.rs
deleted file mode 100644
index 0b5b4cb..0000000
--- a/src/bin/client/games/manager.rs
+++ /dev/null
@@ -1,134 +0,0 @@
1use crate::games::slots::SlotsGame;
2use crate::games::traits::{Game, GameError, GamePhase, GameState};
3use std::collections::HashMap;
4use std::io::Write;
5
6pub struct GameManager {
7 current_session: Option<GameState>,
8 pending_game_type: Option<String>,
9 games: HashMap<String, Box<dyn Game>>,
10}
11
12impl GameManager {
13 pub fn new() -> Self {
14 let mut games: HashMap<String, Box<dyn Game>> = HashMap::new();
15 games.insert("slots".to_string(), Box::new(SlotsGame::new()));
16
17 Self {
18 current_session: None,
19 pending_game_type: None,
20 games,
21 }
22 }
23
24 pub fn get_available_games(&self) -> Vec<(&str, &str)> {
25 self.games
26 .iter()
27 .map(|(k, v)| (k.as_str(), v.get_name()))
28 .collect()
29 }
30
31 pub fn enter_game(&mut self, game_type: &str) -> Result<(), GameError> {
32 if self.current_session.is_some() {
33 return Err(GameError::InvalidAction);
34 }
35
36 if !self.games.contains_key(game_type) {
37 return Err(GameError::InvalidAction);
38 }
39
40 self.pending_game_type = Some(game_type.to_string());
41
42 self.current_session = Some(GameState {
43 game_type: game_type.to_string(),
44 bet_amount: 10,
45 phase: GamePhase::WaitingToStart,
46 data: HashMap::new(),
47 });
48
49 Ok(())
50 }
51
52 pub fn start_game(
53 &mut self,
54 stream: &mut crate::connection::TlsStream,
55 ) -> Result<(), GameError> {
56 let game_type = self
57 .pending_game_type
58 .as_ref()
59 .ok_or(GameError::InvalidAction)?;
60
61 let state = self
62 .current_session
63 .as_mut()
64 .ok_or(GameError::InvalidAction)?;
65
66 if state.phase != GamePhase::WaitingToStart && state.phase != GamePhase::Finished {
67 return Err(GameError::InvalidAction);
68 }
69
70 let bet_amount = state.bet_amount;
71
72 let writer = stream;
73 writeln!(writer, "PLAY_START {} {}", game_type, bet_amount)
74 .map_err(|_| GameError::NetworkError)?;
75
76 Ok(())
77 }
78
79 pub fn get_current_game(&self) -> Option<&dyn Game> {
80 self.current_session
81 .as_ref()
82 .and_then(|session| self.games.get(&session.game_type).map(|game| game.as_ref()))
83 }
84
85 pub fn get_current_state(&self) -> Option<&GameState> {
86 self.current_session.as_ref()
87 }
88
89 pub fn get_current_render_lines(&self) -> Option<usize> {
90 let session = self.current_session.as_ref()?;
91 let game = self.games.get(&session.game_type)?;
92 Some(game.render_lines(session))
93 }
94
95 pub fn is_in_game(&self) -> bool {
96 self.current_session.is_some()
97 }
98
99 pub fn is_game_started(&self) -> bool {
100 self.current_session
101 .as_ref()
102 .map(|s| s.phase != GamePhase::WaitingToStart)
103 .unwrap_or(false)
104 }
105
106 pub fn reset_phase_to_waiting(&mut self) {
107 if let Some(ref mut session) = self.current_session {
108 session.phase = GamePhase::WaitingToStart;
109 session.data.clear();
110 }
111 }
112
113 pub fn end_game(&mut self) {
114 self.current_session = None;
115 self.pending_game_type = None;
116 }
117
118 pub fn adjust_bet(&mut self, delta: i64) -> Result<u64, GameError> {
119 if let Some(ref session) = self.current_session {
120 if session.phase != GamePhase::WaitingToStart {
121 return Err(GameError::InvalidAction);
122 }
123 }
124
125 if let Some(ref mut session) = self.current_session {
126 let current_bet = session.bet_amount as i64;
127 let new_bet = (current_bet + delta).max(10) as u64;
128 session.bet_amount = new_bet;
129 Ok(new_bet)
130 } else {
131 Err(GameError::InvalidAction)
132 }
133 }
134}
diff --git a/src/bin/client/games/mod.rs b/src/bin/client/games/mod.rs
deleted file mode 100644
index aa74574..0000000
--- a/src/bin/client/games/mod.rs
+++ /dev/null
@@ -1,3 +0,0 @@
1pub mod manager;
2pub mod slots;
3pub mod traits;
diff --git a/src/bin/client/games/slots.rs b/src/bin/client/games/slots.rs
deleted file mode 100644
index ff55274..0000000
--- a/src/bin/client/games/slots.rs
+++ /dev/null
@@ -1,61 +0,0 @@
1use crate::games::traits::{Game, GameState};
2
3pub struct SlotsGame;
4
5impl SlotsGame {
6 pub fn new() -> Self {
7 Self
8 }
9
10 fn render_reels(&self, reels: &str) -> String {
11 if reels.is_empty() {
12 "[ ? ] [ ? ] [ ? ]".to_string()
13 } else {
14 let chars: Vec<char> = reels.chars().collect();
15 format!(
16 "[ {} ] [ {} ] [ {} ]\r",
17 chars.get(0).unwrap_or(&'?'),
18 chars.get(1).unwrap_or(&'?'),
19 chars.get(2).unwrap_or(&'?')
20 )
21 }
22 }
23}
24
25impl Game for SlotsGame {
26 fn get_name(&self) -> &'static str {
27 "Slots"
28 }
29
30 fn render(&self, state: &GameState) -> String {
31 let reels = state.data.get("reels").cloned().unwrap_or_default();
32
33 format!(
34 "=== SLOTS ===\r\nBet: {} PLN\r\n{}\r\n\r\nPress [SPACE] to spin!\r\n\r\nControls: [+/-] Adjust Bet [Q] Quit",
35 state.bet_amount,
36 self.render_reels(&reels)
37 )
38 }
39
40 fn render_lines(&self, _state: &GameState) -> usize {
41 6
42 }
43
44 fn get_help(&self) -> String {
45 "SLOTS GAME\n\
46 =========\n\
47 Match symbols to win!\n\
48 \n\
49 Payouts:\n\
50 Three of a kind: 2x-28x bet\n\
51 Two of a kind: 1x-3x bet\n\
52 \n\
53 Symbols: C=Cherry, L=Lemon, O=Orange, G=Grapes, D=Diamond, S=Seven\n\
54 \n\
55 Controls:\n\
56 SPACE - Spin the reels\n\
57 +/- - Adjust bet amount\n\
58 Q - Exit the game"
59 .to_string()
60 }
61}
diff --git a/src/bin/client/games/traits.rs b/src/bin/client/games/traits.rs
deleted file mode 100644
index aed0c24..0000000
--- a/src/bin/client/games/traits.rs
+++ /dev/null
@@ -1,32 +0,0 @@
1use std::collections::HashMap;
2
3#[derive(Debug, Clone)]
4pub enum GameError {
5 InvalidAction,
6 NetworkError,
7}
8
9#[derive(Debug, Clone, PartialEq)]
10pub enum GamePhase {
11 WaitingToStart,
12 Playing,
13 Finished,
14}
15
16#[derive(Debug, Clone)]
17pub struct GameState {
18 pub game_type: String,
19 pub bet_amount: u64,
20 pub phase: GamePhase,
21 pub data: HashMap<String, String>,
22}
23
24pub trait Game: Send + Sync {
25 fn get_name(&self) -> &'static str;
26
27 fn render(&self, state: &GameState) -> String;
28
29 fn render_lines(&self, state: &GameState) -> usize;
30
31 fn get_help(&self) -> String;
32}