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 { };
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 ];
}

View File

@ -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 = "<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 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<Cache>, msg: Json<battlesnake::SnakeRequest>) -> Json<batt
.expect("wanted to lock cache")
.insert(msg.game.id.clone(), gs);
Json(battlesnake::StartResponse{
Json(battlesnake::StartResponse {
color: "#5ce8c3".to_string(),
head_type: "beluga".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(
msg: &battlesnake::SnakeRequest,
head: &battlesnake::Coord,
target: &battlesnake::Coord
target: &battlesnake::Coord,
) -> Option<Vec<battlesnake::Coord>> {
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 = "<msg>")]
fn end(
cache_state: State<Cache>,
msg: Json<battlesnake::SnakeRequest>,
) -> String {
fn end(cache_state: State<Cache>, msg: Json<battlesnake::SnakeRequest>) -> 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::<String, GameState>::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();
}