From 97d8ec4ee872119b6685a5bbd89e2e65b31a3578 Mon Sep 17 00:00:00 2001 From: Christine Dodrill Date: Fri, 14 Feb 2020 15:22:31 +0000 Subject: [PATCH] docker.nix --- docker.nix | 22 ++++++++++++++++++++++ shell.nix | 11 +++-------- src/main.rs | 54 ++++++++++++++++++++++++++--------------------------- 3 files changed, 51 insertions(+), 36 deletions(-) create mode 100644 docker.nix diff --git a/docker.nix b/docker.nix new file mode 100644 index 0000000..dfd6427 --- /dev/null +++ b/docker.nix @@ -0,0 +1,22 @@ +{ system ? builtins.currentSystem }: + +let + sources = import ./nix/sources.nix; + pkgs = import sources.nixpkgs { }; + callPackage = pkgs.lib.callPackageWith pkgs; + pneuma = callPackage ./pneuma.nix { }; + + dockerImage = pkg: + pkgs.dockerTools.buildImage { + name = "xena/pneuma"; + tag = pkg.version; + + contents = [ pkg ]; + + config = { + Cmd = [ "/bin/pneuma" ]; + WorkingDir = "/"; + }; + }; + +in dockerImage pneuma diff --git a/shell.nix b/shell.nix index bc0fe5b..b02014a 100644 --- a/shell.nix +++ b/shell.nix @@ -3,12 +3,7 @@ let niv = import sources.niv { }; pkgs = import sources.nixpkgs { }; rust = import ./nix/rust.nix { inherit sources; }; -in -pkgs.mkShell { - buildInputs = [ - rust.rust - niv.niv - - pkgs.glibc - ]; +in pkgs.mkShell { + buildInputs = [ rust.rust niv.niv ]; + nativeBuildInputs = [ pkgs.removeReferencesTo ]; } diff --git a/src/main.rs b/src/main.rs index 218d68c..adbce66 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,14 +1,12 @@ #![feature(proc_macro_hygiene, decl_macro)] -#[macro_use] extern crate rocket; -#[macro_use] extern crate rocket_contrib; -#[macro_use] extern crate serde_derive; +#[macro_use] +extern crate rocket; use rocket::State; use rocket_contrib::json::Json; -use pathfinding::grid::Grid; use std::collections::HashMap; -use std::sync::{Arc, Mutex}; +use std::sync::Mutex; mod battlesnake; @@ -31,11 +29,14 @@ fn ping() -> &'static str { } #[post("/start", format = "json", data = "")] -fn start(cache: State, msg: Json) -> Json { +fn start( + cache: State, + msg: Json, +) -> Json { let head = msg.you.body[0]; let target = find_target(&msg); let path = find_path(&msg, &head, &target); - let gs = GameState{ + let gs = GameState { target: *target, path: path, }; @@ -45,7 +46,7 @@ fn start(cache: State, msg: Json) -> Json, msg: Json) -> Json Option> { let path = pathfinding::directed::astar::astar( head, @@ -68,15 +69,12 @@ fn find_path( None => return None, Some(x) => { return Some(x.0); - }, + } } } #[post("/end", format = "json", data = "")] -fn end( - cache_state: State, - msg: Json, -) -> String { +fn end(cache_state: State, msg: Json) -> String { cache_state .lock() .expect("wanted cache to be lockable") @@ -104,7 +102,7 @@ fn make_move( None => { gs.path = None; let target = msg.board.safe_neighbors(&head)[0].0; - let next_move = battlesnake::Line{ + let next_move = battlesnake::Line { start: &head, end: &target, } @@ -119,18 +117,23 @@ fn make_move( let next_move = battlesnake::Line { start: &head, end: &next, - }.direction().to_string(); - println!("moving to {} {:?}, target: {:?}", next_move, next, gs.target); + } + .direction() + .to_string(); + println!( + "moving to {} {:?}, target: {:?}", + next_move, next, gs.target + ); Json(battlesnake::MoveResponse { move_field: next_move, }) - }, + } } } fn find_target<'a>(gs: &'a battlesnake::SnakeRequest) -> &'a battlesnake::Coord { let head = &gs.you.body[0]; - //if gs.you.health > 30 { + if gs.you.health > 30 { let mut lowest_score: u32 = 99999; let mut coord: &battlesnake::Coord = &gs.you.body.last().unwrap(); @@ -143,21 +146,16 @@ fn find_target<'a>(gs: &'a battlesnake::SnakeRequest) -> &'a battlesnake::Coord } return coord; - //} + } - //return gs.you.body.last().unwrap(); + return gs.you.body.last().unwrap(); } fn main() { let map = HashMap::::new(); let mutex_map = Mutex::from(map); - rocket::ignite().mount("/", routes![ - index, - start, - ping, - make_move, - end, - ]) + rocket::ignite() + .mount("/", routes![index, start, ping, make_move, end,]) .manage(mutex_map) .launch(); }