summaryrefslogtreecommitdiffstats
path: root/src/client/games/crash.rs
diff options
context:
space:
mode:
authorsalmonik <salmonik@tuta.io>2026-11-01 18:03:24 +0100
committersalmonik <salmonik@tuta.io>2026-11-01 18:03:24 +0100
commit37dedc342c087506e502c78a952b45b1c91602fb (patch)
tree85f4bf0c17823ba670d2d49c921191ca27aca64b /src/client/games/crash.rs
parentv4.0.1: introduce Command/View and port roulette to the shared boundary (diff)
downloadzagan-bet-37dedc342c087506e502c78a952b45b1c91602fb.tar.gz
zagan-bet-37dedc342c087506e502c78a952b45b1c91602fb.zip
v4.0.2: port slots, blackjack and crash to structured views
Diffstat (limited to '')
-rw-r--r--src/client/games/crash.rs67
1 files changed, 34 insertions, 33 deletions
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 @@
1use super::traits::{Game, GameState}; 1use super::traits::{Game, GameState};
2use crate::client::view::CrashView;
2 3
3pub const CRASH_BOARD_LINES: usize = 6; 4pub const CRASH_BOARD_LINES: usize = 6;
4 5
@@ -9,67 +10,67 @@ impl CrashGame {
9 Self 10 Self
10 } 11 }
11 12
12 fn value<'a>(state: &'a GameState, key: &str) -> Option<&'a str> { 13 fn status_text(view: &CrashView) -> String {
13 state.data.get(key).map(String::as_str) 14 match view.status.as_str() {
14 }
15
16 fn status_text(state: &GameState) -> String {
17 let status = Self::value(state, "status").unwrap_or("ready");
18 match status {
19 "starting" => "Starting...".to_string(), 15 "starting" => "Starting...".to_string(),
20 "running" => "Rising... cash out before it crashes.".to_string(), 16 "running" => "Rising... cash out before it crashes.".to_string(),
21 "cashed" => format!( 17 "cashed" => format!(
22 "Cashed out at {}x Won {} Kr", 18 "Cashed out at {:.2}x Won {} Kr",
23 Self::value(state, "multiplier").unwrap_or("1.00"), 19 view.multiplier, view.payout
24 Self::value(state, "payout").unwrap_or("0")
25 ), 20 ),
26 "crashed" => format!( 21 "crashed" => format!(
27 "CRASHED at {}x Bet lost", 22 "CRASHED at {}x Bet lost",
28 Self::value(state, "crash_point").unwrap_or("?") 23 view.crash_point
24 .map(|point| format!("{point:.2}"))
25 .unwrap_or_else(|| "?".to_string())
29 ), 26 ),
30 "forfeit" => "Forfeited. Bet lost.".to_string(), 27 "forfeit" => "Forfeited. Bet lost.".to_string(),
31 _ => "Press [SPACE] to start.".to_string(), 28 _ => "Press [SPACE] to start.".to_string(),
32 } 29 }
33 } 30 }
34 31
35 fn controls(state: &GameState) -> &'static str { 32 fn controls(view: &CrashView) -> &'static str {
36 if Self::value(state, "status") == Some("running") { 33 if view.status == "running" {
37 "[C] Cash out [Q] Forfeit" 34 "[C] Cash out [Q] Forfeit"
38 } else { 35 } else {
39 "[SPACE] Play [+/=/-] Bet [Q] Quit" 36 "[SPACE] Play [+/=/-] Bet [Q] Quit"
40 } 37 }
41 } 38 }
42}
43 39
44impl Game for CrashGame { 40 /// Render a crash view to terminal lines. The GUI paints the same view.
45 fn get_name(&self) -> &'static str { 41 pub fn render_view(view: &CrashView) -> String {
46 "Crash" 42 let balance = view
47 } 43 .balance
48 44 .map(|balance| balance.to_string())
49 fn render(&self, state: &GameState) -> String { 45 .unwrap_or_else(|| "?".to_string());
50 let balance = Self::value(state, "balance").unwrap_or("?");
51 let multiplier = Self::value(state, "multiplier").unwrap_or("1.00");
52 let potential = Self::value(state, "potential")
53 .map(str::to_string)
54 .unwrap_or_else(|| state.bet_amount.to_string());
55 46
56 if let Some(error) = Self::value(state, "error") { 47 if let Some(error) = &view.error {
57 return format!( 48 return format!(
58 "=== CRASH ===\r\nBalance: {balance} Kr Bet: {} Kr\r\n\r\nERROR: {error}\r\n\r\n[SPACE] Play [+/=/-] Bet [Q] Quit", 49 "=== CRASH ===\r\nBalance: {balance} Kr Bet: {} Kr\r\n\r\nERROR: {error}\r\n\r\n[SPACE] Play [+/=/-] Bet [Q] Quit",
59 state.bet_amount 50 view.bet
60 ); 51 );
61 } 52 }
62 53
63 format!( 54 format!(
64 "=== CRASH ===\r\nBalance: {} Kr Bet: {} Kr\r\nMultiplier: {}x\r\nPotential: {} Kr\r\n{}\r\n{}", 55 "=== CRASH ===\r\nBalance: {} Kr Bet: {} Kr\r\nMultiplier: {:.2}x\r\nPotential: {} Kr\r\n{}\r\n{}",
65 balance, 56 balance,
66 state.bet_amount, 57 view.bet,
67 multiplier, 58 view.multiplier,
68 potential, 59 view.potential,
69 Self::status_text(state), 60 Self::status_text(view),
70 Self::controls(state) 61 Self::controls(view)
71 ) 62 )
72 } 63 }
64}
65
66impl Game for CrashGame {
67 fn get_name(&self) -> &'static str {
68 "Crash"
69 }
70
71 fn render(&self, state: &GameState) -> String {
72 Self::render_view(&CrashView::from_state(state))
73 }
73 74
74 fn render_lines(&self, _state: &GameState) -> usize { 75 fn render_lines(&self, _state: &GameState) -> usize {
75 CRASH_BOARD_LINES 76 CRASH_BOARD_LINES