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>")]
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];
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 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(),
})
let mut cache = cache_state.lock().expect("wanted cache to be unlockable");
let gs = cache.get_mut(&msg.game.id).unwrap();
if gs.path.is_none() {
gs.target = *find_target(&msg);
gs.path = find_path(&msg, &head, &gs.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(),
})
match gs.path.as_mut().unwrap().pop() {
None => {
gs.path = None;
Json(battlesnake::MoveResponse {
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 {