From 9a5ec69554a5b93bb57d3f35828751d4d2241958 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Sun, 21 Jan 2024 15:34:54 +0100 Subject: feat: add anomaly searcher script --- Cargo.toml | 6 +++++ src/anomaly-searcher.rs | 68 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 src/anomaly-searcher.rs diff --git a/Cargo.toml b/Cargo.toml index ca8466d..379a62b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,6 +2,7 @@ name = "chessnost" version = "0.1.0" edition = "2021" +default-run = "chessnost" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html @@ -21,3 +22,8 @@ harness = false [[bench]] name = "search" harness = false + +[[bin]] +name = "anomaly-searcher" +path = "src/anomaly-searcher.rs" + diff --git a/src/anomaly-searcher.rs b/src/anomaly-searcher.rs new file mode 100644 index 0000000..6b5cc15 --- /dev/null +++ b/src/anomaly-searcher.rs @@ -0,0 +1,68 @@ +use std::env; +use std::fs::File; +use std::io::{self, BufRead, Write}; +use std::path::Path; +use std::process::{Command, Stdio}; + +fn read_game_log(log_file: &str) -> Option> { + if let Ok(file) = File::open(log_file) { + let lines: Result, _> = io::BufReader::new(file).lines().collect(); + return lines.ok(); + } + + None +} + +fn main() { + let args: Vec = env::args().collect(); + + if args.len() != 2 { + eprintln!("Usage: ./anomaly-searcher game.log"); + std::process::exit(1); + } + + let game_log_file = &args[1]; + if let Some(lines) = read_game_log(game_log_file) { + let mut engine_process = Command::new("./target/release/chessnost") + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .spawn() + .expect("Failed to start the engine process"); + + let mut engine_stdin = engine_process.stdin.take().expect("Failed to open engine STDIN"); + let mut engine_stdout = io::BufReader::new(engine_process.stdout.take().expect("Failed to open engine STDOUT")); + + for line in lines.iter().filter(|l| !l.contains("info string")) { + if line.starts_with("<<") { + // Send the line to the engine without printing it + writeln!(engine_stdin, "{}", &line[3..]).expect("Failed to write to engine STDIN"); + println!("{}", line); + } else if line.starts_with(">>") { + // Wait for the engine's output + let mut response = String::new(); + while response.is_empty() || response.contains("info string") { + response = String::new(); + engine_stdout.read_line(&mut response).expect("Failed to read from engine STDOUT"); + } + + // Print the engine's response + print!(">> {}", response); + + // Compare the engine's response to the original log + let original_response = line[3..].trim(); // Remove the ">> " prefix + if response.trim() != original_response { + println!("** {}\n", original_response); + } + } + } + + // Close the engine's STDIN to signal that we're done sending commands + drop(engine_stdin); + + // Wait for the engine process to finish + let _ = engine_process.wait(); + } else { + eprintln!("Error reading the game log."); + std::process::exit(1); + } +} -- cgit v1.2.3