docker.nix

This commit is contained in:
Cadey Ratio 2020-02-14 15:22:31 +00:00
parent 030fce4bb9
commit 97d8ec4ee8
3 changed files with 51 additions and 36 deletions

22
docker.nix Normal file
View File

@ -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

View File

@ -3,12 +3,7 @@ let
niv = import sources.niv { }; niv = import sources.niv { };
pkgs = import sources.nixpkgs { }; pkgs = import sources.nixpkgs { };
rust = import ./nix/rust.nix { inherit sources; }; rust = import ./nix/rust.nix { inherit sources; };
in in pkgs.mkShell {
pkgs.mkShell { buildInputs = [ rust.rust niv.niv ];
buildInputs = [ nativeBuildInputs = [ pkgs.removeReferencesTo ];
rust.rust
niv.niv
pkgs.glibc
];
} }

View File

@ -1,14 +1,12 @@
#![feature(proc_macro_hygiene, decl_macro)] #![feature(proc_macro_hygiene, decl_macro)]
#[macro_use] extern crate rocket; #[macro_use]
#[macro_use] extern crate rocket_contrib; extern crate rocket;
#[macro_use] extern crate serde_derive;
use rocket::State; use rocket::State;
use rocket_contrib::json::Json; use rocket_contrib::json::Json;
use pathfinding::grid::Grid;
use std::collections::HashMap; use std::collections::HashMap;
use std::sync::{Arc, Mutex}; use std::sync::Mutex;
mod battlesnake; mod battlesnake;
@ -31,11 +29,14 @@ fn ping() -> &'static str {
} }
#[post("/start", format = "json", data = "<msg>")] #[post("/start", format = "json", data = "<msg>")]
fn start(cache: State<Cache>, msg: Json<battlesnake::SnakeRequest>) -> Json<battlesnake::StartResponse> { fn start(
cache: State<Cache>,
msg: Json<battlesnake::SnakeRequest>,
) -> Json<battlesnake::StartResponse> {
let head = msg.you.body[0]; let head = msg.you.body[0];
let target = find_target(&msg); let target = find_target(&msg);
let path = find_path(&msg, &head, &target); let path = find_path(&msg, &head, &target);
let gs = GameState{ let gs = GameState {
target: *target, target: *target,
path: path, path: path,
}; };
@ -45,7 +46,7 @@ fn start(cache: State<Cache>, msg: Json<battlesnake::SnakeRequest>) -> Json<batt
.expect("wanted to lock cache") .expect("wanted to lock cache")
.insert(msg.game.id.clone(), gs); .insert(msg.game.id.clone(), gs);
Json(battlesnake::StartResponse{ Json(battlesnake::StartResponse {
color: "#5ce8c3".to_string(), color: "#5ce8c3".to_string(),
head_type: "beluga".to_string(), head_type: "beluga".to_string(),
tail_type: "skinny".to_string(), tail_type: "skinny".to_string(),
@ -55,7 +56,7 @@ fn start(cache: State<Cache>, msg: Json<battlesnake::SnakeRequest>) -> Json<batt
fn find_path( fn find_path(
msg: &battlesnake::SnakeRequest, msg: &battlesnake::SnakeRequest,
head: &battlesnake::Coord, head: &battlesnake::Coord,
target: &battlesnake::Coord target: &battlesnake::Coord,
) -> Option<Vec<battlesnake::Coord>> { ) -> Option<Vec<battlesnake::Coord>> {
let path = pathfinding::directed::astar::astar( let path = pathfinding::directed::astar::astar(
head, head,
@ -68,15 +69,12 @@ fn find_path(
None => return None, None => return None,
Some(x) => { Some(x) => {
return Some(x.0); return Some(x.0);
}, }
} }
} }
#[post("/end", format = "json", data = "<msg>")] #[post("/end", format = "json", data = "<msg>")]
fn end( fn end(cache_state: State<Cache>, msg: Json<battlesnake::SnakeRequest>) -> String {
cache_state: State<Cache>,
msg: Json<battlesnake::SnakeRequest>,
) -> String {
cache_state cache_state
.lock() .lock()
.expect("wanted cache to be lockable") .expect("wanted cache to be lockable")
@ -104,7 +102,7 @@ fn make_move(
None => { None => {
gs.path = None; gs.path = None;
let target = msg.board.safe_neighbors(&head)[0].0; let target = msg.board.safe_neighbors(&head)[0].0;
let next_move = battlesnake::Line{ let next_move = battlesnake::Line {
start: &head, start: &head,
end: &target, end: &target,
} }
@ -119,18 +117,23 @@ fn make_move(
let next_move = battlesnake::Line { let next_move = battlesnake::Line {
start: &head, start: &head,
end: &next, 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 { Json(battlesnake::MoveResponse {
move_field: next_move, move_field: next_move,
}) })
}, }
} }
} }
fn find_target<'a>(gs: &'a battlesnake::SnakeRequest) -> &'a battlesnake::Coord { fn find_target<'a>(gs: &'a battlesnake::SnakeRequest) -> &'a battlesnake::Coord {
let head = &gs.you.body[0]; let head = &gs.you.body[0];
//if gs.you.health > 30 { if gs.you.health > 30 {
let mut lowest_score: u32 = 99999; let mut lowest_score: u32 = 99999;
let mut coord: &battlesnake::Coord = &gs.you.body.last().unwrap(); 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 coord;
//} }
//return gs.you.body.last().unwrap(); return gs.you.body.last().unwrap();
} }
fn main() { fn main() {
let map = HashMap::<String, GameState>::new(); let map = HashMap::<String, GameState>::new();
let mutex_map = Mutex::from(map); let mutex_map = Mutex::from(map);
rocket::ignite().mount("/", routes![ rocket::ignite()
index, .mount("/", routes![index, start, ping, make_move, end,])
start,
ping,
make_move,
end,
])
.manage(mutex_map) .manage(mutex_map)
.launch(); .launch();
} }