-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
305 lines (270 loc) · 12.9 KB
/
main.cpp
File metadata and controls
305 lines (270 loc) · 12.9 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#include "classes/MapPoint.h"
#include "classes/Map.h"
#include <iostream>
#include <unistd.h>
#include <restinio/all.hpp>
#include <map>
#include <string>
using nlohmann::json;
using router_t = restinio::router::express_router_t<>;
void test_bad_order1(Map<RoadObject> & map){
// test unreal delivery time
try {
std::map<string, int> orderProducts = {
pair<string, int>("Экран", 2),
pair<string, int>("Акустическая система", 2),
pair<string, int>("Световое оформление", 2),
pair<string, int>("Дым-машина", 2),
};
map.processOrder(13, 17, 10, orderProducts);
} catch (CantDeliveryException& ex) {
cout << ex.PrintMessage();
throw ex;
}
}
void test_bad_order2(Map<RoadObject> & map){
// test bad product
try {
std::map<string, int> orderProducts = {pair<string, int>("Экран", 2),
pair<string, int>("Акустическая система", 2),
pair<string, int>("Световое оформление", 2),
pair<string, int>("Дым-машина", 2),
pair<string, int>("Неведомое совершенно", 2),
};
map.processOrder(13, 17, 10, orderProducts);
} catch (CantDeliveryException& ex) {
cout << ex.PrintMessage();
throw ex;
}
}
int test_road(Map<RoadObject> & map){
// map.insert(10, 15);
map.insert(11, 17);
map.insert(12, 16);
map.insert(12, 17);
map.insert(13, 17);
};
std::map<string, int> getDefaultProducts(){
std::map<string, int> defaultProducts = {
pair<string, int>("Экран", 2),
pair<string, int>("Сцена", 2),
pair<string, int>("Акустическая система", 2),
pair<string, int>("DJ оборудование", 2),
pair<string, int>("Световое оформление", 2),
pair<string, int>("Баннер", 2),
pair<string, int>("Подиум", 2),
pair<string, int>("Дым-машина", 2),
pair<string, int>("Конфетти-машина", 2),
pair<string, int>("Радио-микрофон", 2),
pair<string, int>("Плазма", 2),
pair<string, int>("Ферма сценическа", 2),
pair<string, int>("Ферма Основание для ферм", 2),
};
return defaultProducts;
}
int test_initials(Map<RoadObject> & map) {
//test insert
std::map<string, int> a = {
pair<string, int>("Экран", 3),
pair<string, int>("Сцена", 1),
pair<string, int>("DJ оборудование", 2),
pair<string, int>("Подиум", 2),
pair<string, int>("Конфетти-машина", 1),
pair<string, int>("Радио-микрофон", 3),
};
map.addStore(10, 15, "TestStore", a);
a = {
pair<string, int>("Световое оформление", 4),
pair<string, int>("Подиум", 1),
pair<string, int>("Дым-машина", 5),
pair<string, int>("Конфетти-машина", 4),
pair<string, int>("Ферма сценическа", 2),
pair<string, int>("Ферма Основание для ферм", 2),
};
map.addStore(19, 50, "TestStore2", a);
a = {
pair<string, int>("DJ оборудование", 1),
pair<string, int>("Дым-машина", 1),
pair<string, int>("Конфетти-машина", 4),
pair<string, int>("Радио-микрофон", 2),
pair<string, int>("Плазма", 2),
};
map.addStore(2, 47, "TestStore3", a);
a = {
pair<string, int>("DJ оборудование", 1),
pair<string, int>("Баннер", 5),
pair<string, int>("Конфетти-машина", 5),
};
map.addStore(4, 25, "TestStore4", a);
};
void test_order(Map<RoadObject> & map){
// std::map<string, int> orderProducts = {
// pair<string, int>("Экран", 1),
// pair<string, int>("Сцена", 1),
// pair<string, int>("Баннер", 1),
// pair<string, int>("DJ оборудование", 1),
// pair<string, int>("Плазма", 1)
// };
std::map<string, int> orderProducts = {
pair<string, int>("DJ оборудование", 4),
pair<string, int>("Дым-машина", 3),
};
try {
map.processOrder(13, 17, 64, orderProducts);
} catch (CantDeliveryException& ex) {
cout << ex.PrintMessage();
}
}
using my_router_t = restinio::router::express_router_t<>;
auto make_request_handler(Map<RoadObject> * map){
auto router = std::make_unique<my_router_t>();
router->http_get(R"(/tick)",
[map](auto req, auto params) mutable {
string body = map->jsonState;
return req->create_response()
.set_body(std::move( body ))
.append_header( restinio::http_field::content_type, "application/json; charset=utf-8")
.append_header("Access-Control-Allow-Origin", "*")
.done();
});
router->http_post(R"(/order)",
[map](auto req, auto params) mutable {
std::string s = req->body();
json j2 = json::parse(s);
string body = "OK";
std::map<string, int> orderProducts;
for (json x : j2["products"]){
auto count = x["count"].get<int>();
if (count > 50) {
json err;
err["error"] = "too many items";
return req->create_response(400, "Bad Request")
.set_body(std::move( err.dump() ))
.done();
}
orderProducts[x["name"].get<string>()] = count;
}
try{
map->processOrder(j2["point"]["x"].get<int>(), j2["point"]["y"].get<int>(),
j2["deliveryTick"].get<int>(), orderProducts);
} catch (PastIntervalException& ex) {
json err;
err["error"] = "too late";
return req->create_response(400, "Bad Request")
.set_body(std::move( err.dump() ))
.done();
}
return req->create_response()
.set_body(std::move( body ))
.append_header( restinio::http_field::content_type, "application/json; charset=utf-8")
.append_header("Access-Control-Allow-Origin", "*")
.done();
});
router->http_post(R"(/stopStartWorld)",
[map](auto req, auto params) mutable {
map->runLoop = !map->runLoop;
string body = (map->runLoop)? "started": "stopped";
return req->create_response()
.set_body(std::move( body ))
.append_header( restinio::http_field::content_type, "application/json; charset=utf-8")
.append_header("Access-Control-Allow-Origin", "*")
.done();
});
router->http_post(R"(/store)",
[map](auto req, auto params) mutable {
std::string s = req->body();
const auto token = req->header().get_field(
"auth" );
if (token !="q1wefsa^%)"){
return req->create_response(403, "No Accept")
.done();
}
cout << s <<endl;
json j2 = json::parse(s);
string body = "OK";
std::map<string, int> storeProducts;
for (json x : j2["products"]){
storeProducts[x["name"].get<string>()] = x.at("count").get<int>();
}
try{
map->addStore(j2["point"]["x"].get<int>(),
j2["point"]["y"].get<int>(), j2["name"].get<string>(), storeProducts);;
} catch (...) {
}
return req->create_response()
.set_body(std::move( body ))
.append_header( restinio::http_field::content_type, "application/json; charset=utf-8")
.append_header("Access-Control-Allow-Origin", "*")
.append_header("Access-Control-Allow-Headers", "*")
.done();
});
router->http_post(R"(/login)",
[map](auto req, auto params) mutable {
std::string s = req->body();
json j2 = json::parse(s);
string body = "";
if (j2["login"].get<string>() == "admin" & j2["password"].get<string>() == "admin"){
json resp;
resp["token"] = "q1wefsa^%)";
body = resp.dump();
}
return req->create_response()
.set_body(std::move( body ))
.append_header( restinio::http_field::content_type, "application/json; charset=utf-8")
.append_header("Access-Control-Allow-Origin", "*")
.append_header("Access-Control-Allow-Headers", "*")
.done();
});
router->http_get(R"(/)",
[map](auto req, auto params) mutable {
auto sf = restinio::sendfile( "cli.html");
return req->create_response()
.set_body(std::move( sf ) )
// .append_header( restinio::http_field::content_type, "text/plain; charset=utf-8")
.append_header("Access-Control-Allow-Origin", "*")
.append_header("Access-Control-Allow-Headers", "*")
.done();
});
router->non_matched_request_handler(
[]( auto req ){
return
req->create_response( 200, "any")
.append_header( restinio::http_field::content_type, "application/json; charset=utf-8")
.append_header("Access-Control-Allow-Origin", "*")
.append_header("Access-Control-Allow-Headers", "*")
.done();
} );
return router;
}
int main() {
Map<RoadObject> * map = new Map<RoadObject>(20,50);
test_initials(*map); //stores
test_road(*map);
auto timer = [map]() mutable{
auto runTimer = [map]() mutable{
int i = map->getTick() ;
i++;
while (map->runLoop) {
map->nextTick(i++);
std::this_thread::sleep_for(std::chrono::seconds(1));
}
while (!map->runLoop){
std::this_thread::sleep_for(std::chrono::seconds(1));
}
};
while(map->loaded){
auto future = std::async(runTimer);
};
};
auto future = std::async(timer);
struct my_traits : public restinio::default_single_thread_traits_t {
using request_handler_t = my_router_t;
};
restinio::run(
restinio::on_this_thread<my_traits>()
.address("127.0.0.1")
.port(8080)
.request_handler(make_request_handler(map))
);
return 0;
}