ugh
This commit is contained in:
parent
68bf72c071
commit
67f127df21
64
src/main.rs
64
src/main.rs
|
@ -12,7 +12,9 @@ use std::sync::{Arc, Mutex};
|
||||||
|
|
||||||
mod battlesnake;
|
mod battlesnake;
|
||||||
|
|
||||||
type Cache = Arc<Mutex<HashMap<String, GameState>>>;
|
type Cache = Mutex<HashMap<String, GameState>>;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
pub struct GameState {
|
pub struct GameState {
|
||||||
path: Option<Vec<battlesnake::Coord>>,
|
path: Option<Vec<battlesnake::Coord>>,
|
||||||
target: battlesnake::Coord,
|
target: battlesnake::Coord,
|
||||||
|
@ -38,7 +40,9 @@ fn begin(cache: State<Cache>, msg: Json<battlesnake::SnakeRequest>) -> Json<batt
|
||||||
path: path,
|
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{
|
Json(battlesnake::StartResponse{
|
||||||
color: "#5ce8c3".to_string(),
|
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(
|
let path = pathfinding::directed::astar::astar(
|
||||||
head,
|
head,
|
||||||
|n| msg.board.safe_neighbors(n).into_iter(),
|
|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>")]
|
#[post("/move", format = "json", data = "<msg>")]
|
||||||
fn make_move(msg: Json<battlesnake::SnakeRequest>) -> Json<battlesnake::MoveResponse> {
|
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 target = find_target(&msg);
|
||||||
println!("target: {:?}", target);
|
let path = find_path(&msg, &head, &target);
|
||||||
|
gs = &GameState{
|
||||||
|
target: *target,
|
||||||
|
path: path,
|
||||||
|
};
|
||||||
|
|
||||||
let head = &msg.you.body[0];
|
path.as_ref()
|
||||||
println!("me: {:?}", head);
|
});
|
||||||
for ne in &msg.board.safe_neighbors(&head) {
|
let mut inner = gs_path.expect("what");
|
||||||
println!("{:?} {}", ne.0, battlesnake::Line{
|
let next = inner[0];
|
||||||
start: &head,
|
inner.pop();
|
||||||
end: &ne.0,
|
|
||||||
}.direction());
|
if inner.len() == 0 {
|
||||||
|
gs.path = None;
|
||||||
}
|
}
|
||||||
|
|
||||||
println!("{:?}", pathfinding::directed::astar::astar(
|
return Json(battlesnake::MoveResponse{
|
||||||
head,
|
move_field: battlesnake::Line{
|
||||||
|n| msg.board.safe_neighbors(n).into_iter(),
|
start: &head,
|
||||||
|n| (battlesnake::manhattan(n, &target) as usize),
|
end: &next,
|
||||||
|n| n == target,
|
}.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(),
|
||||||
|
@ -112,13 +137,12 @@ fn find_target<'a>(gs: &'a battlesnake::SnakeRequest) -> &'a battlesnake::Coord
|
||||||
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);
|
||||||
let arc_mutex_map = Arc::from(mutex_map);
|
|
||||||
rocket::ignite().mount("/", routes![
|
rocket::ignite().mount("/", routes![
|
||||||
index,
|
index,
|
||||||
begin,
|
begin,
|
||||||
ping,
|
ping,
|
||||||
make_move,
|
make_move,
|
||||||
])
|
])
|
||||||
.manage(arc_mutex_map)
|
.manage(mutex_map)
|
||||||
.launch();
|
.launch();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue