98 lines
3.5 KiB
C++
98 lines
3.5 KiB
C++
#include "configs/shops.h"
|
|
#include "project_parser.h"
|
|
#include <iostream>
|
|
|
|
namespace cursebreaker {
|
|
|
|
// Static helper functions
|
|
static std::string getAttributeValue(tinyxml2::XMLElement* element, const char* attribute, const std::string& defaultValue = "");
|
|
static int getAttributeValueInt(tinyxml2::XMLElement* element, const char* attribute, int defaultValue = 0);
|
|
static bool getAttributeValueBool(tinyxml2::XMLElement* element, const char* attribute, bool defaultValue = false);
|
|
static void parseShop(tinyxml2::XMLElement* shopElement);
|
|
static void parseInventory(tinyxml2::XMLElement* shopElement, Shop& shop);
|
|
|
|
bool loadShopsFromXML(const std::string& filepath) {
|
|
tinyxml2::XMLDocument doc;
|
|
tinyxml2::XMLError result = doc.LoadFile(filepath.c_str());
|
|
|
|
if (result != tinyxml2::XML_SUCCESS) {
|
|
std::cerr << "Failed to load shops XML file: " << filepath << std::endl;
|
|
return false;
|
|
}
|
|
|
|
tinyxml2::XMLElement* root = doc.FirstChildElement("shops");
|
|
if (!root) {
|
|
std::cerr << "Invalid XML structure: missing 'shops' root element" << std::endl;
|
|
return false;
|
|
}
|
|
|
|
g_parsedProject.m_shops.clear();
|
|
|
|
// Parse all shops
|
|
for (tinyxml2::XMLElement* shopElement = root->FirstChildElement("shop");
|
|
shopElement != nullptr;
|
|
shopElement = shopElement->NextSiblingElement("shop")) {
|
|
parseShop(shopElement);
|
|
}
|
|
|
|
std::cout << "Loaded " << g_parsedProject.m_shops.size() << " shops from XML" << std::endl;
|
|
return true;
|
|
}
|
|
|
|
void parseShop(tinyxml2::XMLElement* shopElement) {
|
|
Shop shop;
|
|
|
|
// Parse basic attributes
|
|
shop.id = static_cast<uint16_t>(getAttributeValueInt(shopElement, "id"));
|
|
shop.name = getAttributeValue(shopElement, "name");
|
|
shop.description = getAttributeValue(shopElement, "description");
|
|
shop.location = getAttributeValue(shopElement, "location");
|
|
|
|
// Parse child elements
|
|
parseInventory(shopElement, shop);
|
|
|
|
g_parsedProject.m_shops[shop.id] = std::move(shop);
|
|
}
|
|
|
|
void parseInventory(tinyxml2::XMLElement* shopElement, Shop& shop) {
|
|
for (tinyxml2::XMLElement* itemElement = shopElement->FirstChildElement("item");
|
|
itemElement != nullptr;
|
|
itemElement = itemElement->NextSiblingElement("item")) {
|
|
|
|
ShopItem item;
|
|
item.itemId = static_cast<uint16_t>(getAttributeValueInt(itemElement, "itemid"));
|
|
item.price = static_cast<uint16_t>(getAttributeValueInt(itemElement, "price"));
|
|
item.stock = static_cast<uint16_t>(getAttributeValueInt(itemElement, "stock"));
|
|
item.levelRequired = static_cast<uint16_t>(getAttributeValueInt(itemElement, "levelrequired"));
|
|
|
|
shop.inventory.push_back(item);
|
|
}
|
|
}
|
|
|
|
const Shop* getShopById(uint16_t id) {
|
|
auto it = g_parsedProject.m_shops.find(id);
|
|
return (it != g_parsedProject.m_shops.end()) ? &it->second : nullptr;
|
|
}
|
|
|
|
const std::unordered_map<uint16_t, Shop>& getAllShops() {
|
|
return g_parsedProject.m_shops;
|
|
}
|
|
|
|
std::string getAttributeValue(tinyxml2::XMLElement* element, const char* attribute, const std::string& defaultValue) {
|
|
const char* value = element->Attribute(attribute);
|
|
return value ? std::string(value) : defaultValue;
|
|
}
|
|
|
|
int getAttributeValueInt(tinyxml2::XMLElement* element, const char* attribute, int defaultValue) {
|
|
int value = defaultValue;
|
|
element->QueryIntAttribute(attribute, &value);
|
|
return value;
|
|
}
|
|
|
|
bool getAttributeValueBool(tinyxml2::XMLElement* element, const char* attribute, bool defaultValue) {
|
|
bool value = defaultValue;
|
|
element->QueryBoolAttribute(attribute, &value);
|
|
return value;
|
|
}
|
|
|
|
} // namespace cursebreaker
|