Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ use serde_json::Result;
use rand::seq::SliceRandom;

const ANT_HIVE_URL: &str = "0.0.0.0:7070";

// available actions and directions
const ACTIONS: [&'static str; 5] = ["stay", "move", "eat", "take", "put"];
const DIRECTIONS: [&'static str; 4] = ["up", "down", "right", "left"];

// starting listen for http calls on port :7070
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| {
Expand All @@ -18,10 +21,10 @@ async fn main() -> std::io::Result<()> {
.await
}

// sim will make http post request to your bot
#[post("/")]
async fn post_async(req_body: String) -> impl Responder {

// Here desiarilze
let request: Request = match deserialize_request(&req_body) {
Err(_) => Request {id: String::from(""), tick: 0, ants: Vec::new()},
Ok(ok_ants) => ok_ants
Expand All @@ -30,18 +33,19 @@ async fn post_async(req_body: String) -> impl Responder {
let order_from_ants: Vec<Order> = request.ants.iter()
.map(|ant| Order {
ant_id: ant.id,
act: String::from(*ACTIONS.choose(&mut rand::thread_rng()).unwrap()),
dir: String::from(*DIRECTIONS.choose(&mut rand::thread_rng()).unwrap())})
act: String::from(*ACTIONS.choose(&mut rand::thread_rng()).unwrap()), // pick random action from array on line 9
dir: String::from(*DIRECTIONS.choose(&mut rand::thread_rng()).unwrap())}) // pick random direction from array on line 10
.collect();

// prepare response json object
let responce = Responce {orders: order_from_ants};

// Here serialize
// reading request body with information about map and ants
let response_body = serde_json::to_string(&responce).unwrap();
HttpResponse::Ok().body(response_body)
}


// parce json from request body
fn deserialize_request(request: &String ) -> Result<Request> {
let result: Result<Request> = serde_json::from_str(&*request.to_string());
result
Expand Down Expand Up @@ -81,4 +85,11 @@ struct Request {
#[derive(Serialize, Deserialize)]
struct Responce {
pub orders: Vec<Order>
}
}

// this code available at https://github.com/anthive/rust
// to test it localy, submit post request with payload.json using postman or curl
// curl -X 'POST' -d @payload.json http://localhost:7070

// have fun!