Files
pokerfreak/src/guard/validate.rs
T
mmintel 836c16c9a4 Sprechweise geputzt: kein echt/live/Mensch mehr in Kommentaren und Texten
Die Welt soll nichts verraten. Woerter, die ein Gegenueber implizieren (echt vs. unecht,
live, Mensch), sind aus Prosa, Kommentaren und Docstrings raus; Namen (mensch_ruhe_s,
echte_chops) und der Altwert "echt" im Spielplan bleiben. Nur Kommentare und Docstrings
angefasst, damit keine Einrueckung faellt.
2026-11-01 22:11:05 +01:00

130 lines
4.3 KiB
Rust

use crate::card::Card;
use crate::equity::deck_capacity::fits;
use crate::fault::Fault;
use crate::game::{ActionKind, Spot};
fn invalid(field: &str, detail: &str) -> Fault {
Fault::Invalid { field: field.to_string(), detail: detail.to_string() }
}
fn chip(field: &str, value: f64) -> Result<(), Fault> {
if value.is_finite() && value >= 0.0 {
Ok(())
} else {
Err(invalid(field, "must be a finite, non-negative amount"))
}
}
fn optional_chip(field: &str, value: Option<f64>) -> Result<(), Fault> {
value.map_or(Ok(()), |v| chip(field, v))
}
fn has_duplicates<T: PartialEq>(items: &[T]) -> bool {
items.iter().enumerate().any(|(i, a)| items[..i].contains(a))
}
fn cards(spot: &Spot) -> Result<(), Fault> {
let hole = spot.rules().hole_cards;
if spot.hero_cards.len() != hole {
return Err(invalid(
"heroCards",
&format!("expected {hole} cards for {}, got {}", spot.variant.name(), spot.hero_cards.len()),
));
}
let board = spot.street.board_len();
if spot.board.len() != board {
return Err(invalid(
"board",
&format!("expected {board} cards on {}, got {}", spot.street.name(), spot.board.len()),
));
}
let all: Vec<_> = spot.hero_cards.iter().chain(spot.board.iter()).collect();
if has_duplicates(&all) {
return Err(invalid("cards", "duplicate card across heroCards and board"));
}
Ok(())
}
fn outside_deck(spot: &Spot) -> Result<(), Fault> {
let deck = spot.rules().deck;
let strange = |field: &str, cards: &[Card]| match cards.iter().find(|c| !deck.contains(c.rank)) {
Some(card) => Err(invalid(
field,
&format!("{} is not in the {} deck", card.short(), spot.variant.name()),
)),
None => Ok(()),
};
strange("heroCards", &spot.hero_cards)?;
strange("board", &spot.board)
}
/// Hero plus every villain must be dealt a hand from the deck, with the board and burns.
fn players_fit(spot: &Spot) -> Result<(), Fault> {
let players = 1 + spot.live_villains().count();
if fits(&spot.rules(), players) {
Ok(())
} else {
Err(invalid(
"villains",
&format!("{players} players do not fit the {} deck with board and burns", spot.variant.name()),
))
}
}
fn chips(spot: &Spot) -> Result<(), Fault> {
if !(spot.big_blind.is_finite() && spot.big_blind > 0.0) {
return Err(invalid("bigBlind", "must be a finite, positive amount"));
}
chip("pot", spot.pot)?;
chip("toCall", spot.to_call)?;
chip("heroStack", spot.hero_stack)?;
optional_chip("minRaiseTo", spot.min_raise_to)?;
optional_chip("maxRaiseTo", spot.max_raise_to)?;
if let (Some(min), Some(max)) = (spot.min_raise_to, spot.max_raise_to) {
if min > max {
return Err(invalid("minRaiseTo", "exceeds maxRaiseTo"));
}
}
if let Some(sizes) = &spot.allowed_sizes {
if sizes.iter().any(|s| !(s.is_finite() && *s > 0.0)) {
return Err(invalid("allowedSizes", "every size must be finite and positive"));
}
}
spot.villains.iter().try_for_each(|v| chip("villains.stack", v.stack))
}
fn actions(spot: &Spot) -> Result<(), Fault> {
if spot.legal.is_empty() {
return Err(invalid("legalActions", "empty"));
}
if has_duplicates(&spot.legal) {
return Err(invalid("legalActions", "duplicate entry"));
}
if !(2..=10).contains(&spot.seats) {
return Err(invalid("seats", "must be between 2 and 10"));
}
if spot.is_legal(ActionKind::Check) && spot.to_call > 0.0 {
return Err(Fault::Inconsistent("CHECK legal while toCall > 0".to_string()));
}
let sizeable = spot.allowed_sizes.as_ref().is_some_and(|s| !s.is_empty())
|| spot.min_raise_to.is_some()
|| spot.max_raise_to.is_some()
|| spot.hero_stack > 0.0;
if spot.is_legal(ActionKind::Raise) && !sizeable {
return Err(Fault::Inconsistent(
"RAISE legal but no way to size it (no allowedSizes, no bounds, no stack)".to_string(),
));
}
Ok(())
}
/// Consistency check of a decoded spot (cards, counts, chips, sizes, legal actions).
pub fn check(spot: Spot) -> Result<Spot, Fault> {
cards(&spot)?;
outside_deck(&spot)?;
players_fit(&spot)?;
chips(&spot)?;
actions(&spot)?;
Ok(spot)
}