diff options
author | eug-vs <eugene@eug-vs.xyz> | 2024-01-21 15:34:54 +0100 |
---|---|---|
committer | eug-vs <eugene@eug-vs.xyz> | 2024-01-21 15:34:54 +0100 |
commit | 9a5ec69554a5b93bb57d3f35828751d4d2241958 (patch) | |
tree | 856a4ff04fda6a4bad3e81df77060e91619356e0 /src | |
parent | 7372ece3176242e5ea472c57f84030ac37f590e6 (diff) | |
download | chessnost-9a5ec69554a5b93bb57d3f35828751d4d2241958.tar.gz |
feat: add anomaly searcher script
Diffstat (limited to 'src')
-rw-r--r-- | src/anomaly-searcher.rs | 68 |
1 files changed, 68 insertions, 0 deletions
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<Vec<String>> { + if let Ok(file) = File::open(log_file) { + let lines: Result<Vec<String>, _> = io::BufReader::new(file).lines().collect(); + return lines.ok(); + } + + None +} + +fn main() { + let args: Vec<String> = 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); + } +} |