From 2915d826f744612e0d975282f5bddf13270b9f3f Mon Sep 17 00:00:00 2001 From: salmonik Date: Wed, 28 Oct 2026 23:19:52 +0100 Subject: v1.1.0: highlight winning symbols on the slots board --- src/bin/client/repl.rs | 31 +++++++++++++++++++++++++++- src/client/games/manager.rs | 11 ++++++++++ src/client/games/response.rs | 18 +++++++++++----- src/client/games/slots.rs | 29 +++++++++++++++++++++----- src/server/core.rs | 7 ++++++- src/server/games/slots.rs | 49 ++++++++++++++++++++++++++++++++++++-------- 6 files changed, 125 insertions(+), 20 deletions(-) (limited to 'src') diff --git a/src/bin/client/repl.rs b/src/bin/client/repl.rs index 033fb1a..58cb541 100644 --- a/src/bin/client/repl.rs +++ b/src/bin/client/repl.rs @@ -240,8 +240,12 @@ impl ClientState { .map(|line| { if line.starts_with("===") { line.as_str().bold().cyan().to_string() - } else if line.contains("JACKPOT") || line.contains("Two of a kind") { + } else if line.contains('{') { + style_winning_cells(line) + } else if line.contains("WIN!") || line.starts_with("Won ") { line.as_str().bold().green().to_string() + } else if line.starts_with("Winning lines:") { + line.as_str().yellow().to_string() } else if line.starts_with("ERROR:") { line.as_str().red().to_string() } else { @@ -436,6 +440,31 @@ impl ClientState { } } +fn style_winning_cells(line: &str) -> String { + let chars: Vec = line.chars().collect(); + let mut out = String::new(); + let mut index = 0; + + while index < chars.len() { + if chars[index] == '{' { + let start = index; + while index < chars.len() && chars[index] != '}' { + index += 1; + } + if index < chars.len() { + index += 1; + } + let cell: String = chars[start..index].iter().collect(); + out.push_str(&cell.as_str().bold().yellow().to_string()); + } else { + out.push(chars[index]); + index += 1; + } + } + + out +} + fn map_key(key: crossterm::event::KeyEvent) -> Option { if key.modifiers.contains(KeyModifiers::CONTROL) { match key.code { diff --git a/src/client/games/manager.rs b/src/client/games/manager.rs index 8436142..168c49d 100644 --- a/src/client/games/manager.rs +++ b/src/client/games/manager.rs @@ -76,6 +76,7 @@ impl GameManager { session.data.remove("reels"); session.data.remove("win_type"); session.data.remove("win_lines"); + session.data.remove("win_cells"); session.data.remove("winnings"); Ok(()) } @@ -101,6 +102,15 @@ impl GameManager { .collect::>() .join(","), ); + session.data.insert( + "win_cells".to_string(), + result + .win_cells + .iter() + .map(usize::to_string) + .collect::>() + .join(","), + ); session .data .insert("winnings".to_string(), result.winnings.to_string()); @@ -118,6 +128,7 @@ impl GameManager { session.data.remove("reels"); session.data.remove("win_type"); session.data.remove("win_lines"); + session.data.remove("win_cells"); session.data.remove("winnings"); session.data.insert("error".to_string(), message.into()); } diff --git a/src/client/games/response.rs b/src/client/games/response.rs index de22a75..d7f9d45 100644 --- a/src/client/games/response.rs +++ b/src/client/games/response.rs @@ -7,6 +7,7 @@ pub struct SpinResult { pub reels: String, pub win_type: String, pub win_lines: Vec, + pub win_cells: Vec, } /// Parse one server reply to `PLAY_START`. @@ -52,11 +53,11 @@ pub fn parse_spin_response(response: &str) -> Result { let win_lines = parts .get(7) - .map(|raw| { - raw.split(',') - .filter_map(|value| value.trim().parse::().ok()) - .collect() - }) + .map(|raw| parse_indices(raw)) + .unwrap_or_default(); + let win_cells = parts + .get(8) + .map(|raw| parse_indices(raw)) .unwrap_or_default(); Ok(SpinResult { @@ -67,5 +68,12 @@ pub fn parse_spin_response(response: &str) -> Result { reels: parts[5].to_string(), win_type: parts[6].to_string(), win_lines, + win_cells, }) } + +fn parse_indices(raw: &str) -> Vec { + raw.split(',') + .filter_map(|value| value.trim().parse::().ok()) + .collect() +} diff --git a/src/client/games/slots.rs b/src/client/games/slots.rs index 39c43b2..c367fcb 100644 --- a/src/client/games/slots.rs +++ b/src/client/games/slots.rs @@ -13,22 +13,40 @@ impl SlotsGame { Self } - fn cell(chars: &[char], row: usize, col: usize) -> char { - if chars.len() == REELS * ROWS { + fn cell(chars: &[char], row: usize, col: usize, winning: &[usize]) -> String { + let symbol = if chars.len() == REELS * ROWS { chars.get(col * ROWS + row).copied().unwrap_or('?') } else if chars.len() == 3 && row == 0 { // Legacy 3-symbol server: degrade to the first row. chars.get(col).copied().unwrap_or('?') } else { '?' + }; + + if winning.contains(&(col * ROWS + row)) { + format!("{{ {} }}", symbol) + } else { + format!("[ {} ]", symbol) } } - fn render_grid(&self, reels: &str) -> [String; ROWS] { + fn highlighted_cells(state: &GameState) -> Vec { + state + .data + .get("win_cells") + .map(|raw| { + raw.split(',') + .filter_map(|value| value.trim().parse::().ok()) + .collect() + }) + .unwrap_or_default() + } + + fn render_grid(&self, reels: &str, winning: &[usize]) -> [String; ROWS] { let chars: Vec = reels.chars().collect(); std::array::from_fn(|row| { (0..REELS) - .map(|col| format!("[ {} ]", Self::cell(&chars, row, col))) + .map(|col| Self::cell(&chars, row, col, winning)) .collect::>() .join(" ") }) @@ -84,7 +102,8 @@ impl Game for SlotsGame { .get("balance") .map(String::as_str) .unwrap_or("?"); - let grid = self.render_grid(&reels); + let winning = Self::highlighted_cells(state); + let grid = self.render_grid(&reels, &winning); format!( "=== SLOTS ===\r\nBet: {} PLN Lines: {} Balance: {} PLN\r\n{}\r\n{}\r\n{}\r\n\r\n{}\r\n{}\r\n[+/-] Bet [SPACE] Spin [Q] Quit", diff --git a/src/server/core.rs b/src/server/core.rs index 33cafbf..350fe18 100644 --- a/src/server/core.rs +++ b/src/server/core.rs @@ -254,6 +254,11 @@ impl ServerCore { .get("win_lines") .cloned() .unwrap_or_else(|| "-".to_string()); + let win_cells = result + .result_data + .get("win_cells") + .cloned() + .unwrap_or_else(|| "-".to_string()); match database::apply_spin( &mut self.conn, @@ -268,7 +273,7 @@ impl ServerCore { Ok(new_balance) => { let _ = self.sessions.remove_session(&session_id); format!( - "GAME_RESULT {session_id} {winnings} {bet_amount} {new_balance} {reels} {win_type} {win_lines}" + "GAME_RESULT {session_id} {winnings} {bet_amount} {new_balance} {reels} {win_type} {win_lines} {win_cells}" ) } Err(error) => { diff --git a/src/server/games/slots.rs b/src/server/games/slots.rs index 2ef1b46..1d71c9d 100644 --- a/src/server/games/slots.rs +++ b/src/server/games/slots.rs @@ -22,6 +22,13 @@ struct Payout { five: u64, } +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct LineWin { + pub line: usize, + /// Column-major cell indices of the matched symbols. + pub cells: Vec, +} + pub struct SlotsGame { payouts: HashMap, /// Deterministic grid for tests and demos, in column-major order. @@ -65,10 +72,11 @@ impl SlotsGame { .collect() } - /// Evaluate one spin. Returns the total win and the 1-based winning lines. - pub fn calculate_payout(&self, grid: &[char], bet: u64) -> (u64, Vec) { + /// Evaluate one spin. Returns the total win and the winning lines with + /// the column-major cells that should be highlighted. + pub fn calculate_payout(&self, grid: &[char], bet: u64) -> (u64, Vec) { let mut total = 0u64; - let mut winning_lines = Vec::new(); + let mut wins = Vec::new(); for (index, pattern) in PAYLINES.iter().enumerate() { let Some(&first) = grid.get(pattern[0]) else { @@ -99,10 +107,19 @@ impl SlotsGame { }; total = total.saturating_add(bet.saturating_mul(multiplier)); - winning_lines.push(index + 1); + let cells = pattern + .iter() + .enumerate() + .take(run) + .map(|(col, row)| col * ROWS + row) + .collect(); + wins.push(LineWin { + line: index + 1, + cells, + }); } - (total, winning_lines) + (total, wins) } /// Exact theoretical return to player for the current paytable. @@ -213,12 +230,27 @@ impl Game for SlotsGame { ))); } - let (winnings, winning_lines) = self.calculate_payout(&grid, state.bet_amount); + let (winnings, wins) = self.calculate_payout(&grid, state.bet_amount); let win_type = classify_win(winnings, state.bet_amount); - let win_lines = if winning_lines.is_empty() { + let win_lines = if wins.is_empty() { + "-".to_string() + } else { + wins.iter() + .map(|win| win.line.to_string()) + .collect::>() + .join(",") + }; + + let mut cells: Vec = wins + .iter() + .flat_map(|win| win.cells.iter().copied()) + .collect(); + cells.sort_unstable(); + cells.dedup(); + let win_cells = if cells.is_empty() { "-".to_string() } else { - winning_lines + cells .iter() .map(usize::to_string) .collect::>() @@ -229,6 +261,7 @@ impl Game for SlotsGame { result_data.insert("reels".to_string(), reel_string.clone()); result_data.insert("win_type".to_string(), win_type.to_string()); result_data.insert("win_lines".to_string(), win_lines); + result_data.insert("win_cells".to_string(), win_cells); Ok(GameResult { session_id: state.session_id.clone(), -- cgit v1.2.3