aboutsummaryrefslogtreecommitdiff
path: root/src/openvic-simulation/economy/trading
diff options
context:
space:
mode:
author wvpm <24685035+wvpm@users.noreply.github.com>2024-10-28 11:07:15 +0100
committer wvpm <24685035+wvpm@users.noreply.github.com>2024-11-04 14:09:10 +0100
commit89e9c05f4d6276efa82fe486156357f841f864c3 (patch)
tree559be137cbbc7339c9d62658bf8a33366852bae5 /src/openvic-simulation/economy/trading
parent4eba08af501560e9139a562c53fbbc8b694fb4cf (diff)
Add market placeholder
Diffstat (limited to 'src/openvic-simulation/economy/trading')
-rw-r--r--src/openvic-simulation/economy/trading/MarketInstance.cpp37
-rw-r--r--src/openvic-simulation/economy/trading/MarketInstance.hpp15
-rw-r--r--src/openvic-simulation/economy/trading/MarketSellOrder.cpp13
-rw-r--r--src/openvic-simulation/economy/trading/MarketSellOrder.hpp22
-rw-r--r--src/openvic-simulation/economy/trading/SellResult.cpp11
-rw-r--r--src/openvic-simulation/economy/trading/SellResult.hpp16
6 files changed, 114 insertions, 0 deletions
diff --git a/src/openvic-simulation/economy/trading/MarketInstance.cpp b/src/openvic-simulation/economy/trading/MarketInstance.cpp
new file mode 100644
index 0000000..5539cdb
--- /dev/null
+++ b/src/openvic-simulation/economy/trading/MarketInstance.cpp
@@ -0,0 +1,37 @@
+#include "MarketInstance.hpp"
+
+#include "openvic-simulation/economy/GoodDefinition.hpp"
+#include "openvic-simulation/economy/GoodInstance.hpp"
+#include "openvic-simulation/economy/trading/MarketSellOrder.hpp"
+
+using namespace OpenVic;
+
+bool MarketInstance::setup(GoodInstanceManager& new_good_instance_manager) {
+ good_instance_manager = &new_good_instance_manager;
+ return true;
+}
+
+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());
+ good_instance->add_market_sell_order(market_sell_order.get_quantity(), market_sell_order.get_after_trade());
+}
+
+void MarketInstance::execute_orders() {
+ std::vector<GoodInstance>& good_instances = good_instance_manager->get_good_instances();
+ for (GoodInstance& good_instance : good_instances) {
+ std::vector<fixed_point_t> const& market_sell_quantaties = good_instance.get_market_sell_order_quantities();
+ std::vector<std::function<void(const SellResult)>> const& market_sell_callbacks = good_instance.get_market_sell_order_callbacks();
+ const fixed_point_t price = good_instance.get_price();
+
+ for(int i = 0; i < market_sell_quantaties.size(); i++) {
+ const fixed_point_t market_sell_quantity = market_sell_quantaties[i];
+ market_sell_callbacks[i]({
+ market_sell_quantity,
+ market_sell_quantity * price
+ });
+ }
+
+ good_instance.clear_orders();
+ }
+}
diff --git a/src/openvic-simulation/economy/trading/MarketInstance.hpp b/src/openvic-simulation/economy/trading/MarketInstance.hpp
new file mode 100644
index 0000000..2d8a651
--- /dev/null
+++ b/src/openvic-simulation/economy/trading/MarketInstance.hpp
@@ -0,0 +1,15 @@
+#pragma once
+
+#include "openvic-simulation/economy/GoodInstance.hpp"
+#include "openvic-simulation/economy/trading/MarketSellOrder.hpp"
+
+namespace OpenVic {
+ struct MarketInstance {
+ private:
+ GoodInstanceManager* PROPERTY(good_instance_manager);
+ public:
+ bool setup(GoodInstanceManager& new_good_instance_manager);
+ void place_market_sell_order(const MarketSellOrder market_sell_order);
+ void execute_orders();
+ };
+} \ No newline at end of file
diff --git a/src/openvic-simulation/economy/trading/MarketSellOrder.cpp b/src/openvic-simulation/economy/trading/MarketSellOrder.cpp
new file mode 100644
index 0000000..349bac6
--- /dev/null
+++ b/src/openvic-simulation/economy/trading/MarketSellOrder.cpp
@@ -0,0 +1,13 @@
+#include "MarketSellOrder.hpp"
+
+using namespace OpenVic;
+
+MarketSellOrder::MarketSellOrder(
+ GoodDefinition const& new_good,
+ const fixed_point_t new_quantity,
+ const std::function<void(const SellResult)> new_after_trade
+):
+ good { &new_good },
+ quantity { new_quantity },
+ after_trade { new_after_trade }
+ {} \ No newline at end of file
diff --git a/src/openvic-simulation/economy/trading/MarketSellOrder.hpp b/src/openvic-simulation/economy/trading/MarketSellOrder.hpp
new file mode 100644
index 0000000..48006df
--- /dev/null
+++ b/src/openvic-simulation/economy/trading/MarketSellOrder.hpp
@@ -0,0 +1,22 @@
+#pragma once
+
+#include "openvic-simulation/economy/GoodDefinition.hpp"
+#include "openvic-simulation/economy/trading/SellResult.hpp"
+#include "openvic-simulation/types/fixed_point/FixedPoint.hpp"
+#include "openvic-simulation/utility/Getters.hpp"
+
+namespace OpenVic {
+ struct MarketSellOrder {
+ private:
+ GoodDefinition const* const PROPERTY(good);
+ const fixed_point_t PROPERTY(quantity);
+ const std::function<void(const SellResult)> PROPERTY(after_trade);
+
+ public:
+ MarketSellOrder(
+ GoodDefinition const& new_good,
+ const fixed_point_t new_quantity,
+ const std::function<void(const SellResult)> new_after_trade
+ );
+ };
+} \ No newline at end of file
diff --git a/src/openvic-simulation/economy/trading/SellResult.cpp b/src/openvic-simulation/economy/trading/SellResult.cpp
new file mode 100644
index 0000000..91de626
--- /dev/null
+++ b/src/openvic-simulation/economy/trading/SellResult.cpp
@@ -0,0 +1,11 @@
+#include "SellResult.hpp"
+
+using namespace OpenVic;
+
+SellResult::SellResult(
+ const fixed_point_t new_quantity_sold,
+ const fixed_point_t new_money_gained
+) :
+ quantity_sold { new_quantity_sold },
+ money_gained { new_money_gained }
+ {} \ No newline at end of file
diff --git a/src/openvic-simulation/economy/trading/SellResult.hpp b/src/openvic-simulation/economy/trading/SellResult.hpp
new file mode 100644
index 0000000..f3a40ad
--- /dev/null
+++ b/src/openvic-simulation/economy/trading/SellResult.hpp
@@ -0,0 +1,16 @@
+#pragma once
+
+#include "openvic-simulation/types/fixed_point/FixedPoint.hpp"
+
+namespace OpenVic {
+ struct SellResult {
+ private:
+ fixed_point_t PROPERTY(quantity_sold);
+ fixed_point_t PROPERTY(money_gained);
+ public:
+ SellResult(
+ const fixed_point_t new_quantity_sold,
+ const fixed_point_t new_money_gained
+ );
+ };
+} \ No newline at end of file