diff --git a/CMakeLists.txt b/CMakeLists.txt index e386176..a0393f0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -81,7 +81,7 @@ target_include_directories(BillySheet target_include_directories(BillySheet INTERFACE $) set_target_properties(BillySheet PROPERTIES - CXX_STANDARD 23 + CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF INTERPROCEDURAL_OPTIMIZATION ON diff --git a/Unit testing/billy_objects_tests.cpp b/Unit testing/billy_objects_tests.cpp index d762848..d1b73ab 100644 --- a/Unit testing/billy_objects_tests.cpp +++ b/Unit testing/billy_objects_tests.cpp @@ -165,19 +165,6 @@ TEST_CASE("[D] Double erase no throw", "[pushpop][1]") { REQUIRE_FALSE(gestionnaire.erase_object(sheet, weapons::Bow)); } -TEST_CASE("[D] Insert no more than 3", "[pushpop][2]") { - BillyObjects gestionnaire{}; - CharacterSheet sheet; - REQUIRE(sheet.get_objects().empty()); - - REQUIRE_NOTHROW(gestionnaire.insert_object(sheet, weapons::Sword)); - REQUIRE(gestionnaire.insert_object(sheet, weapons::Morgenstern)); - REQUIRE(gestionnaire.insert_object(sheet, weapons::Bow)); - REQUIRE(sheet.get_objects().size() == 3); - REQUIRE_FALSE(gestionnaire.insert_object(sheet, equipments::Chainmail)); - REQUIRE(sheet.get_objects().size() == 3); -} - TEST_CASE("[D] Printing Billy's objects", "[printing]") { for (const auto &object: BillyObjects::all_objects) { const std::unique_ptr obj = test::get_obj(object); diff --git a/Unit testing/sheet_tests.cpp b/Unit testing/sheet_tests.cpp index 19814c7..39de7ec 100644 --- a/Unit testing/sheet_tests.cpp +++ b/Unit testing/sheet_tests.cpp @@ -22,7 +22,7 @@ TEST_CASE("[E] Serialize sheet", "[serialize][0]") { } TEST_CASE("[F] Deserialize sheet", "[deserialize][0]") { - const auto deserializer = [] { + const auto deserializer = []() { std::ifstream file{ "character_sheet.json" }; return json::parse(file); }(); diff --git a/include/character_sheet.hpp b/include/character_sheet.hpp index ee54cf3..09f710a 100644 --- a/include/character_sheet.hpp +++ b/include/character_sheet.hpp @@ -80,10 +80,15 @@ namespace character { [[nodiscard]] const BillyObjects::container &get_objects() const { return objects; } [[nodiscard]] classe get_current_class() const { - if (nb_weapons >= 2) { return classe::Guerrier; } - if (nb_equipments >= 2) { return classe::Prudent; } - if (nb_tools >= 2) { return classe::Paysan; } - return classe::Debrouillard; + if (nb_weapons >= 2) { + return classe::Guerrier; + } else if (nb_equipments >= 2) { + return classe::Prudent; + } else if (nb_tools >= 2) { + return classe::Paysan; + } else { + return classe::Debrouillard; + } } [[nodiscard]] std::uint32_t get_health_point() const { return health_point; } @@ -121,24 +126,24 @@ namespace character { }; static void to_json(json &j, const CharacterSheet &billy) { - j.emplace("caractere", billy.get_caractere()); - j.emplace("adresse", billy.get_adresse()); - j.emplace("endurance", billy.get_endurance()); - j.emplace("chance", billy.get_chance()); - j.emplace("habilete", billy.get_habilete()); - j.emplace("classe", billy.get_current_class()); - j.emplace("health_point", billy.get_health_point()); - j.emplace("armor", billy.get_armor()); - j.emplace("damage", billy.get_damage()); - j.emplace("glory", billy.get_glory()); - j.emplace("money", billy.get_money()); - j.emplace("nb_weapons", billy.get_nb_weapons()); - j.emplace("nb_equipments", billy.get_nb_equipments()); - j.emplace("nb_tools", billy.get_nb_tools()); + j["caractere"] = billy.get_caractere(); + j["adresse"] = billy.get_adresse(); + j["endurance"] = billy.get_endurance(); + j["chance"] = billy.get_chance(); + j["habilete"] = billy.get_habilete(); + j["classe"] = billy.get_current_class(); + j["health_point"] = billy.get_health_point(); + j["armor"] = billy.get_armor(); + j["damage"] = billy.get_damage(); + j["glory"] = billy.get_glory(); + j["money"] = billy.get_money(); + j["nb_weapons"] = billy.get_nb_weapons(); + j["nb_equipments"] = billy.get_nb_equipments(); + j["nb_tools"] = billy.get_nb_tools(); j.emplace(std::pair{ BillyObjects::json_key, json::array() }); BillyObjects::to_json(j.at(BillyObjects::json_key), billy.get_objects()); } } -#endif //BILLYSHEET_CHARACTER_SHEET_HPP +#endif //BILLYSHEET_CHARACTER_SHEET_HPP \ No newline at end of file diff --git a/src/billy_objects.cpp b/src/billy_objects.cpp index d8cba8b..2beca1f 100644 --- a/src/billy_objects.cpp +++ b/src/billy_objects.cpp @@ -46,9 +46,9 @@ namespace character { auto &local_chance = static_cast(sheet.chance); std::visit(overloaded{ - [&](const weapons &) { ++sheet.nb_weapons; }, - [&](const equipments &) { ++sheet.nb_equipments; }, - [&](const tools &) { ++sheet.nb_tools; }, + [&](const weapons &arg) { ++sheet.nb_weapons; }, + [&](const equipments &arg) { ++sheet.nb_equipments; }, + [&](const tools &arg) { ++sheet.nb_tools; }, }, objType); change_carac(object, sheet, local_habilete, local_adresse, local_endurance, local_chance, plus); @@ -99,13 +99,13 @@ namespace character { void BillyObjects::check_dagger_conditions(CharacterSheet &sheet) { if (const auto it_dagger = sheet.get_objects().find(tools::Dagger); it_dagger != sheet.get_objects().cend()) { - const std::size_t count_weapons = std::ranges::count_if(sheet.get_objects(), - [](container::const_reference node) { - return std::get_if(&node.first) != - nullptr; - } + const std::size_t count_weapons = std::count_if(sheet.get_objects().cbegin(), + sheet.get_objects().cend(), + [](container::const_reference node) { + return std::get_if(&node.first) != nullptr; + } ); - if (count_weapons > 1 || sheet.get_objects().contains(weapons::Bow)) { + if (count_weapons > 1 || sheet.get_objects().find(weapons::Bow) != sheet.get_objects().cend()) { sheet.habilete.materiel -= it_dagger->second->add_materiel(sheet.get_habilete().type); } } @@ -130,31 +130,26 @@ namespace character { case toolsHash: billy.emplace(static_cast(value), std::make_unique(static_cast(value))); break; + default: + throw std::logic_error("Wrong hash for from_json in BillyObjects::from_json"); } } } void BillyObjects::to_json(json &j, const container &billy) { - std::ranges::for_each(billy | std::views::keys, - [&j](const auto &obj) { - std::visit(overloaded{ - [&j](const weapons weapon) { - j.emplace_back(std::pair{ - weaponsHash, static_cast(weapon) - }); - }, - [&j](const equipments equipment) { - j.emplace_back(std::pair{ - equipmentHash, static_cast(equipment) - }); - }, - [&j](const tools tool) { - j.emplace_back(std::pair{ - toolsHash, static_cast(tool) - }); - } - }, - obj); - }); + for (const auto &[object_type, _]: billy) { + std::visit(overloaded{ + [&j](const weapons weapon) { + j.emplace_back(std::pair{ weaponsHash, static_cast(weapon) }); + }, + [&j](const equipments equipment) { + j.emplace_back(std::pair{ equipmentHash, static_cast(equipment) }); + }, + [&j](const tools tool) { + j.emplace_back(std::pair{ toolsHash, static_cast(tool) }); + } + }, + object_type); + } } }