From 7772f8871348b7b52cb0a478bb76df68d8799a07 Mon Sep 17 00:00:00 2001 From: Hop311 Date: Fri, 8 Sep 2023 17:12:22 +0100 Subject: More refactoring and duplicate code removal --- src/openvic-simulation/economy/Good.cpp | 140 ++++++++++++++++++++++++++++++++ src/openvic-simulation/economy/Good.hpp | 79 ++++++++++++++++++ 2 files changed, 219 insertions(+) create mode 100644 src/openvic-simulation/economy/Good.cpp create mode 100644 src/openvic-simulation/economy/Good.hpp (limited to 'src/openvic-simulation/economy') diff --git a/src/openvic-simulation/economy/Good.cpp b/src/openvic-simulation/economy/Good.cpp new file mode 100644 index 0000000..e3dbd3e --- /dev/null +++ b/src/openvic-simulation/economy/Good.cpp @@ -0,0 +1,140 @@ +#include "Good.hpp" + +#include + +using namespace OpenVic; +using namespace OpenVic::NodeTools; + +GoodCategory::GoodCategory(const std::string_view new_identifier) : HasIdentifier { new_identifier } {} + +Good::Good(const std::string_view new_identifier, colour_t new_colour, GoodCategory const& new_category, price_t new_base_price, + bool new_available_from_start, bool new_tradeable, bool new_money, bool new_overseas_penalty) + : HasIdentifierAndColour { new_identifier, new_colour, true }, + category { new_category }, + base_price { new_base_price }, + available_from_start { new_available_from_start }, + tradeable { new_tradeable }, + money { new_money }, + overseas_penalty { new_overseas_penalty } { + assert(base_price > NULL_PRICE); +} + +GoodCategory const& Good::get_category() const { + return category; +} + +Good::price_t Good::get_base_price() const { + return base_price; +} + +Good::price_t Good::get_price() const { + return price; +} + +bool Good::get_available_from_start() const { + return available_from_start; +} + +bool Good::get_available() const { + return available; +} + +bool Good::get_tradeable() const { + return tradeable; +} + +bool Good::get_money() const { + return money; +} + +bool Good::get_overseas_penalty() { + return overseas_penalty; +} + +void Good::reset_to_defaults() { + available = available_from_start; + price = base_price; +} + +GoodManager::GoodManager() : good_categories { "good categories" }, goods { "goods" } {} + +bool GoodManager::add_good_category(const std::string_view identifier) { + if (identifier.empty()) { + Logger::error("Invalid good category identifier - empty!"); + return false; + } + return good_categories.add_item({ identifier }); +} + +bool GoodManager::add_good(const std::string_view identifier, colour_t colour, GoodCategory const* category, + Good::price_t base_price, bool available_from_start, bool tradeable, bool money, bool overseas_penalty) { + if (identifier.empty()) { + Logger::error("Invalid good identifier - empty!"); + return false; + } + if (colour > MAX_COLOUR_RGB) { + Logger::error("Invalid good colour for ", identifier, ": ", colour_to_hex_string(colour)); + return false; + } + if (category == nullptr) { + Logger::error("Invalid good category for ", identifier, ": null"); + return false; + } + if (base_price <= Good::NULL_PRICE) { + Logger::error("Invalid base price for ", identifier, ": ", base_price); + return false; + } + return goods.add_item({ identifier, colour, *category, base_price, available_from_start, tradeable, money, overseas_penalty }); +} + +void GoodManager::reset_to_defaults() { + for (Good& good : goods.get_items()) + good.reset_to_defaults(); +} + +bool GoodManager::load_good_file(ast::NodeCPtr root) { + size_t total_expected_goods = 0; + bool ret = expect_dictionary_reserve_length( + good_categories, + [this, &total_expected_goods](std::string_view key, ast::NodeCPtr value) -> bool { + bool ret = expect_list_and_length( + [&total_expected_goods](size_t size) -> size_t { + total_expected_goods += size; + return 0; + }, + success_callback + )(value); + ret &= add_good_category(key); + return ret; + } + )(root); + lock_good_categories(); + goods.reserve(goods.size() + total_expected_goods); + ret &= expect_dictionary( + [this](std::string_view good_category_key, ast::NodeCPtr good_category_value) -> bool { + GoodCategory const* good_category = get_good_category_by_identifier(good_category_key); + + return expect_dictionary( + [this, good_category](std::string_view key, ast::NodeCPtr value) -> bool { + colour_t colour = NULL_COLOUR; + Good::price_t base_price; + bool available_from_start, tradeable = true; + bool money, overseas_penalty = false; + + bool ret = expect_dictionary_keys( + "color", ONE_EXACTLY, expect_colour(assign_variable_callback(colour)), + "cost", ONE_EXACTLY, expect_fixed_point(assign_variable_callback(base_price)), + "available_from_start", ZERO_OR_ONE, expect_bool(assign_variable_callback(available_from_start)), + "tradeable", ZERO_OR_ONE, expect_bool(assign_variable_callback(tradeable)), + "money", ZERO_OR_ONE, expect_bool(assign_variable_callback(money)), + "overseas_penalty", ZERO_OR_ONE, expect_bool(assign_variable_callback(overseas_penalty)) + )(value); + ret &= add_good(key, colour, good_category, base_price, available_from_start, tradeable, money, overseas_penalty); + return ret; + } + )(good_category_value); + } + )(root); + lock_goods(); + return ret; +} diff --git a/src/openvic-simulation/economy/Good.hpp b/src/openvic-simulation/economy/Good.hpp new file mode 100644 index 0000000..d5cd532 --- /dev/null +++ b/src/openvic-simulation/economy/Good.hpp @@ -0,0 +1,79 @@ +#pragma once + +#include "openvic-simulation/types/IdentifierRegistry.hpp" +#include "openvic-simulation/dataloader/NodeTools.hpp" + +namespace OpenVic { + struct GoodManager; + + struct GoodCategory : HasIdentifier { + friend struct GoodManager; + + private: + GoodCategory(const std::string_view new_identifier); + + public: + GoodCategory(GoodCategory&&) = default; + }; + + /* REQUIREMENTS: + * + * ECON-3 , ECON-4 , ECON-5 , ECON-6 , ECON-7 , ECON-8 , ECON-9 , ECON-10, ECON-11, ECON-12, ECON-13, ECON-14, + * ECON-15, ECON-16, ECON-17, ECON-18, ECON-19, ECON-20, ECON-21, ECON-22, ECON-23, ECON-24, ECON-25, ECON-26, + * ECON-27, ECON-28, ECON-29, ECON-30, ECON-31, ECON-32, ECON-33, ECON-34, ECON-35, ECON-36, ECON-37, ECON-38, + * ECON-39, ECON-40, ECON-41, ECON-42, ECON-43, ECON-44, ECON-45, ECON-46, ECON-47, ECON-48, ECON-49, ECON-50 + * + * ECON-123, ECON-124, ECON-125, ECON-126, ECON-127, ECON-128, ECON-129, ECON-130, ECON-131, ECON-132, ECON-133, ECON-134, + * ECON-135, ECON-136, ECON-137, ECON-138, ECON-139, ECON-140, ECON-141, ECON-142, ECON-234, ECON-235, ECON-236, ECON-237, + * ECON-238, ECON-239, ECON-240, ECON-241, ECON-242, ECON-243, ECON-244, ECON-245, ECON-246, ECON-247, ECON-248, ECON-249, + * ECON-250, ECON-251, ECON-252, ECON-253, ECON-254, ECON-255, ECON-256, ECON-257, ECON-258, ECON-259, ECON-260, ECON-261 + */ + struct Good : HasIdentifierAndColour { + friend struct GoodManager; + + using price_t = fixed_point_t; + static constexpr price_t NULL_PRICE = fixed_point_t::_0(); + + private: + GoodCategory const& category; + const price_t base_price; + price_t price; + const bool available_from_start, tradeable, money, overseas_penalty; + bool available; + + Good(const std::string_view new_identifier, colour_t new_colour, GoodCategory const& new_category, price_t new_base_price, + bool new_available_from_start, bool new_tradeable, bool new_money, bool new_overseas_penalty); + + public: + Good(Good&&) = default; + + GoodCategory const& get_category() const; + price_t get_base_price() const; + price_t get_price() const; + bool get_available_from_start() const; + bool get_available() const; + bool get_tradeable() const; + bool get_money() const; + bool get_overseas_penalty(); + void reset_to_defaults(); + }; + + struct GoodManager { + private: + IdentifierRegistry good_categories; + IdentifierRegistry goods; + + public: + GoodManager(); + + bool add_good_category(const std::string_view identifier); + IDENTIFIER_REGISTRY_ACCESSORS_CUSTOM_PLURAL(GoodCategory, good_category, good_categories) + + bool add_good(const std::string_view identifier, colour_t colour, GoodCategory const* category, Good::price_t base_price, + bool available_from_start, bool tradeable, bool money, bool overseas_penalty); + IDENTIFIER_REGISTRY_ACCESSORS(Good, good) + + void reset_to_defaults(); + bool load_good_file(ast::NodeCPtr root); + }; +} -- cgit v1.2.3-56-ga3b1