Skip to content

Commit b05c7c1

Browse files
committed
fix
1 parent 1951a4b commit b05c7c1

File tree

7 files changed

+54
-334
lines changed

7 files changed

+54
-334
lines changed

sprint4/problems/leave_game/solution/errors.ini

Lines changed: 0 additions & 325 deletions
This file was deleted.

sprint4/problems/leave_game/solution/src/http_server.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ namespace http_server {
135135
// чтобы продлить время жизни сессии до вызова лямбды.
136136
// Используется generic-лямбда функция, способная принять response произвольного типа
137137
request_handler_(std::move(request), [self = this->shared_from_this()](auto&& response) {
138-
self->Write(std::move(response));
138+
self->Write(std::move(response));
139139
});
140140
}
141141

sprint4/problems/leave_game/solution/src/main.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,8 @@ int main(int argc, const char* argv[]) {
108108
const auto address = net::ip::make_address("0.0.0.0");
109109
constexpr net::ip::port_type port = 8080;
110110
http_server::ServeHttp(ioc, {address, port}, [&logging_handler](auto&& req, auto&& send) {
111-
logging_handler(std::forward<decltype(req)>(req), std::forward<decltype(send)>(send));
111+
logging_handler(std::forward<decltype(req)>(req),
112+
std::forward<decltype(send)>(send));
112113
});
113114

114115
// Cообщает тестам о том, что сервер запущен и готов обрабатывать запросы
@@ -117,9 +118,9 @@ int main(int argc, const char* argv[]) {
117118
auto ms = std::chrono::milliseconds(static_cast<int>(arg.period));
118119
auto ticker =
119120
std::make_shared<game_time::Ticker>(strand, ms,
120-
[&app](std::chrono::milliseconds delta) {
121-
app.Tick(delta);
122-
}
121+
[&app](std::chrono::milliseconds delta) {
122+
app.Tick(delta);
123+
}
123124
);
124125
ticker->Start();
125126

sprint4/problems/leave_game/solution/src/model.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ using namespace std::literals;
388388
geom::Point2D end(start.x + dog->GetSpeed().x * delta_time,
389389
start.y + dog->GetSpeed().y * delta_time);
390390

391-
gatherers.push_back({start, end, 0.6}); // Ширина собаки = 0.6
391+
gatherers.push_back({start, end, 0.3}); // Ширина собаки = 0.6
392392
}
393393
return gatherers;
394394
}

sprint4/problems/leave_game/solution/src/request_handler.h

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,31 @@ namespace http_handler {
301301
};
302302

303303
BOOST_LOG_TRIVIAL(info) << logging::add_value(additional_data, jv) << "response sent";
304+
305+
static int64_t total_us = 0;
306+
static int count = 0;
307+
static auto last_print = std::chrono::steady_clock::now();
308+
309+
total_us += resp_duration;
310+
++count;
311+
312+
auto now = std::chrono::steady_clock::now();
313+
if (now - last_print >= std::chrono::seconds(10)) {
314+
if (count > 0) {
315+
double avg = static_cast<double>(total_us) / count;
316+
317+
std::ofstream out("response_avg.log", std::ios::app); // ⬅️ append mode
318+
if (out.is_open()) {
319+
auto time_now = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
320+
out << std::put_time(std::localtime(&time_now), "[%F %T] ")
321+
<< "avg /api/v1/game/state: " << avg << " us over " << count << " requests\n";
322+
}
323+
324+
total_us = 0;
325+
count = 0;
326+
last_print = now;
327+
}
328+
}
304329
}
305330

