xtask for 2-player local game
This commit is contained in:
parent
bed2b1be81
commit
548b7ff7a6
@ -2,7 +2,7 @@ use std::{
|
|||||||
env,
|
env,
|
||||||
net::TcpStream,
|
net::TcpStream,
|
||||||
path::{Path, PathBuf},
|
path::{Path, PathBuf},
|
||||||
process::Command,
|
process::{Child, Command},
|
||||||
thread::sleep,
|
thread::sleep,
|
||||||
time::Duration,
|
time::Duration,
|
||||||
};
|
};
|
||||||
@ -20,6 +20,7 @@ fn try_main() -> Result<(), DynError> {
|
|||||||
let task = env::args().nth(1);
|
let task = env::args().nth(1);
|
||||||
match task.as_deref() {
|
match task.as_deref() {
|
||||||
Some("local") => local()?,
|
Some("local") => local()?,
|
||||||
|
Some("local2") => local2()?,
|
||||||
_ => print_help(),
|
_ => print_help(),
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
@ -30,27 +31,15 @@ fn print_help() {
|
|||||||
"Tasks:
|
"Tasks:
|
||||||
|
|
||||||
local runs the snake on a local game
|
local runs the snake on a local game
|
||||||
|
local2 runs the snake twice on a local game
|
||||||
"
|
"
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn local() -> Result<(), DynError> {
|
fn local() -> Result<(), DynError> {
|
||||||
let cargo = env::var("CARGO").unwrap_or_else(|_| "cargo".to_string());
|
let mut snake = run_snake(8000)?;
|
||||||
|
|
||||||
let mut snake = Command::new(cargo)
|
let game = Command::new("./battlesnake-cli")
|
||||||
.current_dir(project_root())
|
|
||||||
.args(
|
|
||||||
["run", "--bin", "battlesnake"]
|
|
||||||
.map(str::to_string)
|
|
||||||
.into_iter()
|
|
||||||
.chain(env::args().skip(1)),
|
|
||||||
)
|
|
||||||
.spawn()?;
|
|
||||||
|
|
||||||
while snake.try_wait()?.is_none() {
|
|
||||||
// check if port 8000 has been opened. Only then the game can be started
|
|
||||||
if TcpStream::connect(("127.0.0.1", 8000)).is_ok() {
|
|
||||||
let _game = Command::new("./battlesnake-cli")
|
|
||||||
.current_dir(project_root())
|
.current_dir(project_root())
|
||||||
.args([
|
.args([
|
||||||
"play",
|
"play",
|
||||||
@ -67,14 +56,68 @@ fn local() -> Result<(), DynError> {
|
|||||||
"--browser",
|
"--browser",
|
||||||
])
|
])
|
||||||
.status();
|
.status();
|
||||||
break;
|
|
||||||
} else {
|
game.and(snake.kill())?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn local2() -> Result<(), DynError> {
|
||||||
|
let mut snake1 = run_snake(8000)?;
|
||||||
|
let mut snake2 = match run_snake(8001) {
|
||||||
|
Ok(snake) => snake,
|
||||||
|
Err(e) => {
|
||||||
|
let _ = snake1.kill();
|
||||||
|
return Err(e);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let game = Command::new("./battlesnake-cli")
|
||||||
|
.current_dir(project_root())
|
||||||
|
.args([
|
||||||
|
"play",
|
||||||
|
"-W",
|
||||||
|
"11",
|
||||||
|
"-H",
|
||||||
|
"11",
|
||||||
|
"--name",
|
||||||
|
"local test1",
|
||||||
|
"--url",
|
||||||
|
"http://localhost:8000",
|
||||||
|
"--name",
|
||||||
|
"local test2",
|
||||||
|
"--url",
|
||||||
|
"http://localhost:8001",
|
||||||
|
"-g",
|
||||||
|
"duel",
|
||||||
|
"--browser",
|
||||||
|
])
|
||||||
|
.status();
|
||||||
|
|
||||||
|
game.and(snake1.kill()).and(snake2.kill())?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run_snake(port: u16) -> Result<Child, DynError> {
|
||||||
|
let cargo = env::var("CARGO").unwrap_or_else(|_| "cargo".to_string());
|
||||||
|
let mut snake = Command::new(cargo)
|
||||||
|
.current_dir(project_root())
|
||||||
|
.env("PORT", port.to_string())
|
||||||
|
.args(
|
||||||
|
["run", "--bin", "battlesnake"]
|
||||||
|
.map(str::to_string)
|
||||||
|
.into_iter()
|
||||||
|
.chain(env::args().skip(2)),
|
||||||
|
)
|
||||||
|
.spawn()?;
|
||||||
|
loop {
|
||||||
|
if let Some(status) = snake.try_wait()? {
|
||||||
|
Err(format!("{status}"))?;
|
||||||
|
}
|
||||||
|
if TcpStream::connect(("127.0.0.1", port)).is_ok() {
|
||||||
|
break Ok(snake);
|
||||||
|
}
|
||||||
sleep(Duration::from_secs(1));
|
sleep(Duration::from_secs(1));
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
snake.kill()?;
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn project_root() -> PathBuf {
|
fn project_root() -> PathBuf {
|
||||||
|
Loading…
Reference in New Issue
Block a user