summaryrefslogtreecommitdiffstats
path: root/src/client/games/manager.rs
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/client/games/manager.rs90
1 files changed, 90 insertions, 0 deletions
diff --git a/src/client/games/manager.rs b/src/client/games/manager.rs
index 4e0218b..4d9e6a8 100644
--- a/src/client/games/manager.rs
+++ b/src/client/games/manager.rs
@@ -1,9 +1,11 @@
1use super::blackjack::BlackjackGame; 1use super::blackjack::BlackjackGame;
2use super::crash::CrashGame;
2use super::response::{RouletteResult, SpinResult}; 3use super::response::{RouletteResult, SpinResult};
3use super::roulette::RouletteGame; 4use super::roulette::RouletteGame;
4use super::slots::SlotsGame; 5use super::slots::SlotsGame;
5use super::traits::{Game, GameError, GamePhase, GameState}; 6use super::traits::{Game, GameError, GamePhase, GameState};
6use crate::blackjack::BlackjackState; 7use crate::blackjack::BlackjackState;
8use crate::crash::{payout, CrashState};
7use crate::roulette::Bets; 9use crate::roulette::Bets;
8use std::collections::HashMap; 10use std::collections::HashMap;
9 11
@@ -18,6 +20,7 @@ impl GameManager {
18 games.insert("slots".to_string(), Box::new(SlotsGame::new())); 20 games.insert("slots".to_string(), Box::new(SlotsGame::new()));
19 games.insert("roulette".to_string(), Box::new(RouletteGame::new())); 21 games.insert("roulette".to_string(), Box::new(RouletteGame::new()));
20 games.insert("blackjack".to_string(), Box::new(BlackjackGame::new())); 22 games.insert("blackjack".to_string(), Box::new(BlackjackGame::new()));
23 games.insert("crash".to_string(), Box::new(CrashGame::new()));
21 24
22 Self { 25 Self {
23 current_session: None, 26 current_session: None,
@@ -57,6 +60,10 @@ impl GameManager {
57 } else if game_type == "blackjack" { 60 } else if game_type == "blackjack" {
58 data.insert("status".to_string(), "ready".to_string()); 61 data.insert("status".to_string(), "ready".to_string());
59 10 62 10
63 } else if game_type == "crash" {
64 data.insert("status".to_string(), "ready".to_string());
65 data.insert("multiplier".to_string(), "1.00".to_string());
66 10
60 } else { 67 } else {
61 10 68 10
62 }; 69 };
@@ -173,11 +180,19 @@ impl GameManager {
173 session.data.remove("winnings"); 180 session.data.remove("winnings");
174 session.data.remove("number"); 181 session.data.remove("number");
175 session.data.remove("color"); 182 session.data.remove("color");
183 session.data.remove("payout");
184 session.data.remove("crash_point");
176 if session.game_type == "roulette" { 185 if session.game_type == "roulette" {
177 session 186 session
178 .data 187 .data
179 .insert("status".to_string(), "place".to_string()); 188 .insert("status".to_string(), "place".to_string());
180 } 189 }
190 if session.game_type == "crash" {
191 session
192 .data
193 .insert("status".to_string(), "ready".to_string());
194 session.data.remove("session_id");
195 }
181 session.data.insert("error".to_string(), message.into()); 196 session.data.insert("error".to_string(), message.into());
182 } 197 }
183 198
@@ -309,6 +324,81 @@ impl GameManager {
309 } 324 }
310 } 325 }
311 326
327 pub fn apply_crash_state(&mut self, state: CrashState) {
328 let Some(session) = self.current_session.as_mut() else {
329 return;
330 };
331 let running = state.is_running();
332 session.phase = if running {
333 GamePhase::Playing
334 } else {
335 GamePhase::Finished
336 };
337 session.bet_amount = state.bet;
338 session.data.remove("error");
339 for key in ["crash_point", "payout"] {
340 session.data.remove(key);
341 }
342 session.data.insert("status".to_string(), state.phase.clone());
343 session
344 .data
345 .insert("session_id".to_string(), state.session_id);
346 session
347 .data
348 .insert("balance".to_string(), state.balance.to_string());
349 session
350 .data
351 .insert("multiplier".to_string(), format!("{:.2}", state.multiplier));
352 session
353 .data
354 .insert("potential".to_string(), state.potential.to_string());
355 session
356 .data
357 .insert("payout".to_string(), state.payout.to_string());
358 if let Some(crash_point) = state.crash_point {
359 session
360 .data
361 .insert("crash_point".to_string(), format!("{crash_point:.2}"));
362 }
363 }
364
365 /// Redraw the live multiplier from the client's local clock.
366 pub fn set_crash_multiplier(&mut self, multiplier: f64) {
367 let Some(session) = self.current_session.as_mut() else {
368 return;
369 };
370 let bet = session.bet_amount;
371 session
372 .data
373 .insert("multiplier".to_string(), format!("{multiplier:.2}"));
374 session
375 .data
376 .insert("potential".to_string(), payout(bet, multiplier).to_string());
377 }
378
379 pub fn set_crash_status(&mut self, status: &str) {
380 if let Some(session) = self.current_session.as_mut() {
381 session
382 .data
383 .insert("status".to_string(), status.to_string());
384 }
385 }
386
387 pub fn crash_status(&self) -> String {
388 self.current_session
389 .as_ref()
390 .and_then(|session| session.data.get("status"))
391 .cloned()
392 .unwrap_or_else(|| "ready".to_string())
393 }
394
395 pub fn crash_session_id(&self) -> Option<String> {
396 self.current_session
397 .as_ref()
398 .and_then(|session| session.data.get("session_id"))
399 .cloned()
400 }
401
312 pub fn get_current_game(&self) -> Option<&dyn Game> { 402 pub fn get_current_game(&self) -> Option<&dyn Game> {
313 self.current_session 403 self.current_session
314 .as_ref() 404 .as_ref()