Compare commits

..

No commits in common. "da1215d8274cc41576795e45b6ca4d3004e1ba7e" and "b3c59ffe0d17bbd3a09f88c2b6b4572052b62deb" have entirely different histories.

9 changed files with 106 additions and 115 deletions

View file

@ -77,8 +77,8 @@ add_library(BillySheet SHARED ${SOURCES})
target_include_directories(BillySheet target_include_directories(BillySheet
PUBLIC PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INDLUDEDIR}/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 17
@ -89,7 +89,7 @@ set_target_properties(BillySheet PROPERTIES
PUBLIC_HEADER "include/billy_objects.hpp;include/character_sheet.hpp;include/generic_object.hpp;include/characteristic.hpp" PUBLIC_HEADER "include/billy_objects.hpp;include/character_sheet.hpp;include/generic_object.hpp;include/characteristic.hpp"
) )
target_compile_definitions(BillySheet PRIVATE target_compile_definitions(BillySheet PUBLIC
$<$<CONFIG:Debug>:_GLIBCXX_DEBUG> $<$<CONFIG:Debug>:_GLIBCXX_DEBUG>
) )

View file

@ -23,17 +23,11 @@ namespace character::test {
static std::unique_ptr<GenericObject> get_obj(const billyEnums &object) noexcept { static std::unique_ptr<GenericObject> get_obj(const billyEnums &object) noexcept {
return std::visit(overloaded{ return std::visit(overloaded{
[](const weapons &arg) { [](const weapons &arg) { return std::unique_ptr<GenericObject>(std::make_unique<Weapons>(arg)); },
return std::unique_ptr<GenericObject>(std::make_unique<Weapons>(arg)); [](const equipments &arg) { return std::unique_ptr<GenericObject>(std::make_unique<Equipments>(arg)); },
}, [](const tools &arg) { return std::unique_ptr<GenericObject>(std::make_unique<Tools>(arg)); },
[](const equipments &arg) { },
return std::unique_ptr<GenericObject>(std::make_unique<Equipments>(arg)); object);
},
[](const tools &arg) {
return std::unique_ptr<GenericObject>(std::make_unique<Tools>(arg));
},
},
object);
} }
} }

View file

