Correct code, I suppose. It compiles.

This commit is contained in:
Pcornat 2024-10-30 17:04:21 +01:00
parent 965d0c1a35
commit 40f692ea5e
Signed by: Pcornat
GPG Key ID: E0326CC678A00BDD
2 changed files with 69 additions and 282 deletions

View File

@ -10,46 +10,21 @@
#include <functional> #include <functional>
#include <variant> #include <variant>
#include <string_view> #include <string_view>
#include <unordered_map>
#include <ankerl/svector.h> #include <ankerl/svector.h>
#include "characteristic/characteristic.hpp" #include "characteristic/characteristic.hpp"
#include "generic_object.hpp"
// helper type for the visitor
template<typename... Ts>
struct overloaded : Ts ... { using Ts::operator()...; };
template<typename... Ts>
overloaded(Ts...) -> overloaded<Ts...>;
namespace character { namespace character {
class CharacterSheet; class CharacterSheet;
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 BillyObjects final { class BillyObjects final {
public: public:
static constexpr std::size_t max_num_obj{ 3 }; static constexpr std::size_t max_num_obj{ 3 };
using billyObject = std::variant<weapons, equipments, tools>; using container = std::unordered_map<billyEnums, billyObjects>;
using container = ankerl::svector<billyObject, max_num_obj>;
static constexpr std::array<BillyObjects::billyObject, 12> all_objects{ static constexpr std::array<billyEnums, 12> all_objects{
weapons::Sword, weapons::Sword,
weapons::Lance, weapons::Lance,
weapons::Morgenstern, weapons::Morgenstern,
@ -66,22 +41,7 @@ namespace character {
static constexpr std::string_view json_key{ "billy_objects" }; static constexpr std::string_view json_key{ "billy_objects" };
static constexpr std::string_view sword{ "Sword" }; static std::string_view billy_object_to_string(const billyObjects &object) noexcept;
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 cooking_pot{ "Cooking pot" };
static constexpr std::string_view pamphlet_tourist{ "Touristic pamphlet" };
static constexpr std::string_view medic_kit{ "Medic kit" };
static constexpr std::string_view fourche{ "Fourche" };
static constexpr std::string_view dagger{ "Dagger" };
static constexpr std::string_view rock_climbing_kit{ "Rock climbing kit" };
static constexpr std::string_view sack_of_grain{ "Sack of grain" };
static std::string_view billy_object_to_string(const billyObject &object) noexcept;
static void to_json(json &j, const BillyObjects::container &billy); static void to_json(json &j, const BillyObjects::container &billy);
@ -91,47 +51,30 @@ namespace character {
~BillyObjects() noexcept = default; ~BillyObjects() noexcept = default;
[[nodiscard]] bool push_object(const billyObject &object, CharacterSheet &sheet) noexcept; [[nodiscard]] bool insert_object(CharacterSheet &sheet, const billyEnums objType) noexcept;
void pop_object(CharacterSheet &sheet) noexcept; void erase_object(CharacterSheet &sheet, const billyEnums objToErase) noexcept;
[[nodiscard]] static ankerl::svector<bool, 3> check_conformity(const CharacterSheet &sheet) noexcept;
[[nodiscard]] const std::plus<std::uint32_t> &get_plus_operation() const { return plus; } [[nodiscard]] const std::plus<std::uint32_t> &get_plus_operation() const { return plus; }
[[nodiscard]] const std::minus<std::uint32_t> &get_minus_operation() const { return minus; } [[nodiscard]] const std::minus<std::uint32_t> &get_minus_operation() const { return minus; }
private:
std::plus<std::uint32_t> plus;
std::minus<std::uint32_t> minus;
static void change_carac_weapon(const weapons &arg,
CharacterSheet &sheet,
characteristic::Characteristic &localHabilete,
characteristic::Characteristic &localAdresse,
characteristic::Characteristic &localEndurance,
const std::function<std::uint32_t(std::uint32_t, std::uint32_t)> &operation) noexcept;
static void change_carac_equipment(const equipments &arg,
CharacterSheet &sheet,
characteristic::Characteristic &localHabilete,
characteristic::Characteristic &localAdresse,
characteristic::Characteristic &localEndurance,
characteristic::Characteristic &localChance,
const std::function<std::uint32_t(std::uint32_t, std::uint32_t)> &primary,
const std::function<std::uint32_t(std::uint32_t, std::uint32_t)> &complement) noexcept;
static void change_carac_tools(const tools &arg,
CharacterSheet &sheet,
characteristic::Characteristic &localHabilete,
characteristic::Characteristic &localAdresse,
characteristic::Characteristic &localEndurance,
characteristic::Characteristic &localChance,
const std::function<std::uint32_t(std::uint32_t, std::uint32_t)> &operation) noexcept;
static void check_dagger_conditions(const CharacterSheet &sheet, static void check_dagger_conditions(const CharacterSheet &sheet,
characteristic::Characteristic &localHabilete, characteristic::Characteristic &localHabilete,
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);
private:
std::plus<std::uint32_t> plus;
std::minus<std::uint32_t> minus;
static void change_carac(const billyObjects &arg,
CharacterSheet &sheet,
characteristic::Characteristic &localHabilete,
characteristic::Characteristic &localAdresse,
characteristic::Characteristic &localEndurance,
characteristic::Characteristic &localChance,
const std::function<std::uint32_t(std::uint32_t, std::uint32_t)> &operation) noexcept;
}; };
} }

View File

@ -17,10 +17,16 @@ constexpr std::uint32_t toolsHash = const_hash("tools");
namespace character { namespace character {
using characteristic::Characteristic; using characteristic::Characteristic;
bool BillyObjects::push_object(const billyObject &object, CharacterSheet &sheet) noexcept {
bool BillyObjects::insert_object(CharacterSheet &sheet, const billyEnums objType) noexcept {
if (sheet.objects.size() < 3) { if (sheet.objects.size() < 3) {
sheet.objects.emplace_back(object); sheet.objects.emplace(objType, std::visit(overloaded{
sheet.available_objects.erase(object); [](const 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)); },
}, objType));
const auto &object = sheet.objects[objType];
sheet.available_objects.erase(objType);
auto &local_habilete = static_cast<Characteristic &>(sheet.habilete); auto &local_habilete = static_cast<Characteristic &>(sheet.habilete);
auto &local_adresse = static_cast<Characteristic &>(sheet.adresse); auto &local_adresse = static_cast<Characteristic &>(sheet.adresse);
@ -28,42 +34,21 @@ 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) { [&](const weapons &arg) { ++sheet.nb_weapons; },
++sheet.nb_weapons; [&](const equipments &arg) { ++sheet.nb_equipments; },
change_carac_weapon(arg, sheet, local_habilete, local_adresse, local_endurance, plus); [&](const tools &arg) { ++sheet.nb_tools; },
}, }, objType);
[&](const equipments &arg) { change_carac(object, sheet, local_habilete, local_adresse, local_endurance, local_chance, plus);
++sheet.nb_equipments;
change_carac_equipment(arg,
sheet,
local_habilete,
local_adresse,
local_endurance,
local_chance,
plus,
minus);
},
[&](const tools &arg) {
++sheet.nb_tools;
change_carac_tools(arg,
sheet,
local_habilete,
local_adresse,
local_endurance,
local_chance,
plus);
},
}, object);
return true; return true;
} }
return false; return false;
} }
void BillyObjects::pop_object(CharacterSheet &sheet) noexcept { void BillyObjects::erase_object(CharacterSheet &sheet, const billyEnums objToErase) noexcept {
if (!sheet.objects.empty()) { if (!sheet.objects.empty()) {
const billyObject obj = sheet.objects.back(); const billyObjects obj{ sheet.objects[objToErase].release() };
sheet.objects.pop_back(); sheet.objects.erase(objToErase);
sheet.available_objects.insert(obj); sheet.available_objects.insert(objToErase);
auto &local_habilete = static_cast<Characteristic &>(sheet.habilete); auto &local_habilete = static_cast<Characteristic &>(sheet.habilete);
auto &local_adresse = static_cast<Characteristic &>(sheet.adresse); auto &local_adresse = static_cast<Characteristic &>(sheet.adresse);
@ -71,171 +56,49 @@ 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) { [&](const weapons &arg) { --sheet.nb_weapons; },
--sheet.nb_weapons; [&](const equipments &arg) { --sheet.nb_equipments; },
change_carac_weapon(arg, sheet, local_habilete, local_adresse, local_endurance, minus); [&](const tools &arg) { --sheet.nb_tools; }
}, }, objToErase);
[&](const equipments &arg) { change_carac(obj, sheet, local_habilete, local_adresse, local_endurance, local_chance, minus);
--sheet.nb_equipments;
change_carac_equipment(arg,
sheet,
local_habilete,
local_adresse,
local_endurance,
local_chance,
minus,
plus);
},
[&](const tools &arg) {
--sheet.nb_tools;
change_carac_tools(arg,
sheet,
local_habilete,
local_adresse,
local_endurance,
local_chance,
minus);
}
}, obj);
} }
} }
void BillyObjects::change_carac_tools(const tools &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) noexcept { const std::function<std::uint32_t(std::uint32_t, std::uint32_t)> &operation) noexcept {
switch (arg) { localHabilete.materiel = operation(localHabilete.materiel, arg->add_materiel(localHabilete.type));
case tools::Fourche: localAdresse.materiel = operation(localAdresse.materiel, arg->add_materiel(localAdresse.type));
localHabilete.materiel = operation(localHabilete.materiel, 1); localEndurance.materiel = operation(localEndurance.materiel, arg->add_materiel(localEndurance.type));
localEndurance.materiel = operation(localEndurance.materiel, 3); localChance.materiel = operation(localChance.materiel, arg->add_materiel(localChance.type));
break; sheet.armor = operation(sheet.armor, arg->add_armor());
case tools::Dagger: sheet.damage = operation(sheet.armor, arg->add_damage());
sheet.critique = operation(sheet.critique, 6); sheet.critique = operation(sheet.critique, arg->add_critique());
break;
case tools::RockClimbingKit:
localAdresse.materiel = operation(localAdresse.materiel, 1);
break;
case tools::SackOfGrain:
localEndurance.materiel = operation(localEndurance.materiel, 2);
localChance.materiel = operation(localChance.materiel, 2);
break;
}
}
void BillyObjects::change_carac_equipment(const equipments &arg,
CharacterSheet &sheet,
Characteristic &localHabilete,
Characteristic &localAdresse,
Characteristic &localEndurance,
Characteristic &localChance,
const std::function<std::uint32_t(std::uint32_t, std::uint32_t)> &primary,
const std::function<std::uint32_t(std::uint32_t, std::uint32_t)> &complement) noexcept {
switch (arg) {
case equipments::Chainmail:
localHabilete.materiel = complement(localHabilete.materiel, 1);
localAdresse.materiel = complement(localAdresse.materiel, 1);
localEndurance.materiel = primary(localEndurance.materiel, 1);
sheet.armor = primary(sheet.armor, 2);
break;
case equipments::CookingPot:
localEndurance.materiel = primary(localEndurance.materiel, 2);
sheet.armor = primary(sheet.armor, 1);
break;
case equipments::PamphletTourist:
localChance.materiel = primary(localChance.materiel, 4);
break;
case equipments::MedicKit:
localChance.materiel = primary(localChance.materiel, 1);
break;
}
}
void BillyObjects::change_carac_weapon(const weapons &arg,
CharacterSheet &sheet,
Characteristic &localHabilete,
Characteristic &localAdresse,
Characteristic &localEndurance,
const std::function<std::uint32_t(std::uint32_t, std::uint32_t)> &operation) noexcept {
switch (arg) {
case weapons::Sword:
localHabilete.materiel = operation(localHabilete.materiel, 4);
break;
case weapons::Lance:
localHabilete.materiel = operation(localHabilete.materiel, 3);
localAdresse.materiel = operation(localAdresse.materiel, 1);
break;
case weapons::Morgenstern:
localHabilete.materiel = operation(localHabilete.materiel, 1);
localEndurance.materiel = operation(localEndurance.materiel, 1);
sheet.damage = operation(sheet.damage, 1);
break;
case weapons::Bow:
localHabilete.materiel = operation(localHabilete.materiel, 3);
localAdresse.materiel = operation(localAdresse.materiel, 1);
break;
}
} }
void BillyObjects::check_dagger_conditions(const CharacterSheet &sheet, void BillyObjects::check_dagger_conditions(const CharacterSheet &sheet,
Characteristic &localHabilete, Characteristic &localHabilete,
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) {
int count_weapons = 0; if (const auto it_dagger = sheet.objects.find(tools::Dagger); it_dagger != sheet.objects.cend()) {
bool is_there_bow = false; const std::size_t count_weapons = std::count_if(sheet.objects.cbegin(),
std::for_each(sheet.objects.cbegin(), sheet.objects.cend(), [&](const billyObject &object) -> void { sheet.objects.cend(),
if (const weapons *p = std::get_if<weapons>(std::addressof(object)); p != nullptr) { [](container::const_reference node) {
++count_weapons; return std::get_if<weapons>(&node.first) != nullptr;
if (*p == weapons::Bow) { });
is_there_bow = true; const bool is_there_bow = sheet.objects.find(weapons::Bow) != sheet.objects.cend();
} if (count_weapons > 1 || is_there_bow) {
localHabilete.materiel = operation(localHabilete.materiel,
-it_dagger->second->add_materiel(localHabilete.type));
} }
});
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 { std::string_view BillyObjects::billy_object_to_string(const billyObjects &object) noexcept {
return std::visit(overloaded{ return object->to_string();
[](const weapons &arg) {
switch (arg) {
case weapons::Sword:
return sword;
case weapons::Lance:
return lance;
case weapons::Morgenstern:
return morgenstern;
case weapons::Bow:
return bow;
}
},
[](const equipments &arg) {
switch (arg) {
case equipments::Chainmail:
return chainmail;
case equipments::CookingPot:
return cooking_pot;
case equipments::MedicKit:
return medic_kit;
case equipments::PamphletTourist:
return pamphlet_tourist;
}
},
[](const tools &arg) {
switch (arg) {
case tools::Fourche:
return fourche;
case tools::Dagger:
return dagger;
case tools::RockClimbingKit:
return rock_climbing_kit;
case tools::SackOfGrain:
return sack_of_grain;
}
}
}, object);
} }
void BillyObjects::from_json(const json &j, BillyObjects::container &billy) { void BillyObjects::from_json(const json &j, BillyObjects::container &billy) {
@ -244,20 +107,21 @@ namespace character {
const std::uint8_t value = element[1].get<std::uint8_t>(); const std::uint8_t value = element[1].get<std::uint8_t>();
switch (key) { switch (key) {
case weaponsHash: case weaponsHash:
billy.push_back(static_cast<weapons>(value)); billy.emplace(static_cast<weapons>(value), std::make_unique<Weapons>(static_cast<weapons>(value)));
break; break;
case equipmentHash: case equipmentHash:
billy.push_back(static_cast<equipments>(value)); billy.emplace(static_cast<equipments>(value),
std::make_unique<Equipments>(static_cast<equipments>(value)));
break; break;
case toolsHash: case toolsHash:
billy.push_back(static_cast<tools>(value)); billy.emplace(static_cast<tools>(value), std::make_unique<Tools>(static_cast<tools>(value)));
break; break;
} }
} }
} }
void BillyObjects::to_json(json &j, const BillyObjects::container &billy) { void BillyObjects::to_json(json &j, const BillyObjects::container &billy) {
for (const auto &object: 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) });
@ -268,27 +132,7 @@ namespace character {
[&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); }, object_type);
} }
} }
ankerl::svector<bool, 3> BillyObjects::check_conformity(const CharacterSheet &sheet) noexcept {
ankerl::svector<bool, 3> 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;
}
} }