306331
template <class SomeRequestHandler>
@@ -334,7 +359,7 @@ namespace http_handler {
334359
try {
335360
// Этот assert не выстрелит, так как лямбда-функция будет выполняться
336361
// внутри strand
337-
assert(self->api_strand_.running_in_this_thread());
362+
// assert(self->api_strand_.running_in_this_thread());
338363

339364
ResponseVariant result = self->api_handler_.RouteRequest(req);
340365
std::visit([&send](auto&& res){
@@ -352,7 +377,15 @@ namespace http_handler {
352377
}
353378
};
354379

355-
return net::dispatch(api_strand_, handle);
380+
if (req.method() == http::verb::get || req.method() == http::verb::head) {
381+
// обработка без strand — параллельно
382+
return handle();
383+
} else {
384+
// изменение состояния — через strand
385+
return net::dispatch(api_strand_, handle);
386+
}
387+
388+
// return net::dispatch(api_strand_, handle);
356389
}
357390

358391
std::visit([&send](auto&& result){
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
22 serialization::archive 19 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 4 map1 5 Map 1 3 0 0 4 0 0 0 0 0 0 0 40 0 40 0 40 30 40 30 0 30 0 0 0 30 0 0 1 0 0 0 0 0 5 5 0 0 30 20 0 0 1 0 0 0 0 0 2 o0 40 30 0 0 5 0 4.00000000000000000e+00 0 0 2 13 0 0 0 0 10 1 30 0 0 0 1 0 0 1 0 0 0 5 1 0 0 2.43666205854252804e-01 2.08980774111891687e+00 6 0 0 0 1 13 0 0 0 0 0 0 0 3 13 0 0 0 4 map3 0 0 2 0 107 {"name":"key","file":"assets/key.obj","type":"obj","rotation":90,"color":"#338844","scale":3E-2,"value":10} 112 {"name":"wallet","file":"assets/wallet.obj","type":"obj","rotation":0,"color":"#883344","scale":1E-2,"value":30} 4 town 2 0 107 {"name":"key","file":"assets/key.obj","type":"obj","rotation":90,"color":"#338844","scale":3E-2,"value":10} 107 {"name":"key","file":"assets/key.obj","type":"obj","rotation":90,"color":"#338844","scale":3E-2,"value":10} 4 map1 2 0 107 {"name":"key","file":"assets/key.obj","type":"obj","rotation":90,"color":"#338844","scale":3E-2,"value":10} 112 {"name":"wallet","file":"assets/wallet.obj","type":"obj","rotation":0,"color":"#883344","scale":1E-2,"value":30} 0 0 1 13 0 0 0 4 map1 0 0 0 3 0 4 map1 5 Map 1 3 4 0 0 0 40 0 40 0 40 30 40 30 0 30 0 0 0 30 1 0 5 5 30 20 1 0 2 o0 40 30 5 0 4.00000000000000000e+00 4 town 4 Town 3 22 0 0 0 40 0 40 0 40 30 40 30 0 30 0 15 40 15 20 0 20 30 0 22 17 22 17 18 17 27 10 22 10 30 0 10 10 10 10 10 10 5 10 5 20 5 20 30 20 40 20 40 10 40 20 40 30 40 30 0 30 10 20 25 25 25 30 25 30 30 20 20 25 20 30 15 30 20 35 20 40 20 40 25 35 25 30 30 30 35 20 0 2 2 6 6 12 7 6 6 22 2 6 11 32 2 6 11 22 16 4 3 33 16 4 3 34 21 4 3 22 21 4 3 22 26 5 3 34 26 5 3 28 21 4 3 2 16 5 5 9 16 6 3 12 24 3 4 2 24 6 4 12 1 7 2 11 34 4 4 22 31 6 2 22 35 6 4 17 41 7 4 1 0 2 o0 40 30 5 0 3.00000000000000000e+00 4 map3 5 Map 3 3 9 0 2 2 2 16 12 7 12 0 20 0 20 14 14 16 14 14 8 7 8 14 2 7 20 7 12 0 20 0 20 14 8 14 2 16 14 16 5 0 4 2 6 3 14 2 4 3 4 9 2 5 9 9 4 3 14 9 4 3 1 0 2 o1 8 14 1 -1 3.00000000000000000e+00 3 13 0 4 map1 0 4 town 1 4 map3 2 3.00000000000000000e+00 1.00000000000000000e+01 0 0 0 0 0 0 0 0
1+
22 serialization::archive 19 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 4 map3 5 Map 3 3 0 0 9 0 0 0 0 0 2 2 2 16 12 7 12 0 20 0 20 14 14 16 14 14 8 7 8 14 2 7 20 7 12 0 20 0 20 14 8 14 2 16 14 16 0 0 5 0 0 0 0 0 4 2 0 0 6 3 14 2 4 3 4 9 2 5 9 9 4 3 14 9 4 3 0 0 1 0 0 0 0 0 2 o1 8 14 0 0 1 -1 3.00000000000000000e+00 0 0 2 13 0 0 0 1 30 0 10 0 0 0 1 0 0 1 0 0 0 2 1 0 0 2.17542094974606659e+00 6.24604289878066155e+00 10 0 4 town 4 Town 3 22 0 0 0 40 0 40 0 40 30 40 30 0 30 0 15 40 15 20 0 20 30 0 22 17 22 17 18 17 27 10 22 10 30 0 10 10 10 10 10 10 5 10 5 20 5 20 30 20 40 20 40 10 40 20 40 30 40 30 0 30 10 20 25 25 25 30 25 30 30 20 20 25 20 30 15 30 20 35 20 40 20 40 25 35 25 30 30 30 35 20 0 2 2 6 6 12 7 6 6 22 2 6 11 32 2 6 11 22 16 4 3 33 16 4 3 34 21 4 3 22 21 4 3 22 26 5 3 34 26 5 3 28 21 4 3 2 16 5 5 9 16 6 3 12 24 3 4 2 24 6 4 12 1 7 2 11 34 4 4 22 31 6 2 22 35 6 4 17 41 7 4 1 0 2 o0 40 30 5 0 3.00000000000000000e+00 2 13 0 1 10 0 10 0 1 2 0 7 0 2.98830904302591591e+01 1.97334402558639397e+01 9 1 1.98563934613122512e+01 4.55034768354743857e+00 10 1 0 0 2 2 0 0 0 1 1 0 0 0 0 3 13 0 0 0 4 map3 0 0 2 0 107 {"name":"key","file":"assets/key.obj","type":"obj","rotation":90,"color":"#338844","scale":3E-2,"value":10} 112 {"name":"wallet","file":"assets/wallet.obj","type":"obj","rotation":0,"color":"#883344","scale":1E-2,"value":30} 4 town 2 0 107 {"name":"key","file":"assets/key.obj","type":"obj","rotation":90,"color":"#338844","scale":3E-2,"value":10} 107 {"name":"key","file":"assets/key.obj","type":"obj","rotation":90,"color":"#338844","scale":3E-2,"value":10} 4 map1 2 0 107 {"name":"key","file":"assets/key.obj","type":"obj","rotation":90,"color":"#338844","scale":3E-2,"value":10} 112 {"name":"wallet","file":"assets/wallet.obj","type":"obj","rotation":0,"color":"#883344","scale":1E-2,"value":30} 0 0 2 2 0 0 0 4 town 1 4 map3 0 0 0 3 0 4 map1 5 Map 1 3 4 0 0 0 40 0 40 0 40 30 40 30 0 30 0 0 0 30 1 0 5 5 30 20 1 0 2 o0 40 30 5 0 4.00000000000000000e+00 4 town 4 Town 3 22 0 0 0 40 0 40 0 40 30 40 30 0 30 0 15 40 15 20 0 20 30 0 22 17 22 17 18 17 27 10 22 10 30 0 10 10 10 10 10 10 5 10 5 20 5 20 30 20 40 20 40 10 40 20 40 30 40 30 0 30 10 20 25 25 25 30 25 30 30 20 20 25 20 30 15 30 20 35 20 40 20 40 25 35 25 30 30 30 35 20 0 2 2 6 6 12 7 6 6 22 2 6 11 32 2 6 11 22 16 4 3 33 16 4 3 34 21 4 3 22 21 4 3 22 26 5 3 34 26 5 3 28 21 4 3 2 16 5 5 9 16 6 3 12 24 3 4 2 24 6 4 12 1 7 2 11 34 4 4 22 31 6 2 22 35 6 4 17 41 7 4 1 0 2 o0 40 30 5 0 3.00000000000000000e+00 4 map3 5 Map 3 3 9 0 2 2 2 16 12 7 12 0 20 0 20 14 14 16 14 14 8 7 8 14 2 7 20 7 12 0 20 0 20 14 8 14 2 16 14 16 5 0 4 2 6 3 14 2 4 3 4 9 2 5 9 9 4 3 14 9 4 3 1 0 2 o1 8 14 1 -1 3.00000000000000000e+00 3 13 0 4 map1 0 4 town 1 4 map3 2 3.00000000000000000e+00 1.00000000000000000e+01 0 0 0 0 0 0 0 0

sprint4/problems/leave_game/solution/static/js/game.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,15 @@ class GameState {
210210
}
211211
}
212212

213+
startKeepAlive() {
214+
setInterval(() => {
215+
// Если не нажимается ничего и ничего не отправлялось явно
216+
if (!this.keyState.getLastKey()) {
217+
this._pressKey("", function(){});
218+
}
219+
}, 50); // каждые 50 мс
220+
}
221+
213222
_pressKey(keys, then) {
214223
const self = this;
215224
$.post({
@@ -233,6 +242,8 @@ class GameState {
233242
return;
234243
}
235244

245+
this.startKeepAlive();
246+
236247
this.started = true;
237248
this._applyDesiredState();
238249
this._instantApplyState();

0 commit comments

Comments
 (0)