diff --git a/main.rs b/main.rs index 80548c3..ca86a53 100644 --- a/main.rs +++ b/main.rs @@ -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(|| { @@ -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 @@ -30,18 +33,19 @@ async fn post_async(req_body: String) -> impl Responder { let order_from_ants: Vec = 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 { let result: Result = serde_json::from_str(&*request.to_string()); result @@ -81,4 +85,11 @@ struct Request { #[derive(Serialize, Deserialize)] struct Responce { pub orders: Vec -} \ No newline at end of file +} + +// 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! +