terminal-poker 1.0.1

Heads-up No-Limit Texas Hold'em poker for the terminal
Documentation
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
use std::collections::VecDeque;
use std::time::{Duration, Instant};

use crate::bot::rule_based::RuleBasedBot;
use crate::game::actions::Action;
use crate::game::state::{GamePhase, GameState, Player, BIG_BLIND, SMALL_BLIND};
use crate::stats::persistence::StatsStore;

const DELAY_BOT_ACTION_MS: u64 = 2500;
const DELAY_BOT_ACTION_AFTER_REVEAL_MS: u64 = 3500;
const DELAY_CARD_REVEAL_MS: u64 = 500;
const DELAY_CARD_REVEAL_AFTER_BOT_MS: u64 = 2500;
const DELAY_NEW_HAND_MS: u64 = 1200;
const DELAY_SHOWDOWN_REVEAL_MS: u64 = 1000;
const DELAY_SHOWDOWN_RESULT_MS: u64 = 1000;
const DELAY_POST_SB_MS: u64 = 500;
const DELAY_POST_BB_MS: u64 = 800;
const DELAY_ALLIN_RUNOUT_MS: u64 = 1200;

#[derive(Debug, Clone)]
pub enum GameEvent {
    BotAction,
    StartNewHand,
    PostSmallBlind,
    PostBigBlind,
    RevealCards,
    RevealShowdown,
    ShowResult,
}

#[derive(Debug, Clone)]
pub struct ActionLogEntry {
    pub street: String,
    pub text: String,
}

pub struct App {
    pub game_state: GameState,
    pub bot: RuleBasedBot,
    pub show_help: bool,
    pub show_stats: bool,
    pub raise_input: String,
    pub message: Option<String>,
    pub action_log: Vec<ActionLogEntry>,
    pub pending_events: VecDeque<GameEvent>,
    pub next_event_at: Option<Instant>,
    pub raise_mode: bool,
    pub visible_board_len: usize,
    pub visible_player_bet: u32,
    pub visible_bot_bet: u32,
    pub player_last_action: Option<Action>,
    pub bot_last_action: Option<Action>,
    pub bot_thinking: bool,
    pub tick_count: u64,
    pub thinking_start_tick: u64,
    pub showdown_revealed: bool,
    pub showdown_result_shown: bool,
    starting_stack_bb: u32,
    last_phase: GamePhase,
    saw_flop_this_hand: bool,
    recorded_vpip_this_hand: bool,
    three_bet_opportunity_recorded: bool,
    player_raised_preflop: bool,
    cbet_opportunity_recorded: bool,
    facing_cbet: bool,
}

impl App {
    pub fn new(starting_stack_bb: u32, aggression: f64) -> Self {
        let game_state = GameState::new(starting_stack_bb);
        let initial_phase = game_state.phase;
        Self {
            game_state,
            bot: RuleBasedBot::new(aggression),
            show_help: false,
            show_stats: false,
            raise_input: String::new(),
            message: None,
            action_log: Vec::new(),
            pending_events: VecDeque::new(),
            next_event_at: None,
            raise_mode: false,
            visible_board_len: 0,

            visible_player_bet: 0,
            visible_bot_bet: 0,
            player_last_action: None,
            bot_last_action: None,
            bot_thinking: false,
            tick_count: 0,
            thinking_start_tick: 0,
            showdown_revealed: false,
            showdown_result_shown: false,
            starting_stack_bb,
            last_phase: initial_phase,
            saw_flop_this_hand: false,
            recorded_vpip_this_hand: false,
            three_bet_opportunity_recorded: false,
            player_raised_preflop: false,
            cbet_opportunity_recorded: false,
            facing_cbet: false,
        }
    }

    pub fn toggle_help(&mut self) {
        self.show_help = !self.show_help;
        if self.show_help {
            self.show_stats = false;
        }
    }

    pub fn toggle_stats(&mut self) {
        self.show_stats = !self.show_stats;
        if self.show_stats {
            self.show_help = false;
        }
    }

