From c12e742ca980d7213e2bc08b902b63117aea54f6 Mon Sep 17 00:00:00 2001 From: wvpm <24685035+wvpm@users.noreply.github.com> Date: Mon, 4 Nov 2024 13:17:55 +0100 Subject: Implement artisanal production --- .../economy/trading/MarketInstance.cpp | 25 ++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'src/openvic-simulation/economy/trading/MarketInstance.cpp') diff --git a/src/openvic-simulation/economy/trading/MarketInstance.cpp b/src/openvic-simulation/economy/trading/MarketInstance.cpp index 5539cdb..fc0c215 100644 --- a/src/openvic-simulation/economy/trading/MarketInstance.cpp +++ b/src/openvic-simulation/economy/trading/MarketInstance.cpp @@ -3,6 +3,8 @@ #include "openvic-simulation/economy/GoodDefinition.hpp" #include "openvic-simulation/economy/GoodInstance.hpp" #include "openvic-simulation/economy/trading/MarketSellOrder.hpp" +#include "economy/trading/BuyResult.hpp" +#include "types/fixed_point/FixedPoint.hpp" using namespace OpenVic; @@ -11,6 +13,16 @@ bool MarketInstance::setup(GoodInstanceManager& new_good_instance_manager) { return true; } +void MarketInstance::place_buy_up_to_order(const BuyUpToOrder buy_up_to_order) { + GoodDefinition const* const good = buy_up_to_order.get_good(); + GoodInstance* const good_instance = good_instance_manager->get_good_instance_by_identifier(good->get_identifier()); + good_instance->add_buy_up_to_order( + buy_up_to_order.get_max_quantity(), + buy_up_to_order.get_money_to_spend(), + buy_up_to_order.get_after_trade() + ); +} + void MarketInstance::place_market_sell_order(const MarketSellOrder market_sell_order) { GoodDefinition const* const good = market_sell_order.get_good(); GoodInstance* const good_instance = good_instance_manager->get_good_instance_by_identifier(good->get_identifier()); @@ -31,6 +43,19 @@ void MarketInstance::execute_orders() { market_sell_quantity * price }); } + + std::vector const& buy_up_to_max_quantities = good_instance.get_buy_up_to_order_max_quantities(); + std::vector const& buy_up_to_money_to_spend = good_instance.get_buy_up_to_order_money_to_spend(); + std::vector> const& buy_up_to_max_callbacks = good_instance.get_buy_up_to_order_callbacks(); + + for(int i = 0; i < buy_up_to_max_quantities.size(); i++) { + const fixed_point_t money_to_spend = buy_up_to_money_to_spend[i]; + const fixed_point_t quantity_bought = std::min(buy_up_to_max_quantities[i], money_to_spend / price); + buy_up_to_max_callbacks[i]({ + quantity_bought, + money_to_spend - quantity_bought * price + }); + } good_instance.clear_orders(); } -- cgit v1.2.3-56-ga3b1