This commit is contained in:
Cadey Ratio 2019-12-18 03:20:09 +00:00
parent 68bf72c071
commit 67f127df21
1 changed files with 45 additions and 21 deletions

View File

@ -12,7 +12,9 @@ use std::sync::{Arc, Mutex};
mod battlesnake;
type Cache = Arc<Mutex<HashMap<String, GameState>>>;
type Cache = Mutex<HashMap<String, GameState>>;
#[derive(Debug, Clone)]
pub struct GameState {
path: Option<Vec<battlesnake::Coord>>,
target: battlesnake::Coord,
@ -38,7 +40,9 @@ fn begin(cache: State<Cache>, msg: Json<battlesnake::SnakeRequest>) -> Json<batt
path: path,
};
cache.inner().get_mut().unwrap().insert(msg.game.id, gs);
if let Some(x) = cache.lock().expect("wanted cache to be unlockable").get_mut(&msg.game.id) {
*x = gs;
};
Json(battlesnake::StartResponse{
color: "#5ce8c3".to_string(),
@ -47,7 +51,11 @@ fn begin(cache: State<Cache>, msg: Json<battlesnake::SnakeRequest>) -> Json<batt
})
}
fn find_path(msg: &battlesnake::SnakeRequest, head: &battlesnake::Coord, target: &battlesnake::Coord) -> Option<Vec<battlesnake::Coord>> {
fn find_path(
msg: &battlesnake::SnakeRequest,
head: &battlesnake::Coord,
target: &battlesnake::Coord
) -> Option<Vec<battlesnake::Coord>> {
let path = pathfinding::directed::astar::astar(
head,
|n| msg.board.safe_neighbors(n).into_iter(),
@ -64,25 +72,42 @@ fn find_path(msg: &battlesnake::SnakeRequest, head: &battlesnake::Coord, target:
}
#[post("/move", format = "json", data = "<msg>")]
fn make_move(msg: Json<battlesnake::SnakeRequest>) -> Json<battlesnake::MoveResponse> {
let target = find_target(&msg);
println!("target: {:?}", target);
fn make_move(cache: State<Cache>, msg: Json<battlesnake::SnakeRequest>) -> Json<battlesnake::MoveResponse> {
let head = msg.you.body[0];
if let Some(mut gs) = cache.lock().expect("wanted cache to be unlockable").get(&msg.game.id) {
let gs_path = gs.path.as_ref().or_else(|| {
let target = find_target(&msg);
let path = find_path(&msg, &head, &target);
gs = &GameState{
target: *target,
path: path,
};
let head = &msg.you.body[0];
println!("me: {:?}", head);
for ne in &msg.board.safe_neighbors(&head) {
println!("{:?} {}", ne.0, battlesnake::Line{
start: &head,
end: &ne.0,
}.direction());
path.as_ref()
});
let mut inner = gs_path.expect("what");
let next = inner[0];
inner.pop();
if inner.len() == 0 {
gs.path = None;
}
return Json(battlesnake::MoveResponse{
move_field: battlesnake::Line{
start: &head,
end: &next,
}.direction().to_string(),
})
}
println!("{:?}", pathfinding::directed::astar::astar(
head,
|n| msg.board.safe_neighbors(n).into_iter(),
|n| (battlesnake::manhattan(n, &target) as usize),
|n| n == target,
));
let target = find_target(&msg);
let path = find_path(&msg, &head, &target);
let gs = GameState{
target: *target,
path: path,
};
println!("{:?}", gs);
Json(battlesnake::MoveResponse {
move_field: "up".to_string(),
@ -112,13 +137,12 @@ fn find_target<'a>(gs: &'a battlesnake::SnakeRequest) -> &'a battlesnake::Coord
fn main() {
let map = HashMap::<String, GameState>::new();
let mutex_map = Mutex::from(map);
let arc_mutex_map = Arc::from(mutex_map);
rocket::ignite().mount("/", routes![
index,
begin,
ping,
make_move,
])
.manage(arc_mutex_map)
.manage(mutex_map)
.launch();
}