pneuma/src/main.rs

78 lines
1.8 KiB
Rust

#![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 = "<msg>")]
fn begin(msg: Json<battlesnake::SnakeRequest>) -> Json<battlesnake::StartResponse> {
Json(battlesnake::StartResponse{
color: "#5ce8c3".to_string(),
head_type: "beluga".to_string(),
tail_type: "skinny".to_string(),
})
}
#[post("/move", format = "json", data = "<msg>")]
fn make_move(msg: Json<battlesnake::SnakeRequest>) -> Json<battlesnake::MoveResponse> {
let mut g = Grid::new(msg.board.height, msg.board.width);
g.fill();
for sn in &msg.board.snakes {
for bd in &sn.body {
g.remove_vertex((bd.x, bd.y))
}
}
let target = find_target(&msg, &g);
// do pathfinding?
battlesnake::MoveResponse {
move_field: "up".to_string(),
}
}
fn find_target(gs: &battlesnake::SnakeRequest, g: &Grid) -> &battlesnake::Coord {
let head = gs.you.body[0];
if gs.you.health > 30 {
let mut lowestScore: usize = 99999;
let mut coord: &battlesnake::Coord;
for food in &gs.board.food {
let score = g.distance(&(head.x, head.y), &(food.x, food.y));
if score < lowestScore {
lowestScore = score;
coord = food;
}
}
return coord;
}
return gs.you.body.last().unwrap();
}
fn main() {
rocket::ignite().mount("/", routes![
index,
begin,
ping,
]).launch();
}