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-2023 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
25namespace cqsp {
26namespace common {
27namespace components {
28struct Order {
29 Order() : price(0), quantity(0) {}
30 Order(double price, double quantity, entt::entity agent) : price(price), quantity(quantity), agent(agent) {}
31
35 double price;
36 double quantity;
37
42 entt::entity agent;
43};
44
45inline bool operator<(const Order& lhs, const Order& rhs) { return lhs.price < rhs.price; }
46
47inline bool operator>(const Order& lhs, const Order& rhs) { return lhs.price > rhs.price; }
48
49template <class T>
50class SortedOrderList : public std::vector<Order> {
51 public:
52 using std::vector<Order>::vector;
53 void put(const Order& order) {
54 SortedOrderList<T>::iterator it = std::lower_bound(begin(), end(), order, T());
55 insert(it, order);
56 }
57};
58
63
68
70 std::map<entt::entity, DescendingSortedOrderList> sell_orders;
71 std::map<entt::entity, AscendingSortedOrderList> buy_orders;
72
73 void AddSellOrder(entt::entity good, Order&& order) { sell_orders[good].put(order); }
74
75 void AddBuyOrder(entt::entity good, Order&& order) { buy_orders[good].put(order); }
76
77 double GetDemand(entt::entity good) {
78 const AscendingSortedOrderList& buy_list = buy_orders[good];
79 double demand = 0;
80 for (const Order& order : buy_list) {
81 demand += order.quantity;
82 }
83 return demand;
84 }
85
86 double GetSupply(entt::entity good) {
87 const DescendingSortedOrderList& sell_list = sell_orders[good];
88 double supply = 0;
89 for (const Order& order : sell_list) {
90 supply += order.quantity;
91 }
92 return supply;
93 }
94};
95} // namespace components
96} // namespace common
97} // namespace cqsp
void put(const Order &order)
Definition: auction.h:53
SortedOrderList< std::less< Order > > AscendingSortedOrderList
Smallest to greatest
Definition: auction.h:67
bool operator>(const Order &lhs, const Order &rhs)
Definition: auction.h:47
bool operator<(const Order &lhs, const Order &rhs)
Definition: auction.h:45
SortedOrderList< std::greater< Order > > DescendingSortedOrderList
Greatest to smallest
Definition: auction.h:62
When adding assets, it is extremely crucial that you read cqsp::asset::AssetLoader::LoadResources to ...
Definition: clientctx.h:21
double GetDemand(entt::entity good)
Definition: auction.h:77
double GetSupply(entt::entity good)
Definition: auction.h:86
std::map< entt::entity, DescendingSortedOrderList > sell_orders
Definition: auction.h:70
void AddBuyOrder(entt::entity good, Order &&order)
Definition: auction.h:75
std::map< entt::entity, AscendingSortedOrderList > buy_orders
Definition: auction.h:71
void AddSellOrder(entt::entity good, Order &&order)
Definition: auction.h:73
Definition: auction.h:28
double quantity
Definition: auction.h:36
double price
Price per unit
Definition: auction.h:35
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:42
Order(double price, double quantity, entt::entity agent)
Definition: auction.h:30
Order()
Definition: auction.h:29