Files
pokerfreak/src/wire/decode_tournament.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

48 lines
2.0 KiB
Rust

use crate::fault::Fault;
use crate::icm::lage::Turnierlage;
use crate::json::field::{at, join, optional, required};
use crate::json::member::{items_member, opt_number_member, opt_u64_member};
use crate::json::scalar::{number, whole_u64};
use crate::json::Value;
const PATH: &str = "tournament";
fn numbers(object: &Value, key: &str) -> Result<Vec<f64>, Fault> {
let path = join(PATH, key);
items_member(object, PATH, key)?.iter().enumerate().map(|(i, v)| number(v, &at(&path, i))).collect()
}
fn whole<T: TryFrom<u64>>(n: u64, key: &str) -> Result<T, Fault> {
T::try_from(n).map_err(|_| Fault::Invalid { field: join(PATH, key), detail: format!("{n} is out of range") })
}
/// Extension field `tournament`: the tournament situation the answer is priced in (ICM-lite).
///
/// ```text
/// "tournament": {"payouts": [27.76, 18.27,...], "stacks": [8000, 12500,...],
/// "heroIndex": 0, "ante": 250, "level": 18}
/// ```
///
/// - `payouts`: prize of place 1, 2,... (paid places only), any money unit.
/// - `stacks`: chips of every player still in the tournament at hand start, hero included,
/// in the unit of the request's amounts.
/// - `heroIndex`: hero's index in `stacks`.
/// - `ante`, `level` (optional, default 0): informational.
///
/// Absent or `null` means cash. Wrong types are a `Fault`; values that are well-formed but
/// cannot be priced (no payouts, hero outside the field, a stack of 0) make the engine
/// abstain (`Turnierlage::is_usable`).
pub fn tournament(root: &Value) -> Result<Option<Turnierlage>, Fault> {
let Some(object) = optional(root, "", PATH)? else {
return Ok(None);
};
let hero = whole_u64(required(object, PATH, "heroIndex")?, &join(PATH, "heroIndex"))?;
Ok(Some(Turnierlage {
payouts: numbers(object, "payouts")?,
stacks: numbers(object, "stacks")?,
hero: whole(hero, "heroIndex")?,
ante: opt_number_member(object, PATH, "ante")?.unwrap_or(0.0),
level: opt_u64_member(object, PATH, "level")?.map(|n| whole(n, "level")).transpose()?.unwrap_or(0),
}))
}