summaryrefslogtreecommitdiffstats
path: root/src/client/games/blackjack.rs
blob: a34eb63dd0395f04b2553e8deff35f1de91f949d (plain) (blame)
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
use super::traits::{Game, GameState};
use crate::blackjack::{card_label, BlackjackState};

pub const BLACKJACK_BOARD_LINES: usize = 6;

pub struct BlackjackGame;

impl BlackjackGame {
    pub fn new() -> Self {
        Self
    }

    fn state(state: &GameState) -> Option<BlackjackState> {
        state
            .data
            .get("blackjack")
            .and_then(|raw| serde_json::from_str(raw).ok())
    }

    fn cards(cards: &[String]) -> String {
        cards
            .iter()
            .map(|card| {
                if card == "?" {
                    "[ ? ]".to_string()
                } else {
                    format!("[{}]", card_label(card))
                }
            })
            .collect::<Vec<_>>()
            .join(" ")
    }

    fn result_text(result: &str, winnings: u64) -> String {
        match result {
            "blackjack" => format!("BLACKJACK! Won {winnings} PLN"),
            "win" => format!("Win! Won {winnings} PLN"),
            "push" => "Push. Bet returned.".to_string(),
            "lose" => "Dealer wins.".to_string(),
            "bust" => "Bust.".to_string(),
            "cancelled" => "Cancelled.".to_string(),
            _ => "Hand complete.".to_string(),
        }
    }
}

impl Game for BlackjackGame {
    fn get_name(&self) -> &'static str {
        "Blackjack"
    }

    fn render(&self, state: &GameState) -> String {
        let status = state
            .data
            .get("status")
            .map(String::as_str)
            .unwrap_or("ready");
        let balance = state
            .data
            .get("balance")
            .map(String::as_str)
            .unwrap_or("?");
        let hand = Self::state(state);

        if let Some(error) = state.data.get("error") {
            let bet = hand.as_ref().map(|hand| hand.bet).unwrap_or(state.bet_amount);
            return format!(
                "=== BLACKJACK ===\r\nBalance: {balance} PLN   Bet: {bet} PLN\r\n\r\n\r\nERROR: {error}\r\n[Q] Quit"
            );
        }

        let (dealer, dealer_total, player, player_total, bet, text, controls) =
            if let Some(hand) = &hand {
                let bet = if status == "player" {
                    hand.bet
                } else {
                    state.bet_amount
                };
                let text = match status {
                    "player" => {
                        if hand.actions.iter().any(|action| action == "double") {
                            "Hit, stand or double.".to_string()
                        } else {
                            "Hit or stand.".to_string()
                        }
                    }
                    "dealing" => "Dealing...".to_string(),
                    "cancelling" => "Cancelling...".to_string(),
                    "done" => Self::result_text(&hand.result, hand.winnings),
                    _ => "Press [SPACE] to deal.".to_string(),
                };
                let controls = if status == "player" {
                    if hand.actions.iter().any(|action| action == "double") {
                        "[H]it [S]tand [D]ouble [Q]uit".to_string()
                    } else {
                        "[H]it [S]tand [Q]uit".to_string()
                    }
                } else {
                    "[SPACE] New hand   [+/-] Bet   [Q] Quit".to_string()
                };
                (
                    Self::cards(&hand.dealer),
                    hand.dealer_total,
                    Self::cards(&hand.player),
                    hand.player_total,
                    bet,
                    text,
                    controls,
                )
            } else {
                (
                    "—".to_string(),
                    0,
                    "—".to_string(),
                    0,
                    state.bet_amount,
                    "Press [SPACE] to deal.".to_string(),
                    "[SPACE] Deal   [+/-] Bet   [Q] Quit".to_string(),
                )
            };

        format!(
            "=== BLACKJACK ===\r\nBalance: {} PLN   Bet: {} PLN\r\nDealer: {}   {}\r\nYou:    {}   {}\r\n{}\r\n{}",
            balance, bet, dealer, dealer_total, player, player_total, text, controls
        )
    }

    fn render_lines(&self, _state: &GameState) -> usize {
        BLACKJACK_BOARD_LINES
    }

    fn get_help(&self) -> String {
        "BLACKJACK\n\
        =========\n\
        Six-deck shoe, fresh for every hand.\n\
        Dealer stands on all 17s.\n\
        Blackjack pays 3:2.\n\
        Double is allowed on the first two cards.\n\
        \n\
        H - hit\n\
        S - stand\n\
        D - double (when available)\n\
        SPACE - deal a new hand\n\
        +/- - change the next hand's bet\n\
        Q - leave the table"
            .to_string()
    }
}