Conquer Space 0.0.0
A space themed grand strategy game set in the near future, with realistic orbital mechanics, and an emphasis on economics and politics.
auction.h
Go to the documentation of this file.
1/* Conquer Space
2 * Copyright (C) 2021-2025 Conquer Space
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17#pragma once
18
19#include <functional>
20#include <map>
21#include <vector>
22
23#include <entt/entt.hpp>
24
26struct Order {
27 Order() : price(0), quantity(0) {}
28 Order(double price, double quantity, entt::entity agent) : price(price), quantity(quantity), agent(agent) {}
29
33 double price;
34 double quantity;
35
40 entt::entity agent;
41};
42
43inline bool operator<(const Order& lhs, const Order& rhs) { return lhs.price < rhs.price; }
44
45inline bool operator>(const Order& lhs, const Order& rhs) { return lhs.price > rhs.price; }
46
47template <class T>
48class SortedOrderList : public std::vector<Order> {
49 public:
50 using std::vector<Order>::vector;
51 void put(const Order& order) {
52 SortedOrderList<T>::iterator it = std::lower_bound(begin(), end(), order, T());
53 insert(it, order);
54 }
55};
56
61
66
68 std::map<entt::entity, DescendingSortedOrderList> sell_orders;
69 std::map<entt::entity, AscendingSortedOrderList> buy_orders;
70
71 void AddSellOrder(entt::entity good, Order&& order) { sell_orders[good].put(order); }
72
73 void AddBuyOrder(entt::entity good, Order&& order) { buy_orders[good].put(order); }
74
75 double GetDemand(entt::entity good) {
76 const AscendingSortedOrderList& buy_list = buy_orders[good];
77 double demand = 0;
78 for (const Order& order : buy_list) {
79 demand += order.quantity;
80 }
81 return demand;
82 }
83
84 double GetSupply(entt::entity good) {
85 const DescendingSortedOrderList& sell_list = sell_orders[good];
86 double supply = 0;
87 for (const Order& order : sell_list) {
88 supply += order.quantity;
89 }
90 return supply;
91 }
92};
93} // namespace cqsp::common::components
void put(const Order &order)
Definition: auction.h:51
Definition: area.h:23
SortedOrderList< std::less< Order > > AscendingSortedOrderList
Smallest to greatest
Definition: auction.h:65
bool operator>(const Order &lhs, const Order &rhs)
Definition: auction.h:45
bool operator<(const Order &lhs, const Order &rhs)
Definition: auction.h:43
SortedOrderList< std::greater< Order > > DescendingSortedOrderList
Greatest to smallest
Definition: auction.h:60
double GetDemand(entt::entity good)
Definition: auction.h:75
double GetSupply(entt::entity good)
Definition: auction.h:84
std::map< entt::entity, DescendingSortedOrderList > sell_orders
Definition: auction.h:68
void AddBuyOrder(entt::entity good, Order &&order)
Definition: auction.h:73
std::map< entt::entity, AscendingSortedOrderList > buy_orders
Definition: auction.h:69
void AddSellOrder(entt::entity good, Order &&order)
Definition: auction.h:71
Definition: auction.h:26
double quantity
Definition: auction.h:34
double price
Price per unit
Definition: auction.h:33
entt::entity agent
The agent that owns this order. Goods sold will go to the agent, money recieved will also go to the a...
Definition: auction.h:40
Order(double price, double quantity, entt::entity agent)
Definition: auction.h:28
Order()
Definition: auction.h:27