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, Fault> { let path = join(PATH, key); items_member(object, PATH, key)?.iter().enumerate().map(|(i, v)| number(v, &at(&path, i))).collect() } fn whole>(n: u64, key: &str) -> Result { 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, 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), })) }