From c683476e4a8042455899be806157c3201bc2ec7e Mon Sep 17 00:00:00 2001 From: Caleb LeNoir Date: Fri, 30 Oct 2026 07:15:46 -0400 Subject: [PATCH] Keep ten standard fights' tables as goldens and check them in CI --- .gitattributes | 1 + .github/workflows/ci.yml | 2 + README.md | 4 ++ sim/goldens/01-one-l1-d8.txt | 9 +++ sim/goldens/01-one-l1-d8.yml | 6 ++ sim/goldens/02-one-l3-d10.txt | 9 +++ sim/goldens/02-one-l3-d10.yml | 6 ++ sim/goldens/03-three-l1-d6.txt | 9 +++ sim/goldens/03-three-l1-d6.yml | 6 ++ sim/goldens/04-one-l2-d8-mechanics.txt | 9 +++ sim/goldens/04-one-l2-d8-mechanics.yml | 7 ++ sim/goldens/05-one-l2-d8-flee.txt | 9 +++ sim/goldens/05-one-l2-d8-flee.yml | 6 ++ sim/goldens/06-two-l2-d10-flee.txt | 10 +++ sim/goldens/06-two-l2-d10-flee.yml | 11 +++ sim/goldens/07-one-l3-d10-drink.txt | 10 +++ sim/goldens/07-one-l3-d10-drink.yml | 9 +++ sim/goldens/08-two-l1-rolled.txt | 9 +++ sim/goldens/08-two-l1-rolled.yml | 6 ++ sim/goldens/09-careful.txt | 10 +++ sim/goldens/09-careful.yml | 9 +++ sim/goldens/10-naive.txt | 10 +++ sim/goldens/10-naive.yml | 9 +++ src/bin/fight.rs | 77 +++++++++++++------- src/sim/golden.rs | 99 ++++++++++++++++++++++++++ src/sim/mod.rs | 1 + 26 files changed, 326 insertions(+), 27 deletions(-) create mode 100644 sim/goldens/01-one-l1-d8.txt create mode 100644 sim/goldens/01-one-l1-d8.yml create mode 100644 sim/goldens/02-one-l3-d10.txt create mode 100644 sim/goldens/02-one-l3-d10.yml create mode 100644 sim/goldens/03-three-l1-d6.txt create mode 100644 sim/goldens/03-three-l1-d6.yml create mode 100644 sim/goldens/04-one-l2-d8-mechanics.txt create mode 100644 sim/goldens/04-one-l2-d8-mechanics.yml create mode 100644 sim/goldens/05-one-l2-d8-flee.txt create mode 100644 sim/goldens/05-one-l2-d8-flee.yml create mode 100644 sim/goldens/06-two-l2-d10-flee.txt create mode 100644 sim/goldens/06-two-l2-d10-flee.yml create mode 100644 sim/goldens/07-one-l3-d10-drink.txt create mode 100644 sim/goldens/07-one-l3-d10-drink.yml create mode 100644 sim/goldens/08-two-l1-rolled.txt create mode 100644 sim/goldens/08-two-l1-rolled.yml create mode 100644 sim/goldens/09-careful.txt create mode 100644 sim/goldens/09-careful.yml create mode 100644 sim/goldens/10-naive.txt create mode 100644 sim/goldens/10-naive.yml create mode 100644 src/sim/golden.rs diff --git a/.gitattributes b/.gitattributes index 657b784..6e9e09c 100644 --- a/.gitattributes +++ b/.gitattributes @@ -3,3 +3,4 @@ data/**/*.yml -text tests/fixtures/*.json -text parity/** -text src/store/shape.txt -text +sim/** -text diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1aa47b5..ca433a7 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -30,6 +30,8 @@ jobs: - run: cargo test --workspace --locked - name: the engine sweep against its goldens run: cargo run --release --locked --bin parity -- --check parity + - name: the standard fights against their goldens + run: cargo run --release --locked --bin fight -- --check sim runner: name: the engine sweep through the game's runner, and the game's gates diff --git a/README.md b/README.md index 491b21a..4e186f1 100644 --- a/README.md +++ b/README.md @@ -401,6 +401,10 @@ fight; and a fight's tally agreeing with its `playthrough_blows` rows. that can be negative. - Tables that Ruby keeps in a `Hash` are arrays of pairs here, so their order survives. +- A change that moves fight odds re-blesses the fight goldens in the same + PR: `cargo run --release --bin fight -- --write sim`, then keep the diff of + `sim/goldens/*.txt`. CI runs `fight --check sim`, which fails on any table + that moved. ## Testing diff --git a/sim/goldens/01-one-l1-d8.txt b/sim/goldens/01-one-l1-d8.txt new file mode 100644 index 0000000..fe2ffdd --- /dev/null +++ b/sim/goldens/01-one-l1-d8.txt @@ -0,0 +1,9 @@ +01-one-l1-d8: 25 fights, seeds 1..25, told driver, policy attack + rate 95% interval + won 1.000 [0.867, 1.000] + died 0.000 [0.000, 0.133] + fled 0.000 [0.000, 0.133] + rounds mean 2.28 1:4 2:12 3:7 4:2 + hp left mean 12.12 2:1 4:1 5:1 7:1 10:3 11:2 12:5 13:2 14:1 15:3 16:1 18:4 (survivors) + damage taken 5.88, dealt 10.56 a fight + blows taken 1.28, struck 2.28 a fight diff --git a/sim/goldens/01-one-l1-d8.yml b/sim/goldens/01-one-l1-d8.yml new file mode 100644 index 0000000..8e6b5f7 --- /dev/null +++ b/sim/goldens/01-one-l1-d8.yml @@ -0,0 +1,6 @@ +# The house body (level 3, d8, 18 hit points) against one level-1 d8 foe, +# attacking to the end. Exact P(win): 0.9854. +seeds: 1..25 +foes: + - {count: 1, stats: {level: 1, hit_die: 8}} +policy: attack diff --git a/sim/goldens/02-one-l3-d10.txt b/sim/goldens/02-one-l3-d10.txt new file mode 100644 index 0000000..385ad21 --- /dev/null +++ b/sim/goldens/02-one-l3-d10.txt @@ -0,0 +1,9 @@ +02-one-l3-d10: 25 fights, seeds 1..25, told driver, policy attack + rate 95% interval + won 0.320 [0.172, 0.516] + died 0.680 [0.484, 0.828] + fled 0.000 [0.000, 0.133] + rounds mean 4.08 3:4 4:15 5:6 + hp left mean 3.50 1:1 2:2 3:3 5:1 9:1 (survivors) + damage taken 19.16, dealt 18.28 a fight + blows taken 3.76, struck 4.08 a fight diff --git a/sim/goldens/02-one-l3-d10.yml b/sim/goldens/02-one-l3-d10.yml new file mode 100644 index 0000000..20b1d72 --- /dev/null +++ b/sim/goldens/02-one-l3-d10.yml @@ -0,0 +1,6 @@ +# The house body against one level-3 d10 foe, attacking to the end. +# Exact P(win): 0.2338. +seeds: 1..25 +foes: + - {count: 1, stats: {level: 3, hit_die: 10}} +policy: attack diff --git a/sim/goldens/03-three-l1-d6.txt b/sim/goldens/03-three-l1-d6.txt new file mode 100644 index 0000000..fd2c479 --- /dev/null +++ b/sim/goldens/03-three-l1-d6.txt @@ -0,0 +1,9 @@ +03-three-l1-d6: 25 fights, seeds 1..25, told driver, policy attack + rate 95% interval + won 0.160 [0.064, 0.347] + died 0.840 [0.653, 0.936] + fled 0.000 [0.000, 0.133] + rounds mean 2.92 1:1 2:9 3:9 4:4 5:1 6:1 + hp left mean 2.75 2:2 3:1 4:1 (survivors) + damage taken 18.88, dealt 12.20 a fight + blows taken 5.20, struck 2.92 a fight diff --git a/sim/goldens/03-three-l1-d6.yml b/sim/goldens/03-three-l1-d6.yml new file mode 100644 index 0000000..d506355 --- /dev/null +++ b/sim/goldens/03-three-l1-d6.yml @@ -0,0 +1,6 @@ +# The house body against three level-1 d6 foes, attacking the first one +# standing to the end. Exact P(win): 0.1914. +seeds: 1..25 +foes: + - {count: 3, stats: {level: 1, hit_die: 6}} +policy: attack diff --git a/sim/goldens/04-one-l2-d8-mechanics.txt b/sim/goldens/04-one-l2-d8-mechanics.txt new file mode 100644 index 0000000..33fafa1 --- /dev/null +++ b/sim/goldens/04-one-l2-d8-mechanics.txt @@ -0,0 +1,9 @@ +04-one-l2-d8-mechanics: 25 fights, seeds 1..25, mechanics driver, policy attack + rate 95% interval + won 0.840 [0.653, 0.936] + died 0.160 [0.064, 0.347] + fled 0.000 [0.000, 0.133] + rounds mean 3.16 2:8 3:6 4:10 5:1 + hp left mean 8.86 1:1 2:1 4:3 6:5 7:1 10:2 11:1 13:2 14:1 15:2 16:1 17:1 (survivors) + damage taken 10.92, dealt 14.72 a fight + blows taken 2.32, struck 3.16 a fight diff --git a/sim/goldens/04-one-l2-d8-mechanics.yml b/sim/goldens/04-one-l2-d8-mechanics.yml new file mode 100644 index 0000000..759d460 --- /dev/null +++ b/sim/goldens/04-one-l2-d8-mechanics.yml @@ -0,0 +1,7 @@ +# The house body against one level-2 d8 foe, attacking to the end, on the +# mechanics loop: the loop the told driver must agree with seed for seed. +seeds: 1..25 +driver: mechanics +foes: + - {count: 1, stats: {level: 2, hit_die: 8}} +policy: attack diff --git a/sim/goldens/05-one-l2-d8-flee.txt b/sim/goldens/05-one-l2-d8-flee.txt new file mode 100644 index 0000000..ef8acbd --- /dev/null +++ b/sim/goldens/05-one-l2-d8-flee.txt @@ -0,0 +1,9 @@ +05-one-l2-d8-flee: 25 fights, seeds 1..25, told driver, policy flee_below + rate 95% interval + won 0.400 [0.234, 0.593] + died 0.320 [0.172, 0.516] + fled 0.280 [0.143, 0.476] + rounds mean 2.92 2:8 3:11 4:6 + hp left mean 9.18 1:1 2:2 3:2 5:1 6:1 10:2 11:1 13:2 14:1 15:2 16:1 17:1 (survivors) + damage taken 12.32, dealt 11.20 a fight + blows taken 2.52, struck 2.32 a fight diff --git a/sim/goldens/05-one-l2-d8-flee.yml b/sim/goldens/05-one-l2-d8-flee.yml new file mode 100644 index 0000000..9a7b83b --- /dev/null +++ b/sim/goldens/05-one-l2-d8-flee.yml @@ -0,0 +1,6 @@ +# The house body against one level-2 d8 foe, running to the next room at +# 8 hit points or below: the parting blow. +seeds: 1..25 +foes: + - {count: 1, stats: {level: 2, hit_die: 8}} +policy: {kind: flee_below, hp: 8} diff --git a/sim/goldens/06-two-l2-d10-flee.txt b/sim/goldens/06-two-l2-d10-flee.txt new file mode 100644 index 0000000..d58fcdc --- /dev/null +++ b/sim/goldens/06-two-l2-d10-flee.txt @@ -0,0 +1,10 @@ +06-two-l2-d10-flee: 25 fights, seeds 1..25, told driver, policy flee_below + rate 95% interval + won 0.000 [0.000, 0.133] + died 1.000 [0.867, 1.000] + fled 0.000 [0.000, 0.133] + rounds mean 2.72 2:12 3:8 4:5 + hp left (nobody survived) + damage taken 27.36, dealt 3.12 a fight + blows taken 5.04, struck 0.72 a fight + used drinks 0.84, throws 1.00 a fight diff --git a/sim/goldens/06-two-l2-d10-flee.yml b/sim/goldens/06-two-l2-d10-flee.yml new file mode 100644 index 0000000..0657f5d --- /dev/null +++ b/sim/goldens/06-two-l2-d10-flee.yml @@ -0,0 +1,11 @@ +# The house body against two level-2 d10 foes, with a healing draught and a +# brick in hand: throw the brick first, drink at 9 hit points or below, and +# run to the next room at 6 or below. +seeds: 1..25 +player: + carrying: + - {name: healing draught, use_kind: healing} + - {name: brick, bulk: heavy} +foes: + - {count: 2, stats: {level: 2, hit_die: 10}} +policy: {kind: flee_below, hp: 6, drink_below: 9, throw_first: true} diff --git a/sim/goldens/07-one-l3-d10-drink.txt b/sim/goldens/07-one-l3-d10-drink.txt new file mode 100644 index 0000000..e3caaff --- /dev/null +++ b/sim/goldens/07-one-l3-d10-drink.txt @@ -0,0 +1,10 @@ +07-one-l3-d10-drink: 25 fights, seeds 1..25, told driver, policy drink_below + rate 95% interval + won 0.560 [0.371, 0.733] + died 0.440 [0.267, 0.629] + fled 0.000 [0.000, 0.133] + rounds mean 5.04 3:1 4:7 5:10 6:4 7:3 + hp left mean 5.50 1:2 2:2 3:1 5:3 8:3 9:2 11:1 (survivors) + damage taken 23.88, dealt 20.48 a fight + blows taken 4.48, struck 4.12 a fight + used drinks 0.92, throws 0.00 a fight diff --git a/sim/goldens/07-one-l3-d10-drink.yml b/sim/goldens/07-one-l3-d10-drink.yml new file mode 100644 index 0000000..3a93214 --- /dev/null +++ b/sim/goldens/07-one-l3-d10-drink.yml @@ -0,0 +1,9 @@ +# The house body against one level-3 d10 foe with a healing draught, +# drinking at 8 hit points or below and otherwise attacking to the end. +seeds: 1..25 +player: + carrying: + - {name: healing draught, use_kind: healing} +foes: + - {count: 1, stats: {level: 3, hit_die: 10}} +policy: {kind: drink_below, hp: 8} diff --git a/sim/goldens/08-two-l1-rolled.txt b/sim/goldens/08-two-l1-rolled.txt new file mode 100644 index 0000000..ad1d0b9 --- /dev/null +++ b/sim/goldens/08-two-l1-rolled.txt @@ -0,0 +1,9 @@ +08-two-l1-rolled: 25 fights, seeds 1..25, told driver, policy attack + rate 95% interval + won 0.480 [0.300, 0.665] + died 0.520 [0.335, 0.700] + fled 0.000 [0.000, 0.133] + rounds mean 2.96 2:9 3:9 4:6 5:1 + hp left mean 7.25 1:1 2:1 3:2 6:2 8:1 9:1 10:1 11:1 13:1 15:1 (survivors) + damage taken 15.68, dealt 13.92 a fight + blows taken 3.44, struck 2.96 a fight diff --git a/sim/goldens/08-two-l1-rolled.yml b/sim/goldens/08-two-l1-rolled.yml new file mode 100644 index 0000000..c92429d --- /dev/null +++ b/sim/goldens/08-two-l1-rolled.yml @@ -0,0 +1,6 @@ +# The house body against two level-1 foes of a rolled hit die, attacking +# to the end: the engine's own roll for a foe's stats. +seeds: 1..25 +foes: + - {count: 2, stats: {level: 1, hit_die: rolled}} +policy: attack diff --git a/sim/goldens/09-careful.txt b/sim/goldens/09-careful.txt new file mode 100644 index 0000000..b95e14a --- /dev/null +++ b/sim/goldens/09-careful.txt @@ -0,0 +1,10 @@ +09-careful: 25 fights, seeds 1..25, told driver, policy careful + rate 95% interval + won 0.160 [0.064, 0.347] + died 0.360 [0.202, 0.555] + fled 0.480 [0.300, 0.665] + rounds mean 2.24 1:8 2:5 3:11 5:1 + hp left mean 7.69 1:1 2:2 4:3 5:1 7:1 8:1 11:3 12:1 13:2 15:1 (survivors) + damage taken 17.44, dealt 4.88 a fight + blows taken 3.52, struck 1.00 a fight + used drinks 0.40, throws 0.00 a fight diff --git a/sim/goldens/09-careful.yml b/sim/goldens/09-careful.yml new file mode 100644 index 0000000..22dda8e --- /dev/null +++ b/sim/goldens/09-careful.yml @@ -0,0 +1,9 @@ +# The dungeon studies' careful player, at the scale of one fight, against +# two level-1 foes of a rolled hit die, with one healing draught. +seeds: 1..25 +player: + carrying: + - {name: healing draught, use_kind: healing} +foes: + - {count: 2, stats: {level: 1, hit_die: rolled}} +policy: careful diff --git a/sim/goldens/10-naive.txt b/sim/goldens/10-naive.txt new file mode 100644 index 0000000..1bc29fe --- /dev/null +++ b/sim/goldens/10-naive.txt @@ -0,0 +1,10 @@ +10-naive: 25 fights, seeds 1..25, told driver, policy naive + rate 95% interval + won 0.600 [0.407, 0.766] + died 0.400 [0.234, 0.593] + fled 0.000 [0.000, 0.133] + rounds mean 3.44 2:8 3:5 4:6 5:5 6:1 + hp left mean 7.47 2:1 3:4 6:2 8:1 9:2 10:1 11:2 13:1 15:1 (survivors) + damage taken 18.36, dealt 13.88 a fight + blows taken 3.92, struck 2.92 a fight + used drinks 0.52, throws 0.00 a fight diff --git a/sim/goldens/10-naive.yml b/sim/goldens/10-naive.yml new file mode 100644 index 0000000..5460fd5 --- /dev/null +++ b/sim/goldens/10-naive.yml @@ -0,0 +1,9 @@ +# The dungeon studies' naive player against the careful one's fight +# (09-careful): two level-1 foes of a rolled hit die, one healing draught. +seeds: 1..25 +player: + carrying: + - {name: healing draught, use_kind: healing} +foes: + - {count: 2, stats: {level: 1, hit_die: rolled}} +policy: naive diff --git a/src/bin/fight.rs b/src/bin/fight.rs index a2c90cb..c9bc521 100644 --- a/src/bin/fight.rs +++ b/src/bin/fight.rs @@ -12,14 +12,19 @@ //! //! `fight MATCHUP.yml --replay SEED` plays that one fight again and prints //! it line by line: what was typed, every blow, and everybody's hit points. +//! +//! `fight --check DIR` plays every matchup in `DIR/goldens` and fails when +//! a table differs from the golden kept beside it (`sim::golden`). +//! `fight --write DIR` writes each golden again whose table moved. -use renderedstep_engine::sim::{self, matchup::Matchup, tally, Driver}; +use renderedstep_engine::sim::{self, golden, matchup::Matchup, tally, Driver}; use std::path::Path; use std::process::ExitCode; use std::time::Instant; const USAGE: &str = "usage: fight MATCHUP.yml [--seeds A..B] [--driver told|mechanics] \ - [--threads N] [--json] [--replay SEED]"; + [--threads N] [--json] [--replay SEED] \ + | fight --check DIR | fight --write DIR"; fn main() -> ExitCode { // SQLite counts every allocation under one lock for the whole process, @@ -39,6 +44,15 @@ fn main() -> ExitCode { } fn run(args: Vec) -> Result<(), String> { + match args.as_slice() { + [flag, dir] if flag == "--check" => return check(Path::new(dir)), + [flag, dir] if flag == "--write" => { + let written = golden::write(Path::new(dir))?; + println!("WROTE: {} golden(s): {}", written.len(), written.join(", ")); + return Ok(()); + } + _ => {} + } let mut file = None; let mut json = false; let mut replay = None; @@ -104,36 +118,45 @@ fn run(args: Vec) -> Result<(), String> { return Ok(()); } - let started = Instant::now(); - let fights = sim::run(&matchup, &matchup.seeds, matchup.threads)?; - let seconds = started.elapsed().as_secs_f64(); - let summary = tally::Summary::of(&fights); - let (first, last) = ( - matchup.seeds.first().copied().unwrap_or_default(), - matchup.seeds.last().copied().unwrap_or_default(), - ); - if json { - let mut out = summary.json(); - out["matchup"] = matchup.name.clone().into(); - out["driver"] = matchup.driver.as_str().into(); - out["seeds"] = format!("{first}..{last}").into(); - out["policy"] = matchup.policy.kind.clone().into(); - println!("{out}"); - } else { - let heading = format!( - "{}: {} fights, seeds {first}..{last}, {} driver, policy {}", - matchup.name, - fights.len(), - matchup.driver.as_str(), - matchup.policy.kind, - ); - print!("{}", summary.text(&heading)); + if !json { + let started = Instant::now(); + print!("{}", golden::table(&matchup)?); + let seconds = started.elapsed().as_secs_f64(); eprintln!( " ({:.1}s, {:.0} fights/s on {} thread(s))", seconds, - fights.len() as f64 / seconds.max(1e-9), + matchup.seeds.len() as f64 / seconds.max(1e-9), matchup.threads.max(1) ); + return Ok(()); } + let fights = sim::run(&matchup, &matchup.seeds, matchup.threads)?; + let (first, last) = ( + matchup.seeds.first().copied().unwrap_or_default(), + matchup.seeds.last().copied().unwrap_or_default(), + ); + let mut out = tally::Summary::of(&fights).json(); + out["matchup"] = matchup.name.clone().into(); + out["driver"] = matchup.driver.as_str().into(); + out["seeds"] = format!("{first}..{last}").into(); + out["policy"] = matchup.policy.kind.clone().into(); + println!("{out}"); Ok(()) } + +fn check(dir: &Path) -> Result<(), String> { + let (played, failures) = golden::check(dir)?; + if failures.is_empty() { + println!("AGREED: {} fight golden(s).", played.len()); + Ok(()) + } else { + Err(format!( + "{}\nMOVED: {} of {} fight golden(s); if the odds were meant to move, \ + run fight --write {} and keep the diff in the same PR.", + failures.join("\n"), + failures.len(), + played.len(), + dir.display() + )) + } +} diff --git a/src/sim/golden.rs b/src/sim/golden.rs new file mode 100644 index 0000000..0e927d0 --- /dev/null +++ b/src/sim/golden.rs @@ -0,0 +1,99 @@ +//! Fight goldens: the standard matchups in `DIR/goldens/*.yml`, each played +//! over its own seeds, and the table each one printed kept beside it as +//! `DIR/goldens/*.txt`. A rule that moves fight odds moves a table, so +//! `fight --check` fails until `fight --write` blesses the new one and the +//! diff shows what moved. + +use super::matchup::Matchup; +use super::tally::Summary; +use std::path::{Path, PathBuf}; + +/// One golden matchup and the table it plays to now. +pub struct Played { + pub name: String, + pub golden: PathBuf, + pub table: String, +} + +/// The table a golden keeps: [`Summary::text`] under a heading that names +/// the fights, with nothing in it that varies from run to run. +pub fn table(matchup: &Matchup) -> Result { + let fights = super::run(matchup, &matchup.seeds, matchup.threads)?; + let (first, last) = ( + matchup.seeds.first().copied().unwrap_or_default(), + matchup.seeds.last().copied().unwrap_or_default(), + ); + let heading = format!( + "{}: {} fights, seeds {first}..{last}, {} driver, policy {}", + matchup.name, + fights.len(), + matchup.driver.as_str(), + matchup.policy.kind, + ); + Ok(Summary::of(&fights).text(&heading)) +} + +/// Plays every matchup in `DIR/goldens`, in name order. +pub fn play(dir: &Path) -> Result, String> { + let goldens = dir.join("goldens"); + let mut files: Vec = std::fs::read_dir(&goldens) + .map_err(|error| format!("{}: {error}", goldens.display()))? + .filter_map(|entry| entry.ok().map(|entry| entry.path())) + .filter(|path| path.extension().is_some_and(|ext| ext == "yml")) + .collect(); + files.sort(); + if files.is_empty() { + return Err(format!("{}: no matchups", goldens.display())); + } + files + .iter() + .map(|file| { + let matchup = Matchup::load(file)?; + Ok(Played { + name: matchup.name.clone(), + golden: file.with_extension("txt"), + table: table(&matchup)?, + }) + }) + .collect() +} + +/// Every golden that does not hold its matchup's table now, each with what +/// it holds and what was played; and every table with no matchup. +pub fn check(dir: &Path) -> Result<(Vec, Vec), String> { + let played = play(dir)?; + let mut failures = Vec::new(); + for one in &played { + match std::fs::read_to_string(&one.golden) { + Ok(kept) if kept == one.table => {} + Ok(kept) => failures.push(format!( + "{} moved.\n--- kept\n{kept}+++ played\n{}", + one.golden.display(), + one.table + )), + Err(error) => failures.push(format!("{}: {error}", one.golden.display())), + } + } + let goldens = dir.join("goldens"); + for entry in std::fs::read_dir(&goldens).map_err(|error| error.to_string())? { + let path = entry.map_err(|error| error.to_string())?.path(); + if path.extension().is_some_and(|ext| ext == "txt") && !path.with_extension("yml").exists() + { + failures.push(format!("{}: no matchup plays it", path.display())); + } + } + Ok((played, failures)) +} + +/// Writes each golden whose table moved, and names them. +pub fn write(dir: &Path) -> Result, String> { + let mut written = Vec::new(); + for one in play(dir)? { + if std::fs::read_to_string(&one.golden).ok().as_deref() != Some(one.table.as_str()) { + std::fs::write(&one.golden, &one.table) + .map_err(|error| format!("{}: {error}", one.golden.display()))?; + written.push(one.name); + } + } + Ok(written) +} diff --git a/src/sim/mod.rs b/src/sim/mod.rs index 173cbe1..03e6e41 100644 --- a/src/sim/mod.rs +++ b/src/sim/mod.rs @@ -18,6 +18,7 @@ //! [`tally::Summary`] counts what many fights did. pub mod arena; +pub mod golden; pub mod matchup; pub mod policy; pub mod tally;