1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
use super::response::SpinResult;
use super::slots::SlotsGame;
use super::traits::{Game, GameError, GamePhase, GameState};
use std::collections::HashMap;
pub struct GameManager {
current_session: Option<GameState>,
games: HashMap<String, Box<dyn Game>>,
}
impl GameManager {
pub fn new() -> Self {
let mut games: HashMap<String, Box<dyn Game>> = HashMap::new();
games.insert("slots".to_string(), Box::new(SlotsGame::new()));
Self {
current_session: None,
games,
}
}
pub fn get_available_games(&self) -> Vec<(&str, &str)> {
let mut games: Vec<(&str, &str)> = self
.games
.iter()
.map(|(key, game)| (key.as_str(), game.get_name()))
.collect();
games.sort_unstable_by_key(|(key, _)| *key);
games
}
pub fn enter_game(&mut self, game_type: &str) -> Result<(), GameError> {
if self.current_session.is_some() {
return Err(GameError::InvalidAction);
}
if !self.games.contains_key(game_type) {
return Err(GameError::InvalidAction);
}
self.current_session = Some(GameState {
game_type: game_type.to_string(),
bet_amount: 10,
phase: GamePhase::WaitingToStart,
data: HashMap::new(),
});
Ok(())
}
/// The wire request for a spin, if the current phase allows one.
pub fn spin_request(&self) -> Option<String> {
let session = self.current_session.as_ref()?;
if session.phase != GamePhase::WaitingToStart && session.phase != GamePhase::Finished {
return None;
}
Some(format!(
"PLAY_START {} {}",
session.game_type, session.bet_amount
))
}
/// Move the board into its spinning state and drop the previous result
/// so stale reels/winnings cannot leak into the new spin.
pub fn begin_spin(&mut self) -> Result<(), GameError> {
let session = self
.current_session
.as_mut()
.ok_or(GameError::InvalidAction)?;
if session.phase != GamePhase::WaitingToStart && session.phase != GamePhase::Finished {
return Err(GameError::InvalidAction);
}
session.phase = GamePhase::Playing;
session.data.remove("error");
session.data.remove("reels");
session.data.remove("win_type");
session.data.remove("win_lines");
session.data.remove("winnings");
Ok(())
}
/// Store the settled spin so `render()` can draw it in place.
pub fn apply_result(&mut self, result: SpinResult) {
let Some(session) = self.current_session.as_mut() else {
return;
};
session.phase = GamePhase::Finished;
session.bet_amount = result.bet_amount;
session.data.remove("error");
session.data.insert("reels".to_string(), result.reels);
session
.data
.insert("win_type".to_string(), result.win_type);
session.data.insert(
"win_lines".to_string(),
result
.win_lines
.iter()
.map(usize::to_string)
.collect::<Vec<_>>()
.join(","),
);
session
.data
.insert("winnings".to_string(), result.winnings.to_string());
session
.data
.insert("balance".to_string(), result.balance.to_string());
}
/// Show a failed spin without leaving the player stuck in `Playing`.
pub fn set_error(&mut self, message: impl Into<String>) {
let Some(session) = self.current_session.as_mut() else {
return;
};
session.phase = GamePhase::WaitingToStart;
session.data.remove("reels");
session.data.remove("win_type");
session.data.remove("win_lines");
session.data.remove("winnings");
session.data.insert("error".to_string(), message.into());
}
pub fn set_balance(&mut self, balance: i64) {
if let Some(session) = self.current_session.as_mut() {
session
.data
.insert("balance".to_string(), balance.to_string());
}
}
pub fn get_current_game(&self) -> Option<&dyn Game> {
self.current_session
.as_ref()
.and_then(|session| self.games.get(&session.game_type).map(|game| game.as_ref()))
}
pub fn get_current_state(&self) -> Option<&GameState> {
self.current_session.as_ref()
}
pub fn is_in_game(&self) -> bool {
self.current_session.is_some()
}
pub fn end_game(&mut self) {
self.current_session = None;
}
pub fn set_bet(&mut self, bet: u64) -> Result<u64, GameError> {
let session = self
.current_session
.as_mut()
.ok_or(GameError::InvalidAction)?;
if session.phase != GamePhase::WaitingToStart && session.phase != GamePhase::Finished {
return Err(GameError::InvalidAction);
}
session.bet_amount = bet.max(10);
Ok(session.bet_amount)
}
pub fn adjust_bet(&mut self, delta: i64) -> Result<u64, GameError> {
let session = self
.current_session
.as_mut()
.ok_or(GameError::InvalidAction)?;
if session.phase != GamePhase::WaitingToStart && session.phase != GamePhase::Finished {
return Err(GameError::InvalidAction);
}
let current_bet = session.bet_amount as i64;
session.bet_amount = (current_bet + delta).max(10) as u64;
Ok(session.bet_amount)
}
}
|