M_shop.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #include "stdafx.h"
  2. #include "tinyxml2/tinyxml2.h"
  3. #include "LuaCfgHelper.h"
  4. #include <iostream>
  5. #include <boost/algorithm/string.hpp>
  6. #include "M_shop.h"
  7. std::auto_ptr<M_shop> M_shop::msSingleton(nullptr);
  8. int M_shop::GetCount()
  9. {
  10. return (int)mMapData.size();
  11. }
  12. const M_shopData* M_shop::GetData(int ID)
  13. {
  14. auto it = mMapData.find(ID);
  15. if (it != mMapData.end())
  16. {
  17. return &it->second;
  18. }
  19. return NULL;
  20. }
  21. const std::map<int, M_shopData>& M_shop::GetMapData()
  22. {
  23. return mMapData;
  24. }
  25. void M_shop::Load()
  26. {
  27. tinyxml2::XMLDocument xmlDoc;
  28. std::string content = FileUtils::getInstance()->getStringFromFile("Config/M_shop.xml");
  29. auto result = xmlDoc.Parse(content.c_str(), content.length());
  30. if (result != tinyxml2::XML_SUCCESS)
  31. {
  32. CCLOGERROR("Result:%d", result);
  33. CCASSERT(false, "result != tinyxml2::XML_SUCCESS");
  34. return;
  35. }
  36. auto root = xmlDoc.RootElement();
  37. if (root == NULL)
  38. {
  39. CCASSERT(false, "root == NULL");
  40. return;
  41. }
  42. auto element = root->FirstChildElement("Data");
  43. while (element != NULL)
  44. {
  45. M_shopData data;
  46. data.mID = element->IntAttribute("ID");
  47. data.mshop_type = element->IntAttribute("shop_type");
  48. {
  49. const char* cost_Item = element->Attribute("cost_Item");
  50. std::vector<std::string> veccost_Item;
  51. boost::split(veccost_Item, cost_Item, boost::is_any_of(","));
  52. int temp;
  53. for (unsigned int i = 0; i < veccost_Item.size(); i++)
  54. {
  55. if (tinyxml2::XMLUtil::ToInt(veccost_Item[i].c_str(), &temp))
  56. {
  57. data.mcost_Item.push_back(temp);
  58. }
  59. }
  60. }
  61. {
  62. const char* reward = element->Attribute("reward");
  63. std::vector<std::string> vecreward;
  64. boost::split(vecreward, reward, boost::is_any_of(","));
  65. int temp;
  66. for (unsigned int i = 0; i < vecreward.size(); i++)
  67. {
  68. if (tinyxml2::XMLUtil::ToInt(vecreward[i].c_str(), &temp))
  69. {
  70. data.mreward.push_back(temp);
  71. }
  72. }
  73. }
  74. data.micon = element->Attribute("icon");
  75. data.msort = element->IntAttribute("sort");
  76. data.mcondition = element->IntAttribute("condition");
  77. data.mvalue = element->Attribute("value");
  78. if (mMapData.find(data.mID) != mMapData.end())std::cout <<"data refind:" << data.mID << std::endl;
  79. CCASSERT(mMapData.find(data.mID) == mMapData.end(), "data.mID is exists");
  80. mMapData.insert(std::make_pair(data.mID, data));
  81. element = element->NextSiblingElement();
  82. }
  83. CCLOG("M_shop Loaded. Load Data:%u", mMapData.size());
  84. }
  85. void M_shop::LoadLua()
  86. {
  87. LuaEngine::getInstance()->executeScriptFile("config/M_shop");
  88. lua_State* L = LuaEngine::getInstance()->getLuaStack()->getLuaState();
  89. lua_getglobal(L, "M_shop");
  90. CCASSERT(lua_istable(L, -1) == 1, "is not table");
  91. lua_pushstring(L, "datas");
  92. lua_gettable(L, -2);
  93. CCASSERT(lua_istable(L, -1) == 1, "is not table");
  94. lua_pushnil(L);
  95. while(lua_next(L, 2))
  96. {
  97. CCASSERT(lua_istable(L, -1) == 1, "is not table");
  98. M_shopData data;
  99. LuaCfgHelper::readInt(L, "ID", data.mID);
  100. LuaCfgHelper::readInt(L, "shop_type", data.mshop_type);
  101. LuaCfgHelper::readVectorInt(L, "cost_Item", data.mcost_Item);
  102. LuaCfgHelper::readVectorInt(L, "reward", data.mreward);
  103. LuaCfgHelper::readString(L, "icon", data.micon);
  104. LuaCfgHelper::readInt(L, "sort", data.msort);
  105. LuaCfgHelper::readInt(L, "condition", data.mcondition);
  106. LuaCfgHelper::readString(L, "value", data.mvalue);
  107. if (mMapData.find(data.mID) != mMapData.end())std::cout <<"data refind:" << data.mID << std::endl;
  108. CCASSERT(mMapData.find(data.mID) == mMapData.end(), "data.mID is exists");
  109. mMapData.insert(std::make_pair(data.mID, data));
  110. lua_pop(L, 1);
  111. }
  112. lua_settop(L, 0);
  113. CCLOG("M_shop Loaded. Load Data:%u", mMapData.size());
  114. }
  115. void M_shop::Reload()
  116. {
  117. mMapData.clear();
  118. Load();
  119. }
  120. M_shop* M_shop::GetSingleton()
  121. {
  122. if (msSingleton.get() == nullptr)
  123. {
  124. msSingleton.reset(new M_shop());
  125. }
  126. return msSingleton.get();
  127. }
  128. void M_shop::Release()
  129. {
  130. msSingleton.reset(nullptr);
  131. }