Compare commits

..

3 commits

Author SHA1 Message Date
9d81562917
New test case to cover 2026-02-04 16:04:20 +01:00
27b3812725
Simplify 2026-02-04 16:04:08 +01:00
02d7895033
C++23 2026-02-04 16:03:51 +01:00
5 changed files with 64 additions and 51 deletions

View file

@ -81,7 +81,7 @@ target_include_directories(BillySheet
target_include_directories(BillySheet INTERFACE $<INSTALL_INTERFACE:include/billySheet>) target_include_directories(BillySheet INTERFACE $<INSTALL_INTERFACE:include/billySheet>)
set_target_properties(BillySheet PROPERTIES set_target_properties(BillySheet PROPERTIES
CXX_STANDARD 17 CXX_STANDARD 23
CXX_STANDARD_REQUIRED ON CXX_STANDARD_REQUIRED ON
CXX_EXTENSIONS OFF CXX_EXTENSIONS OFF
INTERPROCEDURAL_OPTIMIZATION ON INTERPROCEDURAL_OPTIMIZATION ON

View file

@ -165,6 +165,19 @@ TEST_CASE("[D] Double erase no throw", "[pushpop][1]") {
REQUIRE_FALSE(gestionnaire.erase_object(sheet, weapons::Bow)); 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]") { TEST_CASE("[D] Printing Billy's objects", "[printing]") {
for (const auto &object: BillyObjects::all_objects) { for (const auto &object: BillyObjects::all_objects) {
const std::unique_ptr<GenericObject> obj = test::get_obj(object); const std::unique_ptr<GenericObject> obj = test::get_obj(object);

View file

@ -22,7 +22,7 @@ TEST_CASE("[E] Serialize sheet", "[serialize][0]") {
} }
TEST_CASE("[F] Deserialize sheet", "[deserialize][0]") { TEST_CASE("[F] Deserialize sheet", "[deserialize][0]") {
const auto deserializer = []() { const auto deserializer = [] {
std::ifstream file{ "character_sheet.json" }; std::ifstream file{ "character_sheet.json" };
return json::parse(file); return json::parse(file);
}(); }();

View file

@ -80,16 +80,11 @@ namespace character {
[[nodiscard]] const BillyObjects::container &get_objects() const { return objects; } [[nodiscard]] const BillyObjects::container &get_objects() const { return objects; }
[[nodiscard]] classe get_current_class() const { [[nodiscard]] classe get_current_class() const {
if (nb_weapons >= 2) { if (nb_weapons >= 2) { return classe::Guerrier; }
return classe::Guerrier; if (nb_equipments >= 2) { return classe::Prudent; }
} else if (nb_equipments >= 2) { if (nb_tools >= 2) { return classe::Paysan; }
return classe::Prudent;
} else if (nb_tools >= 2) {
return classe::Paysan;
} else {
return classe::Debrouillard; return classe::Debrouillard;
} }
}
[[nodiscard]] std::uint32_t get_health_point() const { return health_point; } [[nodiscard]] std::uint32_t get_health_point() const { return health_point; }
@ -126,20 +121,20 @@ namespace character {
}; };
static void to_json(json &j, const CharacterSheet &billy) { static void to_json(json &j, const CharacterSheet &billy) {
j["caractere"] = billy.get_caractere(); j.emplace("caractere", billy.get_caractere());
j["adresse"] = billy.get_adresse(); j.emplace("adresse", billy.get_adresse());
j["endurance"] = billy.get_endurance(); j.emplace("endurance", billy.get_endurance());
j["chance"] = billy.get_chance(); j.emplace("chance", billy.get_chance());
j["habilete"] = billy.get_habilete(); j.emplace("habilete", billy.get_habilete());
j["classe"] = billy.get_current_class(); j.emplace("classe", billy.get_current_class());
j["health_point"] = billy.get_health_point(); j.emplace("health_point", billy.get_health_point());
j["armor"] = billy.get_armor(); j.emplace("armor", billy.get_armor());
j["damage"] = billy.get_damage(); j.emplace("damage", billy.get_damage());
j["glory"] = billy.get_glory(); j.emplace("glory", billy.get_glory());
j["money"] = billy.get_money(); j.emplace("money", billy.get_money());
j["nb_weapons"] = billy.get_nb_weapons(); j.emplace("nb_weapons", billy.get_nb_weapons());
j["nb_equipments"] = billy.get_nb_equipments(); j.emplace("nb_equipments", billy.get_nb_equipments());
j["nb_tools"] = billy.get_nb_tools(); j.emplace("nb_tools", billy.get_nb_tools());
j.emplace(std::pair{ BillyObjects::json_key, json::array() }); j.emplace(std::pair{ BillyObjects::json_key, json::array() });
BillyObjects::to_json(j.at(BillyObjects::json_key), billy.get_objects()); BillyObjects::to_json(j.at(BillyObjects::json_key), billy.get_objects());
} }

View file

@ -46,9 +46,9 @@ namespace character {
auto &local_chance = static_cast<Characteristic &>(sheet.chance); auto &local_chance = static_cast<Characteristic &>(sheet.chance);
std::visit(overloaded{ std::visit(overloaded{
[&](const weapons &arg) { ++sheet.nb_weapons; }, [&](const weapons &) { ++sheet.nb_weapons; },
[&](const equipments &arg) { ++sheet.nb_equipments; }, [&](const equipments &) { ++sheet.nb_equipments; },
[&](const tools &arg) { ++sheet.nb_tools; }, [&](const tools &) { ++sheet.nb_tools; },
}, },
objType); objType);
change_carac(object, sheet, local_habilete, local_adresse, local_endurance, local_chance, plus); 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) { void BillyObjects::check_dagger_conditions(CharacterSheet &sheet) {
if (const auto it_dagger = sheet.get_objects().find(tools::Dagger); it_dagger != sheet.get_objects().cend()) { if (const auto it_dagger = sheet.get_objects().find(tools::Dagger); it_dagger != sheet.get_objects().cend()) {
const std::size_t count_weapons = std::count_if(sheet.get_objects().cbegin(), const std::size_t count_weapons = std::ranges::count_if(sheet.get_objects(),
sheet.get_objects().cend(),
[](container::const_reference node) { [](container::const_reference node) {
return std::get_if<weapons>(&node.first) != nullptr; return std::get_if<weapons>(&node.first) !=
nullptr;
} }
); );
if (count_weapons > 1 || sheet.get_objects().find(weapons::Bow) != sheet.get_objects().cend()) { if (count_weapons > 1 || sheet.get_objects().contains(weapons::Bow)) {
sheet.habilete.materiel -= it_dagger->second->add_materiel(sheet.get_habilete().type); sheet.habilete.materiel -= it_dagger->second->add_materiel(sheet.get_habilete().type);
} }
} }
@ -130,26 +130,31 @@ namespace character {
case toolsHash: case toolsHash:
billy.emplace(static_cast<tools>(value), std::make_unique<Tools>(static_cast<tools>(value))); billy.emplace(static_cast<tools>(value), std::make_unique<Tools>(static_cast<tools>(value)));
break; break;
default:
throw std::logic_error("Wrong hash for from_json in BillyObjects::from_json");
} }
} }
} }
void BillyObjects::to_json(json &j, const container &billy) { void BillyObjects::to_json(json &j, const container &billy) {
for (const auto &[object_type, _]: billy) { std::ranges::for_each(billy | std::views::keys,
[&j](const auto &obj) {
std::visit(overloaded{ std::visit(overloaded{
[&j](const weapons weapon) { [&j](const weapons weapon) {
j.emplace_back(std::pair{ weaponsHash, static_cast<std::uint8_t>(weapon) }); j.emplace_back(std::pair{
weaponsHash, static_cast<std::uint8_t>(weapon)
});
}, },
[&j](const equipments equipment) { [&j](const equipments equipment) {
j.emplace_back(std::pair{ equipmentHash, static_cast<std::uint8_t>(equipment) }); j.emplace_back(std::pair{
equipmentHash, static_cast<std::uint8_t>(equipment)
});
}, },
[&j](const tools tool) { [&j](const tools tool) {
j.emplace_back(std::pair{ toolsHash, static_cast<std::uint8_t>(tool) }); j.emplace_back(std::pair{
toolsHash, static_cast<std::uint8_t>(tool)
});
} }
}, },
object_type); obj);
} });
} }
} }