From c7eac74bb550c28be9eb75742f25d974b7742634 Mon Sep 17 00:00:00 2001 From: Joel Machens Date: Mon, 2 Oct 2023 21:27:51 -0500 Subject: Initial Government Type Loading --- src/openvic-simulation/politics/Government.cpp | 94 ++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 src/openvic-simulation/politics/Government.cpp (limited to 'src/openvic-simulation/politics/Government.cpp') diff --git a/src/openvic-simulation/politics/Government.cpp b/src/openvic-simulation/politics/Government.cpp new file mode 100644 index 0000000..50f20e6 --- /dev/null +++ b/src/openvic-simulation/politics/Government.cpp @@ -0,0 +1,94 @@ +#include "Government.hpp" + +#include + +#include "openvic-simulation/dataloader/NodeTools.hpp" +#include "openvic-simulation/GameManager.hpp" + +using namespace OpenVic; +using namespace OpenVic::NodeTools; + +GovernmentType::GovernmentType(std::string_view new_identifier, std::vector new_ideologies, bool new_elections, bool new_appoint_ruling_party, uint16_t new_election_duration, std::string_view new_flag_type_identifier) + : HasIdentifier { new_identifier }, ideologies { new_ideologies }, elections { new_elections }, appoint_ruling_party { new_appoint_ruling_party }, election_duration { new_election_duration }, flag_type_identifier { new_flag_type_identifier } {} + +bool GovernmentType::is_ideology_compatible(std::string_view ideology) const { + return std::find(ideologies.begin(), ideologies.end(), ideology) != ideologies.end(); +} + +bool GovernmentType::holds_elections() const { + return elections; +} + +bool GovernmentType::can_appoint_ruling_party() const { + return appoint_ruling_party; +} + +uint16_t GovernmentType::get_election_duration() const { + return election_duration; +} + +std::string_view GovernmentType::get_flag_type() const { + return flag_type_identifier; +} + +GovernmentTypeManager::GovernmentTypeManager() : government_types { "government types" } {} + +bool GovernmentTypeManager::add_government_type(std::string_view identifier, std::vector ideologies, bool elections, bool appoint_ruling_party, uint16_t election_duration, std::string_view flag_type) { + if (identifier.empty()) { + Logger::error("Invalid government type identifier - empty!"); + return false; + } + + if (ideologies.empty()) { + Logger::error("No compatible ideologies defined for government type ", identifier); + return false; + } + + if (elections && election_duration == 0) { + Logger::error("No or invalid election duration for government type ", identifier); + return false; + } + + return government_types.add_item({ identifier, ideologies, elections, appoint_ruling_party, election_duration, flag_type }); +} + +bool GovernmentTypeManager::load_government_types_file(ast::NodeCPtr root) { + bool ret = expect_dictionary( + [this](std::string_view government_type_identifier, ast::NodeCPtr value) -> bool { + std::vector ideologies; + bool elections = false, appoint_ruling_party = false; + uint16_t election_duration = 0; + std::string_view flag_type_identifier = "republic"; + + bool ret = expect_dictionary_keys_and_length( + default_length_callback, + ALLOW_OTHER_KEYS, + "election", ONE_EXACTLY, expect_bool(assign_variable_callback(elections)), + "duration", ZERO_OR_ONE, expect_uint(assign_variable_callback_uint(election_duration)), + "appoint_ruling_party", ONE_EXACTLY, expect_bool(assign_variable_callback(appoint_ruling_party)), + "flagType", ZERO_OR_ONE, expect_identifier(assign_variable_callback(flag_type_identifier)) + )(value); + + ret &= expect_dictionary( + [this, ideologies](std::string_view key, ast::NodeCPtr value) -> bool { + static const std::set> reserved_keys = { + "election", "duration", "appoint_ruling_party", "flagType" + }; + if (reserved_keys.find(key) != reserved_keys.end()) return true; + return expect_assign( + [this, ideologies](std::string_view ideology_key, ast::NodeCPtr value) -> bool { + // TODO: grab reference to ideology and shove in vector + return true; + } + )(value); + } + )(value); + + ret &= add_government_type(government_type_identifier, ideologies, elections, appoint_ruling_party, election_duration, flag_type_identifier); + return ret; + } + )(root); + lock_government_types(); + + return ret; +} \ No newline at end of file -- cgit v1.2.3-56-ga3b1 From 3a691705be925011fac5172c5f2b374262ec2122 Mon Sep 17 00:00:00 2001 From: BrickPi <49528459+BrickPi@users.noreply.github.com> Date: Tue, 3 Oct 2023 10:17:08 -0500 Subject: Finish Government Type Loading --- src/openvic-simulation/dataloader/Dataloader.cpp | 2 +- src/openvic-simulation/politics/Government.cpp | 11 ++++++----- src/openvic-simulation/politics/Government.hpp | 4 ++-- 3 files changed, 9 insertions(+), 8 deletions(-) (limited to 'src/openvic-simulation/politics/Government.cpp') diff --git a/src/openvic-simulation/dataloader/Dataloader.cpp b/src/openvic-simulation/dataloader/Dataloader.cpp index b4b23aa..fffbb51 100644 --- a/src/openvic-simulation/dataloader/Dataloader.cpp +++ b/src/openvic-simulation/dataloader/Dataloader.cpp @@ -318,7 +318,7 @@ bool Dataloader::load_defines(GameManager& game_manager) const { Logger::error("Failed to load ideologies!"); ret = false; } - if (!game_manager.get_government_type_manager().load_government_types_file(_parse_defines(lookup_file(governments_file)).get_file_node())) { + if (!game_manager.get_government_type_manager().load_government_types_file(game_manager.get_ideology_manager(), _parse_defines(lookup_file(governments_file)).get_file_node())) { Logger::error("Failed to load government types!"); ret = false; } diff --git a/src/openvic-simulation/politics/Government.cpp b/src/openvic-simulation/politics/Government.cpp index 50f20e6..2874b42 100644 --- a/src/openvic-simulation/politics/Government.cpp +++ b/src/openvic-simulation/politics/Government.cpp @@ -52,9 +52,9 @@ bool GovernmentTypeManager::add_government_type(std::string_view identifier, std return government_types.add_item({ identifier, ideologies, elections, appoint_ruling_party, election_duration, flag_type }); } -bool GovernmentTypeManager::load_government_types_file(ast::NodeCPtr root) { +bool GovernmentTypeManager::load_government_types_file(IdeologyManager& ideology_manager, ast::NodeCPtr root) { bool ret = expect_dictionary( - [this](std::string_view government_type_identifier, ast::NodeCPtr value) -> bool { + [this, &ideology_manager](std::string_view government_type_identifier, ast::NodeCPtr value) -> bool { std::vector ideologies; bool elections = false, appoint_ruling_party = false; uint16_t election_duration = 0; @@ -70,14 +70,15 @@ bool GovernmentTypeManager::load_government_types_file(ast::NodeCPtr root) { )(value); ret &= expect_dictionary( - [this, ideologies](std::string_view key, ast::NodeCPtr value) -> bool { + [this, &ideology_manager, &ideologies](std::string_view key, ast::NodeCPtr value) -> bool { static const std::set> reserved_keys = { "election", "duration", "appoint_ruling_party", "flagType" }; if (reserved_keys.find(key) != reserved_keys.end()) return true; return expect_assign( - [this, ideologies](std::string_view ideology_key, ast::NodeCPtr value) -> bool { - // TODO: grab reference to ideology and shove in vector + [this, &ideology_manager, &ideologies](std::string_view ideology_key, ast::NodeCPtr value) -> bool { + if (ideology_manager.get_ideology_by_identifier(ideology_key) == nullptr) { return false; } + ideologies.push_back(std::string{ideology_key}); return true; } )(value); diff --git a/src/openvic-simulation/politics/Government.hpp b/src/openvic-simulation/politics/Government.hpp index a124780..a3642fb 100644 --- a/src/openvic-simulation/politics/Government.hpp +++ b/src/openvic-simulation/politics/Government.hpp @@ -10,7 +10,7 @@ namespace OpenVic { friend struct GovernmentTypeManager; private: - const std::vector ideologies; + std::vector ideologies; const bool elections, appoint_ruling_party; const uint16_t election_duration; const std::string_view flag_type_identifier; @@ -37,6 +37,6 @@ namespace OpenVic { bool add_government_type(std::string_view identifier, std::vector ideologies, bool elections, bool appoint_ruling_party, uint16_t election_duration, std::string_view flag_type); IDENTIFIER_REGISTRY_ACCESSORS(GovernmentType, government_type) - bool load_government_types_file(ast::NodeCPtr root); + bool load_government_types_file(IdeologyManager& ideology_manager, ast::NodeCPtr root); }; } // namespace OpenVic -- cgit v1.2.3-56-ga3b1 From accb976f6d77cb02ed98e50c1bebd84134dc17cd Mon Sep 17 00:00:00 2001 From: BrickPi <49528459+BrickPi@users.noreply.github.com> Date: Tue, 3 Oct 2023 10:25:24 -0500 Subject: Add Requirements --- src/openvic-simulation/politics/Government.cpp | 1 + 1 file changed, 1 insertion(+) (limited to 'src/openvic-simulation/politics/Government.cpp') diff --git a/src/openvic-simulation/politics/Government.cpp b/src/openvic-simulation/politics/Government.cpp index 2874b42..1e3884c 100644 --- a/src/openvic-simulation/politics/Government.cpp +++ b/src/openvic-simulation/politics/Government.cpp @@ -52,6 +52,7 @@ bool GovernmentTypeManager::add_government_type(std::string_view identifier, std return government_types.add_item({ identifier, ideologies, elections, appoint_ruling_party, election_duration, flag_type }); } +/* REQUIREMENTS: FS-525, SIM-27 */ bool GovernmentTypeManager::load_government_types_file(IdeologyManager& ideology_manager, ast::NodeCPtr root) { bool ret = expect_dictionary( [this, &ideology_manager](std::string_view government_type_identifier, ast::NodeCPtr value) -> bool { -- cgit v1.2.3-56-ga3b1 From 53b787831ff773e595f9fbe8523b8c382586bd7a Mon Sep 17 00:00:00 2001 From: BrickPi <49528459+BrickPi@users.noreply.github.com> Date: Tue, 3 Oct 2023 13:41:47 -0500 Subject: Fix Extraneous Callback --- src/openvic-simulation/politics/Government.cpp | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) (limited to 'src/openvic-simulation/politics/Government.cpp') diff --git a/src/openvic-simulation/politics/Government.cpp b/src/openvic-simulation/politics/Government.cpp index 1e3884c..76617a8 100644 --- a/src/openvic-simulation/politics/Government.cpp +++ b/src/openvic-simulation/politics/Government.cpp @@ -76,13 +76,9 @@ bool GovernmentTypeManager::load_government_types_file(IdeologyManager& ideology "election", "duration", "appoint_ruling_party", "flagType" }; if (reserved_keys.find(key) != reserved_keys.end()) return true; - return expect_assign( - [this, &ideology_manager, &ideologies](std::string_view ideology_key, ast::NodeCPtr value) -> bool { - if (ideology_manager.get_ideology_by_identifier(ideology_key) == nullptr) { return false; } - ideologies.push_back(std::string{ideology_key}); - return true; - } - )(value); + if (ideology_manager.get_ideology_by_identifier(key) == nullptr) return false; + ideologies.push_back(std::string{key}); + return true; } )(value); -- cgit v1.2.3-56-ga3b1 From f15504062abfc24c1e65b660315198fe7838db8c Mon Sep 17 00:00:00 2001 From: Joel Machens Date: Tue, 3 Oct 2023 19:09:28 -0500 Subject: Fix Data Types --- src/openvic-simulation/politics/Government.cpp | 33 ++++++++++++++++---------- src/openvic-simulation/politics/Government.hpp | 17 ++++++------- 2 files changed, 29 insertions(+), 21 deletions(-) (limited to 'src/openvic-simulation/politics/Government.cpp') diff --git a/src/openvic-simulation/politics/Government.cpp b/src/openvic-simulation/politics/Government.cpp index 76617a8..869ac3d 100644 --- a/src/openvic-simulation/politics/Government.cpp +++ b/src/openvic-simulation/politics/Government.cpp @@ -8,13 +8,17 @@ using namespace OpenVic; using namespace OpenVic::NodeTools; -GovernmentType::GovernmentType(std::string_view new_identifier, std::vector new_ideologies, bool new_elections, bool new_appoint_ruling_party, uint16_t new_election_duration, std::string_view new_flag_type_identifier) +GovernmentType::GovernmentType(std::string_view new_identifier, std::vector new_ideologies, bool new_elections, bool new_appoint_ruling_party, Timespan new_election_duration, std::string_view new_flag_type_identifier) : HasIdentifier { new_identifier }, ideologies { new_ideologies }, elections { new_elections }, appoint_ruling_party { new_appoint_ruling_party }, election_duration { new_election_duration }, flag_type_identifier { new_flag_type_identifier } {} -bool GovernmentType::is_ideology_compatible(std::string_view ideology) const { +bool GovernmentType::is_ideology_compatible(Ideology const* ideology) const { return std::find(ideologies.begin(), ideologies.end(), ideology) != ideologies.end(); } +std::vector const& GovernmentType::get_ideologies() const { + return ideologies; +} + bool GovernmentType::holds_elections() const { return elections; } @@ -23,7 +27,7 @@ bool GovernmentType::can_appoint_ruling_party() const { return appoint_ruling_party; } -uint16_t GovernmentType::get_election_duration() const { +Timespan GovernmentType::get_election_duration() const { return election_duration; } @@ -33,7 +37,7 @@ std::string_view GovernmentType::get_flag_type() const { GovernmentTypeManager::GovernmentTypeManager() : government_types { "government types" } {} -bool GovernmentTypeManager::add_government_type(std::string_view identifier, std::vector ideologies, bool elections, bool appoint_ruling_party, uint16_t election_duration, std::string_view flag_type) { +bool GovernmentTypeManager::add_government_type(std::string_view identifier, std::vector ideologies, bool elections, bool appoint_ruling_party, Timespan election_duration, std::string_view flag_type) { if (identifier.empty()) { Logger::error("Invalid government type identifier - empty!"); return false; @@ -53,16 +57,15 @@ bool GovernmentTypeManager::add_government_type(std::string_view identifier, std } /* REQUIREMENTS: FS-525, SIM-27 */ -bool GovernmentTypeManager::load_government_types_file(IdeologyManager& ideology_manager, ast::NodeCPtr root) { +bool GovernmentTypeManager::load_government_types_file(IdeologyManager const& ideology_manager, ast::NodeCPtr root) { bool ret = expect_dictionary( [this, &ideology_manager](std::string_view government_type_identifier, ast::NodeCPtr value) -> bool { - std::vector ideologies; + std::vector ideologies; bool elections = false, appoint_ruling_party = false; - uint16_t election_duration = 0; + uint16_t election_duration = 0; /* in months */ std::string_view flag_type_identifier = "republic"; - bool ret = expect_dictionary_keys_and_length( - default_length_callback, + bool ret = expect_dictionary_keys( ALLOW_OTHER_KEYS, "election", ONE_EXACTLY, expect_bool(assign_variable_callback(elections)), "duration", ZERO_OR_ONE, expect_uint(assign_variable_callback_uint(election_duration)), @@ -71,18 +74,22 @@ bool GovernmentTypeManager::load_government_types_file(IdeologyManager& ideology )(value); ret &= expect_dictionary( - [this, &ideology_manager, &ideologies](std::string_view key, ast::NodeCPtr value) -> bool { + [this, &ideology_manager, &ideologies, government_type_identifier](std::string_view key, ast::NodeCPtr value) -> bool { static const std::set> reserved_keys = { "election", "duration", "appoint_ruling_party", "flagType" }; if (reserved_keys.find(key) != reserved_keys.end()) return true; - if (ideology_manager.get_ideology_by_identifier(key) == nullptr) return false; - ideologies.push_back(std::string{key}); + Ideology const* ideology = ideology_manager.get_ideology_by_identifier(key); + if (ideology == nullptr) { + Logger::error("When loading government type ", government_type_identifier, ", specified ideology ", key, " is invalid!"); + return false; + } + ideologies.push_back(ideology); return true; } )(value); - ret &= add_government_type(government_type_identifier, ideologies, elections, appoint_ruling_party, election_duration, flag_type_identifier); + ret &= add_government_type(government_type_identifier, ideologies, elections, appoint_ruling_party, Timespan(election_duration * 30), flag_type_identifier); return ret; } )(root); diff --git a/src/openvic-simulation/politics/Government.hpp b/src/openvic-simulation/politics/Government.hpp index a3642fb..a8fc00f 100644 --- a/src/openvic-simulation/politics/Government.hpp +++ b/src/openvic-simulation/politics/Government.hpp @@ -10,20 +10,21 @@ namespace OpenVic { friend struct GovernmentTypeManager; private: - std::vector ideologies; + std::vector ideologies; const bool elections, appoint_ruling_party; - const uint16_t election_duration; - const std::string_view flag_type_identifier; + const Timespan election_duration; + const std::string flag_type_identifier; - GovernmentType(std::string_view new_identifier, std::vector new_ideologies, bool new_elections, bool new_appoint_ruling_party, uint16_t new_election_duration, std::string_view new_flag_type_identifier); + GovernmentType(std::string_view new_identifier, std::vector new_ideologies, bool new_elections, bool new_appoint_ruling_party, Timespan new_election_duration, std::string_view new_flag_type_identifier); public: GovernmentType(GovernmentType&&) = default; - bool is_ideology_compatible(std::string_view ideology) const; + bool is_ideology_compatible(Ideology const* ideology) const; + std::vector const& get_ideologies() const; bool holds_elections() const; bool can_appoint_ruling_party() const; - uint16_t get_election_duration() const; + Timespan get_election_duration() const; std::string_view get_flag_type() const; }; @@ -34,9 +35,9 @@ namespace OpenVic { public: GovernmentTypeManager(); - bool add_government_type(std::string_view identifier, std::vector ideologies, bool elections, bool appoint_ruling_party, uint16_t election_duration, std::string_view flag_type); + bool add_government_type(std::string_view identifier, std::vector ideologies, bool elections, bool appoint_ruling_party, Timespan election_duration, std::string_view flag_type); IDENTIFIER_REGISTRY_ACCESSORS(GovernmentType, government_type) - bool load_government_types_file(IdeologyManager& ideology_manager, ast::NodeCPtr root); + bool load_government_types_file(IdeologyManager const& ideology_manager, ast::NodeCPtr root); }; } // namespace OpenVic -- cgit v1.2.3-56-ga3b1