summaryrefslogtreecommitdiffstats
path: root/src/client/games/blackjack.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/client/games/blackjack.rs')
-rw-r--r--src/client/games/blackjack.rs59
1 files changed, 26 insertions, 33 deletions
diff --git a/src/client/games/blackjack.rs b/src/client/games/blackjack.rs
index 8fa5693..e8f251f 100644
--- a/src/client/games/blackjack.rs
+++ b/src/client/games/blackjack.rs
@@ -1,5 +1,6 @@
1use super::traits::{Game, GameState}; 1use super::traits::{Game, GameState};
2use crate::blackjack::{BlackjackState, card_label}; 2use crate::blackjack::card_label;
3use crate::client::view::BlackjackView;
3 4
4pub const BLACKJACK_BOARD_LINES: usize = 6; 5pub const BLACKJACK_BOARD_LINES: usize = 6;
5 6
@@ -10,13 +11,6 @@ impl BlackjackGame {
10 Self 11 Self
11 } 12 }
12 13
13 fn state(state: &GameState) -> Option<BlackjackState> {
14 state
15 .data
16 .get("blackjack")
17 .and_then(|raw| serde_json::from_str(raw).ok())
18 }
19
20 fn cards(cards: &[String]) -> String { 14 fn cards(cards: &[String]) -> String {
21 cards 15 cards
22 .iter() 16 .iter()
@@ -43,40 +37,29 @@ impl BlackjackGame {
43 _ => "Hand complete.".to_string(), 37 _ => "Hand complete.".to_string(),
44 } 38 }
45 } 39 }
46}
47 40
48impl Game for BlackjackGame { 41 /// Render a blackjack view to terminal lines. The GUI paints the same view.
49 fn get_name(&self) -> &'static str { 42 pub fn render_view(view: &BlackjackView) -> String {
50 "Blackjack" 43 let balance = view
51 } 44 .balance
52 45 .map(|balance| balance.to_string())
53 fn render(&self, state: &GameState) -> String { 46 .unwrap_or_else(|| "?".to_string());
54 let status = state
55 .data
56 .get("status")
57 .map(String::as_str)
58 .unwrap_or("ready");
59 let balance = state.data.get("balance").map(String::as_str).unwrap_or("?");
60 let hand = Self::state(state);
61 47
62 if let Some(error) = state.data.get("error") { 48 if let Some(error) = &view.error {
63 let bet = hand 49 let bet = view.hand.as_ref().map(|hand| hand.bet).unwrap_or(view.bet);
64 .as_ref()
65 .map(|hand| hand.bet)
66 .unwrap_or(state.bet_amount);
67 return format!( 50 return format!(
68 "=== BLACKJACK ===\r\nBalance: {balance} Kr Bet: {bet} Kr\r\n\r\n\r\nERROR: {error}\r\n[Q] Quit" 51 "=== BLACKJACK ===\r\nBalance: {balance} Kr Bet: {bet} Kr\r\n\r\n\r\nERROR: {error}\r\n[Q] Quit"
69 ); 52 );
70 } 53 }
71 54
72 let (dealer, dealer_total, player, player_total, bet, text, controls) = 55 let (dealer, dealer_total, player, player_total, bet, text, controls) =
73 if let Some(hand) = &hand { 56 if let Some(hand) = &view.hand {
74 let bet = if status == "player" { 57 let bet = if view.status == "player" {
75 hand.bet 58 hand.bet
76 } else { 59 } else {
77 state.bet_amount 60 view.bet
78 }; 61 };
79 let text = match status { 62 let text = match view.status.as_str() {
80 "player" => { 63 "player" => {
81 if hand.actions.iter().any(|action| action == "double") { 64 if hand.actions.iter().any(|action| action == "double") {
82 "Hit, stand or double.".to_string() 65 "Hit, stand or double.".to_string()
@@ -89,7 +72,7 @@ impl Game for BlackjackGame {
89 "done" => Self::result_text(&hand.result, hand.winnings), 72 "done" => Self::result_text(&hand.result, hand.winnings),
90 _ => "Press [SPACE] to deal.".to_string(), 73 _ => "Press [SPACE] to deal.".to_string(),
91 }; 74 };
92 let controls = if status == "player" { 75 let controls = if view.status == "player" {
93 if hand.actions.iter().any(|action| action == "double") { 76 if hand.actions.iter().any(|action| action == "double") {
94 "[H]it [S]tand [D]ouble [Q] Forfeit".to_string() 77 "[H]it [S]tand [D]ouble [Q] Forfeit".to_string()
95 } else { 78 } else {
@@ -113,7 +96,7 @@ impl Game for BlackjackGame {
113 0, 96 0,
114 "—".to_string(), 97 "—".to_string(),
115 0, 98 0,
116 state.bet_amount, 99 view.bet,
117 "Press [SPACE] to deal.".to_string(), 100 "Press [SPACE] to deal.".to_string(),
118 "[SPACE] Deal [+/-] Bet [Q] Quit".to_string(), 101 "[SPACE] Deal [+/-] Bet [Q] Quit".to_string(),
119 ) 102 )
@@ -124,6 +107,16 @@ impl Game for BlackjackGame {
124 balance, bet, dealer, dealer_total, player, player_total, text, controls 107 balance, bet, dealer, dealer_total, player, player_total, text, controls
125 ) 108 )
126 } 109 }
110}
111
112impl Game for BlackjackGame {
113 fn get_name(&self) -> &'static str {
114 "Blackjack"
115 }
116
117 fn render(&self, state: &GameState) -> String {
118 Self::render_view(&BlackjackView::from_state(state))
119 }
127 120
128 fn render_lines(&self, _state: &GameState) -> usize { 121 fn render_lines(&self, _state: &GameState) -> usize {
129 BLACKJACK_BOARD_LINES 122 BLACKJACK_BOARD_LINES