aboutsummaryrefslogtreecommitdiff
path: root/src/openvic-simulation/economy/trading/MarketInstance.cpp
diff options
context:
space:
mode:
author wvpm <24685035+wvpm@users.noreply.github.com>2024-11-04 13:17:55 +0100
committer wvpm <24685035+wvpm@users.noreply.github.com>2024-11-04 14:38:19 +0100
commitc12e742ca980d7213e2bc08b902b63117aea54f6 (patch)
treed128c5f6abe32904b9a5a2a03718ace64c85c2e2 /src/openvic-simulation/economy/trading/MarketInstance.cpp
parent89e9c05f4d6276efa82fe486156357f841f864c3 (diff)
Implement artisanal productionartisanal_production
Diffstat (limited to 'src/openvic-simulation/economy/trading/MarketInstance.cpp')
-rw-r--r--src/openvic-simulation/economy/trading/MarketInstance.cpp25
1 files changed, 25 insertions, 0 deletions
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<fixed_point_t> const& buy_up_to_max_quantities = good_instance.get_buy_up_to_order_max_quantities();
+ std::vector<fixed_point_t> const& buy_up_to_money_to_spend = good_instance.get_buy_up_to_order_money_to_spend();
+ std::vector<std::function<void(const BuyResult)>> 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();
}