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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
|
use super::response::{RouletteResult, SpinResult};
use super::roulette::RouletteGame;
use super::slots::SlotsGame;
use super::traits::{Game, GameError, GamePhase, GameState};
use crate::roulette::Bets;
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()));
games.insert("roulette".to_string(), Box::new(RouletteGame::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);
}
let mut data = HashMap::new();
let bet_amount = if game_type == "roulette" {
data.insert(
"bets".to_string(),
serde_json::to_string(&Bets::default()).unwrap_or_else(|_| "{}".to_string()),
);
data.insert("chip".to_string(), "10".to_string());
data.insert("status".to_string(), "place".to_string());
data.insert("number_input".to_string(), String::new());
0
} else {
10
};
self.current_session = Some(GameState {
game_type: game_type.to_string(),
bet_amount,
phase: GamePhase::WaitingToStart,
data,
});
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;
}
if session.game_type == "roulette" {
let bets = session
.data
.get("bets")
.cloned()
.unwrap_or_else(|| "{}".to_string());
return Some(format!(
"PLAY_START roulette {} {}",
session.bet_amount, bets
));
}
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("win_cells");
session.data.remove("winnings");
session.data.remove("number");
session.data.remove("color");
if session.game_type == "roulette" {
session
.data
.insert("status".to_string(), "spinning".to_string());
}
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(
"win_cells".to_string(),
result
.win_cells
.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("win_cells");
session.data.remove("winnings");
session.data.remove("number");
session.data.remove("color");
if session.game_type == "roulette" {
session
.data
.insert("status".to_string(), "place".to_string());
}
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 roulette_bets(&self) -> Bets {
self.current_session
.as_ref()
.and_then(|session| session.data.get("bets"))
.and_then(|raw| serde_json::from_str(raw).ok())
.unwrap_or_default()
}
pub fn set_roulette_bets(&mut self, bets: &Bets) {
if let Some(session) = self.current_session.as_mut() {
session.data.insert(
"bets".to_string(),
serde_json::to_string(bets).unwrap_or_else(|_| "{}".to_string()),
);
session.bet_amount = bets.total();
}
}
pub fn roulette_chip(&self) -> u64 {
self.current_session
.as_ref()
.and_then(|session| session.data.get("chip"))
.and_then(|value| value.parse().ok())
.unwrap_or(10)
}
pub fn set_roulette_chip(&mut self, chip: u64) {
if let Some(session) = self.current_session.as_mut() {
session.data.insert("chip".to_string(), chip.to_string());
}
}
pub fn set_roulette_number_input(&mut self, value: &str) {
if let Some(session) = self.current_session.as_mut() {
session
.data
.insert("number_input".to_string(), value.to_string());
}
}
pub fn set_roulette_status(&mut self, status: &str) {
if let Some(session) = self.current_session.as_mut() {
session
.data
.insert("status".to_string(), status.to_string());
if status != "error" {
session.data.remove("error");
}
}
}
pub fn apply_roulette_result(&mut self, result: RouletteResult) {
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("bets".to_string(), result.bets);
session
.data
.insert("number".to_string(), result.number.to_string());
session.data.insert("color".to_string(), result.color);
session
.data
.insert("winnings".to_string(), result.winnings.to_string());
session
.data
.insert("balance".to_string(), result.balance.to_string());
session
.data
.insert("status".to_string(), "result".to_string());
session
.data
.insert("number_input".to_string(), String::new());
}
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);
}
if session.game_type == "roulette" {
session
.data
.insert("chip".to_string(), bet.max(10).to_string());
return Ok(bet.max(10));
}
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)
}
}
|