From 37dedc342c087506e502c78a952b45b1c91602fb Mon Sep 17 00:00:00 2001 From: salmonik Date: Sun, 1 Nov 2026 18:03:24 +0100 Subject: v4.0.2: port slots, blackjack and crash to structured views --- src/client/games/crash.rs | 67 ++++++++++++++++++++++++----------------------- 1 file changed, 34 insertions(+), 33 deletions(-) (limited to 'src/client/games/crash.rs') diff --git a/src/client/games/crash.rs b/src/client/games/crash.rs index 383e807..affb642 100644 --- a/src/client/games/crash.rs +++ b/src/client/games/crash.rs @@ -1,4 +1,5 @@ use super::traits::{Game, GameState}; +use crate::client::view::CrashView; pub const CRASH_BOARD_LINES: usize = 6; @@ -9,67 +10,67 @@ impl CrashGame { Self } - fn value<'a>(state: &'a GameState, key: &str) -> Option<&'a str> { - state.data.get(key).map(String::as_str) - } - - fn status_text(state: &GameState) -> String { - let status = Self::value(state, "status").unwrap_or("ready"); - match status { + fn status_text(view: &CrashView) -> String { + match view.status.as_str() { "starting" => "Starting...".to_string(), "running" => "Rising... cash out before it crashes.".to_string(), "cashed" => format!( - "Cashed out at {}x Won {} Kr", - Self::value(state, "multiplier").unwrap_or("1.00"), - Self::value(state, "payout").unwrap_or("0") + "Cashed out at {:.2}x Won {} Kr", + view.multiplier, view.payout ), "crashed" => format!( "CRASHED at {}x Bet lost", - Self::value(state, "crash_point").unwrap_or("?") + view.crash_point + .map(|point| format!("{point:.2}")) + .unwrap_or_else(|| "?".to_string()) ), "forfeit" => "Forfeited. Bet lost.".to_string(), _ => "Press [SPACE] to start.".to_string(), } } - fn controls(state: &GameState) -> &'static str { - if Self::value(state, "status") == Some("running") { + fn controls(view: &CrashView) -> &'static str { + if view.status == "running" { "[C] Cash out [Q] Forfeit" } else { "[SPACE] Play [+/=/-] Bet [Q] Quit" } } -} -impl Game for CrashGame { - fn get_name(&self) -> &'static str { - "Crash" - } - - fn render(&self, state: &GameState) -> String { - let balance = Self::value(state, "balance").unwrap_or("?"); - let multiplier = Self::value(state, "multiplier").unwrap_or("1.00"); - let potential = Self::value(state, "potential") - .map(str::to_string) - .unwrap_or_else(|| state.bet_amount.to_string()); + /// Render a crash view to terminal lines. The GUI paints the same view. + pub fn render_view(view: &CrashView) -> String { + let balance = view + .balance + .map(|balance| balance.to_string()) + .unwrap_or_else(|| "?".to_string()); - if let Some(error) = Self::value(state, "error") { + if let Some(error) = &view.error { return format!( "=== CRASH ===\r\nBalance: {balance} Kr Bet: {} Kr\r\n\r\nERROR: {error}\r\n\r\n[SPACE] Play [+/=/-] Bet [Q] Quit", - state.bet_amount + view.bet ); } format!( - "=== CRASH ===\r\nBalance: {} Kr Bet: {} Kr\r\nMultiplier: {}x\r\nPotential: {} Kr\r\n{}\r\n{}", + "=== CRASH ===\r\nBalance: {} Kr Bet: {} Kr\r\nMultiplier: {:.2}x\r\nPotential: {} Kr\r\n{}\r\n{}", balance, - state.bet_amount, - multiplier, - potential, - Self::status_text(state), - Self::controls(state) + view.bet, + view.multiplier, + view.potential, + Self::status_text(view), + Self::controls(view) ) } +} + +impl Game for CrashGame { + fn get_name(&self) -> &'static str { + "Crash" + } + + fn render(&self, state: &GameState) -> String { + Self::render_view(&CrashView::from_state(state)) + } fn render_lines(&self, _state: &GameState) -> usize { CRASH_BOARD_LINES -- cgit v1.2.3