-
-
Notifications
You must be signed in to change notification settings - Fork 661
/
Copy pathio_store.cpp
364 lines (308 loc) · 10.5 KB
/
io_store.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/**
* Canary - A free and open-source MMORPG server emulator
* Copyright (©) 2019-2022 OpenTibiaBR <[email protected]>
* Repository: https://github.com/opentibiabr/canary
* License: https://github.com/opentibiabr/canary/blob/main/LICENSE
* Contributors: https://github.com/opentibiabr/canary/graphs/contributors
* Website: https://docs.opentibiabr.org/
*/
#include "pch.hpp"
#include "io/io_store.hpp"
#include "creatures/monsters/monsters.hpp"
#include "creatures/players/player.hpp"
#include "utils/tools.hpp"
bool IOStore::loadFromXml() {
pugi::xml_document doc;
auto folder = g_configManager().getString(CORE_DIRECTORY, __FUNCTION__) + "/XML/store/store.xml";
if (!doc.load_file(folder.c_str())) {
printXMLError(__FUNCTION__, folder, doc.load_file(folder.c_str()));
consoleHandlerExit();
return false;
}
auto storeNode = doc.child("store");
pugi::xml_node homeNode = storeNode.child("home");
if (!loadStoreHome(homeNode)) {
return false;
}
for (pugi::xml_node category : storeNode.children("category")) {
auto categoryName = std::string(category.attribute("name").as_string());
auto categoryIcon = std::string(category.attribute("icon").as_string());
auto categoryRookString = std::string(category.attribute("rookgaard").as_string());
bool categoryRook;
if (categoryRookString == "yes") {
categoryRook = true;
} else {
categoryRook = false;
}
Category newCategory(categoryName, categoryIcon, categoryRook);
pugi::xml_node child = category.first_child();
if (child && std::string(child.name()) == "subcategory") {
for (pugi::xml_node subcategory : category.children("subcategory")) {
auto subCategoryName = std::string(subcategory.attribute("name").as_string());
auto subCategoryIcon = std::string(subcategory.attribute("icon").as_string());
auto subCategoryRookString = std::string(category.attribute("rookgaard").as_string());
bool subCategoryRook;
if (subCategoryRookString == "yes") {
subCategoryRook = true;
} else {
subCategoryRook = false;
}
auto subCategoryStateString = std::string(subcategory.attribute("state").as_string());
States_t subCategoryState;
if (auto it = stringToOfferStateMap.find(subCategoryStateString);
it != stringToOfferStateMap.end()) {
subCategoryState = it->second;
} else {
subCategoryState = States_t::NONE;
}
Category newSubCategory(subCategoryName, subCategoryIcon, subCategoryRook, subCategoryState);
for (pugi::xml_node offer : subcategory.children("offer")) {
auto thisOffer = loadOfferFromXml(&newSubCategory, offer);
if (!thisOffer) {
return false;
}
}
subCategoryVector.push_back(newSubCategory);
newCategory.addSubCategory(newSubCategory);
}
} else if (child && std::string(child.name()) == "offer") {
newCategory.setSpecialCategory(true);
for (pugi::xml_node offer : category.children("offer")) {
auto thisOffer = loadOfferFromXml(&newCategory, offer);
if (!thisOffer) {
return false;
}
}
}
addCategory(newCategory);
}
return true;
}
bool IOStore::loadOfferFromXml(Category* category, pugi::xml_node offer) {
auto name = std::string(offer.attribute("name").as_string());
if (name.empty()) {
g_logger().warn("Offer name empty.");
return false;
}
std::string icon = "";
if (offer.attribute("icon")) {
icon = std::string(offer.attribute("icon").as_string());
}
auto id = static_cast<uint32_t>(offer.attribute("offerId").as_uint());
if (id == 0) {
g_logger().warn("Offer {} id is 0.", name);
return false;
}
auto price = static_cast<uint32_t>(offer.attribute("price").as_uint());
if (price == 0) {
g_logger().warn("Offer {} price is 0.", name);
return false;
}
auto typeString = std::string(offer.attribute("type").as_string());
OfferTypes_t type;
if (auto it = stringToOfferTypeMap.find(typeString);
it != stringToOfferTypeMap.end()) {
type = it->second;
} else {
g_logger().warn("Offer {} type is none.", name);
return false;
}
OutfitIds outfitId;
if (type == OfferTypes_t::OUTFIT || type == OfferTypes_t::HIRELING) {
auto femaleId = static_cast<uint16_t>(offer.attribute("female").as_uint());
auto maleId = static_cast<uint16_t>(offer.attribute("male").as_uint());
outfitId.femaleId = femaleId;
outfitId.maleId = maleId;
}
auto stateString = std::string(offer.attribute("state").as_string());
States_t state = States_t::NONE;
if (auto it = stringToOfferStateMap.find(stateString);
it != stringToOfferStateMap.end()) {
state = it->second;
}
uint16_t count = 1;
if (offer.attribute("count")) {
count = static_cast<uint16_t>(offer.attribute("count").as_uint());
}
uint16_t validUntil = 0;
if (offer.attribute("validUntil")) {
validUntil = static_cast<uint16_t>(offer.attribute("validUntil").as_uint());
}
CoinType coinType = CoinType::Normal;
if (offer.attribute("coinType")) {
coinType = CoinType::Normal;
}
std::string desc = "";
if (offer.attribute("description")) {
desc = std::string(offer.attribute("description").as_string());
}
bool isMovable = false;
if (offer.attribute("movable")) {
isMovable = bool(offer.attribute("movable").as_bool());
}
auto parentName = category->getCategoryName();
Offer newOffer(parentName, name, icon, id, price, type, state, count, validUntil, coinType, desc, outfitId, isMovable);
addOffer(id, newOffer);
category->addOffer(newOffer);
return true;
}
bool IOStore::loadStoreHome(pugi::xml_node homeNode) {
auto bannersNode = homeNode.child("banners");
auto bannerDelay = static_cast<uint8_t>(bannersNode.attribute("delay").as_uint());
setBannerDelay(bannerDelay);
pugi::xml_node bannersChild = bannersNode.first_child();
if (bannersChild && std::string(bannersChild.name()) == "banner") {
for (pugi::xml_node banner : bannersNode.children("banner")) {
BannerInfo tempBanner;
tempBanner.bannerName = std::string(banner.attribute("path").as_string());
if (tempBanner.bannerName.empty()) {
return false;
}
tempBanner.offerId = static_cast<uint32_t>(banner.attribute("bannerOfferId").as_uint());
if (tempBanner.offerId == 0) {
return false;
}
banners.push_back(tempBanner);
}
} else {
return false;
}
auto homeOffersNode = homeNode.child("offers");
pugi::xml_node homeOffersChild = homeOffersNode.first_child();
if (homeOffersChild && std::string(homeOffersChild.name()) == "offer") {
for (pugi::xml_node offer : homeOffersNode.children("offer")) {
auto homeOfferId = static_cast<uint32_t>(offer.attribute("id").as_uint());
homeOffers.push_back(homeOfferId);
}
}
return true;
}
std::vector<Category> IOStore::getCategoryVector() const {
return categoryVector;
}
void IOStore::addCategory(Category newCategory) {
for (const auto &category : categoryVector) {
if (newCategory.getCategoryName() == category.getCategoryName()) {
return;
}
}
categoryVector.push_back(newCategory);
}
const Category* IOStore::getCategoryByName(std::string categoryName) const {
for (const auto &category : categoryVector) {
if (categoryName == category.getCategoryName()) {
return &category;
}
}
return nullptr;
}
const Category* IOStore::getSubCategoryByName(std::string subCategoryName) const {
for (const auto &subCategory : subCategoryVector) {
if (subCategoryName == subCategory.getCategoryName()) {
return &subCategory;
}
}
return nullptr;
}
void IOStore::addOffer(uint32_t offerId, Offer offer) {
if (auto it = offersMap.find(offerId);
it != offersMap.end()) {
return;
}
offersMap.try_emplace(offerId, offer);
auto newOffer = std::pair<uint32_t, Offer>(offerId, offer);
offersMap.insert(newOffer);
}
const Offer* IOStore::getOfferById(uint32_t offerId) const {
if (auto it = offersMap.find(offerId);
it != offersMap.end()) {
return &it->second;
}
return nullptr;
}
std::vector<Offer> IOStore::getOffersContainingSubstring(const std::string &searchString) {
std::vector<Offer> offersVector;
auto lowerSearchString = asLowerCaseString(searchString);
for (const auto &offer : offersMap) {
auto currentOfferName = offer.second.getOfferName();
auto lowerCurrentOfferName = asLowerCaseString(currentOfferName);
if (lowerCurrentOfferName.find(lowerSearchString) != std::string::npos) {
offersVector.push_back(offer.second);
}
}
return offersVector;
}
std::vector<BannerInfo> IOStore::getBannersVector() const {
return banners;
}
std::vector<uint32_t> IOStore::getHomeOffersVector() const {
return homeOffers;
}
uint32_t IOStore::getBannerDelay() const {
return bannerDelay;
}
void IOStore::setBannerDelay(uint8_t delay) {
bannerDelay = delay;
}
const Category* IOStore::findCategory(std::string categoryName) {
auto currentCategory = getCategoryByName(categoryName);
if (!currentCategory) {
currentCategory = getSubCategoryByName(categoryName);
return currentCategory;
}
if (currentCategory->isSpecialCategory()) {
return currentCategory;
}
auto subCat = currentCategory->getFirstSubCategory();
return subCat;
}
const std::vector<std::string> IOStore::getOffersDisableReasonVector() {
return offersDisableReason;
}
// Category Class functions
const Category* Category::getFirstSubCategory() const {
return &subCategories.at(0);
}
std::vector<Category> Category::getSubCategoriesVector() const {
return subCategories;
}
void Category::addSubCategory(Category newSubCategory) {
for (const auto &subCategory : subCategories) {
if (newSubCategory.getCategoryName() == subCategory.getCategoryName()) {
return;
}
}
subCategories.push_back(newSubCategory);
}
std::vector<Offer> Category::getOffersVector() const {
return offers;
}
void Category::addOffer(Offer newOffer) {
for (const auto &offer : offers) {
if (newOffer.getOfferId() == offer.getOfferId()
&& newOffer.getOfferName() == offer.getOfferName()
&& newOffer.getOfferCount() == offer.getOfferCount()) {
return;
}
}
offers.push_back(newOffer);
}
// Offer Functions
ConverType_t Offer::getConverType() const {
if (offerType == OfferTypes_t::MOUNT) {
return ConverType_t::MOUNT;
} else if (offerType == OfferTypes_t::OUTFIT) {
return ConverType_t::OUTFIT;
} else if (offerType == OfferTypes_t::ITEM || offerType == OfferTypes_t::STACKABLE || offerType == OfferTypes_t::HOUSE || offerType == OfferTypes_t::CHARGES || offerType == OfferTypes_t::POUCH) {
return ConverType_t::ITEM;
} else if (offerType == OfferTypes_t::HIRELING) {
return ConverType_t::HIRELING;
}
return ConverType_t::NONE;
}
bool Offer::getUseConfigure() const {
if (offerType == OfferTypes_t::NAMECHANGE || offerType == OfferTypes_t::HIRELING || offerType == OfferTypes_t::HIRELING_NAMECHANGE) {
return true;
}
return false;
}