Merge branch 'master' of artemis/pneuma into master

This commit is contained in:
Cadey Ratio 2019-12-18 12:23:36 +00:00 committed by Gitea
commit 617ca6066e
2 changed files with 301 additions and 310 deletions

550
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -72,46 +72,35 @@ fn find_path(
} }
#[post("/move", format = "json", data = "<msg>")] #[post("/move", format = "json", data = "<msg>")]
fn make_move(cache: State<Cache>, msg: Json<battlesnake::SnakeRequest>) -> Json<battlesnake::MoveResponse> { fn make_move(
cache_state: State<Cache>,
msg: Json<battlesnake::SnakeRequest>,
) -> Json<battlesnake::MoveResponse> {
let head = msg.you.body[0]; 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,
};
path.as_ref() let mut cache = cache_state.lock().expect("wanted cache to be unlockable");
}); let gs = cache.get_mut(&msg.game.id).unwrap();
let mut inner = gs_path.expect("what"); if gs.path.is_none() {
let next = inner[0]; gs.target = *find_target(&msg);
inner.pop(); gs.path = find_path(&msg, &head, &gs.target);
}
if inner.len() == 0 { match gs.path.as_mut().unwrap().pop() {
None => {
gs.path = None; gs.path = None;
}
return Json(battlesnake::MoveResponse{
move_field: battlesnake::Line{
start: &head,
end: &next,
}.direction().to_string(),
})
}
let target = find_target(&msg);
let path = find_path(&msg, &head, &target);
let gs = GameState{
target: *target,
path: path,
};
println!("{:?}", gs);
Json(battlesnake::MoveResponse { Json(battlesnake::MoveResponse {
move_field: "up".to_string(), move_field: "up".to_string(),
}) })
}
Some(next) => Json(battlesnake::MoveResponse {
move_field: battlesnake::Line {
start: &head,
end: &next,
}
.direction()
.to_string(),
}),
}
} }
fn find_target<'a>(gs: &'a battlesnake::SnakeRequest) -> &'a battlesnake::Coord { fn find_target<'a>(gs: &'a battlesnake::SnakeRequest) -> &'a battlesnake::Coord {