trying to cache data

This commit is contained in:
Cadey Ratio 2019-12-18 02:39:20 +00:00
parent 4ece6f80a6
commit 68bf72c071
2 changed files with 49 additions and 8 deletions

View File

@ -85,7 +85,7 @@ impl Board {
} }
if self.is_safe(&down) { if self.is_safe(&down) {
result.push((up, 1)); result.push((down, 1));
} }
result result

View File

@ -4,11 +4,20 @@
#[macro_use] extern crate rocket_contrib; #[macro_use] extern crate rocket_contrib;
#[macro_use] extern crate serde_derive; #[macro_use] extern crate serde_derive;
use rocket::State;
use rocket_contrib::json::Json; use rocket_contrib::json::Json;
use pathfinding::grid::Grid; use pathfinding::grid::Grid;
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
mod battlesnake; mod battlesnake;
type Cache = Arc<Mutex<HashMap<String, GameState>>>;
pub struct GameState {
path: Option<Vec<battlesnake::Coord>>,
target: battlesnake::Coord,
}
#[get("/")] #[get("/")]
fn index() -> &'static str { fn index() -> &'static str {
"Hello, world!" "Hello, world!"
@ -20,7 +29,17 @@ fn ping() -> &'static str {
} }
#[post("/begin", format = "json", data = "<msg>")] #[post("/begin", format = "json", data = "<msg>")]
fn begin(msg: Json<battlesnake::SnakeRequest>) -> Json<battlesnake::StartResponse> { fn begin(cache: State<Cache>, msg: Json<battlesnake::SnakeRequest>) -> Json<battlesnake::StartResponse> {
let head = msg.you.body[0];
let target = find_target(&msg);
let path = find_path(&msg, &head, &target);
let gs = GameState{
target: *target,
path: path,
};
cache.inner().get_mut().unwrap().insert(msg.game.id, gs);
Json(battlesnake::StartResponse{ Json(battlesnake::StartResponse{
color: "#5ce8c3".to_string(), color: "#5ce8c3".to_string(),
head_type: "beluga".to_string(), head_type: "beluga".to_string(),
@ -28,12 +47,29 @@ fn begin(msg: Json<battlesnake::SnakeRequest>) -> Json<battlesnake::StartRespons
}) })
} }
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(),
|n| (battlesnake::manhattan(n, &target) as usize),
|n| n == target,
);
match path {
None => return None,
Some(x) => {
return Some(x.0);
},
}
}
#[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(msg: Json<battlesnake::SnakeRequest>) -> Json<battlesnake::MoveResponse> {
let target = find_target(&msg); let target = find_target(&msg);
println!("target: {:?}", target); println!("target: {:?}", target);
let head = &msg.you.body[0]; let head = &msg.you.body[0];
println!("me: {:?}", head);
for ne in &msg.board.safe_neighbors(&head) { for ne in &msg.board.safe_neighbors(&head) {
println!("{:?} {}", ne.0, battlesnake::Line{ println!("{:?} {}", ne.0, battlesnake::Line{
start: &head, start: &head,
@ -41,12 +77,12 @@ fn make_move(msg: Json<battlesnake::SnakeRequest>) -> Json<battlesnake::MoveResp
}.direction()); }.direction());
} }
pathfinding::directed::astar::astar( println!("{:?}", pathfinding::directed::astar::astar(
&head, head,
|n| msg.board.safe_neighbors(n).iter(), |n| msg.board.safe_neighbors(n).into_iter(),
|n| (battlesnake::manhattan(n, &target) as usize), |n| (battlesnake::manhattan(n, &target) as usize),
|n| n == &target, |n| n == target,
); ));
Json(battlesnake::MoveResponse { Json(battlesnake::MoveResponse {
move_field: "up".to_string(), move_field: "up".to_string(),
@ -74,10 +110,15 @@ fn find_target<'a>(gs: &'a battlesnake::SnakeRequest) -> &'a battlesnake::Coord
} }
fn main() { 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![ rocket::ignite().mount("/", routes![
index, index,
begin, begin,
ping, ping,
make_move, make_move,
]).launch(); ])
.manage(arc_mutex_map)
.launch();
} }