#![feature(proc_macro_hygiene, decl_macro)] #[macro_use] extern crate rocket; #[macro_use] extern crate rocket_contrib; #[macro_use] extern crate serde_derive; use rocket_contrib::json::Json; use pathfinding::grid::Grid; mod battlesnake; #[get("/")] fn index() -> &'static str { "Hello, world!" } #[get("/ping")] fn ping() -> &'static str { "OK - Pneuma online" } #[post("/begin", format = "json", data = "")] fn begin(msg: Json) -> Json { Json(battlesnake::StartResponse{ color: "#5ce8c3".to_string(), head_type: "beluga".to_string(), tail_type: "skinny".to_string(), }) } #[post("/move", format = "json", data = "")] fn make_move(msg: Json) -> Json { let mut g = Grid::new(msg.board.height, msg.board.width); g.fill(); let target = find_target(&msg, &g); println!("{:?}", target); // do pathfinding? Json(battlesnake::MoveResponse { move_field: "up".to_string(), }) } fn find_target<'a>(gs: &'a battlesnake::SnakeRequest, g: &Grid) -> &'a battlesnake::Coord { let head = &gs.you.body[0]; if gs.you.health > 30 { let mut lowestScore: u32 = 99999; let mut coord: &battlesnake::Coord = &gs.you.body.last().unwrap(); for food in &gs.board.food { let score = battlesnake::Line{start: head, end: food}.manhattan(); if score < lowestScore { lowestScore = score; coord = food; } } return coord; } return gs.you.body.last().unwrap(); } fn main() { rocket::ignite().mount("/", routes![ index, begin, ping, make_move, ]).launch(); }