use crate::game::{ActionRecord, SeenAction}; /// What the seat had in on this street before `history[index]`: the last amount of an /// earlier non-ante record of the same seat and street (0 if none). fn already_in(history: &[ActionRecord], index: usize) -> f64 { let record = &history[index]; history[..index] .iter() .rev() .filter(|r| { r.seat == record.seat && r.street == record.street && r.action != SeenAction::PostAnte }) .find_map(|r| r.amount.filter(|a| a.is_finite())) .unwrap_or(0.0) } /// Chips `history[index]` added to the pot. `None` when it cannot be told: a paying action /// without a (usable) amount. Folds and checks add 0. Out-of-range index gives `None`. pub fn contribution(history: &[ActionRecord], index: usize) -> Option { let record = history.get(index)?; match record.action { SeenAction::Fold | SeenAction::Check => Some(0.0), SeenAction::PostAnte => record.amount.filter(|a| a.is_finite() && *a >= 0.0), _ => record .amount .filter(|a| a.is_finite() && *a >= 0.0) .map(|a| (a - already_in(history, index)).max(0.0)), } } /// The pot before each record, summed from the contributions. From the first record whose /// contribution is unknown on, the pot is unknown (`None`). pub fn pot_before(history: &[ActionRecord]) -> Vec> { (0..history.len()) .scan(Some(0.0), |pot, index| { let before = *pot; *pot = before.zip(contribution(history, index)).map(|(p, c)| p + c); Some(before) }) .collect() }