diff --git a/include/billy_objects.hpp b/include/billy_objects.hpp index 1d47850..8e3125d 100644 --- a/include/billy_objects.hpp +++ b/include/billy_objects.hpp @@ -95,7 +95,11 @@ namespace character { void pop_object(CharacterSheet &sheet) noexcept; -// void insert_weapon(weapons weapon, CharacterSheet &sheet) noexcept; + [[nodiscard]] static ankerl::svector check_conformity(const CharacterSheet &sheet) noexcept; + + [[nodiscard]] const std::plus &get_plus_operation() const { return plus; } + + [[nodiscard]] const std::minus &get_minus_operation() const { return minus; } private: std::plus plus; @@ -124,6 +128,10 @@ namespace character { characteristic::Characteristic &localEndurance, characteristic::Characteristic &localChance, const std::function &operation) noexcept; + + static void check_dagger_conditions(const CharacterSheet &sheet, + characteristic::Characteristic &localHabilete, + const std::function &operation); }; } diff --git a/include/character_sheet.hpp b/include/character_sheet.hpp index b043f0a..339a97c 100644 --- a/include/character_sheet.hpp +++ b/include/character_sheet.hpp @@ -1,18 +1,10 @@ #ifndef BILLYSHEET_CHARACTER_SHEET_HPP #define BILLYSHEET_CHARACTER_SHEET_HPP -#include "characteristic/adresse.hpp" -#include "characteristic/endurance.hpp" -#include "characteristic/chance.hpp" -#include "characteristic/habilete.hpp" +#include "characteristic/characteristic.hpp" #include "billy_objects.hpp" -#include #include -namespace gui { - class Gui; -} - namespace character { enum class classe : std::uint8_t { Guerrier = 0, @@ -23,23 +15,20 @@ namespace character { class CharacterSheet final { private: - friend gui::Gui; friend BillyObjects; - std::mt19937_64 engine{ std::random_device{ "rdseed" }() }; - std::string caractere{}; //TODO: bad design pour les caractéristiques. Je dois trouver autre chose. - characteristic::Adresse adresse; + characteristic::Characteristic adresse{ characteristic::characType::Adresse }; - characteristic::Endurance endurance; + characteristic::Characteristic endurance{ characteristic::characType::Endurance }; - characteristic::Chance chance; + characteristic::Characteristic chance{ characteristic::characType::Chance }; - characteristic::Habilete habilete; + characteristic::Characteristic habilete{ characteristic::characType::Habilete }; BillyObjects::container objects; @@ -80,13 +69,13 @@ namespace character { [[nodiscard]] const std::string &get_caractere() const { return caractere; } - [[nodiscard]] const characteristic::Adresse &get_adresse() const { return adresse; } + [[nodiscard]] const characteristic::Characteristic &get_adresse() const { return adresse; } - [[nodiscard]] const characteristic::Endurance &get_endurance() const { return endurance; } + [[nodiscard]] const characteristic::Characteristic &get_endurance() const { return endurance; } - [[nodiscard]] const characteristic::Chance &get_chance() const { return chance; } + [[nodiscard]] const characteristic::Characteristic &get_chance() const { return chance; } - [[nodiscard]] const characteristic::Habilete &get_habilete() const { return habilete; } + [[nodiscard]] const characteristic::Characteristic &get_habilete() const { return habilete; } [[nodiscard]] const BillyObjects::container &get_objects() const { return objects; } diff --git a/include/characteristic/adresse.hpp b/include/characteristic/adresse.hpp deleted file mode 100644 index dbb1ce9..0000000 --- a/include/characteristic/adresse.hpp +++ /dev/null @@ -1,30 +0,0 @@ -#ifndef BILLYSHEET_ADRESSE_HPP -#define BILLYSHEET_ADRESSE_HPP - -#include "characteristic.hpp" -#include - -using json = nlohmann::json; - -namespace character::characteristic { - class Adresse final : public Characteristic { - public: - Adresse() noexcept: Characteristic(1, 0, 0, 0) {} - - Adresse(const std::uint32_t carac, const std::uint32_t materiel, const std::uint32_t additional) : - Characteristic(1, carac, materiel, additional) {} - - ~Adresse() noexcept final = default; - - friend void from_json(const json &j, Adresse &adresse) { - nlohmann::from_json(j, static_cast(adresse)); - } - }; - - static void to_json(json &j, const Adresse &adresse) { - to_json(j, static_cast(adresse)); - } -} - - -#endif //BILLYSHEET_ADRESSE_HPP diff --git a/include/characteristic/chance.hpp b/include/characteristic/chance.hpp deleted file mode 100644 index aed65ee..0000000 --- a/include/characteristic/chance.hpp +++ /dev/null @@ -1,27 +0,0 @@ -#ifndef BILLYSHEET_CHANCE_HPP -#define BILLYSHEET_CHANCE_HPP - -#include "characteristic.hpp" - -namespace character::characteristic { - class Chance final : public Characteristic { - public: - Chance() noexcept: Characteristic(3, 0, 0, 0) {} - - Chance(const std::uint32_t carac, const std::uint32_t materiel, const std::uint32_t additional) : - Characteristic(3, carac, materiel, additional) {} - - ~Chance() noexcept final = default; - - friend void from_json(const json &j, Chance &chance) { - nlohmann::from_json(j, static_cast(chance)); - } - }; - - static void to_json(json &j, const Chance &chance) { - to_json(j, static_cast(chance)); - } -} - - -#endif //BILLYSHEET_CHANCE_HPP diff --git a/include/characteristic/characteristic.hpp b/include/characteristic/characteristic.hpp index d41000f..11d6a27 100644 --- a/include/characteristic/characteristic.hpp +++ b/include/characteristic/characteristic.hpp @@ -11,6 +11,13 @@ namespace character { } namespace character::characteristic { + enum class characType : std::uint8_t { + Adresse = 0, + Endurance = 1, + Chance = 2, + Habilete = 3 + }; + class Characteristic { protected: friend character::BillyObjects; @@ -21,19 +28,40 @@ namespace character::characteristic { std::uint32_t materiel{ 0 }; std::uint32_t additional{ 0 }; + static std::uint32_t get_base(const characType inType) noexcept { + switch (inType) { + case characType::Adresse: + return 1; + case characType::Chance: + return 3; + case characType::Endurance: + case characType::Habilete: + return 2; + } + } + public: + const characType type{ characType::Adresse }; + Characteristic() noexcept = default; - Characteristic(const std::uint32_t base, + Characteristic(const characType inType, const std::uint32_t carac, const std::uint32_t materiel, - const std::uint32_t additional) noexcept: - base(base), carac(carac), materiel(materiel), additional(additional) { (void) get_total(); } + const std::uint32_t additional) : + base(get_base(inType)), + carac(carac), + materiel(materiel), + additional(additional), + type(inType) { (void) get_total(); } + + explicit Characteristic(const characType inType) : Characteristic(inType, 0, 0, 0) {} Characteristic(const Characteristic &charac) noexcept = default; Characteristic &operator=(const Characteristic &charac) noexcept { const_cast(base) = charac.base; + const_cast(type) = charac.type; carac = charac.carac; materiel = charac.materiel; additional = charac.additional; @@ -63,15 +91,17 @@ namespace character::characteristic { friend void from_json(const json &j, Characteristic &charac) { const_cast(charac.base) = j.at("base").get(); + const_cast(charac.type) = static_cast(j.at("type").get()); charac.carac = j.at("carac").get(); charac.materiel = j.at("materiel").get(); charac.additional = j.at("additional").get(); - charac.total = j.at("total").get(); + charac.total = j.at("total").get(); } }; static void to_json(json &j, const Characteristic &charac) { j["base"] = charac.get_base(); + j["type"] = charac.type; j["carac"] = charac.get_carac(); j["materiel"] = charac.get_materiel(); j["additional"] = charac.get_additional(); diff --git a/include/characteristic/endurance.hpp b/include/characteristic/endurance.hpp deleted file mode 100644 index 91ad534..0000000 --- a/include/characteristic/endurance.hpp +++ /dev/null @@ -1,32 +0,0 @@ -#ifndef BILLYSHEET_ENDURANCE_HPP -#define BILLYSHEET_ENDURANCE_HPP - -#include "characteristic.hpp" -#include - -using json = nlohmann::json; - -namespace character::characteristic { - class Endurance final : public Characteristic { - public: - Endurance() noexcept: Characteristic(2, 0, 0, 0) {}; - - Endurance(const std::uint32_t carac, const std::uint32_t materiel, const std::uint32_t additional) noexcept: - Characteristic(2, carac, materiel, additional) {} - - ~Endurance() noexcept final = default; - - [[nodiscard]] std::uint32_t get_max_lp() const noexcept; - - friend void from_json(const json &j, Endurance &endurance) { - nlohmann::from_json(j, static_cast(endurance)); - } - }; - - static void to_json(json &j, const Endurance &endurance) { - to_json(j, static_cast(endurance)); - } -} - - -#endif //BILLYSHEET_ENDURANCE_HPP diff --git a/include/characteristic/habilete.hpp b/include/characteristic/habilete.hpp deleted file mode 100644 index ef5d4ad..0000000 --- a/include/characteristic/habilete.hpp +++ /dev/null @@ -1,28 +0,0 @@ -#ifndef BILLYSHEET_HABILETE_HPP -#define BILLYSHEET_HABILETE_HPP - -#include -#include "characteristic.hpp" - -namespace character::characteristic { - class Habilete final : public Characteristic { - public: - Habilete() noexcept: Characteristic(2, 0, 0, 0) {}; - - Habilete(const uint32_t carac, const uint32_t materiel, const uint32_t additional) noexcept: - Characteristic(2, carac, materiel, additional) {} - - ~Habilete() noexcept final = default; - - friend void from_json(const json &j, Habilete &habilete) { - nlohmann::from_json(j, static_cast(habilete)); - } - }; - - static void to_json(json &j, const Habilete &habilete) { - to_json(j, static_cast(habilete)); - } -} - - -#endif //BILLYSHEET_HABILETE_HPP diff --git a/include/generic_object.hpp b/include/generic_object.hpp new file mode 100644 index 0000000..298985e --- /dev/null +++ b/include/generic_object.hpp @@ -0,0 +1,144 @@ +// +// Created by postaron on 01/09/24. +// + +#ifndef BILLYSHEET_GENERIC_OBJECT_HPP +#define BILLYSHEET_GENERIC_OBJECT_HPP + +#include +#include +#include +#include +#include "characteristic/characteristic.hpp" + +// helper type for the visitor +template +struct overloaded : Ts ... { using Ts::operator()...; }; +template +overloaded(Ts...) -> overloaded; + +namespace character { + using namespace std::string_literals; + + enum class weapons : std::uint8_t { + Sword = 0, + Lance = 1, + Morgenstern = 2, + Bow = 3 + }; + + enum class equipments : std::uint8_t { + Chainmail = 4, + CookingPot = 5, + PamphletTourist = 6, + MedicKit = 7 + }; + + enum class tools : std::uint8_t { + Fourche = 8, + Dagger = 9, + RockClimbingKit = 10, + SackOfGrain = 11 + }; + + class Weapons; + + class Equipments; + + class Tools; + + using billyObjects = std::variant; + using billyEnums = std::variant; + + static billyObjects new_object(const billyEnums &inputObject); + + class GenericObject { + public: + virtual ~GenericObject() = default; + + [[nodiscard]] virtual std::uint32_t add_armor() const noexcept = 0; + + [[nodiscard]] virtual std::uint32_t add_critique() const noexcept = 0; + + [[nodiscard]] virtual std::uint32_t add_damage() const noexcept = 0; + + [[nodiscard]] virtual std::uint32_t add_materiel(const characteristic::characType inType) const noexcept = 0; + + [[nodiscard]] virtual std::string_view to_string() const noexcept = 0; + }; + + class Weapons final : virtual public GenericObject { + public: + const weapons type{ weapons::Sword }; + + friend billyObjects new_object(const billyEnums &inputObject); + + Weapons() = delete; + + ~Weapons() final = default; + + [[nodiscard]] std::uint32_t add_armor() const noexcept final; + + [[nodiscard]] std::uint32_t add_critique() const noexcept final; + + [[nodiscard]] std::uint32_t add_damage() const noexcept final; + + [[nodiscard]] std::uint32_t add_materiel(const characteristic::characType inType) const noexcept final; + + [[nodiscard]] std::string_view to_string() const noexcept override; + + private: + explicit Weapons(const weapons type) : type(type) {} + }; + + class Equipments final : virtual public GenericObject { + public: + const equipments type{ equipments::Chainmail }; + + friend billyObjects new_object(const billyEnums &inputObject); + + Equipments() = delete; + + ~Equipments() final = default; + + [[nodiscard]] std::uint32_t add_armor() const noexcept final; + + [[nodiscard]] std::uint32_t add_critique() const noexcept final; + + [[nodiscard]] std::uint32_t add_damage() const noexcept final; + + [[nodiscard]] std::uint32_t add_materiel(const characteristic::characType inType) const noexcept final; + + [[nodiscard]] std::string_view to_string() const noexcept override; + + private: + explicit Equipments(const equipments type) : type(type) {} + }; + + class Tools final : virtual public GenericObject { + public: + const tools type{ tools::Fourche }; + + friend billyObjects new_object(const billyEnums &inputObject); + + Tools() = delete; + + ~Tools() final = default; + + [[nodiscard]] std::uint32_t add_armor() const noexcept final; + + [[nodiscard]] std::uint32_t add_critique() const noexcept final; + + [[nodiscard]] std::uint32_t add_damage() const noexcept final; + + [[nodiscard]] std::uint32_t add_materiel(const characteristic::characType inType) const noexcept final; + + [[nodiscard]] std::string_view to_string() const noexcept override; + + private: + explicit Tools(const tools type) : type(type) {} + }; + +} // character + +#endif //BILLYSHEET_GENERIC_OBJECT_HPP diff --git a/src/billy_objects.cpp b/src/billy_objects.cpp index a08be7e..b4488e8 100644 --- a/src/billy_objects.cpp +++ b/src/billy_objects.cpp @@ -179,6 +179,24 @@ namespace character { } } + void BillyObjects::check_dagger_conditions(const CharacterSheet &sheet, + Characteristic &localHabilete, + const std::function &operation) { + int count_weapons = 0; + bool is_there_bow = false; + std::for_each(sheet.objects.cbegin(), sheet.objects.cend(), [&](const billyObject &object) -> void { + if (const weapons *p = std::get_if(std::addressof(object)); p != nullptr) { + ++count_weapons; + if (*p == weapons::Bow) { + is_there_bow = true; + } + } + }); + if (count_weapons < 2 && !is_there_bow) { + localHabilete.materiel = operation(localHabilete.materiel, 1); + } + } + std::string_view BillyObjects::billy_object_to_string(const billyObject &object) noexcept { return std::visit(overloaded{ [](const weapons &arg) { @@ -253,4 +271,24 @@ namespace character { }, object); } } + + ankerl::svector BillyObjects::check_conformity(const CharacterSheet &sheet) noexcept { + ankerl::svector output; + std::transform(sheet.get_objects().cbegin(), + sheet.get_objects().cend(), std::back_inserter(output), + [&sheet](const billyObject &object) -> bool { + return std::visit(overloaded{ + [](const weapons weapon) { return false; }, + [](const equipments equipment) { return false; }, + [](const tools tool) { return false; }, + }, object); + }); + const int total = std::accumulate(sheet.get_objects().cbegin(), + sheet.get_objects().cend(), + 0, + [](const int a, const billyObject &object) -> int { + return 0; + }); + return output; + } } \ No newline at end of file diff --git a/src/generic_object.cpp b/src/generic_object.cpp new file mode 100644 index 0000000..7d21f4b --- /dev/null +++ b/src/generic_object.cpp @@ -0,0 +1,170 @@ +// +// Created by postaron on 01/09/24. +// + +#include "generic_object.hpp" + +namespace character { + using characteristic::characType; + + static constexpr std::string_view jsonKey{ "billy_objects" }; + + static constexpr std::string_view sword{ "Sword" }; + static constexpr std::string_view lance{ "Lance" }; + static constexpr std::string_view morgenstern{ "Morgenstern" }; + static constexpr std::string_view bow{ "Bow" }; + + static constexpr std::string_view chainmail{ "Chainmail" }; + static constexpr std::string_view cookingPot{ "Cooking pot" }; + static constexpr std::string_view pamphletTourist{ "Touristic pamphlet" }; + static constexpr std::string_view medicKit{ "Medic kit" }; + + static constexpr std::string_view fourche{ "Fourche" }; + static constexpr std::string_view dagger{ "Dagger" }; + static constexpr std::string_view rockClimbingKit{ "Rock climbing kit" }; + static constexpr std::string_view sackOfGrain{ "Sack of grain" }; + + std::uint32_t Weapons::add_armor() const noexcept { return 0; } + + std::uint32_t Weapons::add_critique() const noexcept { return 0; } + + std::uint32_t Weapons::add_damage() const noexcept { return type == weapons::Morgenstern ? 1 : 0; } + + std::uint32_t Weapons::add_materiel(const characteristic::characType inType) const noexcept { + switch (type) { + case weapons::Sword: + if (inType == characType::Habilete) { + return 4; + } else { + return 0; + } + case weapons::Lance: + switch (inType) { + case characType::Adresse: + return 1; + case characType::Habilete: + return 3; + default: + return 0; + } + case weapons::Morgenstern: + if (inType == characType::Endurance || inType == characType::Habilete) { + return 1; + } else { + return 0; + } + case weapons::Bow: + switch (inType) { + case characType::Habilete: + return 3; + case characType::Adresse: + return 1; + default: + return 0; + } + } + } + + std::string_view Weapons::to_string() const noexcept { + switch (type) { + case weapons::Sword: + return sword; + case weapons::Lance: + return lance; + case weapons::Morgenstern: + return morgenstern; + case weapons::Bow: + return bow; + } + } + + std::uint32_t Equipments::add_armor() const noexcept { + switch (type) { + case equipments::Chainmail: + return 2; + case equipments::CookingPot: + return 1; + default: + return 0; + } + } + + std::uint32_t Equipments::add_critique() const noexcept { return 0; } + + std::uint32_t Equipments::add_damage() const noexcept { return 0; } + + std::uint32_t Equipments::add_materiel(const characteristic::characType inType) const noexcept { + switch (type) { + case equipments::Chainmail: + return inType == characType::Habilete || + inType == characType::Adresse || + inType == characType::Endurance ? 1 : 0; + case equipments::CookingPot: + return inType == characType::Endurance ? 2 : 0; + case equipments::PamphletTourist: + return inType == characType::Chance ? 4 : 0; + case equipments::MedicKit: + return inType == characType::Chance ? 1 : 0; + } + } + + std::string_view Equipments::to_string() const noexcept { + switch (type) { + case equipments::Chainmail: + return chainmail; + case equipments::CookingPot: + return cookingPot; + case equipments::PamphletTourist: + return pamphletTourist; + case equipments::MedicKit: + return medicKit; + } + } + + std::uint32_t Tools::add_armor() const noexcept { return 0; } + + std::uint32_t Tools::add_critique() const noexcept { return type == tools::Dagger ? 6 : 0; } + + std::uint32_t Tools::add_damage() const noexcept { return 0; } + + std::uint32_t Tools::add_materiel(const characteristic::characType inType) const noexcept { + switch (type) { + case tools::Fourche: + switch (inType) { + case characType::Habilete: + return 1; + case characType::Endurance: + return 3; + default: + return 0; + } + case tools::Dagger: + return 0; + case tools::RockClimbingKit: + return inType == characType::Adresse ? 1 : 0; + case tools::SackOfGrain: + return inType == characType::Endurance || inType == characType::Chance ? 2 : 0; + } + } + + std::string_view Tools::to_string() const noexcept { + switch (type) { + case tools::Fourche: + return fourche; + case tools::Dagger: + return dagger; + case tools::RockClimbingKit: + return rockClimbingKit; + case tools::SackOfGrain: + return sackOfGrain; + } + } + + billyObjects new_object(const billyEnums &inputObject) { + return std::visit(overloaded{ + [](const weapons input) { return billyObjects{ Weapons{ input }}; }, + [](const equipments input) { return billyObjects{ Equipments{ input }}; }, + [](const tools input) { return billyObjects{ Tools{ input }}; } + }, inputObject); + } +}