diff options
author | Joel Machens <ajmach6@gmail.com> | 2023-10-03 04:27:51 +0200 |
---|---|---|
committer | Joel Machens <ajmach6@gmail.com> | 2023-10-03 23:55:48 +0200 |
commit | c7eac74bb550c28be9eb75742f25d974b7742634 (patch) | |
tree | d616d3a13da06511614b49853570524da62c5cbe /src/openvic-simulation/politics | |
parent | b62e4d1b83f25c4ceb59455f953b6d32514ab726 (diff) |
Initial Government Type Loading
Diffstat (limited to 'src/openvic-simulation/politics')
-rw-r--r-- | src/openvic-simulation/politics/Government.cpp | 94 | ||||
-rw-r--r-- | src/openvic-simulation/politics/Government.hpp | 42 |
2 files changed, 136 insertions, 0 deletions
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 <set> + +#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<std::string> 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<std::string> 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<std::string> 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<std::string, std::less<void>> 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 diff --git a/src/openvic-simulation/politics/Government.hpp b/src/openvic-simulation/politics/Government.hpp new file mode 100644 index 0000000..a124780 --- /dev/null +++ b/src/openvic-simulation/politics/Government.hpp @@ -0,0 +1,42 @@ +#pragma once + +#include "openvic-simulation/types/IdentifierRegistry.hpp" +#include "openvic-simulation/politics/Ideology.hpp" + +namespace OpenVic { + struct GovernmentTypeManager; + + struct GovernmentType : HasIdentifier { + friend struct GovernmentTypeManager; + + private: + const std::vector<std::string> ideologies; + const bool elections, appoint_ruling_party; + const uint16_t election_duration; + const std::string_view flag_type_identifier; + + GovernmentType(std::string_view new_identifier, std::vector<std::string> new_ideologies, bool new_elections, bool new_appoint_ruling_party, uint16_t new_election_duration, std::string_view new_flag_type_identifier); + + public: + GovernmentType(GovernmentType&&) = default; + + bool is_ideology_compatible(std::string_view ideology) const; + bool holds_elections() const; + bool can_appoint_ruling_party() const; + uint16_t get_election_duration() const; + std::string_view get_flag_type() const; + }; + + struct GovernmentTypeManager { + private: + IdentifierRegistry<GovernmentType> government_types; + + public: + GovernmentTypeManager(); + + bool add_government_type(std::string_view identifier, std::vector<std::string> 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); + }; +} // namespace OpenVic |