From ddf75e39df90aedb25c32009211f56396865d28c Mon Sep 17 00:00:00 2001 From: zaaarf Date: Thu, 14 Dec 2023 01:35:53 +0100 Subject: feat: on_action dataloading --- src/openvic-simulation/misc/Event.cpp | 41 +++++++++++++++++++++++++++++++++++ src/openvic-simulation/misc/Event.hpp | 17 +++++++++++++++ 2 files changed, 58 insertions(+) (limited to 'src/openvic-simulation/misc') diff --git a/src/openvic-simulation/misc/Event.cpp b/src/openvic-simulation/misc/Event.cpp index 59b1f54..b7046af 100644 --- a/src/openvic-simulation/misc/Event.cpp +++ b/src/openvic-simulation/misc/Event.cpp @@ -20,6 +20,9 @@ Event::Event( news_desc_long { new_news_desc_long }, news_desc_medium { new_news_desc_medium }, news_desc_short { new_news_desc_short }, election { new_election }, election_issue_group { new_election_issue_group }, options { std::move(new_options) } {} +OnAction::OnAction(std::string_view new_identifier, weight_map_t new_weighted_events) + : HasIdentifier { new_identifier }, weighted_events { std::move(new_weighted_events) } {} + bool EventManager::register_event( std::string_view identifier, std::string_view title, std::string_view description, std::string_view image, Event::event_type_t type, bool triggered_only, bool major, bool fire_only_once, bool allows_multiple_instances, bool news, @@ -69,6 +72,15 @@ bool EventManager::register_event( }, duplicate_warning_callback); } +bool EventManager::add_on_action(std::string_view identifier, OnAction::weight_map_t weighted_events) { + if (identifier.empty()) { + Logger::error("Invalid decision identifier - empty!"); + return false; + } + + return on_actions.add_item({ identifier, std::move(weighted_events) }); +} + bool EventManager::load_event_file(IssueManager const& issue_manager, ast::NodeCPtr root) { return expect_dictionary( [this, &issue_manager](std::string_view key, ast::NodeCPtr value) -> bool { @@ -132,3 +144,32 @@ bool EventManager::load_event_file(IssueManager const& issue_manager, ast::NodeC } )(root); } + +bool EventManager::load_on_action_file(ast::NodeCPtr root) { + bool ret = expect_dictionary([this](std::string_view identifier, ast::NodeCPtr node) -> bool { + OnAction::weight_map_t weighted_events; + bool ret = expect_dictionary([this, &identifier, &weighted_events](std::string_view weight_str, ast::NodeCPtr event_node) -> bool { + Event const* event = nullptr; + + bool ret = false; + uint64_t weight = StringUtils::string_to_uint64(weight_str, &ret); + if (!ret) { + Logger::error("Invalid weight ", weight_str, " on action ", identifier); + return ret; + } + + ret &= expect_event_identifier(assign_variable_callback_pointer(event))(event_node); + + if (event != nullptr) { + ret &= weighted_events.emplace(event, weight).second; + } else Logger::warning("Non-existing event ", event->get_identifier(), " loaded on action ", identifier, "with weight", weight, "!"); + + return ret; + } + )(node); + ret &= add_on_action(identifier, std::move(weighted_events)); + return ret; + })(root); + on_actions.lock(); + return ret; +} \ No newline at end of file diff --git a/src/openvic-simulation/misc/Event.hpp b/src/openvic-simulation/misc/Event.hpp index d1f7963..a518f78 100644 --- a/src/openvic-simulation/misc/Event.hpp +++ b/src/openvic-simulation/misc/Event.hpp @@ -63,9 +63,23 @@ namespace OpenVic { Event(Event&&) = default; }; + struct OnAction : HasIdentifier { + friend struct EventManager; + using weight_map_t = std::map; + + private: + weight_map_t PROPERTY(weighted_events); + + OnAction(std::string_view new_identifier, weight_map_t new_weighted_events); + + public: + OnAction(OnAction&&) = default; + }; + struct EventManager { private: IdentifierRegistry IDENTIFIER_REGISTRY(event); + IdentifierRegistry IDENTIFIER_REGISTRY(on_action); public: bool register_event( @@ -76,6 +90,9 @@ namespace OpenVic { std::vector&& options ); + bool add_on_action(std::string_view identifier, OnAction::weight_map_t new_weighted_events); + bool load_event_file(IssueManager const& issue_manager, ast::NodeCPtr root); + bool load_on_action_file(ast::NodeCPtr root); }; } // namespace OpenVic -- cgit v1.2.3-56-ga3b1