-
Notifications
You must be signed in to change notification settings - Fork 21
/
iomarket.cpp
329 lines (275 loc) · 10.8 KB
/
iomarket.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
327
328
329
//////////////////////////////////////////////////////////////////////
// OpenTibia - an opensource roleplaying game
//////////////////////////////////////////////////////////////////////
// IOMarket
//////////////////////////////////////////////////////////////////////
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software Foundation,
// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//////////////////////////////////////////////////////////////////////
#include "otpch.h"
#include "iomarket.h"
#include "iologindata.h"
#include "configmanager.h"
extern ConfigManager g_config;
MarketOfferList IOMarket::getActiveOffers(MarketAction_t action, uint16_t itemId)
{
MarketOfferList offerList;
DBQuery query;
query << "SELECT `id`, `player_id`, `amount`, `price`, `created`, `anonymous` FROM `market_offers` WHERE `sale` = " << action << " AND `itemtype` = " << itemId << ";";
Database* db = Database::getInstance();
DBResult* result;
if(!(result = db->storeQuery(query.str())))
return offerList;
const int32_t marketOfferDuration = g_config.getNumber(ConfigManager::MARKET_OFFER_DURATION);
do
{
MarketOffer offer;
offer.amount = result->getDataInt("amount");
offer.price = result->getDataInt("price");
offer.timestamp = result->getDataInt("created") + marketOfferDuration;
offer.counter = result->getDataInt("id") & 0xFFFF;
if(result->getDataInt("anonymous") == 0)
{
IOLoginData::getInstance()->getNameByGuid(result->getDataInt("player_id"), offer.playerName);
if(offer.playerName.empty())
offer.playerName = "Anonymous";
}
else
offer.playerName = "Anonymous";
offerList.push_back(offer);
}
while(result->next());
db->freeResult(result);
return offerList;
}
MarketOfferList IOMarket::getOwnOffers(MarketAction_t action, uint32_t playerId)
{
MarketOfferList offerList;
const int32_t marketOfferDuration = g_config.getNumber(ConfigManager::MARKET_OFFER_DURATION);
DBQuery query;
query << "SELECT `id`, `amount`, `price`, `created`, `anonymous`, `itemtype` FROM `market_offers` WHERE `player_id` = " << playerId << " AND `sale` = " << action << ";";
Database* db = Database::getInstance();
DBResult* result;
if(!(result = db->storeQuery(query.str())))
return offerList;
do
{
MarketOffer offer;
offer.amount = result->getDataInt("amount");
offer.price = result->getDataInt("price");
offer.timestamp = result->getDataInt("created") + marketOfferDuration;
offer.counter = result->getDataInt("id") & 0xFFFF;
offer.itemId = result->getDataInt("itemtype");
offerList.push_back(offer);
}
while(result->next());
db->freeResult(result);
return offerList;
}
HistoryMarketOfferList IOMarket::getOwnHistory(MarketAction_t action, uint32_t playerId)
{
HistoryMarketOfferList offerList;
DBQuery query;
query << "SELECT `id`, `itemtype`, `amount`, `price`, `expires_at`, `state` FROM `market_history` WHERE `player_id` = " << playerId << " AND `sale` = " << action << ";";
Database* db = Database::getInstance();
DBResult* result;
if(!(result = db->storeQuery(query.str())))
return offerList;
do
{
HistoryMarketOffer offer;
offer.itemId = result->getDataInt("itemtype");
offer.amount = result->getDataInt("amount");
offer.price = result->getDataInt("price");
offer.timestamp = result->getDataInt("expires_at");
MarketOfferState_t offerState = (MarketOfferState_t)result->getDataInt("state");
if(offerState == OFFERSTATE_ACCEPTEDEX)
offerState = OFFERSTATE_ACCEPTED;
offer.state = offerState;
offerList.push_back(offer);
}
while(result->next());
db->freeResult(result);
return offerList;
}
ExpiredMarketOfferList IOMarket::getExpiredOffers(MarketAction_t action)
{
ExpiredMarketOfferList offerList;
const time_t lastExpireDate = time(NULL) - g_config.getNumber(ConfigManager::MARKET_OFFER_DURATION);
DBQuery query;
query << "SELECT `id`, `amount`, `price`, `itemtype`, `player_id` FROM `market_offers` WHERE `sale` = " << action << " AND `created` <= " << lastExpireDate << ";";
Database* db = Database::getInstance();
DBResult* result;
if(!(result = db->storeQuery(query.str())))
return offerList;
do
{
ExpiredMarketOffer offer;
offer.id = result->getDataInt("id");
offer.amount = result->getDataInt("amount");
offer.price = result->getDataInt("price");
offer.itemId = result->getDataInt("itemtype");
offer.playerId = result->getDataInt("player_id");
offerList.push_back(offer);
}
while(result->next());
db->freeResult(result);
return offerList;
}
int32_t IOMarket::getPlayerOfferCount(uint32_t playerId)
{
int32_t count = -1;
Database* db = Database::getInstance();
DBResult* result;
DBQuery query;
query << "SELECT COUNT(*) AS `count` FROM `market_offers` WHERE `player_id` = " << playerId << ";";
if(!(result = db->storeQuery(query.str())))
return count;
count = result->getDataInt("count");
db->freeResult(result);
return count;
}
MarketOfferEx IOMarket::getOfferById(uint32_t id)
{
MarketOfferEx offer;
DBQuery query;
query << "SELECT `id`, `sale`, `itemtype`, `amount`, `created`, `price`, `player_id`, `anonymous` FROM `market_offers` WHERE `id` = " << id << ";";
Database* db = Database::getInstance();
DBResult* result;
if((result = db->storeQuery(query.str())))
{
offer.type = (MarketAction_t)result->getDataInt("sale");
offer.amount = result->getDataInt("amount");
offer.counter = result->getDataInt("id") & 0xFFFF;
offer.timestamp = result->getDataInt("created");
offer.price = result->getDataInt("price");
offer.itemId = result->getDataInt("itemtype");
int32_t playerId = result->getDataInt("player_id");
offer.playerId = playerId;
if(result->getDataInt("anonymous") == 0)
{
IOLoginData::getInstance()->getNameByGuid(playerId, offer.playerName);
if(offer.playerName.empty())
offer.playerName = "Anonymous";
}
else
offer.playerName = "Anonymous";
db->freeResult(result);
}
return offer;
}
uint32_t IOMarket::getOfferIdByCounter(uint32_t timestamp, uint16_t counter)
{
const int32_t created = timestamp - g_config.getNumber(ConfigManager::MARKET_OFFER_DURATION);
DBQuery query;
query << "SELECT `id` FROM `market_offers` WHERE `created` = " << created << " AND (`id` & 65535) = " << counter << " LIMIT 1;";
Database* db = Database::getInstance();
DBResult* result;
if((result = db->storeQuery(query.str())))
{
uint32_t offerId = result->getDataInt("id");
db->freeResult(result);
return offerId;
}
return 0;
}
void IOMarket::createOffer(uint32_t playerId, MarketAction_t action, uint32_t itemId, uint16_t amount, uint32_t price, bool anonymous)
{
DBQuery query;
query << "INSERT INTO `market_offers` (`player_id`, `sale`, `itemtype`, `amount`, `price`, `created`, `anonymous`) VALUES (" << playerId << ", " << action << ", " << itemId << ", " << amount << ", " << price << ", " << time(NULL) << ", " << anonymous << ");";
Database::getInstance()->executeQuery(query.str());
}
void IOMarket::acceptOffer(uint32_t offerId, uint16_t amount)
{
DBQuery query;
query << "UPDATE `market_offers` SET `amount` = `amount` - " << amount << " WHERE `id` = " << offerId << ";";
Database::getInstance()->executeQuery(query.str());
}
void IOMarket::deleteOffer(uint32_t offerId)
{
DBQuery query;
query << "DELETE FROM `market_offers` WHERE `id` = " << offerId << ";";
Database::getInstance()->executeQuery(query.str());
}
void IOMarket::appendHistory(uint32_t playerId, MarketAction_t type, uint16_t itemId, uint16_t amount, uint32_t price, time_t timestamp, MarketOfferState_t state)
{
DBQuery query;
query << "INSERT INTO `market_history` (`player_id`, `sale`, `itemtype`, `amount`, `price`, `expires_at`, `inserted`, `state`) VALUES "
<< "(" << playerId << ", " << type << ", " << itemId << ", " << amount << ", " << price << ", "
<< timestamp << ", " << time(NULL) << ", " << state << ");";
Database::getInstance()->executeQuery(query.str());
}
void IOMarket::moveOfferToHistory(uint32_t offerId, MarketOfferState_t state)
{
const int32_t marketOfferDuration = g_config.getNumber(ConfigManager::MARKET_OFFER_DURATION);
Database* db = Database::getInstance();
DBQuery query;
DBResult* result;
query << "SELECT `player_id`, `sale`, `itemtype`, `amount`, `price`, `created` FROM `market_offers` WHERE `id` = " << offerId << ";";
if(!(result = db->storeQuery(query.str())))
return;
query.str("");
query << "DELETE FROM `market_offers` WHERE `id` = " << offerId << ";";
if(!db->executeQuery(query.str()))
{
db->freeResult(result);
return;
}
appendHistory(result->getDataInt("player_id"), (MarketAction_t)result->getDataInt("sale"), result->getDataInt("itemtype"), result->getDataInt("amount"), result->getDataInt("price"), result->getDataInt("created") + marketOfferDuration, state);
db->freeResult(result);
}
void IOMarket::clearOldHistory()
{
const time_t lastExpireDate = time(NULL) - g_config.getNumber(ConfigManager::MARKET_OFFER_DURATION);
DBQuery query;
query << "DELETE FROM `market_history` WHERE `inserted` <= " << lastExpireDate << ";";
Database::getInstance()->executeQuery(query.str());
}
void IOMarket::updateStatistics()
{
Database* db = Database::getInstance();
DBQuery query;
query << "SELECT `sale` AS `sale`, `itemtype` AS `itemtype`, COUNT(`price`) AS `num`, MIN(`price`) AS `min`, MAX(`price`) AS `max`, SUM(`price`) AS `sum` FROM `market_history` WHERE `state` = " << OFFERSTATE_ACCEPTED << " GROUP BY `itemtype`, `sale`;";
DBResult* result;
if(!(result = db->storeQuery(query.str())))
return;
do
{
MarketStatistics* statistics;
if(result->getDataInt("sale") == MARKETACTION_BUY)
statistics = &purchaseStatistics[result->getDataInt("itemtype")];
else
statistics = &saleStatistics[result->getDataInt("itemtype")];
statistics->numTransactions = result->getDataInt("num");
statistics->lowestPrice = result->getDataInt("min");
statistics->totalPrice = result->getDataLong("sum");
statistics->highestPrice = result->getDataInt("max");
}
while(result->next());
db->freeResult(result);
}
MarketStatistics* IOMarket::getPurchaseStatistics(uint16_t itemId)
{
std::map<uint16_t, MarketStatistics>::iterator it = purchaseStatistics.find(itemId);
if(it == purchaseStatistics.end())
return NULL;
return &it->second;
}
MarketStatistics* IOMarket::getSaleStatistics(uint16_t itemId)
{
std::map<uint16_t, MarketStatistics>::iterator it = saleStatistics.find(itemId);
if(it == saleStatistics.end())
return NULL;
return &it->second;
}