48 lines
1.8 KiB
Rust
48 lines
1.8 KiB
Rust
use crate::game::Decision;
|
|
use crate::json::{render, Value};
|
|
|
|
fn three_decimals(value: f64) -> f64 {
|
|
(value * 1000.0).round() / 1000.0
|
|
}
|
|
|
|
/// `{"action", "size", "confidence", "reason"}` in that key order, plus the optional
|
|
/// diagnostic keys `equity` and `category` when they are known.
|
|
pub fn encode_value(decision: &Decision) -> Value {
|
|
let size = decision.choice.size().filter(|s| s.is_finite()).map_or(Value::Null, Value::Number);
|
|
let mut felder = vec![
|
|
("action".to_string(), Value::String(decision.choice.name().to_string())),
|
|
("size".to_string(), size),
|
|
("confidence".to_string(), Value::Number(three_decimals(decision.confidence.value()))),
|
|
("reason".to_string(), Value::String(decision.reason.clone())),
|
|
];
|
|
if let Some(equity) = decision.equity {
|
|
felder.push(("equity".to_string(), Value::Number(three_decimals(equity))));
|
|
}
|
|
if let Some(category) = decision.category {
|
|
felder.push(("category".to_string(), Value::String(format!("{category:?}"))));
|
|
}
|
|
Value::Object(felder)
|
|
}
|
|
|
|
/// The optional `evs` answer: each priced line with its EV in chips. `None` when there are none.
|
|
pub fn encode_evs(decision: &Decision) -> Option<Value> {
|
|
let zeilen: Vec<Value> = decision
|
|
.evs
|
|
.iter()
|
|
.map(|(choice, ev)| {
|
|
let size = choice.size().filter(|s| s.is_finite()).map_or(Value::Null, Value::Number);
|
|
Value::Object(vec![
|
|
("action".to_string(), Value::String(choice.name().to_string())),
|
|
("size".to_string(), size),
|
|
("ev".to_string(), Value::Number(three_decimals(*ev))),
|
|
])
|
|
})
|
|
.collect();
|
|
(!zeilen.is_empty()).then_some(Value::Array(zeilen))
|
|
}
|
|
|
|
/// One response line (no trailing newline).
|
|
pub fn encode(decision: &Decision) -> String {
|
|
render(&encode_value(decision))
|
|
}
|