-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmain.cpp
39 lines (39 loc) · 1.53 KB
/
main.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
//HTTP credits: http://lcs.ios.ac.cn/~maxt/SPelton/reports/report-9aa0d3.html
//JSON credits: https://github.com/nlohmann/json
#include <iostream>
#include "./json.hpp"
#include "./http_stuff.h"
using namespace std;
using namespace nlohmann;
int main(void) {
httplib::Server svr;
svr.Get("/", [](const auto &, auto &res) {
string head = "default"; //TODO: Change head
string tail = "default"; //TODO: Change tail
string author = ""; //TODO: Change your battlesnake username
string color = "#888888"; //TODO: Change a hex color
res.set_content("{\"apiversion\":\"1\", \"head\":\"" + head + "\", \"tail\":\"" + tail + "\", \"color\":\"" + color + "\", " + "\"author\":\"" + author + "\"}", "application/json");
});
svr.Post("/end", [](const auto &, auto &res){
res.set_content("ok", "text/plain");
});
svr.Post("/start", [](const auto &, auto &res){
res.set_content("ok", "text/plain");
});
svr.Post("/move", [](auto &req, auto &res){
const json data = json::parse(req.body);
cout << data;
cout << "\n\n";
//You can get the "you" property like this:
//data["you"];
//Almost alike python dictionary parsing, but with a semicolon at the end of each line.
//You might need to make some structs to store some data in a variable
//Example:
//you_struct you = data["you"];
string moves[4] = {"up", "down", "left", "right"};
int index = rand() % 4;
res.set_content("{\"move\": \"" + moves[index] + "\"}", "text/plain");
});
svr.listen("0.0.0.0", 8080);
cout << "Server started";
}