Compare commits

..

2 Commits

Author SHA1 Message Date
60bd5acd35 add xtask to push docker image 2024-09-02 20:00:05 +02:00
0b02efc752 simplify safety check 2024-09-02 19:59:48 +02:00
3 changed files with 24 additions and 20 deletions

View File

@ -1,8 +1,8 @@
FROM rust:1.80 FROM rust:1.80
COPY . /usr/app COPY battlesnake/ /usr/app
WORKDIR /usr/app WORKDIR /usr/app
RUN cargo install --path battlesnake RUN cargo install --path .
CMD ["battlesnake"] CMD ["battlesnake"]

View File

@ -20,26 +20,12 @@ impl Battlesnake {
/// Check if moving the snake in `direction` is safe. /// Check if moving the snake in `direction` is safe.
#[must_use] #[must_use]
pub fn is_direction_safe(&self, direction: Direction, game: &Game, board: &Board) -> bool { pub fn is_direction_safe(&self, direction: Direction, game: &Game, board: &Board) -> bool {
if self.is_direction_death(direction, game, board) {
return false;
}
let target = self.head.move_to(direction); let target = self.head.move_to(direction);
// check if target is out of bounds
if !((0..board.width).contains(&target.x) && (0..board.height).contains(&target.y)) {
return false;
}
// check if target is inside a snake
if board
.snakes
.iter()
.filter(|snake| {
!(game.ruleset.settings.squad.allow_body_collisions && self.squad == snake.squad)
})
.flat_map(|snake| snake.body.iter())
.any(|&coord| target == coord)
{
return false;
}
// check if a bigger snake could move to this square // check if a bigger snake could move to this square
if board if board
.snakes .snakes

View File

@ -21,6 +21,7 @@ fn try_main() -> Result<(), DynError> {
match task.as_deref() { match task.as_deref() {
Some("local") => local()?, Some("local") => local()?,
Some("local2") => local2()?, Some("local2") => local2()?,
Some("docker") => docker()?,
_ => print_help(), _ => print_help(),
} }
Ok(()) Ok(())
@ -32,6 +33,8 @@ fn print_help() {
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 local2 runs the snake twice on a local game
docker package docker image and upload to docker.mkaenner.de/snake
" "
) )
} }
@ -120,6 +123,21 @@ fn run_snake(port: u16) -> Result<Child, DynError> {
} }
} }
fn docker() -> Result<(), DynError> {
if !Command::new("docker")
.current_dir(project_root())
.args(["build", ".", "-t", "docker.mkaenner.de/snake"])
.status()?
.success()
{
Err("Build of docker image failed")?;
}
Command::new("docker")
.args(["push", "docker.mkaenner.de/snake"])
.status()?;
Ok(())
}
fn project_root() -> PathBuf { fn project_root() -> PathBuf {
Path::new(&env!("CARGO_MANIFEST_DIR")) Path::new(&env!("CARGO_MANIFEST_DIR"))
.ancestors() .ancestors()