-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathItemRequest.cpp
326 lines (274 loc) · 6.36 KB
/
ItemRequest.cpp
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#pragma once
#ifndef WORLDSIM_ITEM_REQUEST_CPP
#define WORLDSIM_ITEM_REQUEST_CPP
/* WorldSim: ItemRequest
#include "ItemRequest.cpp"
Requests for an Item to be made. Requests can be made by any entity with a HasMoney interface, typically
Characters or Government.
Item requests should be more general, like "hunting items with minimum level 1"
we might prefer some kind of functionality request. Eg "An item for digging". "An item which can shoot".
We might need to build a table of item and functionality for different basic tasks
*/
#include <set>
#include <optional>
class ItemRequestCategory
{
public:
CanRequestItem * requester;
ItemAction category;
int value;
int minimumLevel;
bool privateContract;
ItemRequestCategory(CanRequestItem* c, ItemAction t, int v, int m) :
requester(c), category(t), value(v), minimumLevel(m)
{
privateContract=true;
}
std::string toString() const
{
return "Unknown";
}
};
// Comparator for ItemRequestCategory
struct ItemRequestCategoryComparator
{
bool operator() (const ItemRequestCategory& lhs, const ItemRequestCategory& rhs) const
{
// Sort in descending order of value
return lhs.value > rhs.value;
}
};
class ItemRequest
{
public:
CanRequestItem * requester;
ItemType type;
int value;
bool privateContract;
ItemRequest(CanRequestItem* c, ItemType t, int v) : requester(c), type(t), value(v)
{
privateContract=true;
}
std::string toString() const
{
std::string itemType = "UNKNOWN";
if ( type == ITEM_HOE )
{
itemType = "HOE";
}
else if ( type == ITEM_PICKAXE )
{
itemType = "PICKAXE";
}
else if ( type == ITEM_AXE )
{
itemType = "AXE";
}
else if ( type == ITEM_LONGBOW )
{
itemType = "LONGBOW";
}
std::string str = "Request for "+itemType+" at $"+DataTools::toString(value)+".";
return str;
}
};
// Comparator for ItemRequest
struct ItemRequestComparator
{
bool operator() (const ItemRequest& lhs, const ItemRequest& rhs) const
{
// Sort in descending order of value
return lhs.value > rhs.value;
}
};
class ItemRequestManager
{
private:
std::multiset<ItemRequest, ItemRequestComparator> requests;
std::multiset<ItemRequestCategory, ItemRequestCategoryComparator> categoryRequests;
public:
ItemRequestManager()
{
}
void add(CanRequestItem *requester, ItemType type, int value, bool deleteDuplicates = true)
{
if (requester->takeMoney(value))
{
if (deleteDuplicates)
{
for (auto it = requests.begin(); it != requests.end(); )
{
if (it->requester == requester && it->type == type)
{
requester->giveMoney(it->value);
it = requests.erase(it);
continue;
}
++it;
}
}
requests.insert(ItemRequest(requester, type, value));
}
}
void add
(CanRequestItem *requester, ItemAction category, int value, int minimumLevel, bool deleteDuplicates = true)
{
if (requester->takeMoney(value))
{
if (deleteDuplicates)
{
for (auto it = categoryRequests.begin(); it != categoryRequests.end(); )
{
if (it->requester == requester && it->category == category)
{
requester->giveMoney(it->value);
it = categoryRequests.erase(it);
continue;
}
++it;
}
}
categoryRequests.insert(ItemRequestCategory(requester, category, value, minimumLevel));
}
}
void removeAll(HasMoney* requester, ItemType type)
{
for (auto it = requests.begin(); it != requests.end(); )
{
if (it->requester == requester && it->type == type)
{
requester->giveMoney(it->value);
it = requests.erase(it);
continue;
}
++it;
}
}
void removeAll(HasMoney* requester, ItemAction category)
{
for (auto it = categoryRequests.begin(); it != categoryRequests.end(); )
{
if (it->requester == requester && it->category == category)
{
requester->giveMoney(it->value);
it = categoryRequests.erase(it);
continue;
}
++it;
}
}
std::optional<ItemRequest> pullMostValuableRequest(bool returnZeroValues)
{
if (requests.empty())
{
return std::nullopt;
}
auto it = requests.begin();
while (it != requests.end())
{
const ItemRequest& mostValuableRequest = *it;
if (mostValuableRequest.value > 0 || returnZeroValues)
{
requests.erase(it);
return mostValuableRequest;
}
++it;
}
return std::nullopt;
}
std::optional<ItemRequestCategory> pullMostValuableRequestCategory(bool returnZeroValues)
{
if (categoryRequests.empty())
{
return std::nullopt;
}
auto it = categoryRequests.begin();
while (it != categoryRequests.end())
{
const ItemRequestCategory& mostValuableRequest = *it;
if (mostValuableRequest.value > 0 || returnZeroValues)
{
categoryRequests.erase(it);
return mostValuableRequest;
}
++it;
}
return std::nullopt;
}
double getAverageValue(ItemType 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 static_cast<double>(totalValue) / count;
}
double getAverageValue(ItemAction category, int minLevel) const
{
int totalValue = 0;
int count = 0;
for (const auto& request : categoryRequests)
{
if (request.category == category && request.minimumLevel == minLevel)
{
totalValue += request.value;
++count;
}
}
if (count == 0)
{
return 0.0; // Avoid division by zero
}
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()
{
std::cout << "Individual Item Requests:\n";
for (const auto& request : requests)
{
std::cout << request.toString() << "\n";
}
std::cout << "\nCategory Requests:\n";
for (const auto& categoryRequest : categoryRequests)
{
std::cout << categoryRequest.toString() << "\n";
}
}
};
#endif // WORLDSIM_ITEM_REQUEST_CPP