Skip to content

Commit 9e94867

Browse files
committed
fix
1 parent ccbd619 commit 9e94867

File tree

8 files changed

+56
-182
lines changed

8 files changed

+56
-182
lines changed

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

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -329,16 +329,52 @@ Application::Application(model::Game& game, db::ConnectionPool& pool)
329329
return is_not_moving || positions_equal;
330330
}
331331

332-
void Application::SavePlayerStatsToDB(std::shared_ptr<model::Dog> dog) {
332+
333+
std::string Application::GetGameRecords() const {
334+
335+
try {
336+
auto connection_wrap =
337+
connection_pool_.GetConnection();
338+
339+
pqxx::work work{*connection_wrap};
340+
341+
pqxx::result result = work.exec(R"(
342+
SELECT name, score, play_time_ms FROM retired_players
343+
ORDER BY score DESC, play_time_ms, name LIMIT 100;
344+
)");
345+
346+
work.commit();
347+
348+
boost::json::array records_json;
349+
for (const auto& row : result) {
350+
boost::json::object record;
351+
record["name"] = row["name"].c_str();
352+
record["score"] = row["score"].as<int>();
353+
record["playTime"] = row["play_time_ms"].as<int>();
354+
355+
records_json.push_back(record);
356+
}
357+
358+
std::string response_json =
359+
boost::json::serialize(records_json);
360+
361+
return response_json;
362+
363+
} catch (const std::exception& e) {
364+
throw std::runtime_error(e.what());
365+
}
366+
}
367+
368+
void Application::SavePlayerStatsToDB(std::shared_ptr<player::Player> player) {
333369
try {
334370
auto connection = connection_pool_.GetConnection();
335371

336372
pqxx::work work{*connection};
337373

338374
work.exec_prepared("insert_retire",
339-
dog->GetName(),
340-
dog->GetState().score,
341-
dog->CalcPlayTime()
375+
player->GetDogName(),
376+
player->GetDogScore(),
377+
player->CalcPlayTime()
342378
);
343379

344380
work.commit();
@@ -370,7 +406,7 @@ Application::Application(model::Game& game, db::ConnectionPool& pool)
370406
player->SetLastMoveTime(play_time);
371407

372408
if (play_time > dog_retirement_time_) {
373-
SavePlayerStatsToDB(player->GetDog());
409+
SavePlayerStatsToDB(player);
374410

375411
afk_players.push_back(player);
376412
}

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,7 @@ namespace app {
159159
const std::string GetSerializedPlayersList(const Token& token) const;
160160
const std::string GetSerializedGameState(const Token& token) const;
161161

162-
std::string GetGameRecords() const {
163-
return game_.GetGameRecords();
164-
}
162+
std::string GetGameRecords() const;
165163

166164
model::MapService& GetGameMapService() {
167165
return game_.GetMapService();
@@ -217,7 +215,7 @@ namespace app {
217215
std::vector<PlayerMovementInfo>& old,
218216
std::vector<PlayerMovementInfo>& current);
219217

220-
void SavePlayerStatsToDB(std::shared_ptr<model::Dog> dog);
218+
void SavePlayerStatsToDB(std::shared_ptr<player::Player> dog);
221219

222220
std::vector<PlayerMovementInfo> PlayersInfoSnapstot() const;
223221

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ int main(int argc, const char* argv[]) {
7373
// 1. Загружаем карту из файла и строим модель игры
7474
model::Game game = json_loader::LoadGame(arg.config);
7575
game.SetDefaultTickTime(static_cast<double>(tick_time.count()));
76-
game.SetDBConnPool(&conn_pool);
7776

7877
app::Application app(game, conn_pool);
7978

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

Lines changed: 1 addition & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,6 @@ using namespace std::literals;
249249

250250
ConfigureSessionData(result);
251251

252-
result->SetDBConnPool(this->GetDBConnPool());
253-
254252
return result;
255253
}
256254

@@ -504,80 +502,6 @@ using namespace std::literals;
504502
MovePlayer(dog_id, remaining_time);
505503
}
506504
}
507-
508-
bool Dog::IsMoving(model::Pos new_pos) const {
509-
bool is_moving = this->state_.speed.x != 0 ||
510-
this->state_.speed.y != 0;
511-
512-
return is_moving || new_pos != this->state_.position;
513-
}
514-
515-
bool Dog::IsAfk(double current_time, double dog_retirement_time, model::Pos new_pos) {
516-
if (IsMoving(new_pos)) {
517-
this->last_move_time_ = 0;
518-
519-
return false;
520-
}
521-
522-
last_move_time_ += current_time;
523-
524-
return last_move_time_ > dog_retirement_time;
525-
}
526-
527-
void GameSession::SavePlayerStatsToDB(std::shared_ptr<Dog> dog) {
528-
try {
529-
if (!conn_pool_) {
530-
throw std::runtime_error("nullptr conn_pool_");
531-
}
532-
auto connection = conn_pool_->GetConnection();
533-
534-
pqxx::work work{*connection};
535-
536-
work.exec_prepared("insert_retire",
537-
dog->GetName(),
538-
dog->GetState().score,
539-
dog->CalcPlayTime()
540-
);
541-
542-
work.commit();
543-
} catch (const std::exception& e) {
544-
throw std::runtime_error(e.what());
545-
}
546-
}
547-
548-
void GameSession::RetirePlayer(Dog::Id dog_id) {
549-
auto& dog = dogs_.at(dog_id);
550-
551-
SavePlayerStatsToDB(dog);
552-
553-
dogs_.erase(dog_id);
554-
555-
dogs_vector_.erase(
556-
std::remove_if(
557-
dogs_vector_.begin(), dogs_vector_.end(),
558-
[&dog_id](const auto& dog) {
559-
return dog->GetId() == dog_id; }
560-
),
561-
dogs_vector_.end()
562-
);
563-
564-
// BOOST_LOG_TRIVIAL(info) << "SavePlayerStatsToDB completed successfully.";
565-
}
566-
567-
// void GameSession::RemoveAFKPlayers(std::unordered_map<Dog::Id, Pos> new_positions,
568-
// double delta_time) {
569-
// std::vector<Dog::Id> afk_players;
570-
571-
// for (auto& [dog_id, dog] : dogs_) {
572-
// if (dog->IsAfk(delta_time, dog_retirement_time_, new_positions[dog_id])) {
573-
// afk_players.push_back(dog_id);
574-
// }
575-
// }
576-
577-
// for (const auto& dog_id : afk_players) {
578-
// RetirePlayer(dog_id);
579-
// }
580-
// }
581505

582506
void GameSession::Tick(double delta_time) {
583507
using namespace collision_detector;
@@ -586,10 +510,7 @@ using namespace std::literals;
586510
std::unordered_map<Dog::Id, Pos> new_positions =
587511
ComputeNewPositions(delta_time);
588512

589-
// 2. Удаляем афк игроков
590-
// RemoveAFKPlayers(new_positions, delta_time);
591-
592-
// 3. Определяем события сбора предметов
513+
// 2. Определяем события сбора предметов
593514
std::vector<GatheringEvent> events =
594515
DetectGatheringEvents(delta_time);
595516

@@ -760,50 +681,13 @@ using namespace std::literals;
760681
default_tick_time_ = delta_time;
761682
}
762683

763-
std::string Game::GetGameRecords() const {
764-
765-
try {
766-
auto connection_wrap =
767-
session_service_->GetDBConnPool()->GetConnection();
768-
769-
pqxx::work work{*connection_wrap};
770-
771-
pqxx::result result = work.exec(R"(
772-
SELECT name, score, play_time_ms FROM retired_players
773-
ORDER BY score DESC, play_time_ms, name LIMIT 100;
774-
)");
775-
776-
work.commit();
777-
778-
boost::json::array records_json;
779-
for (const auto& row : result) {
780-
boost::json::object record;
781-
record["name"] = row["name"].c_str();
782-
record["score"] = row["score"].as<int>();
783-
record["playTime"] = row["play_time_ms"].as<int>();
784-
785-
records_json.push_back(record);
786-
}
787-
788-
std::string response_json =
789-
boost::json::serialize(records_json);
790-
791-
return response_json;
792-
793-
} catch (const std::exception& e) {
794-
throw std::runtime_error(e.what());
795-
}
796-
}
797-
798684
void Game::LoadGameData(CommonData data,
799685
loot_gen::LootGeneratorConfig config) {
800686
common_data_ = std::make_shared<CommonData>(std::move(data));
801687

802688
map_service_ = MapService(common_data_);
803689

804-
auto pool = session_service_->GetDBConnPool();
805690
session_service_ = std::make_shared<SessionService>((common_data_));
806-
session_service_->SetDBConnPool(pool);
807691

808692
loot_service_ = std::make_shared<LootService>(common_data_);
809693
loot_service_->ConfigureLootGenerator(config.period, config.probability);

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

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -240,12 +240,6 @@ namespace model {
240240
const State& GetState() const noexcept;
241241
const double GetDefaultDogSpeed() const noexcept;
242242
const FoundObjects& GetBag() const noexcept;
243-
244-
int CalcPlayTime() const {
245-
std::chrono::time_point now = std::chrono::steady_clock::now();
246-
return std::chrono::duration_cast<std::chrono::duration<int>>(
247-
now - play_time_).count();
248-
}
249243

250244
void AddToBag(uint64_t loot_id, uint64_t loot_type);
251245
void AddScore(int score);
@@ -260,21 +254,10 @@ namespace model {
260254
void StopDog();
261255

262256
Pos MoveDog(Pos new_position);
263-
264-
bool IsAfk(double now,
265-
double dog_retirement_time,
266-
model::Pos new_pos);
267257

268258
private:
269259
void SetBagCapacity(size_t capacity);
270260

271-
bool IsMoving(model::Pos new_pos) const;
272-
273-
274-
double last_move_time_ = 0;
275-
std::chrono::steady_clock::time_point play_time_ =
276-
std::chrono::steady_clock::now();
277-
278261
State state_;
279262
double default_dog_speed_ = 0;
280263

@@ -327,13 +310,9 @@ namespace model {
327310

328311
void SetDeserializedSessionId(Id id) { id_ = id; }
329312
void SetDeserializedObjVal(uint64_t lost_obect_id) { lost_object_id_ = lost_obect_id; }
330-
void SetDBConnPool(db::ConnectionPool* conn_pool) {
331-
conn_pool_ = conn_pool;
332-
}
333313

334314
int GetLootValue(int loot_type) const;
335315

336-
337316
uint64_t GetLootCount();
338317

339318
void GenerateLoot(int count, int loot_types_count);
@@ -359,16 +338,9 @@ namespace model {
359338
void Tick(double delta_time);
360339

361340
void RemoveDog(Dog::Id id);
362-
363-
void RetirePlayer(Dog::Id dog_id);
364-
365-
void SavePlayerStatsToDB(std::shared_ptr<Dog> dog);
366341

367342
private:
368343

369-
void RemoveAFKPlayer(std::unordered_map<Dog::Id, Pos> new_positions,
370-
double delta_time);
371-
372344
Pos CalculateNewPosition(const Pos& position, const Speed& speed, double delta_time);
373345

374346
std::unordered_map<Dog::Id, Pos>
@@ -419,10 +391,6 @@ namespace model {
419391

420392
size_t bag_capacity_;
421393

422-
db::ConnectionPool* conn_pool_ = nullptr;
423-
424-
double dog_retirement_time_ = 15;
425-
426394
static inline Id general_id_{0};
427395
static inline uint64_t lost_object_id_{0};
428396
};
@@ -494,23 +462,9 @@ namespace model {
494462

495463
void Tick(std::chrono::milliseconds delta_time);
496464

497-
void SetDBConnPool(db::ConnectionPool* conn_pool) {
498-
connection_pool_ = conn_pool;
499-
500-
for (const auto& session : common_data_->sessions_) {
501-
session->SetDBConnPool(conn_pool);
502-
}
503-
}
504-
505-
db::ConnectionPool* GetDBConnPool() const {
506-
return connection_pool_;
507-
}
508-
509465
private:
510466
void ConfigureSessionData(std::shared_ptr<GameSession> session);
511467

512-
db::ConnectionPool* connection_pool_ = nullptr;
513-
514468
std::shared_ptr<CommonData> common_data_;
515469
};
516470

@@ -579,9 +533,6 @@ namespace model {
579533

580534
void SetDefaultTickTime(double delta_time);
581535
void SetDefaultDogSpeed(double default_speed);
582-
void SetDBConnPool(db::ConnectionPool* pool) {
583-
session_service_->SetDBConnPool(pool);
584-
}
585536
private:
586537

587538
double dog_retirement_time = 15000;

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -432,8 +432,7 @@ namespace serialization {
432432
: serialized_data_(game.GetCommonData())
433433
, default_dog_speed_(game.GetDefaultDogSpeed())
434434
, default_tick_time_(game.GetDefaultTickTime())
435-
, config(game.GetGeneratorConfig())
436-
, conn_pool_(game.GetSessionService().GetDBConnPool()) {
435+
, config(game.GetGeneratorConfig()) {
437436
}
438437

439438
template <typename Archive>
@@ -450,8 +449,6 @@ namespace serialization {
450449
game.SetDefaultDogSpeed(default_dog_speed_);
451450
game.SetDefaultTickTime(default_tick_time_);
452451

453-
game.SetDBConnPool(conn_pool_);
454-
455452
return game;
456453
}
457454

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ namespace player {
2727
return dog_->GetName();
2828
}
2929

30+
int GetDogScore() const { return dog_->GetState().score; }
31+
3032
double GetLastMoveTime() const { return last_move_time_; }
3133

3234
void SetLastMoveTime(double value) { last_move_time_ = value; }
@@ -36,6 +38,12 @@ namespace player {
3638

3739
void MovePlayer(std::string direction = "");
3840

41+
int CalcPlayTime() const {
42+
std::chrono::time_point now = std::chrono::steady_clock::now();
43+
return std::chrono::duration_cast<std::chrono::duration<int>>(
44+
now - play_time_).count();
45+
}
46+
3947
const std::shared_ptr<model::GameSession> GetGameSession() const;
4048
const std::shared_ptr<model::Dog> GetDog() const { return dog_; }
4149

0 commit comments

Comments
 (0)