-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLocationRequest.cpp
More file actions
222 lines (181 loc) · 4.44 KB
/
LocationRequest.cpp
File metadata and controls
222 lines (181 loc) · 4.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#pragma once
#ifndef WORLDSIM_LOCATION_REQUEST_CPP
#define WORLDSIM_LOCATION_REQUEST_CPP
/* WorldSim: LocationRequest
#include "LocationRequest.cpp"
Requests for an location to be built. Requests can be made by any entity with a HasMoney interface, typically
Characters or Government.
*/
#include <set>
#include <optional>
class LocationRequest
{
public:
HasMoney * requester;
enumLocation type;
int value;
bool privateContract;
LocationRequest(HasMoney* c, enumLocation t, int v) : requester(c), type(t), value(v)
{
privateContract=true;
}
std::string toString() const
{
std::string locationType = "UNKNOWN_LOCATION";
if ( type == LOCATION_HALL )
{
locationType = "HALL";
}
else if ( type == LOCATION_MINE )
{
locationType = "MINE";
}
else if ( type == LOCATION_DWELLING )
{
locationType = "DWELLING";
}
else if ( type == LOCATION_FARM )
{
locationType = "FARM";
}
std::string str = "Request for "+locationType+" at $"+DataTools::toString(value)+".";
return str;
}
};
// Comparator for LocationRequest
struct LocationRequestComparator
{
bool operator() (const LocationRequest& lhs, const LocationRequest& rhs) const
{
// Sort in descending order of value
return lhs.value > rhs.value;
}
};
class LocationRequestManager
{
private:
std::multiset<LocationRequest, LocationRequestComparator> requests;
public:
LocationRequestManager()
{
}
void add(HasMoney *requester, enumLocation type, int value, bool deleteDuplicates=true)
{
// if ( value == 0 )
// {
// return;
// }
// If the requester has enough money, add the new request
if (requester->takeMoney(value))
{
if ( deleteDuplicates )
{
// Check if the requester already has a request for the same type and remove them.
for (auto it = requests.begin(); it != requests.end(); )
{
if (it->requester == requester && it->type == type)
{
requester->giveMoney(it->value);
//std::cout<<"Money refunded from contract: "<<it->value<<"\n";
it = requests.erase(it); // Erase and move to the next element
continue;
}
++it;
}
}
requests.insert(LocationRequest(requester, type, value));
}
}
void addTreasuryRequest(Character *requester, Stockpile* money, Stockpile* stockpile, enumLocation type, int value)
{
requests.insert(LocationRequest(requester, type, value));
}
void removeAll(HasMoney* requester, enumLocation type)
{
// Remove requests of type and refund
for (auto it = requests.begin(); it != requests.end(); )
{
if (it->requester == requester && it->type == type)
{
requester->giveMoney(it->value);
//std::cout<<"Money refunded from contract: "<<it->value<<"\n";
it = requests.erase(it); // Erase and move to the next element
continue;
}
++it;
}
}
std::optional<LocationRequest> pullMostValuableRequest(bool returnZeroValues)
{
if (requests.empty())
{
return std::nullopt; // Return an empty optional if there are no requests
}
auto it = requests.begin(); // Iterator to the least valuable request
while (it != requests.end())
{
const LocationRequest& mostValuableRequest = *it;
if (mostValuableRequest.value > 0 || returnZeroValues)
{
requests.erase(it); // Erase the most valuable request from the set
return mostValuableRequest; // Return the most valuable request
}
++it; // Move to the next request
}
return std::nullopt; // Return an empty optional if no suitable request is found
}
double getAverageValue(enumLocation type) const
{
int totalValue = 0;
int count = 0;
for (const auto& request : requests)
{
if (request.type == type)
{
totalValue += request.value;
++count;
}
}
if (count == 0)
{
return 0.0; // Return 0 if there are no requests of the specified type
}
return static_cast<double>(totalValue) / count;
}
int getTotalValue() const
{
int totalValue = 0;
for (const auto& request : requests)
{
totalValue += request.value;
}
return totalValue;
}
int getNumContracts() const
{
int totalContracts = 0;
for (const auto& request : requests)
{
totalContracts++;
}
return totalContracts;
}
bool empty() const
{
return requests.empty();
}
int size()
{
return requests.size();
}
void print()
{
// Remove requests of type and refund
for (auto it = requests.begin(); it != requests.end(); )
{
std::cout<<it->toString()<<"\n";
++it;
}
}
};
#endif // WORLDSIM_ITEM_REQUEST_CPP