M_GiftRewardCFG.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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_GiftRewardCFG.h"
  7. std::auto_ptr<M_GiftRewardCFG> M_GiftRewardCFG::msSingleton(nullptr);
  8. int M_GiftRewardCFG::GetCount()
  9. {
  10. return (int)mMapData.size();
  11. }
  12. const M_GiftRewardCFGData* M_GiftRewardCFG::GetData(int RewardType)
  13. {
  14. auto it = mMapData.find(RewardType);
  15. if (it != mMapData.end())
  16. {
  17. return &it->second;
  18. }
  19. return NULL;
  20. }
  21. const std::map<int, M_GiftRewardCFGData>& M_GiftRewardCFG::GetMapData()
  22. {
  23. return mMapData;
  24. }
  25. void M_GiftRewardCFG::Load()
  26. {
  27. tinyxml2::XMLDocument xmlDoc;
  28. std::string content = FileUtils::getInstance()->getStringFromFile("Config/M_GiftRewardCFG.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_GiftRewardCFGData data;
  46. data.mRewardType = element->IntAttribute("RewardType");
  47. data.mStartTime = element->Attribute("StartTime");
  48. data.mEndTime = element->Attribute("EndTime");
  49. {
  50. const char* ItemId = element->Attribute("ItemId");
  51. std::vector<std::string> vecItemId;
  52. boost::split(vecItemId, ItemId, boost::is_any_of(","));
  53. int temp;
  54. for (unsigned int i = 0; i < vecItemId.size(); i++)
  55. {
  56. if (tinyxml2::XMLUtil::ToInt(vecItemId[i].c_str(), &temp))
  57. {
  58. data.mItemId.push_back(temp);
  59. }
  60. }
  61. }
  62. {
  63. const char* ItemCount = element->Attribute("ItemCount");
  64. std::vector<std::string> vecItemCount;
  65. boost::split(vecItemCount, ItemCount, boost::is_any_of(","));
  66. int temp;
  67. for (unsigned int i = 0; i < vecItemCount.size(); i++)
  68. {
  69. if (tinyxml2::XMLUtil::ToInt(vecItemCount[i].c_str(), &temp))
  70. {
  71. data.mItemCount.push_back(temp);
  72. }
  73. }
  74. }
  75. {
  76. const char* ItemId2 = element->Attribute("ItemId2");
  77. std::vector<std::string> vecItemId2;
  78. boost::split(vecItemId2, ItemId2, boost::is_any_of(","));
  79. int temp;
  80. for (unsigned int i = 0; i < vecItemId2.size(); i++)
  81. {
  82. if (tinyxml2::XMLUtil::ToInt(vecItemId2[i].c_str(), &temp))
  83. {
  84. data.mItemId2.push_back(temp);
  85. }
  86. }
  87. }
  88. {
  89. const char* ItemId3 = element->Attribute("ItemId3");
  90. std::vector<std::string> vecItemId3;
  91. boost::split(vecItemId3, ItemId3, boost::is_any_of(","));
  92. int temp;
  93. for (unsigned int i = 0; i < vecItemId3.size(); i++)
  94. {
  95. if (tinyxml2::XMLUtil::ToInt(vecItemId3[i].c_str(), &temp))
  96. {
  97. data.mItemId3.push_back(temp);
  98. }
  99. }
  100. }
  101. data.mPrice = element->IntAttribute("Price");
  102. {
  103. const char* TotalPrice = element->Attribute("TotalPrice");
  104. std::vector<std::string> vecTotalPrice;
  105. boost::split(vecTotalPrice, TotalPrice, boost::is_any_of(","));
  106. int temp;
  107. for (unsigned int i = 0; i < vecTotalPrice.size(); i++)
  108. {
  109. if (tinyxml2::XMLUtil::ToInt(vecTotalPrice[i].c_str(), &temp))
  110. {
  111. data.mTotalPrice.push_back(temp);
  112. }
  113. }
  114. }
  115. data.mVipLevel = element->IntAttribute("VipLevel");
  116. data.mGiftId = element->IntAttribute("GiftId");
  117. if (mMapData.find(data.mRewardType) != mMapData.end())std::cout <<"data refind:" << data.mRewardType << std::endl;
  118. CCASSERT(mMapData.find(data.mRewardType) == mMapData.end(), "data.mRewardType is exists");
  119. mMapData.insert(std::make_pair(data.mRewardType, data));
  120. element = element->NextSiblingElement();
  121. }
  122. CCLOG("M_GiftRewardCFG Loaded. Load Data:%u", mMapData.size());
  123. }
  124. void M_GiftRewardCFG::LoadLua()
  125. {
  126. LuaEngine::getInstance()->executeScriptFile("config/M_GiftRewardCFG");
  127. lua_State* L = LuaEngine::getInstance()->getLuaStack()->getLuaState();
  128. lua_getglobal(L, "M_GiftRewardCFG");
  129. CCASSERT(lua_istable(L, -1) == 1, "is not table");
  130. lua_pushstring(L, "datas");
  131. lua_gettable(L, -2);
  132. CCASSERT(lua_istable(L, -1) == 1, "is not table");
  133. lua_pushnil(L);
  134. while(lua_next(L, 2))
  135. {
  136. CCASSERT(lua_istable(L, -1) == 1, "is not table");
  137. M_GiftRewardCFGData data;
  138. LuaCfgHelper::readInt(L, "RewardType", data.mRewardType);
  139. LuaCfgHelper::readString(L, "StartTime", data.mStartTime);
  140. LuaCfgHelper::readString(L, "EndTime", data.mEndTime);
  141. LuaCfgHelper::readVectorInt(L, "ItemId", data.mItemId);
  142. LuaCfgHelper::readVectorInt(L, "ItemCount", data.mItemCount);
  143. LuaCfgHelper::readVectorInt(L, "ItemId2", data.mItemId2);
  144. LuaCfgHelper::readVectorInt(L, "ItemId3", data.mItemId3);
  145. LuaCfgHelper::readInt(L, "Price", data.mPrice);
  146. LuaCfgHelper::readVectorInt(L, "TotalPrice", data.mTotalPrice);
  147. LuaCfgHelper::readInt(L, "VipLevel", data.mVipLevel);
  148. LuaCfgHelper::readInt(L, "GiftId", data.mGiftId);
  149. if (mMapData.find(data.mRewardType) != mMapData.end())std::cout <<"data refind:" << data.mRewardType << std::endl;
  150. CCASSERT(mMapData.find(data.mRewardType) == mMapData.end(), "data.mRewardType is exists");
  151. mMapData.insert(std::make_pair(data.mRewardType, data));
  152. lua_pop(L, 1);
  153. }
  154. lua_settop(L, 0);
  155. CCLOG("M_GiftRewardCFG Loaded. Load Data:%u", mMapData.size());
  156. }
  157. void M_GiftRewardCFG::Reload()
  158. {
  159. mMapData.clear();
  160. Load();
  161. }
  162. M_GiftRewardCFG* M_GiftRewardCFG::GetSingleton()
  163. {
  164. if (msSingleton.get() == nullptr)
  165. {
  166. msSingleton.reset(new M_GiftRewardCFG());
  167. }
  168. return msSingleton.get();
  169. }
  170. void M_GiftRewardCFG::Release()
  171. {
  172. msSingleton.reset(nullptr);
  173. }