Fish_FishRankCFG.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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_FishRankCFG.h"
  7. std::auto_ptr<Fish_FishRankCFG> Fish_FishRankCFG::msSingleton(nullptr);
  8. int Fish_FishRankCFG::GetCount()
  9. {
  10. return (int)mMapData.size();
  11. }
  12. const Fish_FishRankCFGData* Fish_FishRankCFG::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_FishRankCFGData>& Fish_FishRankCFG::GetMapData()
  22. {
  23. return mMapData;
  24. }
  25. void Fish_FishRankCFG::Load()
  26. {
  27. tinyxml2::XMLDocument xmlDoc;
  28. std::string content = FileUtils::getInstance()->getStringFromFile("Config/Fish_FishRankCFG.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_FishRankCFGData data;
  46. data.mID = element->IntAttribute("ID");
  47. data.mRankName = element->Attribute("RankName");
  48. data.mIntegral = element->Attribute("Integral");
  49. data.mStars1 = element->Attribute("Stars1");
  50. data.mStars2 = element->Attribute("Stars2");
  51. data.mStars3 = element->Attribute("Stars3");
  52. {
  53. const char* RankRewardId = element->Attribute("RankRewardId");
  54. std::vector<std::string> vecRankRewardId;
  55. boost::split(vecRankRewardId, RankRewardId, boost::is_any_of(","));
  56. int temp;
  57. for (unsigned int i = 0; i < vecRankRewardId.size(); i++)
  58. {
  59. if (tinyxml2::XMLUtil::ToInt(vecRankRewardId[i].c_str(), &temp))
  60. {
  61. data.mRankRewardId.push_back(temp);
  62. }
  63. }
  64. }
  65. {
  66. const char* RankRewardCount = element->Attribute("RankRewardCount");
  67. std::vector<std::string> vecRankRewardCount;
  68. boost::split(vecRankRewardCount, RankRewardCount, boost::is_any_of(","));
  69. int temp;
  70. for (unsigned int i = 0; i < vecRankRewardCount.size(); i++)
  71. {
  72. if (tinyxml2::XMLUtil::ToInt(vecRankRewardCount[i].c_str(), &temp))
  73. {
  74. data.mRankRewardCount.push_back(temp);
  75. }
  76. }
  77. }
  78. {
  79. const char* SeaGodRewardId = element->Attribute("SeaGodRewardId");
  80. std::vector<std::string> vecSeaGodRewardId;
  81. boost::split(vecSeaGodRewardId, SeaGodRewardId, boost::is_any_of(","));
  82. int temp;
  83. for (unsigned int i = 0; i < vecSeaGodRewardId.size(); i++)
  84. {
  85. if (tinyxml2::XMLUtil::ToInt(vecSeaGodRewardId[i].c_str(), &temp))
  86. {
  87. data.mSeaGodRewardId.push_back(temp);
  88. }
  89. }
  90. }
  91. {
  92. const char* SeaGodRewardCount = element->Attribute("SeaGodRewardCount");
  93. std::vector<std::string> vecSeaGodRewardCount;
  94. boost::split(vecSeaGodRewardCount, SeaGodRewardCount, boost::is_any_of(","));
  95. int temp;
  96. for (unsigned int i = 0; i < vecSeaGodRewardCount.size(); i++)
  97. {
  98. if (tinyxml2::XMLUtil::ToInt(vecSeaGodRewardCount[i].c_str(), &temp))
  99. {
  100. data.mSeaGodRewardCount.push_back(temp);
  101. }
  102. }
  103. }
  104. data.mGiftId = element->IntAttribute("GiftId");
  105. data.mResources = element->Attribute("Resources");
  106. if (mMapData.find(data.mID) != mMapData.end())std::cout <<"data refind:" << data.mID << std::endl;
  107. CCASSERT(mMapData.find(data.mID) == mMapData.end(), "data.mID is exists");
  108. mMapData.insert(std::make_pair(data.mID, data));
  109. element = element->NextSiblingElement();
  110. }
  111. CCLOG("Fish_FishRankCFG Loaded. Load Data:%u", mMapData.size());
  112. }
  113. void Fish_FishRankCFG::LoadLua()
  114. {
  115. LuaEngine::getInstance()->executeScriptFile("config/Fish_FishRankCFG");
  116. lua_State* L = LuaEngine::getInstance()->getLuaStack()->getLuaState();
  117. lua_getglobal(L, "Fish_FishRankCFG");
  118. CCASSERT(lua_istable(L, -1) == 1, "is not table");
  119. lua_pushstring(L, "datas");
  120. lua_gettable(L, -2);
  121. CCASSERT(lua_istable(L, -1) == 1, "is not table");
  122. lua_pushnil(L);
  123. while(lua_next(L, 2))
  124. {
  125. CCASSERT(lua_istable(L, -1) == 1, "is not table");
  126. Fish_FishRankCFGData data;
  127. LuaCfgHelper::readInt(L, "ID", data.mID);
  128. LuaCfgHelper::readString(L, "RankName", data.mRankName);
  129. LuaCfgHelper::readString(L, "Integral", data.mIntegral);
  130. LuaCfgHelper::readString(L, "Stars1", data.mStars1);
  131. LuaCfgHelper::readString(L, "Stars2", data.mStars2);
  132. LuaCfgHelper::readString(L, "Stars3", data.mStars3);
  133. LuaCfgHelper::readVectorInt(L, "RankRewardId", data.mRankRewardId);
  134. LuaCfgHelper::readVectorInt(L, "RankRewardCount", data.mRankRewardCount);
  135. LuaCfgHelper::readVectorInt(L, "SeaGodRewardId", data.mSeaGodRewardId);
  136. LuaCfgHelper::readVectorInt(L, "SeaGodRewardCount", data.mSeaGodRewardCount);
  137. LuaCfgHelper::readInt(L, "GiftId", data.mGiftId);
  138. LuaCfgHelper::readString(L, "Resources", data.mResources);
  139. if (mMapData.find(data.mID) != mMapData.end())std::cout <<"data refind:" << data.mID << std::endl;
  140. CCASSERT(mMapData.find(data.mID) == mMapData.end(), "data.mID is exists");
  141. mMapData.insert(std::make_pair(data.mID, data));
  142. lua_pop(L, 1);
  143. }
  144. lua_settop(L, 0);
  145. CCLOG("Fish_FishRankCFG Loaded. Load Data:%u", mMapData.size());
  146. }
  147. void Fish_FishRankCFG::Reload()
  148. {
  149. mMapData.clear();
  150. Load();
  151. }
  152. Fish_FishRankCFG* Fish_FishRankCFG::GetSingleton()
  153. {
  154. if (msSingleton.get() == nullptr)
  155. {
  156. msSingleton.reset(new Fish_FishRankCFG());
  157. }
  158. return msSingleton.get();
  159. }
  160. void Fish_FishRankCFG::Release()
  161. {
  162. msSingleton.reset(nullptr);
  163. }