    pub fn new_session(&mut self, stats: &mut StatsStore) {
        self.game_state = GameState::new(self.starting_stack_bb);
        self.last_phase = self.game_state.phase;
        self.saw_flop_this_hand = false;
        self.recorded_vpip_this_hand = false;
        self.three_bet_opportunity_recorded = false;
        self.player_raised_preflop = false;
        self.cbet_opportunity_recorded = false;
        self.facing_cbet = false;
        self.action_log.clear();
        self.pending_events.clear();
        self.next_event_at = None;
        self.raise_mode = false;
        self.raise_input.clear();
        self.visible_board_len = 0;
        self.player_last_action = None;
        self.bot_last_action = None;
        self.bot_thinking = false;
        self.showdown_revealed = false;
        self.showdown_result_shown = false;
        self.message = Some("New session started!".to_string());
        self.initialize(stats);
    }

    pub fn has_pending_events(&self) -> bool {
        !self.pending_events.is_empty()
    }

    fn phase_name(phase: GamePhase) -> &'static str {
        match phase {
            GamePhase::Preflop => "Pre-Flop",
            GamePhase::Flop => "Flop",
            GamePhase::Turn => "Turn",
            GamePhase::River => "River",
            _ => "",
        }
    }

    fn log_action(&mut self, street: &str, text: String) {
        self.action_log.push(ActionLogEntry {
            street: street.to_string(),
            text,
        });
        // Keep a reasonable history (visible area handles scrolling)
        if self.action_log.len() > 100 {
            self.action_log.drain(..50);
        }
    }

    pub fn apply_player_action(&mut self, action: Action, stats: &mut StatsStore) {
        if !self.game_state.is_player_turn() {
            return;
        }

        // Determine if this action is truly aggressive
        // AllIn is only aggressive if it exceeds the current max bet (otherwise it's a call)
        let is_aggressive_action = match action {
            Action::Bet(_) | Action::Raise(_) => true,
            Action::AllIn(amount) => amount > self.game_state.max_bet(),
            _ => false,
        };

        // 3-bet tracking: preflop, facing bot's raise, player hasn't raised yet
        // Must run BEFORE player_raised_preflop is set below
        if !self.three_bet_opportunity_recorded
            && self.game_state.board.is_empty()
            && self.game_state.last_aggressor == Some(Player::Bot)
            && self.game_state.amount_to_call(Player::Human) > 0
            && !self.player_raised_preflop
        {
            self.three_bet_opportunity_recorded = true;
            stats.record_three_bet_opportunity();
            if is_aggressive_action {
                stats.record_three_bet();
            }
        }

        // Track whether player has raised preflop (to distinguish 3-bet from 4-bet+)
        if self.game_state.board.is_empty() && is_aggressive_action {
            self.player_raised_preflop = true;
        }

        // C-bet tracking: flop, player was preflop aggressor, no bet yet this street
        if !self.cbet_opportunity_recorded
            && self.game_state.phase == GamePhase::Flop
            && self.game_state.preflop_aggressor == Some(Player::Human)
            && self.game_state.last_aggressor.is_none()
        {
            self.cbet_opportunity_recorded = true;
            stats.record_cbet_opportunity();
            if is_aggressive_action {
                stats.record_cbet();
            }
        }

        // Fold-to-cbet tracking: player faces a c-bet from the bot
        if self.facing_cbet {
            stats.record_fold_to_cbet_opportunity();
            if matches!(action, Action::Fold) {
                stats.record_fold_to_cbet();
            }
            self.facing_cbet = false;
        }

        // Record action type stats
        match action {
            Action::Call(_) => stats.record_call(),
            Action::Bet(_) => {
                if self.game_state.board.is_empty() {
                    // Preflop: all bets are raises (blinds are forced bets)
                    stats.record_raise();
                    stats.record_pfr();
                } else {
                    stats.record_bet();
                }
            }
            Action::Raise(_) => {
                stats.record_raise();
                if self.game_state.board.is_empty() {
                    stats.record_pfr();
                }
            }
            Action::AllIn(_) => {
                if is_aggressive_action {
                    stats.record_raise();
                    if self.game_state.board.is_empty() {
                        stats.record_pfr();
                    }
                } else {
                    stats.record_call();
                }
            }
            _ => {}
        }

        // VPIP: only track preflop voluntary money, once per hand
        if !self.recorded_vpip_this_hand
            && self.game_state.board.is_empty()
            && !matches!(action, Action::Fold | Action::Check)
        {
            stats.record_vpip();
            self.recorded_vpip_this_hand = true;
        }

        self.raise_mode = false;
        self.raise_input.clear();
        self.player_last_action = Some(action);

        // Snapshot visible state before apply_action (which may advance phase and clear bets/pot)
        self.visible_player_bet = self.projected_bet(Player::Human, action);
        self.visible_bot_bet = self.game_state.bot_bet;

        let street = Self::phase_name(self.game_state.phase);
        let desc = action.description_for("You");
        self.game_state.apply_action(Player::Human, action);
        self.log_action(street, format!("You {}", desc));
        self.message = Some(format!("You {}", desc));

        // Enqueue follow-up events
        self.enqueue_next_events(stats);
    }

    /// Inspect the current game state and enqueue exactly one pending event
    /// with an appropriate delay. Does nothing if events are already pending.
    pub fn enqueue_next_events(&mut self, stats: &mut StatsStore) {
        if !self.pending_events.is_empty() {
            return;
        }

        // Track flop stat
        if !self.saw_flop_this_hand && self.game_state.board.len() >= 3 {
            self.saw_flop_this_hand = true;
            stats.record_saw_flop();
        }

        // Detect street transitions for extra pause
        let phase_changed = self.game_state.phase != self.last_phase;
        if phase_changed {
            self.last_phase = self.game_state.phase;
        }

        match self.game_state.phase {
            GamePhase::HandComplete => {
                // Log the fold result
                if let Some((player, _)) = self.game_state.last_action {
                    let winner_text = if player == Player::Bot {
                        "You win the pot"
                    } else {
                        "Opp wins the pot"
                    };
                    self.log_action("", winner_text.to_string());
                }
                if self.game_state.player_stack > 0 && self.game_state.bot_stack > 0 {
                    self.pending_events.push_back(GameEvent::StartNewHand);
                    self.next_event_at =
                        Some(Instant::now() + Duration::from_millis(DELAY_NEW_HAND_MS));
                }
                // else: session over, main loop detects busted stacks on HandComplete
            }
            GamePhase::Showdown => {
                self.pending_events
                    .push_back(GameEvent::RevealShowdown);
                self.next_event_at =
                    Some(Instant::now() + Duration::from_millis(DELAY_SHOWDOWN_REVEAL_MS));
            }
            GamePhase::SessionEnd | GamePhase::Summary => {
                // Terminal states — nothing to enqueue
            }
            _ => {
                if phase_changed && self.visible_board_len < self.game_state.board.len() {
                    // Street transition delay
                    let reveal_delay =
                        if self.game_state.player_stack == 0 || self.game_state.bot_stack == 0 {
                            // All-in runout: consistent pacing between streets
                            DELAY_ALLIN_RUNOUT_MS
                        } else if self.game_state.last_action.map(|(p, _)| p) == Some(Player::Bot)
                        {
                            // Normal: longer pause after bot's closing action
                            DELAY_CARD_REVEAL_AFTER_BOT_MS
                        } else {
                            DELAY_CARD_REVEAL_MS
                        };
                    self.pending_events.push_back(GameEvent::RevealCards);
                    self.next_event_at =
                        Some(Instant::now() + Duration::from_millis(reveal_delay));
                } else if self.game_state.to_act == Player::Bot {
                    self.pending_events.push_back(GameEvent::BotAction);
                    self.next_event_at =
                        Some(Instant::now() + Duration::from_millis(DELAY_BOT_ACTION_MS));
                    self.bot_thinking = true;
                    self.thinking_start_tick = self.tick_count;
                    self.bot_last_action = None;
                }
                // else: player's turn, wait for input
            }
        }
    }

    /// Process the next pending event if its delay has elapsed.
    /// Called every iteration of the main loop.
    pub fn process_next_event(&mut self, stats: &mut StatsStore) {
        let event_time = match self.next_event_at {
            Some(t) => t,
            None => return,
        };
        if Instant::now() < event_time {
            return;
        }

        let event = match self.pending_events.pop_front() {
            Some(e) => e,
            None => {
                self.next_event_at = None;
                return;
            }
        };

        match event {
            GameEvent::BotAction => {
                self.bot_thinking = false;
                let street = Self::phase_name(self.game_state.phase);
                let bot_action = self.bot.decide(&self.game_state);
                self.bot_last_action = Some(bot_action);

                // Detect bot c-bet: flop, bot was preflop aggressor, no bet yet, aggressive action
                let bot_is_aggressive = match bot_action {
                    Action::Bet(_) | Action::Raise(_) => true,
                    Action::AllIn(amount) => amount > self.game_state.max_bet(),
                    _ => false,
                };
                if self.game_state.phase == GamePhase::Flop
                    && self.game_state.preflop_aggressor == Some(Player::Bot)
                    && self.game_state.last_aggressor.is_none()
                    && bot_is_aggressive
                {
                    self.facing_cbet = true;
                }

                // Snapshot visible bets before apply_action (which may advance phase and clear bets)
                self.visible_bot_bet = self.projected_bet(Player::Bot, bot_action);
                self.visible_player_bet = self.game_state.player_bet;

                let desc = bot_action.description_for("Opp");
                self.game_state.apply_action(Player::Bot, bot_action);
                self.log_action(street, format!("Opp {}", desc));
                self.message = Some(format!("Opp {}", desc));
            }
            GameEvent::StartNewHand => {
                self.saw_flop_this_hand = false;
                self.recorded_vpip_this_hand = false;
                self.three_bet_opportunity_recorded = false;
                self.player_raised_preflop = false;
                self.cbet_opportunity_recorded = false;
                self.facing_cbet = false;
                stats.record_hand_start();
                self.raise_mode = false;
                self.raise_input.clear();
                self.player_last_action = None;
                self.bot_last_action = None;
                self.showdown_revealed = false;
                self.showdown_result_shown = false;
                self.game_state.start_new_hand();
                self.visible_board_len = 0;
                self.visible_player_bet = 0;
                self.visible_bot_bet = 0;
                self.last_phase = self.game_state.phase;
                // Add a separator for the new hand in the historical log
                self.action_log.push(ActionLogEntry {
                    street: String::new(),
                    text: format!("── Hand #{} ──", self.game_state.hand_number),
                });
                self.log_blinds();
                self.pending_events.push_back(GameEvent::PostSmallBlind);
                self.next_event_at =
                    Some(Instant::now() + Duration::from_millis(DELAY_POST_SB_MS));
                return;
            }
            GameEvent::PostSmallBlind => {
                match self.game_state.button {
                    Player::Human => self.visible_player_bet = self.game_state.player_bet,
                    Player::Bot => self.visible_bot_bet = self.game_state.bot_bet,
                }
                self.pending_events.push_back(GameEvent::PostBigBlind);
                self.next_event_at =
                    Some(Instant::now() + Duration::from_millis(DELAY_POST_BB_MS));
                return;
            }
            GameEvent::PostBigBlind => {
                match self.game_state.button {
                    Player::Human => self.visible_bot_bet = self.game_state.bot_bet,
                    Player::Bot => self.visible_player_bet = self.game_state.player_bet,
                }
            }
            GameEvent::RevealCards => {
                self.visible_board_len = self.game_state.board.len();
                self.visible_player_bet = 0;
                self.visible_bot_bet = 0;
                self.player_last_action = None;
                self.bot_last_action = None;

                // All-in runout: skip betting and advance to next street
                if self.game_state.player_stack == 0 || self.game_state.bot_stack == 0 {
                    self.game_state.advance_phase();
                    // Fall through to enqueue_next_events which queues
                    // the next RevealCards or RevealShowdown
                } else {
                    // Normal: check if bot should act next
                    if self.game_state.to_act == Player::Bot {
                        self.pending_events.push_back(GameEvent::BotAction);
                        self.next_event_at =
                            Some(Instant::now() + Duration::from_millis(DELAY_BOT_ACTION_AFTER_REVEAL_MS));
                        self.bot_thinking = true;
                        self.thinking_start_tick = self.tick_count;
                        return;
                    }
                }
            }
            GameEvent::RevealShowdown => {
                self.showdown_revealed = true;
                self.player_last_action = None;
                self.bot_last_action = None;
                // Record stats
                if let Some(ref result) = self.game_state.showdown_result {
                    let won = result.winner == Some(Player::Human);
                    stats.record_showdown(won);
                    if won {
                        stats.record_pot_won(result.pot_won);
                    } else if result.winner == Some(Player::Bot) {
                        stats.record_pot_lost(result.pot_won);
                    }
                }
                self.pending_events.push_back(GameEvent::ShowResult);
                self.next_event_at =
                    Some(Instant::now() + Duration::from_millis(DELAY_SHOWDOWN_RESULT_MS));
                return;
            }
            GameEvent::ShowResult => {
                self.showdown_result_shown = true;
                self.next_event_at = None;
                return;
            }
        }

        // Clear timer and check what to do next
        self.next_event_at = None;
        self.enqueue_next_events(stats);
    }

    fn log_blinds(&mut self) {
        let sb_bb = SMALL_BLIND as f64 / BIG_BLIND as f64;
        let (sb_player, bb_player) = if self.game_state.button == Player::Human {
            ("You", "Opp")
        } else {
            ("Opp", "You")
        };
        self.log_action(
            "Pre-Flop",
            format!("{} post SB ({:.1}BB)", sb_player, sb_bb),
        );
        self.log_action("Pre-Flop", format!("{} post BB (1BB)", bb_player));
    }

    pub fn initialize(&mut self, stats: &mut StatsStore) {
        stats.record_hand_start();
        self.visible_player_bet = 0;
        self.visible_bot_bet = 0;
        self.log_blinds();
        self.pending_events.push_back(GameEvent::PostSmallBlind);
        self.next_event_at = Some(Instant::now() + Duration::from_millis(DELAY_POST_SB_MS));
    }

    /// Compute what a player's bet will be after an action, before apply_action clears it.
    fn projected_bet(&self, player: Player, action: Action) -> u32 {
        let current = match player {
            Player::Human => self.game_state.player_bet,
            Player::Bot => self.game_state.bot_bet,
        };
        let stack = match player {
            Player::Human => self.game_state.player_stack,
            Player::Bot => self.game_state.bot_stack,
        };
        match action {
            Action::Fold | Action::Check => current,
            Action::Call(amount) => current + amount.min(stack),
            Action::Bet(amount) | Action::Raise(amount) => {
                let to_add = amount - current;
                current + to_add.min(stack)
            }
            Action::AllIn(amount) => {
                let to_add = amount - current;
                current + to_add.min(stack)
            }
        }
    }

    pub fn continue_after_showdown(&mut self, _stats: &mut StatsStore) {
        if self.game_state.phase == GamePhase::Showdown && self.showdown_result_shown {
            self.pending_events.clear();
            if self.game_state.player_stack > 0 && self.game_state.bot_stack > 0 {
                self.pending_events.push_back(GameEvent::StartNewHand);
                self.next_event_at = Some(Instant::now()); // immediate — user pressed key
            } else {
                self.game_state.phase = GamePhase::SessionEnd;
                self.next_event_at = None;
            }
        }
    }
}