-
Notifications
You must be signed in to change notification settings - Fork 1
/
bot.cpp
68 lines (55 loc) · 1.81 KB
/
bot.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
#include <string>
#include <vector>
#include "json.hpp"
#include "httplib.h"
#include <random>
using namespace std;
using namespace httplib;
using namespace nlohmann;
// available actions and directions
const vector<string> ACTIONS = { "stay", "move", "eat", "take", "put" };
const vector<string> DIRECTIONS = { "up", "down", "right", "left" };
int main(int argc, char* argv[])
{
// initialize random number generator
random_device rd;
mt19937 mt(rd());
// pick random direction from array on line 14
uniform_int_distribution<size_t> dist2(0, DIRECTIONS.size()-1);
// initialize the http server
Server svr;
// sim will make http post request to your bot
svr.Post("/", [&](const Request& req, Response& res) {
// your bot respons should be json object
auto hive = json::parse(req.body);
// loop through ants and give orders
auto orders = std::vector<json>();
for (auto& ant : hive["ants"].items()) {
json order = {
{"antId", ant.value()["id"] },
{"act", "move" },
{"dir", DIRECTIONS[dist2(mt)]}
};
// add order to your response object from line 32
orders.push_back(order);
}
json response;
response["orders"]=orders;
cout << response.dump() << '\n';
// finish your response and send back json to
res.set_content(response.dump(), "application/json");
// json format sample:
// {"orders": [
// {"antId":1,"act":"move","dir":"down"},
// {"antId":17,"act":"load","dir":"up"}
// ]}
});
// starting listen for http calls on port :7070
svr.listen("0.0.0.0", 7070);
return 0;
}
// this code available at https://github.com/anthive/cpp
// 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!