docker.nix
This commit is contained in:
parent
030fce4bb9
commit
97d8ec4ee8
|
@ -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
|
11
shell.nix
11
shell.nix
|
@ -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
|
|
||||||
];
|
|
||||||
}
|
}
|
||||||
|
|
48
src/main.rs
48
src/main.rs
|
@ -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,7 +29,10 @@ 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);
|
||||||
|
@ -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")
|
||||||
|
@ -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();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue