Fish_SquamaExchangeCFG.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #include "stdafx.h"
  2. #include "tinyxml2/tinyxml2.h"
  3. #include "LuaCfgHelper.h"
  4. #include <iostream>
  5. #include <boost/algorithm/string.hpp>
  6. #include "Fish_SquamaExchangeCFG.h"
  7. std::auto_ptr<Fish_SquamaExchangeCFG> Fish_SquamaExchangeCFG::msSingleton(nullptr);
  8. int Fish_SquamaExchangeCFG::GetCount()
  9. {
  10. return (int)mMapData.size();
  11. }
  12. const Fish_SquamaExchangeCFGData* Fish_SquamaExchangeCFG::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, Fish_SquamaExchangeCFGData>& Fish_SquamaExchangeCFG::GetMapData()
  22. {
  23. return mMapData;
  24. }
  25. void Fish_SquamaExchangeCFG::Load()
  26. {
  27. tinyxml2::XMLDocument xmlDoc;
  28. std::string content = FileUtils::getInstance()->getStringFromFile("Config/Fish_SquamaExchangeCFG.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. Fish_SquamaExchangeCFGData data;
  46. data.mID = element->IntAttribute("ID");
  47. data.mPrice = element->IntAttribute("Price");
  48. {
  49. const char* DiscountGift = element->Attribute("DiscountGift");
  50. std::vector<std::string> vecDiscountGift;
  51. boost::split(vecDiscountGift, DiscountGift, boost::is_any_of(","));
  52. int temp;
  53. for (unsigned int i = 0; i < vecDiscountGift.size(); i++)
  54. {
  55. if (tinyxml2::XMLUtil::ToInt(vecDiscountGift[i].c_str(), &temp))
  56. {
  57. data.mDiscountGift.push_back(temp);
  58. }
  59. }
  60. }
  61. {
  62. const char* SalePrice = element->Attribute("SalePrice");
  63. std::vector<std::string> vecSalePrice;
  64. boost::split(vecSalePrice, SalePrice, boost::is_any_of(","));
  65. int temp;
  66. for (unsigned int i = 0; i < vecSalePrice.size(); i++)
  67. {
  68. if (tinyxml2::XMLUtil::ToInt(vecSalePrice[i].c_str(), &temp))
  69. {
  70. data.mSalePrice.push_back(temp);
  71. }
  72. }
  73. }
  74. data.mFinalPrice = element->IntAttribute("FinalPrice");
  75. {
  76. const char* RewardList = element->Attribute("RewardList");
  77. std::vector<std::string> vecRewardList;
  78. boost::split(vecRewardList, RewardList, boost::is_any_of(","));
  79. int temp;
  80. for (unsigned int i = 0; i < vecRewardList.size(); i++)
  81. {
  82. if (tinyxml2::XMLUtil::ToInt(vecRewardList[i].c_str(), &temp))
  83. {
  84. data.mRewardList.push_back(temp);
  85. }
  86. }
  87. }
  88. {
  89. const char* RewardCount = element->Attribute("RewardCount");
  90. std::vector<std::string> vecRewardCount;
  91. boost::split(vecRewardCount, RewardCount, boost::is_any_of(","));
  92. int temp;
  93. for (unsigned int i = 0; i < vecRewardCount.size(); i++)
  94. {
  95. if (tinyxml2::XMLUtil::ToInt(vecRewardCount[i].c_str(), &temp))
  96. {
  97. data.mRewardCount.push_back(temp);
  98. }
  99. }
  100. }
  101. {
  102. const char* Rate = element->Attribute("Rate");
  103. std::vector<std::string> vecRate;
  104. boost::split(vecRate, Rate, boost::is_any_of(","));
  105. int temp;
  106. for (unsigned int i = 0; i < vecRate.size(); i++)
  107. {
  108. if (tinyxml2::XMLUtil::ToInt(vecRate[i].c_str(), &temp))
  109. {
  110. data.mRate.push_back(temp);
  111. }
  112. }
  113. }
  114. data.mGoldIcon = element->Attribute("GoldIcon");
  115. if (mMapData.find(data.mID) != mMapData.end())std::cout <<"data refind:" << data.mID << std::endl;
  116. CCASSERT(mMapData.find(data.mID) == mMapData.end(), "data.mID is exists");
  117. mMapData.insert(std::make_pair(data.mID, data));
  118. element = element->NextSiblingElement();
  119. }
  120. CCLOG("Fish_SquamaExchangeCFG Loaded. Load Data:%u", mMapData.size());
  121. }
  122. void Fish_SquamaExchangeCFG::LoadLua()
  123. {
  124. LuaEngine::getInstance()->executeScriptFile("config/Fish_SquamaExchangeCFG");
  125. lua_State* L = LuaEngine::getInstance()->getLuaStack()->getLuaState();
  126. lua_getglobal(L, "Fish_SquamaExchangeCFG");
  127. CCASSERT(lua_istable(L, -1) == 1, "is not table");
  128. lua_pushstring(L, "datas");
  129. lua_gettable(L, -2);
  130. CCASSERT(lua_istable(L, -1) == 1, "is not table");
  131. lua_pushnil(L);
  132. while(lua_next(L, 2))
  133. {
  134. CCASSERT(lua_istable(L, -1) == 1, "is not table");
  135. Fish_SquamaExchangeCFGData data;
  136. LuaCfgHelper::readInt(L, "ID", data.mID);
  137. LuaCfgHelper::readInt(L, "Price", data.mPrice);
  138. LuaCfgHelper::readVectorInt(L, "DiscountGift", data.mDiscountGift);
  139. LuaCfgHelper::readVectorInt(L, "SalePrice", data.mSalePrice);
  140. LuaCfgHelper::readInt(L, "FinalPrice", data.mFinalPrice);
  141. LuaCfgHelper::readVectorInt(L, "RewardList", data.mRewardList);
  142. LuaCfgHelper::readVectorInt(L, "RewardCount", data.mRewardCount);
  143. LuaCfgHelper::readVectorInt(L, "Rate", data.mRate);
  144. LuaCfgHelper::readString(L, "GoldIcon", data.mGoldIcon);
  145. if (mMapData.find(data.mID) != mMapData.end())std::cout <<"data refind:" << data.mID << std::endl;
  146. CCASSERT(mMapData.find(data.mID) == mMapData.end(), "data.mID is exists");
  147. mMapData.insert(std::make_pair(data.mID, data));
  148. lua_pop(L, 1);
  149. }
  150. lua_settop(L, 0);
  151. CCLOG("Fish_SquamaExchangeCFG Loaded. Load Data:%u", mMapData.size());
  152. }
  153. void Fish_SquamaExchangeCFG::Reload()
  154. {
  155. mMapData.clear();
  156. Load();
  157. }
  158. Fish_SquamaExchangeCFG* Fish_SquamaExchangeCFG::GetSingleton()
  159. {
  160. if (msSingleton.get() == nullptr)
  161. {
  162. msSingleton.reset(new Fish_SquamaExchangeCFG());
  163. }
  164. return msSingleton.get();
  165. }
  166. void Fish_SquamaExchangeCFG::Release()
  167. {
  168. msSingleton.reset(nullptr);
  169. }