From 7c7646281b66ae0007e8aff0c08bf14bf0b47f11 Mon Sep 17 00:00:00 2001 From: Rex Date: Wed, 15 May 2024 22:43:16 +0800 Subject: [PATCH 1/2] [Modify] Change the type of members in InitGameEvent. (#43) - Add class: 'EventPlayer'. --- cmake/sources.cmake | 104 +++++++++++++++--------------- models/events/event_player.h | 21 ++++++ models/events/init_game_event.cpp | 55 ++++++++++++++++ models/events/init_game_event.h | 31 ++++++--- models/machikoro_game.cpp | 6 +- models/machikoro_game.h | 2 +- presenters/init_game_present.cpp | 57 ++++++++-------- presenters/init_game_present.h | 21 +++--- utils/util.cpp | 11 ++-- 9 files changed, 198 insertions(+), 110 deletions(-) create mode 100644 models/events/event_player.h create mode 100644 models/events/init_game_event.cpp diff --git a/cmake/sources.cmake b/cmake/sources.cmake index 195058f..f9af636 100644 --- a/cmake/sources.cmake +++ b/cmake/sources.cmake @@ -4,20 +4,20 @@ set(CONTROLLER_HEADERS ${CMAKE_SOURCE_DIR}/controllers/clear_game_controller.h ) set(CONTROLLER_SOURCES - ${CMAKE_SOURCE_DIR}/controllers/create_game_controller.cpp ${CMAKE_SOURCE_DIR}/controllers/clear_game_controller.cpp + ${CMAKE_SOURCE_DIR}/controllers/create_game_controller.cpp ${CMAKE_SOURCE_DIR}/controllers/init_game_controller.cpp ) set(PRESENTER_HEADERS ${CMAKE_SOURCE_DIR}/presenters/create_game_present.h + ${CMAKE_SOURCE_DIR}/presenters/init_game_present.h ${CMAKE_SOURCE_DIR}/presenters/clear_game_present.h ${CMAKE_SOURCE_DIR}/presenters/presenter.h - ${CMAKE_SOURCE_DIR}/presenters/init_game_present.h ) set(PRESENTER_SOURCES ${CMAKE_SOURCE_DIR}/presenters/create_game_present.cpp - ${CMAKE_SOURCE_DIR}/presenters/init_game_present.cpp ${CMAKE_SOURCE_DIR}/presenters/clear_game_present.cpp + ${CMAKE_SOURCE_DIR}/presenters/init_game_present.cpp ) set(LOGGER_HEADERS ${CMAKE_SOURCE_DIR}/loggers/logger_base.h @@ -27,99 +27,101 @@ set(LOGGER_SOURCES ${CMAKE_SOURCE_DIR}/loggers/logger.cpp ) set(REPO_HEADERS - ${CMAKE_SOURCE_DIR}/repos/memory_repository.h ${CMAKE_SOURCE_DIR}/repos/repository.h + ${CMAKE_SOURCE_DIR}/repos/memory_repository.h ) set(REPO_SOURCES ${CMAKE_SOURCE_DIR}/repos/memory_repository.cpp ) set(UTIL_HEADERS - ${CMAKE_SOURCE_DIR}/utils/util_base.h ${CMAKE_SOURCE_DIR}/utils/util.h + ${CMAKE_SOURCE_DIR}/utils/util_base.h ) set(UTIL_SOURCES ${CMAKE_SOURCE_DIR}/utils/util.cpp ) set(USECASE_HEADERS - ${CMAKE_SOURCE_DIR}/usecases/create_game.h ${CMAKE_SOURCE_DIR}/usecases/clear_game.h + ${CMAKE_SOURCE_DIR}/usecases/create_game.h ${CMAKE_SOURCE_DIR}/usecases/init_game.h ) set(USECASE_SOURCES ${CMAKE_SOURCE_DIR}/usecases/create_game.cpp - ${CMAKE_SOURCE_DIR}/usecases/init_game.cpp ${CMAKE_SOURCE_DIR}/usecases/clear_game.cpp + ${CMAKE_SOURCE_DIR}/usecases/init_game.cpp ) set(MODEL_HEADERS - ${CMAKE_SOURCE_DIR}/models/dice.h - ${CMAKE_SOURCE_DIR}/models/bank.h - ${CMAKE_SOURCE_DIR}/models/machikoro_game.h - ${CMAKE_SOURCE_DIR}/models/architecture_market.h ${CMAKE_SOURCE_DIR}/models/dice_base.h - ${CMAKE_SOURCE_DIR}/models/player.h - ${CMAKE_SOURCE_DIR}/models/events/init_game_event.h - ${CMAKE_SOURCE_DIR}/models/events/create_game_event.h - ${CMAKE_SOURCE_DIR}/models/events/clear_game_event.h - ${CMAKE_SOURCE_DIR}/models/events/event.h - ${CMAKE_SOURCE_DIR}/models/hand.h - ${CMAKE_SOURCE_DIR}/models/cards/radio_tower.h - ${CMAKE_SOURCE_DIR}/models/cards/stadium.h + ${CMAKE_SOURCE_DIR}/models/architecture_market.h + ${CMAKE_SOURCE_DIR}/models/cards/convenient_store.h + ${CMAKE_SOURCE_DIR}/models/cards/fruit_and_vegetable_market.h + ${CMAKE_SOURCE_DIR}/models/cards/wheat_field.h ${CMAKE_SOURCE_DIR}/models/cards/card.h + ${CMAKE_SOURCE_DIR}/models/cards/shopping_mall.h + ${CMAKE_SOURCE_DIR}/models/cards/cheese_factory.h + ${CMAKE_SOURCE_DIR}/models/cards/forest.h ${CMAKE_SOURCE_DIR}/models/cards/ranch.h - ${CMAKE_SOURCE_DIR}/models/cards/amusement_park.h - ${CMAKE_SOURCE_DIR}/models/cards/mine.h - ${CMAKE_SOURCE_DIR}/models/cards/tv_station.h - ${CMAKE_SOURCE_DIR}/models/cards/wheat_field.h ${CMAKE_SOURCE_DIR}/models/cards/cafe.h ${CMAKE_SOURCE_DIR}/models/cards/family_restaurant.h - ${CMAKE_SOURCE_DIR}/models/cards/apple_orchard.h - ${CMAKE_SOURCE_DIR}/models/cards/business_center.h + ${CMAKE_SOURCE_DIR}/models/cards/radio_tower.h ${CMAKE_SOURCE_DIR}/models/cards/landmark.h ${CMAKE_SOURCE_DIR}/models/cards/building.h - ${CMAKE_SOURCE_DIR}/models/cards/cheese_factory.h + ${CMAKE_SOURCE_DIR}/models/cards/tv_station.h ${CMAKE_SOURCE_DIR}/models/cards/bakery.h - ${CMAKE_SOURCE_DIR}/models/cards/fruit_and_vegetable_market.h - ${CMAKE_SOURCE_DIR}/models/cards/shopping_mall.h - ${CMAKE_SOURCE_DIR}/models/cards/convenient_store.h + ${CMAKE_SOURCE_DIR}/models/cards/amusement_park.h + ${CMAKE_SOURCE_DIR}/models/cards/stadium.h + ${CMAKE_SOURCE_DIR}/models/cards/apple_orchard.h ${CMAKE_SOURCE_DIR}/models/cards/furniture_factory.h - ${CMAKE_SOURCE_DIR}/models/cards/forest.h ${CMAKE_SOURCE_DIR}/models/cards/train_station.h + ${CMAKE_SOURCE_DIR}/models/cards/business_center.h + ${CMAKE_SOURCE_DIR}/models/cards/mine.h + ${CMAKE_SOURCE_DIR}/models/hand.h + ${CMAKE_SOURCE_DIR}/models/player.h + ${CMAKE_SOURCE_DIR}/models/machikoro_game.h + ${CMAKE_SOURCE_DIR}/models/bank.h + ${CMAKE_SOURCE_DIR}/models/events/clear_game_event.h + ${CMAKE_SOURCE_DIR}/models/events/init_game_event.h + ${CMAKE_SOURCE_DIR}/models/events/event.h + ${CMAKE_SOURCE_DIR}/models/events/create_game_event.h + ${CMAKE_SOURCE_DIR}/models/events/event_player.h + ${CMAKE_SOURCE_DIR}/models/dice.h ) set(MODEL_SOURCES - ${CMAKE_SOURCE_DIR}/models/dice.cpp - ${CMAKE_SOURCE_DIR}/models/machikoro_game.cpp ${CMAKE_SOURCE_DIR}/models/bank.cpp ${CMAKE_SOURCE_DIR}/models/player.cpp - ${CMAKE_SOURCE_DIR}/models/hand.cpp + ${CMAKE_SOURCE_DIR}/models/dice.cpp + ${CMAKE_SOURCE_DIR}/models/architecture_market.cpp ${CMAKE_SOURCE_DIR}/models/cards/convenient_store.cpp - ${CMAKE_SOURCE_DIR}/models/cards/landmark.cpp - ${CMAKE_SOURCE_DIR}/models/cards/forest.cpp - ${CMAKE_SOURCE_DIR}/models/cards/radio_tower.cpp - ${CMAKE_SOURCE_DIR}/models/cards/bakery.cpp - ${CMAKE_SOURCE_DIR}/models/cards/shopping_mall.cpp - ${CMAKE_SOURCE_DIR}/models/cards/mine.cpp - ${CMAKE_SOURCE_DIR}/models/cards/cheese_factory.cpp - ${CMAKE_SOURCE_DIR}/models/cards/family_restaurant.cpp ${CMAKE_SOURCE_DIR}/models/cards/wheat_field.cpp ${CMAKE_SOURCE_DIR}/models/cards/amusement_park.cpp + ${CMAKE_SOURCE_DIR}/models/cards/building.cpp + ${CMAKE_SOURCE_DIR}/models/cards/ranch.cpp + ${CMAKE_SOURCE_DIR}/models/cards/fruit_and_vegetable_market.cpp ${CMAKE_SOURCE_DIR}/models/cards/tv_station.cpp - ${CMAKE_SOURCE_DIR}/models/cards/cafe.cpp - ${CMAKE_SOURCE_DIR}/models/cards/train_station.cpp - ${CMAKE_SOURCE_DIR}/models/cards/business_center.cpp + ${CMAKE_SOURCE_DIR}/models/cards/family_restaurant.cpp + ${CMAKE_SOURCE_DIR}/models/cards/shopping_mall.cpp + ${CMAKE_SOURCE_DIR}/models/cards/bakery.cpp ${CMAKE_SOURCE_DIR}/models/cards/apple_orchard.cpp - ${CMAKE_SOURCE_DIR}/models/cards/ranch.cpp - ${CMAKE_SOURCE_DIR}/models/cards/building.cpp + ${CMAKE_SOURCE_DIR}/models/cards/forest.cpp + ${CMAKE_SOURCE_DIR}/models/cards/cheese_factory.cpp + ${CMAKE_SOURCE_DIR}/models/cards/train_station.cpp + ${CMAKE_SOURCE_DIR}/models/cards/mine.cpp + ${CMAKE_SOURCE_DIR}/models/cards/stadium.cpp ${CMAKE_SOURCE_DIR}/models/cards/furniture_factory.cpp + ${CMAKE_SOURCE_DIR}/models/cards/business_center.cpp ${CMAKE_SOURCE_DIR}/models/cards/card.cpp - ${CMAKE_SOURCE_DIR}/models/cards/fruit_and_vegetable_market.cpp - ${CMAKE_SOURCE_DIR}/models/cards/stadium.cpp - ${CMAKE_SOURCE_DIR}/models/architecture_market.cpp + ${CMAKE_SOURCE_DIR}/models/cards/landmark.cpp + ${CMAKE_SOURCE_DIR}/models/cards/cafe.cpp + ${CMAKE_SOURCE_DIR}/models/cards/radio_tower.cpp + ${CMAKE_SOURCE_DIR}/models/hand.cpp + ${CMAKE_SOURCE_DIR}/models/machikoro_game.cpp + ${CMAKE_SOURCE_DIR}/models/events/init_game_event.cpp ) set(E2E_SOURCES ${CMAKE_SOURCE_DIR}/tests/e2e_test/e2e_main.cpp - ${CMAKE_SOURCE_DIR}/tests/e2e_test/test_def.h - ${CMAKE_SOURCE_DIR}/tests/e2e_test/e2e_init_game.cpp ${CMAKE_SOURCE_DIR}/tests/e2e_test/e2e_create_game.cpp + ${CMAKE_SOURCE_DIR}/tests/e2e_test/e2e_init_game.cpp + ${CMAKE_SOURCE_DIR}/tests/e2e_test/test_def.h ) set(UNIT_SOURCES ${CMAKE_SOURCE_DIR}/tests/unittest/unit_operate_effect.cpp diff --git a/models/events/event_player.h b/models/events/event_player.h new file mode 100644 index 0000000..bdbb6c2 --- /dev/null +++ b/models/events/event_player.h @@ -0,0 +1,21 @@ +#ifndef MODELS_EVENTS_EVENT_PLAYER_H +#define MODELS_EVENTS_EVENT_PLAYER_H + +#include +#include + +/** + * @note: Used to pass the player's informations back to Presenter. + */ +class EventPlayer { +public: + std::string player_name_; + + int total_coin_ = 0; + + std::map landmarks_; + + std::map buildings_; +}; + +#endif // MODELS_EVENTS_EVENT_PLAYER_H \ No newline at end of file diff --git a/models/events/init_game_event.cpp b/models/events/init_game_event.cpp new file mode 100644 index 0000000..73ffac2 --- /dev/null +++ b/models/events/init_game_event.cpp @@ -0,0 +1,55 @@ +#include "models/events/init_game_event.h" + +#include "models/bank.h" +#include "models/architecture_market.h" + +InitGameEvent::InitGameEvent(std::shared_ptr util) + : util_(util) +{ +} + +void InitGameEvent::set_bank(Bank* bank) +{ + if (!bank) return; + bank_balance_ = bank->totalCoin(); +} + +void InitGameEvent::set_market(ArchitectureMarket* market) +{ + if (!market) return; + + auto cards = market->cards(); + for (const auto& c : cards) + { + auto& [name, buildings] = c; + market_cards_[util_->getStringFromCardName(name)] = buildings.size(); + } +} + +void InitGameEvent::set_players(PlayerPtrs* players) +{ + if (!players) return; + + for (auto& player : *players) + { + EventPlayer ep; + ep.player_name_ = player->name(); + ep.total_coin_ = player->totalCoin(); + + // Landmarks. + auto landmarks = player->hand().landmarks(); + for (const auto& card : landmarks) + { + ep.landmarks_[util_->getStringFromCardName(card->card_name())] = card->is_activate(); + } + + // Buildings. + auto buildings = player->hand().buildings(); + for (const auto& card : buildings) + { + ep.buildings_[util_->getStringFromCardName(card->card_name())]++; + } + + players_.push_back(std::move(ep)); + } +} \ No newline at end of file diff --git a/models/events/init_game_event.h b/models/events/init_game_event.h index 8cd6c8c..705f5fb 100644 --- a/models/events/init_game_event.h +++ b/models/events/init_game_event.h @@ -3,9 +3,13 @@ #include #include +#include +#include -#include "event.h" #include "models/player.h" +#include "models/events/event.h" +#include "models/events/event_player.h" +#include "utils/util_base.h" class ArchitectureMarket; class Bank; @@ -14,6 +18,8 @@ class InitGameEvent : public Event { public: InitGameEvent() = default; + InitGameEvent(std::shared_ptr util); + ~InitGameEvent() = default; void set_status(const StatusCode& status) override { status_ = status; } @@ -24,32 +30,37 @@ class InitGameEvent : public Event { std::string message() const override { return message_; } - void set_bank(Bank* bank) { bank_ = bank; } + void set_bank(Bank* bank); - Bank* bank() const { return bank_; } + int bank_balance() const { return bank_balance_; } - void set_market(ArchitectureMarket* market) { market_ = market; } + void set_market(ArchitectureMarket* market); - ArchitectureMarket* market() const { return market_; } + std::map market_cards() const { return market_cards_; } - void set_players(PlayerPtrs* players) { players_ = players; } + void set_players(PlayerPtrs* players); - PlayerPtrs* players() const { return players_; } + std::vector players() const { return players_; } void set_player_name(const std::string& name) { player_name_ = name; } std::string player_name() const { return player_name_; } private: + std::shared_ptr util_ = nullptr; + StatusCode status_ = StatusCode::NoContent; std::string message_; - Bank* bank_ = nullptr; + // Bank's balance. + int bank_balance_ = 0; - ArchitectureMarket* market_ = nullptr; + // Market's cards. + std::map market_cards_; - PlayerPtrs* players_ = nullptr; + // Players. + std::vector players_; // Player name for the next turn. std::string player_name_; diff --git a/models/machikoro_game.cpp b/models/machikoro_game.cpp index 37d42de..31cba9e 100644 --- a/models/machikoro_game.cpp +++ b/models/machikoro_game.cpp @@ -3,8 +3,8 @@ #include #include -#include "events/create_game_event.h" -#include "events/init_game_event.h" +#include "models/events/create_game_event.h" +#include "models/events/init_game_event.h" MachiKoroGame::MachiKoroGame(std::shared_ptr logger, std::shared_ptr util) : log_(logger) @@ -37,7 +37,7 @@ std::unique_ptr MachiKoroGame::createGame(std::vector&& player std::unique_ptr MachiKoroGame::initGame() { - auto event = std::make_unique(); + auto event = std::make_unique(util_); // Allocate money. for (auto& player : players_) player->gainCoinFromBank(bank_, 3); diff --git a/models/machikoro_game.h b/models/machikoro_game.h index b11bc06..612ef80 100644 --- a/models/machikoro_game.h +++ b/models/machikoro_game.h @@ -52,7 +52,7 @@ class MachiKoroGame { PlayerPtrs players_; // Current player order. - // Mod by the number of players to know + // Mod by the number of players to know // the players currently playing that round. int current_player_ = 0; }; diff --git a/presenters/init_game_present.cpp b/presenters/init_game_present.cpp index 521e680..96e6c44 100644 --- a/presenters/init_game_present.cpp +++ b/presenters/init_game_present.cpp @@ -19,64 +19,63 @@ void InitGamePresenter::present(Event* event) status_ = static_cast(res->status()); view_model_.reset(); - view_model_ = std::make_unique( - util_, res->message(), res->bank(), res->market(), res->players(), res->player_name()); + view_model_ = std::make_unique(util_, + res->message(), + res->bank_balance(), + res->market_cards(), + res->players(), + res->player_name()); } InitGamePresenter::ViewModel::ViewModel(std::shared_ptr util, const std::string& msg, - Bank* bank, - ArchitectureMarket* market, - PlayerPtrs* players, + int bank_balance, + const std::map& market_cards, + const std::vector& players, const std::string& name) : util_(util) , message_(msg) - , bank_(bank) - , market_(market) + , bank_balance_(bank_balance) + , market_cards_(market_cards) , players_(players) , player_name_(name) { } -InitGamePresenter::ViewModel::~ViewModel() -{ - bank_ = nullptr; - market_ = nullptr; - players_ = nullptr; -} - Json::Value InitGamePresenter::ViewModel::getJson() const { Json::Value res; res["Message"] = message_; - res["BankBalance"] = std::to_string(bank_->totalCoin()); + res["BankBalance"] = std::to_string(bank_balance_); Json::Value market; - const auto& market_cards = market_->cards(); - for (const auto& cards : market_cards) + for (const auto& cards : market_cards_) { - auto& [name, card] = cards; - market[util_->getStringFromCardName(name)] = std::to_string(card.size()); + auto& [card_name, card_num] = cards; + market[card_name] = std::to_string(card_num); } res["Market"] = market; Json::Value players_json(Json::arrayValue); - for (auto& player : *players_) + for (auto& player : players_) { Json::Value player_json; - player_json["Name"] = player->name(); - player_json["Money"] = player->totalCoin(); + player_json["Name"] = player.player_name_; + player_json["Money"] = player.total_coin_; Json::Value hand_json; - for (auto& landmark : player->hand().landmarks()) - hand_json[util_->getStringFromCardName(landmark->card_name())] = - landmark->is_activate(); + for (const auto& landmark : player.landmarks_) + { + auto& [card_name, is_activate] = landmark; + hand_json[card_name] = is_activate; + } std::map card_count; - for (auto& building : player->hand().buildings()) - card_count[util_->getStringFromCardName(building->card_name())]++; - for (auto& building : card_count) - hand_json[building.first] = building.second; + for (const auto& building : player.buildings_) + { + auto& [card_name, count] = building; + card_count[card_name] = count; + } player_json["Hand"] = hand_json; players_json.append(player_json); diff --git a/presenters/init_game_present.h b/presenters/init_game_present.h index 4bcd568..ac7111a 100644 --- a/presenters/init_game_present.h +++ b/presenters/init_game_present.h @@ -7,10 +7,8 @@ #include "json/value.h" #include "drogon/HttpTypes.h" #include "presenter.h" -#include "models/player.h" +#include "models/events/event_player.h" -class ArchitectureMarket; -class Bank; class Event; class UtilBase; @@ -24,12 +22,12 @@ class InitGamePresenter : public Presenter { ViewModel(std::shared_ptr util, const std::string& msg, - Bank* bank, - ArchitectureMarket* market, - PlayerPtrs* players, + int bank_balance, + const std::map& market_cards, + const std::vector& players, const std::string& name); - ~ViewModel(); + ~ViewModel() = default; Json::Value getJson() const; @@ -39,11 +37,14 @@ class InitGamePresenter : public Presenter { // The self-defined message. std::string message_; - Bank* bank_ = nullptr; + // Bank balance. + int bank_balance_; - ArchitectureMarket* market_ = nullptr; + // Market cards. + std::map market_cards_; - PlayerPtrs* players_ = nullptr; + // Players. + std::vector players_; // Player for the next turn. std::string player_name_; diff --git a/utils/util.cpp b/utils/util.cpp index 08eba36..2865ab4 100644 --- a/utils/util.cpp +++ b/utils/util.cpp @@ -75,14 +75,13 @@ std::string Util::getStringFromCardName(const CardName& name) { CardName::FAMILY_RESTAURANT, "Family Restaurant" }, { CardName::APPLE_ORCHARD, "Apple Orchard" }, { CardName::FRUIT_AND_VEGETABLE_MARKET, "Fruit and Vegetable Market" }, - { CardName::TRAIN_STATION, "Train Station"}, - { CardName::SHOPPING_MALL, "Shopping Mall"}, - { CardName::AMUSEMENT_PARK, "Amusement Park"}, - { CardName::RADIO_TOWER, "Radio Tower"} + { CardName::TRAIN_STATION, "Train Station" }, + { CardName::SHOPPING_MALL, "Shopping Mall" }, + { CardName::AMUSEMENT_PARK, "Amusement Park" }, + { CardName::RADIO_TOWER, "Radio Tower" } }; auto it = cardNameStrings.find(name); - if (it != cardNameStrings.end()) - return it->second; + if (it != cardNameStrings.end()) return it->second; return "Unknown CardName"; } \ No newline at end of file From f06e3b59fccd0010c7b5ad039caa9483d2f5331b Mon Sep 17 00:00:00 2001 From: AngTsusiong Date: Sat, 18 May 2024 00:44:12 +0800 Subject: [PATCH 2/2] Refactor code for safer memory management and configuration enhancements This update includes several changes aimed at improving memory management, configuration process. Notably, it migrates the Bank class usage to leverage across various components, ensuring safer memory management and ownership semantics. An adjustment in the class is the modification of the method parameter to accept a rvalue reference. This move aligns with modern C++ practices for resource management, enabling efficient transfer of ownership of card objects into a player's hand without unnecessary copying. On the configuration front, the introduction of JSON parsing for setting up Drogon HTTP listeners and application configurations directly within the codebase provides greater flexibility. Additionally, the codebase witnesses a broad application of in drawing cards from the market, further exemplifying the adoption of modern C++ features to handle operations that may not always succeed. This use of introduces clearer semantics for handling these cases. Signed-off-by: AngTsusiong --- cmake/sources.cmake | 104 ++++++++++---------- main.cpp | 14 +-- models/architecture_market.cpp | 13 +-- models/architecture_market.h | 5 +- models/cards/amusement_park.cpp | 2 +- models/cards/amusement_park.h | 2 +- models/cards/apple_orchard.cpp | 2 +- models/cards/apple_orchard.h | 2 +- models/cards/bakery.cpp | 2 +- models/cards/bakery.h | 2 +- models/cards/building.cpp | 6 +- models/cards/building.h | 8 +- models/cards/business_center.cpp | 2 +- models/cards/business_center.h | 2 +- models/cards/cafe.cpp | 2 +- models/cards/cafe.h | 2 +- models/cards/card.cpp | 2 +- models/cards/card.h | 5 +- models/cards/cheese_factory.cpp | 2 +- models/cards/cheese_factory.h | 2 +- models/cards/convenient_store.cpp | 2 +- models/cards/convenient_store.h | 2 +- models/cards/family_restaurant.cpp | 2 +- models/cards/family_restaurant.h | 2 +- models/cards/forest.cpp | 2 +- models/cards/forest.h | 2 +- models/cards/fruit_and_vegetable_market.cpp | 2 +- models/cards/fruit_and_vegetable_market.h | 2 +- models/cards/furniture_factory.cpp | 2 +- models/cards/furniture_factory.h | 2 +- models/cards/landmark.cpp | 2 +- models/cards/landmark.h | 4 +- models/cards/mine.cpp | 2 +- models/cards/mine.h | 2 +- models/cards/radio_tower.cpp | 2 +- models/cards/radio_tower.h | 2 +- models/cards/ranch.cpp | 2 +- models/cards/ranch.h | 2 +- models/cards/shopping_mall.cpp | 2 +- models/cards/shopping_mall.h | 2 +- models/cards/stadium.cpp | 2 +- models/cards/stadium.h | 2 +- models/cards/train_station.cpp | 2 +- models/cards/train_station.h | 2 +- models/cards/tv_station.cpp | 2 +- models/cards/tv_station.h | 2 +- models/cards/wheat_field.cpp | 4 +- models/cards/wheat_field.h | 2 +- models/events/init_game_event.cpp | 10 +- models/events/init_game_event.h | 2 +- models/hand.cpp | 2 +- models/hand.h | 2 +- models/machikoro_game.cpp | 58 ++++++++++- models/machikoro_game.h | 4 +- models/player.cpp | 6 +- models/player.h | 2 +- tests/e2e_test/e2e_main.cpp | 12 ++- tests/unittest/unit_operate_effect.cpp | 7 +- 58 files changed, 203 insertions(+), 143 deletions(-) diff --git a/cmake/sources.cmake b/cmake/sources.cmake index f9af636..3c5bba4 100644 --- a/cmake/sources.cmake +++ b/cmake/sources.cmake @@ -1,7 +1,7 @@ set(CONTROLLER_HEADERS - ${CMAKE_SOURCE_DIR}/controllers/init_game_controller.h - ${CMAKE_SOURCE_DIR}/controllers/create_game_controller.h ${CMAKE_SOURCE_DIR}/controllers/clear_game_controller.h + ${CMAKE_SOURCE_DIR}/controllers/create_game_controller.h + ${CMAKE_SOURCE_DIR}/controllers/init_game_controller.h ) set(CONTROLLER_SOURCES ${CMAKE_SOURCE_DIR}/controllers/clear_game_controller.cpp @@ -9,26 +9,26 @@ set(CONTROLLER_SOURCES ${CMAKE_SOURCE_DIR}/controllers/init_game_controller.cpp ) set(PRESENTER_HEADERS + ${CMAKE_SOURCE_DIR}/presenters/clear_game_present.h ${CMAKE_SOURCE_DIR}/presenters/create_game_present.h ${CMAKE_SOURCE_DIR}/presenters/init_game_present.h - ${CMAKE_SOURCE_DIR}/presenters/clear_game_present.h ${CMAKE_SOURCE_DIR}/presenters/presenter.h ) set(PRESENTER_SOURCES - ${CMAKE_SOURCE_DIR}/presenters/create_game_present.cpp ${CMAKE_SOURCE_DIR}/presenters/clear_game_present.cpp + ${CMAKE_SOURCE_DIR}/presenters/create_game_present.cpp ${CMAKE_SOURCE_DIR}/presenters/init_game_present.cpp ) set(LOGGER_HEADERS - ${CMAKE_SOURCE_DIR}/loggers/logger_base.h ${CMAKE_SOURCE_DIR}/loggers/logger.h + ${CMAKE_SOURCE_DIR}/loggers/logger_base.h ) set(LOGGER_SOURCES ${CMAKE_SOURCE_DIR}/loggers/logger.cpp ) set(REPO_HEADERS - ${CMAKE_SOURCE_DIR}/repos/repository.h ${CMAKE_SOURCE_DIR}/repos/memory_repository.h + ${CMAKE_SOURCE_DIR}/repos/repository.h ) set(REPO_SOURCES ${CMAKE_SOURCE_DIR}/repos/memory_repository.cpp @@ -46,84 +46,84 @@ set(USECASE_HEADERS ${CMAKE_SOURCE_DIR}/usecases/init_game.h ) set(USECASE_SOURCES - ${CMAKE_SOURCE_DIR}/usecases/create_game.cpp ${CMAKE_SOURCE_DIR}/usecases/clear_game.cpp + ${CMAKE_SOURCE_DIR}/usecases/create_game.cpp ${CMAKE_SOURCE_DIR}/usecases/init_game.cpp ) set(MODEL_HEADERS - ${CMAKE_SOURCE_DIR}/models/dice_base.h ${CMAKE_SOURCE_DIR}/models/architecture_market.h - ${CMAKE_SOURCE_DIR}/models/cards/convenient_store.h - ${CMAKE_SOURCE_DIR}/models/cards/fruit_and_vegetable_market.h - ${CMAKE_SOURCE_DIR}/models/cards/wheat_field.h + ${CMAKE_SOURCE_DIR}/models/bank.h + ${CMAKE_SOURCE_DIR}/models/cards/amusement_park.h + ${CMAKE_SOURCE_DIR}/models/cards/apple_orchard.h + ${CMAKE_SOURCE_DIR}/models/cards/bakery.h + ${CMAKE_SOURCE_DIR}/models/cards/building.h + ${CMAKE_SOURCE_DIR}/models/cards/business_center.h + ${CMAKE_SOURCE_DIR}/models/cards/cafe.h ${CMAKE_SOURCE_DIR}/models/cards/card.h - ${CMAKE_SOURCE_DIR}/models/cards/shopping_mall.h ${CMAKE_SOURCE_DIR}/models/cards/cheese_factory.h - ${CMAKE_SOURCE_DIR}/models/cards/forest.h - ${CMAKE_SOURCE_DIR}/models/cards/ranch.h - ${CMAKE_SOURCE_DIR}/models/cards/cafe.h + ${CMAKE_SOURCE_DIR}/models/cards/convenient_store.h ${CMAKE_SOURCE_DIR}/models/cards/family_restaurant.h - ${CMAKE_SOURCE_DIR}/models/cards/radio_tower.h + ${CMAKE_SOURCE_DIR}/models/cards/forest.h + ${CMAKE_SOURCE_DIR}/models/cards/fruit_and_vegetable_market.h + ${CMAKE_SOURCE_DIR}/models/cards/furniture_factory.h ${CMAKE_SOURCE_DIR}/models/cards/landmark.h - ${CMAKE_SOURCE_DIR}/models/cards/building.h - ${CMAKE_SOURCE_DIR}/models/cards/tv_station.h - ${CMAKE_SOURCE_DIR}/models/cards/bakery.h - ${CMAKE_SOURCE_DIR}/models/cards/amusement_park.h + ${CMAKE_SOURCE_DIR}/models/cards/mine.h + ${CMAKE_SOURCE_DIR}/models/cards/radio_tower.h + ${CMAKE_SOURCE_DIR}/models/cards/ranch.h + ${CMAKE_SOURCE_DIR}/models/cards/shopping_mall.h ${CMAKE_SOURCE_DIR}/models/cards/stadium.h - ${CMAKE_SOURCE_DIR}/models/cards/apple_orchard.h - ${CMAKE_SOURCE_DIR}/models/cards/furniture_factory.h ${CMAKE_SOURCE_DIR}/models/cards/train_station.h - ${CMAKE_SOURCE_DIR}/models/cards/business_center.h - ${CMAKE_SOURCE_DIR}/models/cards/mine.h - ${CMAKE_SOURCE_DIR}/models/hand.h - ${CMAKE_SOURCE_DIR}/models/player.h - ${CMAKE_SOURCE_DIR}/models/machikoro_game.h - ${CMAKE_SOURCE_DIR}/models/bank.h + ${CMAKE_SOURCE_DIR}/models/cards/tv_station.h + ${CMAKE_SOURCE_DIR}/models/cards/wheat_field.h + ${CMAKE_SOURCE_DIR}/models/dice.h + ${CMAKE_SOURCE_DIR}/models/dice_base.h ${CMAKE_SOURCE_DIR}/models/events/clear_game_event.h - ${CMAKE_SOURCE_DIR}/models/events/init_game_event.h - ${CMAKE_SOURCE_DIR}/models/events/event.h ${CMAKE_SOURCE_DIR}/models/events/create_game_event.h + ${CMAKE_SOURCE_DIR}/models/events/event.h ${CMAKE_SOURCE_DIR}/models/events/event_player.h - ${CMAKE_SOURCE_DIR}/models/dice.h + ${CMAKE_SOURCE_DIR}/models/events/init_game_event.h + ${CMAKE_SOURCE_DIR}/models/hand.h + ${CMAKE_SOURCE_DIR}/models/machikoro_game.h + ${CMAKE_SOURCE_DIR}/models/player.h ) set(MODEL_SOURCES - ${CMAKE_SOURCE_DIR}/models/bank.cpp - ${CMAKE_SOURCE_DIR}/models/player.cpp - ${CMAKE_SOURCE_DIR}/models/dice.cpp ${CMAKE_SOURCE_DIR}/models/architecture_market.cpp - ${CMAKE_SOURCE_DIR}/models/cards/convenient_store.cpp - ${CMAKE_SOURCE_DIR}/models/cards/wheat_field.cpp + ${CMAKE_SOURCE_DIR}/models/bank.cpp ${CMAKE_SOURCE_DIR}/models/cards/amusement_park.cpp + ${CMAKE_SOURCE_DIR}/models/cards/apple_orchard.cpp + ${CMAKE_SOURCE_DIR}/models/cards/bakery.cpp ${CMAKE_SOURCE_DIR}/models/cards/building.cpp - ${CMAKE_SOURCE_DIR}/models/cards/ranch.cpp - ${CMAKE_SOURCE_DIR}/models/cards/fruit_and_vegetable_market.cpp - ${CMAKE_SOURCE_DIR}/models/cards/tv_station.cpp + ${CMAKE_SOURCE_DIR}/models/cards/business_center.cpp + ${CMAKE_SOURCE_DIR}/models/cards/cafe.cpp + ${CMAKE_SOURCE_DIR}/models/cards/card.cpp + ${CMAKE_SOURCE_DIR}/models/cards/cheese_factory.cpp + ${CMAKE_SOURCE_DIR}/models/cards/convenient_store.cpp ${CMAKE_SOURCE_DIR}/models/cards/family_restaurant.cpp - ${CMAKE_SOURCE_DIR}/models/cards/shopping_mall.cpp - ${CMAKE_SOURCE_DIR}/models/cards/bakery.cpp - ${CMAKE_SOURCE_DIR}/models/cards/apple_orchard.cpp ${CMAKE_SOURCE_DIR}/models/cards/forest.cpp - ${CMAKE_SOURCE_DIR}/models/cards/cheese_factory.cpp - ${CMAKE_SOURCE_DIR}/models/cards/train_station.cpp - ${CMAKE_SOURCE_DIR}/models/cards/mine.cpp - ${CMAKE_SOURCE_DIR}/models/cards/stadium.cpp + ${CMAKE_SOURCE_DIR}/models/cards/fruit_and_vegetable_market.cpp ${CMAKE_SOURCE_DIR}/models/cards/furniture_factory.cpp - ${CMAKE_SOURCE_DIR}/models/cards/business_center.cpp - ${CMAKE_SOURCE_DIR}/models/cards/card.cpp ${CMAKE_SOURCE_DIR}/models/cards/landmark.cpp - ${CMAKE_SOURCE_DIR}/models/cards/cafe.cpp + ${CMAKE_SOURCE_DIR}/models/cards/mine.cpp ${CMAKE_SOURCE_DIR}/models/cards/radio_tower.cpp + ${CMAKE_SOURCE_DIR}/models/cards/ranch.cpp + ${CMAKE_SOURCE_DIR}/models/cards/shopping_mall.cpp + ${CMAKE_SOURCE_DIR}/models/cards/stadium.cpp + ${CMAKE_SOURCE_DIR}/models/cards/train_station.cpp + ${CMAKE_SOURCE_DIR}/models/cards/tv_station.cpp + ${CMAKE_SOURCE_DIR}/models/cards/wheat_field.cpp + ${CMAKE_SOURCE_DIR}/models/dice.cpp + ${CMAKE_SOURCE_DIR}/models/events/init_game_event.cpp ${CMAKE_SOURCE_DIR}/models/hand.cpp + ${CMAKE_SOURCE_DIR}/models/player.cpp ${CMAKE_SOURCE_DIR}/models/machikoro_game.cpp - ${CMAKE_SOURCE_DIR}/models/events/init_game_event.cpp ) set(E2E_SOURCES - ${CMAKE_SOURCE_DIR}/tests/e2e_test/e2e_main.cpp ${CMAKE_SOURCE_DIR}/tests/e2e_test/e2e_create_game.cpp ${CMAKE_SOURCE_DIR}/tests/e2e_test/e2e_init_game.cpp + ${CMAKE_SOURCE_DIR}/tests/e2e_test/e2e_main.cpp ${CMAKE_SOURCE_DIR}/tests/e2e_test/test_def.h ) set(UNIT_SOURCES - ${CMAKE_SOURCE_DIR}/tests/unittest/unit_operate_effect.cpp ${CMAKE_SOURCE_DIR}/tests/unittest/unit_game.cpp + ${CMAKE_SOURCE_DIR}/tests/unittest/unit_operate_effect.cpp ) diff --git a/main.cpp b/main.cpp index 97beec1..853306e 100644 --- a/main.cpp +++ b/main.cpp @@ -7,13 +7,15 @@ int main() { - // Set HTTP listener address and port - drogon::app().addListener("127.0.0.1", 8080); + const std::string strConfig = + "{\"listeners\":[{\"address\":\"127.0.0.1\",\"port\":8080}],\"app\":{\"log\":{\"use_" + "spdlog\":true,\"log_level\":\"TRACE\"}},\"plugins\":[{\"name\":\"drogon::plugin::" + "AccessLogger\",\"config\":{\"use_spdlog\":true}}]}"; + Json::Value jsonConfig; + Json::Reader reader; + reader.parse(strConfig, jsonConfig); - // Load config file - // drogon::app().loadConfigFile("../config.json"); - - // Run HTTP framework,the method will block in the internal event loop + drogon::app().loadConfigJson(jsonConfig); drogon::app().run(); return 0; diff --git a/models/architecture_market.cpp b/models/architecture_market.cpp index 3dc9527..8935368 100644 --- a/models/architecture_market.cpp +++ b/models/architecture_market.cpp @@ -1,4 +1,5 @@ #include "architecture_market.h" +#include #include "models/cards/wheat_field.h" #include "models/cards/ranch.h" @@ -40,21 +41,21 @@ ArchitectureMarket::ArchitectureMarket() generateBuildingsTemp(6); } -BuildingPtr ArchitectureMarket::drawCard(const CardName& name) +std::optional ArchitectureMarket::drawCard(CardName name) { + if (cards_[name].empty()) return std::nullopt; + auto res = std::move(cards_[name].back()); cards_[name].pop_back(); return res; } -std::map> ArchitectureMarket::cards() const +std::map ArchitectureMarket::cards() const { - std::map> res; + std::map res; for (const auto& card : cards_) { - std::vector buildings; - for (const auto& building : card.second) buildings.push_back(building.get()); - res[card.first] = buildings; + res[card.first] = card.second.size(); } return res; } \ No newline at end of file diff --git a/models/architecture_market.h b/models/architecture_market.h index 4ee1f21..98447d9 100644 --- a/models/architecture_market.h +++ b/models/architecture_market.h @@ -4,6 +4,7 @@ #include #include #include +#include #include "cards/building.h" @@ -17,9 +18,9 @@ class ArchitectureMarket { ~ArchitectureMarket() = default; - BuildingPtr drawCard(const CardName& name); + std::optional drawCard(CardName name); - std::map> cards() const; + std::map cards() const; private: // All the building cards. diff --git a/models/cards/amusement_park.cpp b/models/cards/amusement_park.cpp index 93e3639..84e2e84 100644 --- a/models/cards/amusement_park.cpp +++ b/models/cards/amusement_park.cpp @@ -11,6 +11,6 @@ AmusementPark::AmusementPark() void AmusementPark::OperateEffect(Player* owner, Player* dice_roller, std::vector players, - Bank* bank) + std::shared_ptr bank) { } \ No newline at end of file diff --git a/models/cards/amusement_park.h b/models/cards/amusement_park.h index 7afab71..7bbfcf7 100644 --- a/models/cards/amusement_park.h +++ b/models/cards/amusement_park.h @@ -12,7 +12,7 @@ class AmusementPark : public Landmark { void OperateEffect(Player* owner, Player* dice_roller, std::vector players, - Bank* bank) override; + std::shared_ptr bank) override; }; #endif \ No newline at end of file diff --git a/models/cards/apple_orchard.cpp b/models/cards/apple_orchard.cpp index 3ad15ac..f1257c9 100644 --- a/models/cards/apple_orchard.cpp +++ b/models/cards/apple_orchard.cpp @@ -11,6 +11,6 @@ AppleOrchard::AppleOrchard() void AppleOrchard::OperateEffect(Player* owner, Player* dice_roller, std::vector players, - Bank* bank) + std::shared_ptr bank) { } \ No newline at end of file diff --git a/models/cards/apple_orchard.h b/models/cards/apple_orchard.h index 685b65c..ee7ae80 100644 --- a/models/cards/apple_orchard.h +++ b/models/cards/apple_orchard.h @@ -12,7 +12,7 @@ class AppleOrchard : public Building { void OperateEffect(Player* owner, Player* dice_roller, std::vector players, - Bank* bank) override; + std::shared_ptr bank) override; }; #endif \ No newline at end of file diff --git a/models/cards/bakery.cpp b/models/cards/bakery.cpp index b90e84e..5c49c14 100644 --- a/models/cards/bakery.cpp +++ b/models/cards/bakery.cpp @@ -11,6 +11,6 @@ Bakery::Bakery() void Bakery::OperateEffect(Player* owner, Player* dice_roller, std::vector players, - Bank* bank) + std::shared_ptr bank) { } \ No newline at end of file diff --git a/models/cards/bakery.h b/models/cards/bakery.h index ee1d3f2..b206fa1 100644 --- a/models/cards/bakery.h +++ b/models/cards/bakery.h @@ -12,7 +12,7 @@ class Bakery : public Building { void OperateEffect(Player* owner, Player* dice_roller, std::vector players, - Bank* bank) override; + std::shared_ptr bank) override; }; #endif \ No newline at end of file diff --git a/models/cards/building.cpp b/models/cards/building.cpp index f815a02..035aa58 100644 --- a/models/cards/building.cpp +++ b/models/cards/building.cpp @@ -2,10 +2,10 @@ #include -Building::Building(const CardName& name, - const CardType& type, +Building::Building(CardName name, + CardType type, int price, - const IndustryType& ind, + IndustryType ind, std::vector&& points) : Card(name, type, price) , ind_type_(ind) diff --git a/models/cards/building.h b/models/cards/building.h index 72215ea..1a91bf3 100644 --- a/models/cards/building.h +++ b/models/cards/building.h @@ -27,18 +27,14 @@ BuildingPtrs generateBuildingsTemp(int count) class Building : public Card { public: - Building(const CardName& name, - const CardType& type, - int price, - const IndustryType& ind, - std::vector&& points); + Building(CardName name, CardType type, int price, IndustryType ind, std::vector&& points); ~Building() = default; virtual void OperateEffect(Player* owner, Player* dice_roller, std::vector others, - Bank* bank) = 0; + std::shared_ptr bank) = 0; IndustryType industryType() const { return ind_type_; } diff --git a/models/cards/business_center.cpp b/models/cards/business_center.cpp index d75f111..100db3a 100644 --- a/models/cards/business_center.cpp +++ b/models/cards/business_center.cpp @@ -15,6 +15,6 @@ BusinessCenter::BusinessCenter() void BusinessCenter::OperateEffect(Player* owner, Player* dice_roller, std::vector players, - Bank* bank) + std::shared_ptr bank) { } \ No newline at end of file diff --git a/models/cards/business_center.h b/models/cards/business_center.h index 310d007..905f01c 100644 --- a/models/cards/business_center.h +++ b/models/cards/business_center.h @@ -12,7 +12,7 @@ class BusinessCenter : public Building { void OperateEffect(Player* owner, Player* dice_roller, std::vector players, - Bank* bank) override; + std::shared_ptr bank) override; }; #endif \ No newline at end of file diff --git a/models/cards/cafe.cpp b/models/cards/cafe.cpp index 43a7287..a90de83 100644 --- a/models/cards/cafe.cpp +++ b/models/cards/cafe.cpp @@ -11,6 +11,6 @@ Cafe::Cafe() void Cafe::OperateEffect(Player* owner, Player* dice_roller, std::vector players, - Bank* bank) + std::shared_ptr bank) { } \ No newline at end of file diff --git a/models/cards/cafe.h b/models/cards/cafe.h index add50fd..faa309d 100644 --- a/models/cards/cafe.h +++ b/models/cards/cafe.h @@ -12,7 +12,7 @@ class Cafe : public Building { void OperateEffect(Player* owner, Player* dice_roller, std::vector players, - Bank* bank) override; + std::shared_ptr bank) override; }; #endif \ No newline at end of file diff --git a/models/cards/card.cpp b/models/cards/card.cpp index 316cff6..0fa7479 100644 --- a/models/cards/card.cpp +++ b/models/cards/card.cpp @@ -1,6 +1,6 @@ #include "card.h" -Card::Card(const CardName& name, const CardType& type, int price) +Card::Card(CardName name, CardType type, int price) : card_name_(name) , card_type_(type) , price_(price) diff --git a/models/cards/card.h b/models/cards/card.h index 78b6bb6..46212a7 100644 --- a/models/cards/card.h +++ b/models/cards/card.h @@ -2,6 +2,7 @@ #define MODELS_CARDS_CARD_H #include +#include enum class CardType { CROP, @@ -40,7 +41,7 @@ class Player; class Card { public: - Card(const CardName& name, const CardType& type, int price); + Card(CardName name, CardType type, int price); ~Card() = default; @@ -48,7 +49,7 @@ class Card { virtual void OperateEffect(Player* owner, Player* dice_roller, std::vector others, - Bank* bank) = 0; + std::shared_ptr bank) = 0; CardName card_name() const { return card_name_; } diff --git a/models/cards/cheese_factory.cpp b/models/cards/cheese_factory.cpp index ea60350..ace78f5 100644 --- a/models/cards/cheese_factory.cpp +++ b/models/cards/cheese_factory.cpp @@ -15,6 +15,6 @@ CheeseFactory::CheeseFactory() void CheeseFactory::OperateEffect(Player* owner, Player* dice_roller, std::vector players, - Bank* bank) + std::shared_ptr bank) { } \ No newline at end of file diff --git a/models/cards/cheese_factory.h b/models/cards/cheese_factory.h index 7cbcbaa..8b957da 100644 --- a/models/cards/cheese_factory.h +++ b/models/cards/cheese_factory.h @@ -12,7 +12,7 @@ class CheeseFactory : public Building { void OperateEffect(Player* owner, Player* dice_roller, std::vector players, - Bank* bank) override; + std::shared_ptr bank) override; }; #endif \ No newline at end of file diff --git a/models/cards/convenient_store.cpp b/models/cards/convenient_store.cpp index daf3025..6671831 100644 --- a/models/cards/convenient_store.cpp +++ b/models/cards/convenient_store.cpp @@ -15,6 +15,6 @@ ConvenientStore::ConvenientStore() void ConvenientStore::OperateEffect(Player* owner, Player* dice_roller, std::vector players, - Bank* bank) + std::shared_ptr bank) { } \ No newline at end of file diff --git a/models/cards/convenient_store.h b/models/cards/convenient_store.h index 6c936db..553c48e 100644 --- a/models/cards/convenient_store.h +++ b/models/cards/convenient_store.h @@ -12,7 +12,7 @@ class ConvenientStore : public Building { void OperateEffect(Player* owner, Player* dice_roller, std::vector players, - Bank* bank) override; + std::shared_ptr bank) override; }; #endif \ No newline at end of file diff --git a/models/cards/family_restaurant.cpp b/models/cards/family_restaurant.cpp index 5d6ada3..3600db8 100644 --- a/models/cards/family_restaurant.cpp +++ b/models/cards/family_restaurant.cpp @@ -15,6 +15,6 @@ FamilyRestaurant::FamilyRestaurant() void FamilyRestaurant::OperateEffect(Player* owner, Player* dice_roller, std::vector players, - Bank* bank) + std::shared_ptr bank) { } \ No newline at end of file diff --git a/models/cards/family_restaurant.h b/models/cards/family_restaurant.h index 89adeea..32c0ddd 100644 --- a/models/cards/family_restaurant.h +++ b/models/cards/family_restaurant.h @@ -12,7 +12,7 @@ class FamilyRestaurant : public Building { void OperateEffect(Player* owner, Player* dice_roller, std::vector players, - Bank* bank) override; + std::shared_ptr bank) override; }; #endif \ No newline at end of file diff --git a/models/cards/forest.cpp b/models/cards/forest.cpp index 9452b08..5068208 100644 --- a/models/cards/forest.cpp +++ b/models/cards/forest.cpp @@ -15,6 +15,6 @@ Forest::Forest() void Forest::OperateEffect(Player* owner, Player* dice_roller, std::vector players, - Bank* bank) + std::shared_ptr bank) { } \ No newline at end of file diff --git a/models/cards/forest.h b/models/cards/forest.h index ead3068..e14b4a3 100644 --- a/models/cards/forest.h +++ b/models/cards/forest.h @@ -12,7 +12,7 @@ class Forest : public Building { void OperateEffect(Player* owner, Player* dice_roller, std::vector players, - Bank* bank) override; + std::shared_ptr bank) override; }; #endif \ No newline at end of file diff --git a/models/cards/fruit_and_vegetable_market.cpp b/models/cards/fruit_and_vegetable_market.cpp index 03ffa10..3049cd0 100644 --- a/models/cards/fruit_and_vegetable_market.cpp +++ b/models/cards/fruit_and_vegetable_market.cpp @@ -15,6 +15,6 @@ FruitAndVegetableMarket::FruitAndVegetableMarket() void FruitAndVegetableMarket::OperateEffect(Player* owner, Player* dice_roller, std::vector players, - Bank* bank) + std::shared_ptr bank) { } \ No newline at end of file diff --git a/models/cards/fruit_and_vegetable_market.h b/models/cards/fruit_and_vegetable_market.h index e98b358..28e161b 100644 --- a/models/cards/fruit_and_vegetable_market.h +++ b/models/cards/fruit_and_vegetable_market.h @@ -12,7 +12,7 @@ class FruitAndVegetableMarket : public Building { void OperateEffect(Player* owner, Player* dice_roller, std::vector players, - Bank* bank) override; + std::shared_ptr bank) override; }; #endif \ No newline at end of file diff --git a/models/cards/furniture_factory.cpp b/models/cards/furniture_factory.cpp index d1ca223..3456494 100644 --- a/models/cards/furniture_factory.cpp +++ b/models/cards/furniture_factory.cpp @@ -15,6 +15,6 @@ FurnitureFactory::FurnitureFactory() void FurnitureFactory::OperateEffect(Player* owner, Player* dice_roller, std::vector players, - Bank* bank) + std::shared_ptr bank) { } \ No newline at end of file diff --git a/models/cards/furniture_factory.h b/models/cards/furniture_factory.h index 6f3236f..6ab3114 100644 --- a/models/cards/furniture_factory.h +++ b/models/cards/furniture_factory.h @@ -12,7 +12,7 @@ class FurnitureFactory : public Building { void OperateEffect(Player* owner, Player* dice_roller, std::vector players, - Bank* bank) override; + std::shared_ptr bank) override; }; #endif \ No newline at end of file diff --git a/models/cards/landmark.cpp b/models/cards/landmark.cpp index 3d3aaad..fdb093e 100644 --- a/models/cards/landmark.cpp +++ b/models/cards/landmark.cpp @@ -1,6 +1,6 @@ #include "landmark.h" -Landmark::Landmark(const CardName& name, const CardType& type, int price) +Landmark::Landmark(CardName name, CardType type, int price) : Card(name, type, price) { } \ No newline at end of file diff --git a/models/cards/landmark.h b/models/cards/landmark.h index dd44c77..1bb12f4 100644 --- a/models/cards/landmark.h +++ b/models/cards/landmark.h @@ -12,14 +12,14 @@ using LandmarkPtrs = std::vector; class Landmark : public Card { public: - Landmark(const CardName& name, const CardType& type, int price); + Landmark(CardName name, CardType type, int price); ~Landmark() = default; virtual void OperateEffect(Player* owner, Player* dice_roller, std::vector players, - Bank* bank) = 0; + std::shared_ptr bank) = 0; void activate() { is_activated_ = true; } diff --git a/models/cards/mine.cpp b/models/cards/mine.cpp index ca32dfa..53724ce 100644 --- a/models/cards/mine.cpp +++ b/models/cards/mine.cpp @@ -11,6 +11,6 @@ Mine::Mine() void Mine::OperateEffect(Player* owner, Player* dice_roller, std::vector players, - Bank* bank) + std::shared_ptr bank) { } \ No newline at end of file diff --git a/models/cards/mine.h b/models/cards/mine.h index 56601ab..5aaa0ab 100644 --- a/models/cards/mine.h +++ b/models/cards/mine.h @@ -12,7 +12,7 @@ class Mine : public Building { void OperateEffect(Player* owner, Player* dice_roller, std::vector players, - Bank* bank) override; + std::shared_ptr bank) override; }; #endif \ No newline at end of file diff --git a/models/cards/radio_tower.cpp b/models/cards/radio_tower.cpp index 24c2084..008c77b 100644 --- a/models/cards/radio_tower.cpp +++ b/models/cards/radio_tower.cpp @@ -11,6 +11,6 @@ RadioTower::RadioTower() void RadioTower::OperateEffect(Player* owner, Player* dice_roller, std::vector players, - Bank* bank) + std::shared_ptr bank) { } \ No newline at end of file diff --git a/models/cards/radio_tower.h b/models/cards/radio_tower.h index 95abadc..a253f94 100644 --- a/models/cards/radio_tower.h +++ b/models/cards/radio_tower.h @@ -12,7 +12,7 @@ class RadioTower : public Landmark { void OperateEffect(Player* owner, Player* dice_roller, std::vector players, - Bank* bank) override; + std::shared_ptr bank) override; }; #endif \ No newline at end of file diff --git a/models/cards/ranch.cpp b/models/cards/ranch.cpp index 2950ba4..74c2b35 100644 --- a/models/cards/ranch.cpp +++ b/models/cards/ranch.cpp @@ -15,6 +15,6 @@ Ranch::Ranch() void Ranch::OperateEffect(Player* owner, Player* dice_roller, std::vector players, - Bank* bank) + std::shared_ptr bank) { } \ No newline at end of file diff --git a/models/cards/ranch.h b/models/cards/ranch.h index 2a264d1..bcebff2 100644 --- a/models/cards/ranch.h +++ b/models/cards/ranch.h @@ -12,7 +12,7 @@ class Ranch : public Building { void OperateEffect(Player* owner, Player* dice_roller, std::vector players, - Bank* bank) override; + std::shared_ptr bank) override; }; #endif \ No newline at end of file diff --git a/models/cards/shopping_mall.cpp b/models/cards/shopping_mall.cpp index f1850b3..004df22 100644 --- a/models/cards/shopping_mall.cpp +++ b/models/cards/shopping_mall.cpp @@ -11,6 +11,6 @@ ShoppingMall::ShoppingMall() void ShoppingMall::OperateEffect(Player* owner, Player* dice_roller, std::vector players, - Bank* bank) + std::shared_ptr bank) { } \ No newline at end of file diff --git a/models/cards/shopping_mall.h b/models/cards/shopping_mall.h index 3b6625a..1667797 100644 --- a/models/cards/shopping_mall.h +++ b/models/cards/shopping_mall.h @@ -12,7 +12,7 @@ class ShoppingMall : public Landmark { void OperateEffect(Player* owner, Player* dice_roller, std::vector players, - Bank* bank) override; + std::shared_ptr bank) override; }; #endif \ No newline at end of file diff --git a/models/cards/stadium.cpp b/models/cards/stadium.cpp index 1f15608..741926d 100644 --- a/models/cards/stadium.cpp +++ b/models/cards/stadium.cpp @@ -15,6 +15,6 @@ Stadium::Stadium() void Stadium::OperateEffect(Player* owner, Player* dice_roller, std::vector players, - Bank* bank) + std::shared_ptr bank) { } \ No newline at end of file diff --git a/models/cards/stadium.h b/models/cards/stadium.h index 1d7ca0e..c1e56b3 100644 --- a/models/cards/stadium.h +++ b/models/cards/stadium.h @@ -12,7 +12,7 @@ class Stadium : public Building { void OperateEffect(Player* owner, Player* dice_roller, std::vector players, - Bank* bank) override; + std::shared_ptr bank) override; }; #endif \ No newline at end of file diff --git a/models/cards/train_station.cpp b/models/cards/train_station.cpp index 741b1f4..17c79b1 100644 --- a/models/cards/train_station.cpp +++ b/models/cards/train_station.cpp @@ -11,6 +11,6 @@ TrainStation::TrainStation() void TrainStation::OperateEffect(Player* owner, Player* dice_roller, std::vector players, - Bank* bank) + std::shared_ptr bank) { } \ No newline at end of file diff --git a/models/cards/train_station.h b/models/cards/train_station.h index b0079af..4fe4390 100644 --- a/models/cards/train_station.h +++ b/models/cards/train_station.h @@ -12,7 +12,7 @@ class TrainStation : public Landmark { void OperateEffect(Player* owner, Player* dice_roller, std::vector players, - Bank* bank) override; + std::shared_ptr bank) override; }; #endif \ No newline at end of file diff --git a/models/cards/tv_station.cpp b/models/cards/tv_station.cpp index 3587531..808170b 100644 --- a/models/cards/tv_station.cpp +++ b/models/cards/tv_station.cpp @@ -15,6 +15,6 @@ TvStation::TvStation() void TvStation::OperateEffect(Player* owner, Player* dice_roller, std::vector players, - Bank* bank) + std::shared_ptr bank) { } \ No newline at end of file diff --git a/models/cards/tv_station.h b/models/cards/tv_station.h index a33503e..411462c 100644 --- a/models/cards/tv_station.h +++ b/models/cards/tv_station.h @@ -12,7 +12,7 @@ class TvStation : public Building { void OperateEffect(Player* owner, Player* dice_roller, std::vector players, - Bank* bank) override; + std::shared_ptr bank) override; }; #endif \ No newline at end of file diff --git a/models/cards/wheat_field.cpp b/models/cards/wheat_field.cpp index 24d4706..09225c7 100644 --- a/models/cards/wheat_field.cpp +++ b/models/cards/wheat_field.cpp @@ -11,7 +11,7 @@ WheatField::WheatField() void WheatField::OperateEffect(Player* owner, Player* dice_roller, std::vector players, - Bank* bank) + std::shared_ptr bank) { - owner->gainCoinFromBank(*bank, 1); + owner->gainCoinFromBank(bank, 1); } \ No newline at end of file diff --git a/models/cards/wheat_field.h b/models/cards/wheat_field.h index 6319ab7..5da7f30 100644 --- a/models/cards/wheat_field.h +++ b/models/cards/wheat_field.h @@ -12,7 +12,7 @@ class WheatField : public Building { void OperateEffect(Player* owner, Player* dice_roller, std::vector players, - Bank* bank) override; + std::shared_ptr bank) override; }; #endif \ No newline at end of file diff --git a/models/events/init_game_event.cpp b/models/events/init_game_event.cpp index 73ffac2..097f644 100644 --- a/models/events/init_game_event.cpp +++ b/models/events/init_game_event.cpp @@ -8,10 +8,11 @@ InitGameEvent::InitGameEvent(std::shared_ptr util) { } -void InitGameEvent::set_bank(Bank* bank) +bool InitGameEvent::set_bank(std::shared_ptr bank) { - if (!bank) return; + if (!bank) return false; bank_balance_ = bank->totalCoin(); + return true; } void InitGameEvent::set_market(ArchitectureMarket* market) @@ -19,10 +20,9 @@ void InitGameEvent::set_market(ArchitectureMarket* market) if (!market) return; auto cards = market->cards(); - for (const auto& c : cards) + for (const auto& card : cards) { - auto& [name, buildings] = c; - market_cards_[util_->getStringFromCardName(name)] = buildings.size(); + market_cards_[util_->getStringFromCardName(card.first)] = card.second; } } diff --git a/models/events/init_game_event.h b/models/events/init_game_event.h index 705f5fb..1964a86 100644 --- a/models/events/init_game_event.h +++ b/models/events/init_game_event.h @@ -30,7 +30,7 @@ class InitGameEvent : public Event { std::string message() const override { return message_; } - void set_bank(Bank* bank); + bool set_bank(std::shared_ptr bank); int bank_balance() const { return bank_balance_; } diff --git a/models/hand.cpp b/models/hand.cpp index 01613d1..35290e8 100644 --- a/models/hand.cpp +++ b/models/hand.cpp @@ -19,7 +19,7 @@ Hand::Hand(LandmarkPtrs&& landmarks, BuildingPtrs&& buildings) { } -void Hand::gainCard(BuildingPtr card) +void Hand::gainCard(BuildingPtr&& card) { buildings_.push_back(std::move(card)); } diff --git a/models/hand.h b/models/hand.h index e061b14..9f3d160 100644 --- a/models/hand.h +++ b/models/hand.h @@ -18,7 +18,7 @@ class Hand { ~Hand() = default; - void gainCard(BuildingPtr building); + void gainCard(BuildingPtr&& building); std::vector landmarks() const; diff --git a/models/machikoro_game.cpp b/models/machikoro_game.cpp index 31cba9e..ab64d58 100644 --- a/models/machikoro_game.cpp +++ b/models/machikoro_game.cpp @@ -39,14 +39,64 @@ std::unique_ptr MachiKoroGame::initGame() { auto event = std::make_unique(util_); + if (bank_->totalCoin() != 282) + { + event->set_status(StatusCode::BadRequest); + event->set_message("Failed to init game because bank's total coin is not 282 !"); + log_->error(event->message()); + return event; + } + // Allocate money. - for (auto& player : players_) player->gainCoinFromBank(bank_, 3); + for (auto& player : players_) + { + if (!player->gainCoinFromBank(bank_, 3)) + { + event->set_status(StatusCode::BadRequest); + event->set_message("Failed to init game because bank_ is nullptr !"); + log_->error(event->message()); + return event; + } + else if (player->totalCoin() != 3) + { + event->set_status(StatusCode::BadRequest); + event->set_message("Failed to init game because player's total coin is not 3 !"); + log_->error(event->message()); + return event; + } + } // Get initial buildings. for (auto& player : players_) { - player->hand().gainCard(market_.drawCard(CardName::WHEAT_FIELD)); - player->hand().gainCard(market_.drawCard(CardName::BAKERY)); + auto wheat_field = market_.drawCard(CardName::WHEAT_FIELD); + if (!wheat_field) + { + event->set_status(StatusCode::BadRequest); + event->set_message("Failed to init game because cannot draw wheat field card !"); + log_->error(event->message()); + return event; + } + player->hand().gainCard(std::move(*wheat_field)); + + auto bakery = market_.drawCard(CardName::BAKERY); + if (!bakery) + { + event->set_status(StatusCode::BadRequest); + event->set_message("Failed to init game because cannot draw bakery card !"); + log_->error(event->message()); + return event; + } + player->hand().gainCard(std::move(*bakery)); + } + + log_->info("Bank's total coin: " + std::to_string(bank_->totalCoin())); + if (bank_->totalCoin() != 282 - 12) + { + event->set_status(StatusCode::BadRequest); + event->set_message("Failed to init game because bank's total coin is not 270 !"); + log_->error(event->message()); + return event; } // Choose one player to be the player for the first round. @@ -54,7 +104,7 @@ std::unique_ptr MachiKoroGame::initGame() event->set_status(StatusCode::Ok); event->set_message("Success to init game."); - event->set_bank(&bank_); + event->set_bank(bank_); event->set_market(&market_); event->set_players(&players_); event->set_player_name(players_[current_player_ % players_.size()]->name()); diff --git a/models/machikoro_game.h b/models/machikoro_game.h index 612ef80..aa44537 100644 --- a/models/machikoro_game.h +++ b/models/machikoro_game.h @@ -26,7 +26,7 @@ class MachiKoroGame { std::string game_id() const { return game_id_; } - Bank* bank() { return &bank_; } + std::shared_ptr bank() { return bank_; } ArchitectureMarket* market() { return &market_; } @@ -43,7 +43,7 @@ class MachiKoroGame { std::string game_id_; // Bank. - Bank bank_; + std::shared_ptr bank_ = std::make_shared(); // Architecture Market. ArchitectureMarket market_; diff --git a/models/player.cpp b/models/player.cpp index 2dafbe8..8049c80 100644 --- a/models/player.cpp +++ b/models/player.cpp @@ -15,10 +15,12 @@ void Player::payCoin2Bank(Bank& bank, int coin) bank.gainCoin(coin); } -void Player::gainCoinFromBank(Bank& bank, int coin) +bool Player::gainCoinFromBank(std::shared_ptr bank, int coin) { + if (!bank) return false; coin_ += coin; - bank.payCoin(coin); + bank->payCoin(coin); + return true; } void Player::payCoin2Player(Player* other, int coin) diff --git a/models/player.h b/models/player.h index d5e6db5..75af684 100644 --- a/models/player.h +++ b/models/player.h @@ -25,7 +25,7 @@ class Player { void payCoin2Bank(Bank& bank, int coin); - void gainCoinFromBank(Bank& bank, int coin); + bool gainCoinFromBank(std::shared_ptr bank, int coin); void payCoin2Player(Player* other, int coin); diff --git a/tests/e2e_test/e2e_main.cpp b/tests/e2e_test/e2e_main.cpp index 6d6b475..b5cf856 100644 --- a/tests/e2e_test/e2e_main.cpp +++ b/tests/e2e_test/e2e_main.cpp @@ -15,14 +15,22 @@ using namespace drogon; int main(int argc, char** argv) { + const std::string strConfig = + "{\"listeners\":[{\"address\":\"127.0.0.1\",\"port\":8086}],\"app\":{\"log\":{\"use_" + "spdlog\":true,\"log_level\":\"TRACE\"}},\"plugins\":[{\"name\":\"drogon::plugin::" + "AccessLogger\",\"config\":{\"use_spdlog\":true}}]}"; + Json::Value jsonConfig; + Json::Reader reader; + reader.parse(strConfig, jsonConfig); + std::promise p1; std::future f1 = p1.get_future(); // Start the main loop on another thread. - std::thread th1([&]() { + std::thread th1([&p1, &jsonConfig]() { // Queues the promise to be fulfilled after starting the loop. app().getLoop()->queueInLoop([&p1]() { p1.set_value(); }); - app().addListener("127.0.0.1", 8086); + app().loadConfigJson(jsonConfig); app().run(); }); diff --git a/tests/unittest/unit_operate_effect.cpp b/tests/unittest/unit_operate_effect.cpp index ddaf8b2..959fcf7 100644 --- a/tests/unittest/unit_operate_effect.cpp +++ b/tests/unittest/unit_operate_effect.cpp @@ -17,7 +17,7 @@ class OperateEffectTest: public ::testing::Test { protected: virtual void SetUp() override { // Ensure all players have enough coins. - bank = new Bank(); + bank = std::make_shared(); auto dice = std::make_shared(); card_owner = new Player("card owner", dice); dice_roller = new Player("dice roller", dice); @@ -32,8 +32,7 @@ class OperateEffectTest: public ::testing::Test { } virtual void TearDown() override { - delete bank; - bank = nullptr; + bank.reset(); delete card_owner; card_owner = nullptr; @@ -48,7 +47,7 @@ class OperateEffectTest: public ::testing::Test { player_2 = nullptr; } - Bank* bank = nullptr; + std::shared_ptr bank = nullptr; Player* card_owner = nullptr; Player* dice_roller = nullptr; Player* player_1 = nullptr;