@ -16,7 +16,9 @@ TEST_CASE("[A] Serialize adresse", "[serialize][0]") {
REQUIRE_NOTHROW(tester.at("base") == 1); REQUIRE_NOTHROW(tester.at("base") == 1);
REQUIRE_NOTHROW(tester.at("carac") == 0); REQUIRE_NOTHROW(tester.at("carac") == 0);
REQUIRE_NOTHROW(tester.at("materiel") == 0); REQUIRE_NOTHROW(tester.at("materiel") == 0);
REQUIRE_NOTHROW(tester.at("additional") == 0); { REQUIRE_NOTHROW(tester.at("additional") == 0);
{
std::ofstream file{ "adresse.json" }; std::ofstream file{ "adresse.json" };
file << serializer << std::flush; file << serializer << std::flush;
} }
@ -33,7 +35,9 @@ TEST_CASE("[A] Serialize chance", "[serialize][1]") {
REQUIRE_NOTHROW(tester.at("base") == 3); REQUIRE_NOTHROW(tester.at("base") == 3);
REQUIRE_NOTHROW(tester.at("carac") == 0); REQUIRE_NOTHROW(tester.at("carac") == 0);
REQUIRE_NOTHROW(tester.at("materiel") == 0); REQUIRE_NOTHROW(tester.at("materiel") == 0);
REQUIRE_NOTHROW(tester.at("additional") == 0); { REQUIRE_NOTHROW(tester.at("additional") == 0);
{
std::ofstream file{ "chance.json" }; std::ofstream file{ "chance.json" };
file << serializer << std::flush; file << serializer << std::flush;
} }
@ -50,7 +54,9 @@ TEST_CASE("[A] Serialize endurance", "[serialize][2]") {
REQUIRE_NOTHROW(tester.at("base") == 2); REQUIRE_NOTHROW(tester.at("base") == 2);
REQUIRE_NOTHROW(tester.at("carac") == 0); REQUIRE_NOTHROW(tester.at("carac") == 0);
REQUIRE_NOTHROW(tester.at("materiel") == 0); REQUIRE_NOTHROW(tester.at("materiel") == 0);
REQUIRE_NOTHROW(tester.at("additional") == 0); { REQUIRE_NOTHROW(tester.at("additional") == 0);
{
std::ofstream file{ "endurance.json" }; std::ofstream file{ "endurance.json" };
file << serializer << std::flush; file << serializer << std::flush;
} }
@ -67,7 +73,9 @@ TEST_CASE("[A] Serialize habilete", "[serialize][3]") {
REQUIRE_NOTHROW(tester.at("base") == 2); REQUIRE_NOTHROW(tester.at("base") == 2);
REQUIRE_NOTHROW(tester.at("carac") == 0); REQUIRE_NOTHROW(tester.at("carac") == 0);
REQUIRE_NOTHROW(tester.at("materiel") == 0); REQUIRE_NOTHROW(tester.at("materiel") == 0);
REQUIRE_NOTHROW(tester.at("additional") == 0); { REQUIRE_NOTHROW(tester.at("additional") == 0);
{
std::ofstream file{ "habilete.json" }; std::ofstream file{ "habilete.json" };
file << serializer << std::flush; file << serializer << std::flush;
} }
@ -85,7 +93,7 @@ TEST_CASE("[B] Deserialize adresse", "[deserialize][0]") {
REQUIRE(adresse.get_carac() == 0); REQUIRE(adresse.get_carac() == 0);
REQUIRE(adresse.get_materiel() == 0); REQUIRE(adresse.get_materiel() == 0);
REQUIRE(adresse.get_additional() == 0); REQUIRE(adresse.get_additional() == 0);
}()); }());
} }
TEST_CASE("[B] Deserialize chance", "[deserialize][1]") { TEST_CASE("[B] Deserialize chance", "[deserialize][1]") {
@ -100,7 +108,7 @@ TEST_CASE("[B] Deserialize chance", "[deserialize][1]") {
REQUIRE(adresse.get_carac() == 0); REQUIRE(adresse.get_carac() == 0);
REQUIRE(adresse.get_materiel() == 0); REQUIRE(adresse.get_materiel() == 0);
REQUIRE(adresse.get_additional() == 0); REQUIRE(adresse.get_additional() == 0);
}()); }());
} }
TEST_CASE("[B] Deserialize endurance", "[deserialize][2]") { TEST_CASE("[B] Deserialize endurance", "[deserialize][2]") {
@ -115,7 +123,7 @@ TEST_CASE("[B] Deserialize endurance", "[deserialize][2]") {
REQUIRE(adresse.get_carac() == 0); REQUIRE(adresse.get_carac() == 0);
REQUIRE(adresse.get_materiel() == 0); REQUIRE(adresse.get_materiel() == 0);
REQUIRE(adresse.get_additional() == 0); REQUIRE(adresse.get_additional() == 0);
}()); }());
} }
TEST_CASE("[B] Deserialize habilete", "[deserialize][3]") { TEST_CASE("[B] Deserialize habilete", "[deserialize][3]") {
@ -130,5 +138,5 @@ TEST_CASE("[B] Deserialize habilete", "[deserialize][3]") {
REQUIRE(adresse.get_carac() == 0); REQUIRE(adresse.get_carac() == 0);
REQUIRE(adresse.get_materiel() == 0); REQUIRE(adresse.get_materiel() == 0);
REQUIRE(adresse.get_additional() == 0); REQUIRE(adresse.get_additional() == 0);
}()); }());
} }

View file

@ -66,12 +66,12 @@ namespace character {
std::minus<std::uint32_t> minus; std::minus<std::uint32_t> minus;
static void change_carac(const billyObjects &arg, static void change_carac(const billyObjects &arg,
CharacterSheet &sheet, CharacterSheet &sheet,
characteristic::Characteristic &localHabilete, characteristic::Characteristic &localHabilete,
characteristic::Characteristic &localAdresse, characteristic::Characteristic &localAdresse,
characteristic::Characteristic &localEndurance, characteristic::Characteristic &localEndurance,
characteristic::Characteristic &localChance, characteristic::Characteristic &localChance,
const std::function<std::uint32_t(std::uint32_t, std::uint32_t)> &operation) noexcept; const std::function<std::uint32_t(std::uint32_t, std::uint32_t)> &operation) noexcept;
}; };
} }

View file

