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>)
set_target_properties(BillySheet PROPERTIES
CXX_STANDARD 17
CXX_STANDARD 23
CXX_STANDARD_REQUIRED ON
CXX_EXTENSIONS OFF
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));
}
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<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]") {
const auto deserializer = []() {
const auto deserializer = [] {
std::ifstream file{ "character_sheet.json" };
return json::parse(file);
}();

View file

@ -80,15 +80,10 @@ namespace character {
[[nodiscard]] const BillyObjects::container &get_objects() const { return objects; }
[[nodiscard]] classe get_current_class() const {
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;
}
if (nb_weapons >= 2) { return classe::Guerrier; }
if (nb_equipments >= 2) { return classe::Prudent; }
if (nb_tools >= 2) { return classe::Paysan; }
return classe::Debrouillard;
}
[[nodiscard]] std::uint32_t get_health_point() const { return health_point; }
@ -126,24 +121,24 @@ namespace character {
};
static void to_json(json &j, const CharacterSheet &billy) {
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("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.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

View file

@ -46,9 +46,9 @@ namespace character {
auto &local_chance = static_cast<Characteristic &>(sheet.chance);
std::visit(overloaded{
[&](const weapons &arg) { ++sheet.nb_weapons; },
[&](const equipments &arg) { ++sheet.nb_equipments; },
[&](const tools &arg) { ++sheet.nb_tools; },
[&](const weapons &) { ++sheet.nb_weapons; },
[&](const equipments &) { ++sheet.nb_equipments; },
[&](const tools &) { ++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::count_if(sheet.get_objects().cbegin(),
sheet.get_objects().cend(),
[](container::const_reference node) {
return std::get_if<weapons>(&node.first) != nullptr;
}
const std::size_t count_weapons = std::ranges::count_if(sheet.get_objects(),
[](container::const_reference node) {
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);
}
}
@ -130,26 +130,31 @@ namespace character {
case toolsHash:
billy.emplace(static_cast<tools>(value), std::make_unique<Tools>(static_cast<tools>(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) {
for (const auto &[object_type, _]: billy) {
std::visit(overloaded{
[&j](const weapons weapon) {
j.emplace_back(std::pair{ weaponsHash, static_cast<std::uint8_t>(weapon) });
},
[&j](const equipments equipment) {
j.emplace_back(std::pair{ equipmentHash, static_cast<std::uint8_t>(equipment) });
},
[&j](const tools tool) {
j.emplace_back(std::pair{ toolsHash, static_cast<std::uint8_t>(tool) });
}
},
object_type);
}
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<std::uint8_t>(weapon)
});
},
[&j](const equipments equipment) {
j.emplace_back(std::pair{
equipmentHash, static_cast<std::uint8_t>(equipment)
});
},
[&j](const tools tool) {
j.emplace_back(std::pair{
toolsHash, static_cast<std::uint8_t>(tool)
});
}
},
obj);
});
}
}