From d251b54fe40f6f8f95261b4df65bced2fb728c63 Mon Sep 17 00:00:00 2001 From: salmonik Date: Sun, 1 Nov 2026 19:00:04 +0100 Subject: v4.0.6: move the prompt editor and key mapping out of the core --- src/client/core.rs | 208 ++--------------------------------------------------- 1 file changed, 6 insertions(+), 202 deletions(-) (limited to 'src/client/core.rs') diff --git a/src/client/core.rs b/src/client/core.rs index 98f109f..280eb77 100644 --- a/src/client/core.rs +++ b/src/client/core.rs @@ -3,32 +3,15 @@ use crate::client::games::response::{ parse_blackjack_response, parse_crash_response, parse_history_response, parse_roulette_response, parse_spin_response, }; -use crate::client::games::roulette::Direction; use crate::client::view::{ BlackjackView, Command, CrashView, HistoryView, LobbyView, RouletteView, SlotsView, View, }; -use crate::client::ui::{ - command_names, completion_lines, display_width, help_lines, history_lines, - input_cursor_column, parse_bet_arg, record_history, -}; +use crate::client::ui::{help_lines, history_lines, parse_bet_arg}; use crate::crash::{CrashState, multiplier_at}; use crate::history::{PlayRecord, MAX_HISTORY}; use crate::roulette::{BetTarget, Bets}; use std::time::{Duration, Instant}; -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub enum Key { - Char(char), - Enter, - Backspace, - Up, - Down, - Left, - Right, - Tab, - Esc, - CtrlC, -} #[derive(Debug, Clone, PartialEq, Eq)] pub enum InteractiveCommand { @@ -50,8 +33,6 @@ pub enum Effect { AnimateRoulette { winning: u8, view: View }, /// Remove the game board region. ClearRegion, - /// Redraw the command prompt. `text` is the input without the `> ` prefix. - SetPrompt { text: String, cursor_col: u16 }, /// Send one protocol line and feed the reply to `handle_reply`. SendLine(String), /// Delegate an interactive prompt flow to the terminal adapter. @@ -91,9 +72,6 @@ enum Screen { } pub struct ClientCore { - input: String, - history: Vec, - history_index: usize, game: GameManager, connected: bool, logged_in: Option, @@ -114,9 +92,6 @@ pub struct ClientCore { impl ClientCore { pub fn new() -> Self { Self { - input: String::new(), - history: Vec::new(), - history_index: 0, game: GameManager::new(), connected: false, logged_in: None, @@ -184,40 +159,6 @@ impl ClientCore { self.board_effects() } - pub fn prompt_effect(&self) -> Option { - if self.is_in_game() { - return None; - } - Some(Effect::SetPrompt { - text: self.input.clone(), - cursor_col: input_cursor_column(2, display_width(&self.input)), - }) - } - - pub fn handle_key(&mut self, key: Key) -> Vec { - if self.game.is_in_game() { - return self.handle_game_key(key); - } - - match key { - Key::CtrlC => vec![Effect::Quit], - Key::Enter => self.submit(), - Key::Backspace => { - self.input.pop(); - self.prompt_effects() - } - Key::Char(c) => { - self.input.push(c); - self.prompt_effects() - } - Key::Up => self.history_previous(), - Key::Down => self.history_next(), - Key::Left | Key::Right => Vec::new(), - Key::Tab => self.complete(), - Key::Esc => Vec::new(), - } - } - pub fn handle_reply(&mut self, reply: &str) -> Vec { match self.pending.take() { Some(Pending::Spin) => match parse_spin_response(reply) { @@ -370,31 +311,6 @@ impl ClientCore { } } - fn handle_game_key(&mut self, key: Key) -> Vec { - let game_type = self - .game - .get_current_state() - .map(|state| state.game_type.as_str()); - if game_type == Some("roulette") { - return self.handle_roulette_key(key); - } - if game_type == Some("blackjack") { - return self.handle_blackjack_key(key); - } - if game_type == Some("crash") { - return self.handle_crash_key(key); - } - - match key { - Key::CtrlC => vec![Effect::Quit], - Key::Esc | Key::Char('q') => self.command(Command::Leave), - Key::Char(' ') => self.command(Command::Spin), - Key::Char('+') | Key::Char('=') => self.command(Command::AdjustBet(10)), - Key::Char('-') => self.command(Command::AdjustBet(-10)), - _ => Vec::new(), - } - } - fn spin(&mut self) -> Vec { let Some(request) = self.game.spin_request() else { return Vec::new(); @@ -409,23 +325,6 @@ impl ClientCore { effects } - fn handle_roulette_key(&mut self, key: Key) -> Vec { - match key { - Key::CtrlC => vec![Effect::Quit], - Key::Up => self.command(Command::MoveCursor(Direction::Up)), - Key::Down => self.command(Command::MoveCursor(Direction::Down)), - Key::Left => self.command(Command::MoveCursor(Direction::Left)), - Key::Right => self.command(Command::MoveCursor(Direction::Right)), - Key::Enter => self.command(Command::PlaceBet), - Key::Char(' ') => self.command(Command::Spin), - Key::Char('c') => self.command(Command::ClearBets), - Key::Char('+') | Key::Char('=') => self.command(Command::AdjustChip(10)), - Key::Char('-') => self.command(Command::AdjustChip(-10)), - Key::Char('q') => self.command(Command::Leave), - _ => Vec::new(), - } - } - /// Apply a semantic command. Both frontends drive the core through this. pub fn command(&mut self, command: Command) -> Vec { let game = self.current_game_type().map(str::to_string); @@ -523,7 +422,7 @@ impl ClientCore { } } - fn current_game_type(&self) -> Option<&str> { + pub fn current_game_type(&self) -> Option<&str> { self.game .get_current_state() .map(|state| state.game_type.as_str()) @@ -628,36 +527,6 @@ impl ClientCore { effects } - fn handle_blackjack_key(&mut self, key: Key) -> Vec { - let status = self - .game - .get_current_state() - .and_then(|state| state.data.get("status")) - .cloned() - .unwrap_or_else(|| "ready".to_string()); - let hand = self.game.blackjack_state(); - let player = hand.as_ref().is_some_and(|hand| hand.phase == "player"); - let can_double = hand.as_ref().is_some_and(|hand| { - hand.phase == "player" && hand.actions.iter().any(|action| action == "double") - }); - - match key { - Key::CtrlC => vec![Effect::Quit], - Key::Char('q') => self.command(Command::Leave), - Key::Char(' ') => self.command(Command::Deal), - Key::Char('h') if player => self.command(Command::Hit), - Key::Char('s') if player => self.command(Command::Stand), - Key::Char('d') if can_double => self.command(Command::Double), - Key::Char('+') | Key::Char('=') if matches!(status.as_str(), "ready" | "done") => { - self.command(Command::AdjustBet(10)) - } - Key::Char('-') if matches!(status.as_str(), "ready" | "done") => { - self.command(Command::AdjustBet(-10)) - } - _ => Vec::new(), - } - } - fn start_blackjack(&mut self) -> Vec { let status = self .game @@ -768,25 +637,6 @@ impl ClientCore { self.board_effects() } - fn handle_crash_key(&mut self, key: Key) -> Vec { - let status = self.game.crash_status(); - let running = status == "running"; - - match key { - Key::CtrlC => vec![Effect::Quit], - Key::Char('q') => self.command(Command::Leave), - Key::Char(' ') if !running && self.pending.is_none() => self.command(Command::Start), - Key::Char('c') if running && self.pending.is_none() => { - self.command(Command::CashOut) - } - Key::Char('+') | Key::Char('=') if !running => { - self.command(Command::AdjustBet(10)) - } - Key::Char('-') if !running => self.command(Command::AdjustBet(-10)), - _ => Vec::new(), - } - } - fn start_crash(&mut self) -> Vec { let status = self.game.crash_status(); if !matches!(status.as_str(), "ready" | "cashed" | "crashed" | "forfeit") { @@ -822,16 +672,14 @@ impl ClientCore { vec![Effect::SendLine(format!("CRASH_CASHOUT {session}"))] } - fn submit(&mut self) -> Vec { - let line = self.input.trim().to_string(); - self.input.clear(); + /// Run one command line. The interactive frontend owns the buffer and the + /// history; the core only sees the finished line. + pub fn submit_line(&mut self, line: &str) -> Vec { + let line = line.trim().to_string(); if line.is_empty() { return Vec::new(); } - record_history(&mut self.history, &line); - self.history_index = self.history.len(); - let parts: Vec<&str> = line.splitn(3, ' ').collect(); match parts[0] { "play" => self.cmd_play(&parts), @@ -1047,47 +895,6 @@ impl ClientCore { effects } - fn history_previous(&mut self) -> Vec { - if self.history_index == 0 { - return Vec::new(); - } - self.history_index -= 1; - self.input = self.history[self.history_index].clone(); - self.prompt_effects() - } - - fn history_next(&mut self) -> Vec { - if self.history_index >= self.history.len() { - return Vec::new(); - } - self.history_index += 1; - self.input = if self.history_index == self.history.len() { - String::new() - } else { - self.history[self.history_index].clone() - }; - self.prompt_effects() - } - - fn complete(&mut self) -> Vec { - let prefix = self.input.trim(); - let matches: Vec<&'static str> = command_names() - .into_iter() - .filter(|command| command.starts_with(prefix)) - .collect(); - - let mut effects = Vec::new(); - if matches.len() == 1 { - self.input = matches[0].to_string(); - } else if matches.len() > 1 { - effects.push(Effect::Print(completion_lines(&matches, 76))); - } - if let Some(prompt) = self.prompt_effect() { - effects.push(prompt); - } - effects - } - /// Snapshot of the current screen for a frontend to draw. pub fn view(&self) -> Option { if let Some(state) = self.game.get_current_state() { @@ -1126,9 +933,6 @@ impl ClientCore { self.view().map(Effect::Render).into_iter().collect() } - fn prompt_effects(&self) -> Vec { - self.prompt_effect().into_iter().collect() - } } fn auth_result(reply: &str) -> Result<(), String> { -- cgit v1.2.3