@ -7,14 +7,15 @@
namespace character { namespace character {
enum class classe : std::uint8_t { enum class classe : std::uint8_t {
Guerrier = 0, Guerrier = 0,
Prudent = 1, Prudent = 1,
Paysan = 2, Paysan = 2,
Debrouillard = 3, Debrouillard = 3,
}; };
class CharacterSheet final { class CharacterSheet final {
private: private:
friend BillyObjects; friend BillyObjects;
std::string caractere{}; std::string caractere{};
@ -32,8 +33,8 @@ namespace character {
BillyObjects::container objects; BillyObjects::container objects;
std::unordered_set<billyEnums> available_objects{ std::unordered_set<billyEnums> available_objects{
BillyObjects::all_objects.cbegin(), BillyObjects::all_objects.cbegin(),
BillyObjects::all_objects.cend() BillyObjects::all_objects.cend()
}; };
std::uint32_t health_point{ 0 }; std::uint32_t health_point{ 0 };
@ -53,7 +54,6 @@ namespace character {
std::uint32_t nb_equipments{ 0 }; std::uint32_t nb_equipments{ 0 };
std::uint32_t nb_tools{ 0 }; std::uint32_t nb_tools{ 0 };
public: public:
CharacterSheet() = default; CharacterSheet() = default;
@ -146,4 +146,4 @@ namespace character {
} }
#endif //BILLYSHEET_CHARACTER_SHEET_HPP #endif //BILLYSHEET_CHARACTER_SHEET_HPP

View file

@ -1,7 +1,6 @@
#ifndef BILLYSHEET_CHARACTERISTIC_HPP #ifndef BILLYSHEET_CHARACTERISTIC_HPP
#define BILLYSHEET_CHARACTERISTIC_HPP #define BILLYSHEET_CHARACTERISTIC_HPP
#include <algorithm>
#include <cstdint> #include <cstdint>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
@ -13,10 +12,10 @@ namespace character {
namespace character::characteristic { namespace character::characteristic {
enum class characType : std::uint8_t { enum class characType : std::uint8_t {
Adresse = 0, Adresse = 0,
Endurance = 1, Endurance = 1,
Chance = 2, Chance = 2,
Habilete = 3 Habilete = 3
}; };
class Characteristic { class Characteristic {
@ -47,14 +46,14 @@ namespace character::characteristic {
Characteristic() noexcept = default; Characteristic() noexcept = default;
Characteristic(const characType inType, Characteristic(const characType inType,
const std::uint32_t carac, const std::uint32_t carac,
const std::uint32_t materiel, const std::uint32_t materiel,
const std::uint32_t additional) : const std::uint32_t additional) :
base(get_base(inType)), base(get_base(inType)),
carac(carac), carac(carac),
materiel(materiel), materiel(materiel),
additional(additional), additional(additional),
type(inType) { (void) get_total(); } type(inType) { (void) get_total(); }
explicit Characteristic(const characType inType) : Characteristic(inType, 0, 0, 0) {} explicit Characteristic(const characType inType) : Characteristic(inType, 0, 0, 0) {}
@ -83,7 +82,9 @@ namespace character::characteristic {
[[nodiscard]] std::size_t get_total() const noexcept { [[nodiscard]] std::size_t get_total() const noexcept {
if (total == defaultValue::max()) { if (total == defaultValue::max()) {
total = static_cast<std::int32_t>(base + carac + materiel + additional); total = static_cast<std::int32_t>(base + carac + materiel + additional);
total = std::max(total, 0); if (total < 0) {
total = 0;
}
} }
return total; return total;
} }
@ -109,4 +110,4 @@ namespace character::characteristic {
} }
#endif //BILLYSHEET_CHARACTERISTIC_HPP #endif //BILLYSHEET_CHARACTERISTIC_HPP

View file

@ -12,10 +12,7 @@
// helper type for the visitor // helper type for the visitor
template<typename... Ts> template<typename... Ts>
struct overloaded : Ts... { struct overloaded : Ts ... { using Ts::operator()...; };
using Ts::operator()...;
};
template<typename... Ts> template<typename... Ts>
overloaded(Ts...) -> overloaded<Ts...>; overloaded(Ts...) -> overloaded<Ts...>;
@ -27,24 +24,24 @@ namespace character {
using namespace std::string_literals; using namespace std::string_literals;
enum class weapons : std::uint8_t { enum class weapons : std::uint8_t {
Sword = 0, Sword = 0,
Lance = 1, Lance = 1,
Morgenstern = 2, Morgenstern = 2,
Bow = 3 Bow = 3
}; };
enum class equipments : std::uint8_t { enum class equipments : std::uint8_t {
Chainmail = 4, Chainmail = 4,
CookingPot = 5, CookingPot = 5,
PamphletTourist = 6, PamphletTourist = 6,
MedicKit = 7 MedicKit = 7
}; };
enum class tools : std::uint8_t { enum class tools : std::uint8_t {
Fourche = 8, Fourche = 8,
Dagger = 9, Dagger = 9,
RockClimbingKit = 10, RockClimbingKit = 10,
SackOfGrain = 11 SackOfGrain = 11
}; };
class Weapons; class Weapons;
@ -142,6 +139,7 @@ namespace character {
[[nodiscard]] billyEnums get_type() const noexcept final { return type; } [[nodiscard]] billyEnums get_type() const noexcept final { return type; }
}; };
} // character } // character
#endif //BILLYSHEET_GENERIC_OBJECT_HPP #endif //BILLYSHEET_GENERIC_OBJECT_HPP

View file

@ -1,7 +1,6 @@
#include "billy_objects.hpp" #include "billy_objects.hpp"
#include "characteristic.hpp" #include "characteristic.hpp"
#include "character_sheet.hpp" #include "character_sheet.hpp"
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
constexpr std::uint32_t const_hash(const char *input) { constexpr std::uint32_t const_hash(const char *input) {
@ -19,24 +18,16 @@ namespace character {
bool BillyObjects::insert_object(CharacterSheet &sheet, const billyEnums objType) noexcept { bool BillyObjects::insert_object(CharacterSheet &sheet, const billyEnums objType) noexcept {
if (sheet.objects.size() < 3) { if (sheet.objects.size() < 3) {
sheet.objects.emplace(objType, sheet.objects.emplace(objType,
std::visit(overloaded{ std::visit(overloaded{
[](const weapons &arg) { [](const weapons &arg) {
return std::unique_ptr<GenericObject>( return std::unique_ptr<GenericObject>(std::make_unique<Weapons>(arg));
std::make_unique<Weapons>(arg) },
); [](const equipments &arg) {
}, return std::unique_ptr<GenericObject>(std::make_unique<Equipments>(arg));
[](const equipments &arg) { },
return std::unique_ptr<GenericObject>( [](const tools &arg) { return std::unique_ptr<GenericObject>(std::make_unique<Tools>(arg)); },
std::make_unique<Equipments>(arg) },
); objType));
},
[](const tools &arg) {
return std::unique_ptr<GenericObject>(
std::make_unique<Tools>(arg)
);
},
},
objType));
const auto &object = sheet.objects[objType]; const auto &object = sheet.objects[objType];
sheet.available_objects.erase(objType); sheet.available_objects.erase(objType);
@ -46,11 +37,11 @@ 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 &arg) { ++sheet.nb_weapons; },
[&](const equipments &arg) { ++sheet.nb_equipments; }, [&](const equipments &arg) { ++sheet.nb_equipments; },
[&](const tools &arg) { ++sheet.nb_tools; }, [&](const tools &arg) { ++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);
return true; return true;
} }
@ -69,23 +60,22 @@ 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 &arg) { --sheet.nb_weapons; },
[&](const equipments &arg) { --sheet.nb_equipments; }, [&](const equipments &arg) { --sheet.nb_equipments; },
[&](const tools &arg) { --sheet.nb_tools; } [&](const tools &arg) { --sheet.nb_tools; }
}, },
objToErase); objToErase);
change_carac(obj, sheet, local_habilete, local_adresse, local_endurance, local_chance, minus); change_carac(obj, sheet, local_habilete, local_adresse, local_endurance, local_chance, minus);
} }
} }
void BillyObjects::change_carac(const billyObjects &arg, void BillyObjects::change_carac(const billyObjects &arg,
CharacterSheet &sheet, CharacterSheet &sheet,
Characteristic &localHabilete, Characteristic &localHabilete,
Characteristic &localAdresse, Characteristic &localAdresse,
Characteristic &localEndurance, Characteristic &localEndurance,
Characteristic &localChance, Characteristic &localChance,
const std::function<std::uint32_t(std::uint32_t, std::uint32_t)> &operation) const std::function<std::uint32_t(std::uint32_t, std::uint32_t)> &operation) noexcept {
noexcept {
localHabilete.materiel = operation(localHabilete.materiel, arg->add_materiel(localHabilete.type)); localHabilete.materiel = operation(localHabilete.materiel, arg->add_materiel(localHabilete.type));
localAdresse.materiel = operation(localAdresse.materiel, arg->add_materiel(localAdresse.type)); localAdresse.materiel = operation(localAdresse.materiel, arg->add_materiel(localAdresse.type));
localEndurance.materiel = operation(localEndurance.materiel, arg->add_materiel(localEndurance.type)); localEndurance.materiel = operation(localEndurance.materiel, arg->add_materiel(localEndurance.type));
@ -98,10 +88,10 @@ 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::count_if(sheet.get_objects().cbegin(),
sheet.get_objects().cend(), 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 (const bool is_there_bow = sheet.get_objects().find(weapons::Bow) != sheet.get_objects().cend(); if (const bool is_there_bow = sheet.get_objects().find(weapons::Bow) != sheet.get_objects().cend();
count_weapons > 1 || is_there_bow) { count_weapons > 1 || is_there_bow) {
@ -124,7 +114,7 @@ namespace character {
break; break;
case equipmentHash: case equipmentHash:
billy.emplace(static_cast<equipments>(value), billy.emplace(static_cast<equipments>(value),
std::make_unique<Equipments>(static_cast<equipments>(value))); std::make_unique<Equipments>(static_cast<equipments>(value)));
break; break;
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)));
@ -138,17 +128,17 @@ namespace character {
void BillyObjects::to_json(json &j, const container &billy) { void BillyObjects::to_json(json &j, const container &billy) {
for (const auto &[object_type, _]: billy) { for (const auto &[object_type, _]: billy) {
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); object_type);
} }
} }
} }

View file

@ -166,4 +166,4 @@ namespace character {
return sackOfGrain; return sackOfGrain;
} }
} }
} }