Fish_IntermediateExchange.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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_IntermediateExchange.h"
  7. std::auto_ptr<Fish_IntermediateExchange> Fish_IntermediateExchange::msSingleton(nullptr);
  8. int Fish_IntermediateExchange::GetCount()
  9. {
  10. return (int)mMapData.size();
  11. }
  12. const Fish_IntermediateExchangeData* Fish_IntermediateExchange::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_IntermediateExchangeData>& Fish_IntermediateExchange::GetMapData()
  22. {
  23. return mMapData;
  24. }
  25. void Fish_IntermediateExchange::Load()
  26. {
  27. tinyxml2::XMLDocument xmlDoc;
  28. std::string content = FileUtils::getInstance()->getStringFromFile("Config/Fish_IntermediateExchange.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_IntermediateExchangeData data;
  46. data.mID = element->IntAttribute("ID");
  47. data.mname = element->Attribute("name");
  48. {
  49. const char* itemId = element->Attribute("itemId");
  50. std::vector<std::string> vecitemId;
  51. boost::split(vecitemId, itemId, boost::is_any_of(","));
  52. int temp;
  53. for (unsigned int i = 0; i < vecitemId.size(); i++)
  54. {
  55. if (tinyxml2::XMLUtil::ToInt(vecitemId[i].c_str(), &temp))
  56. {
  57. data.mitemId.push_back(temp);
  58. }
  59. }
  60. }
  61. {
  62. const char* itemcount = element->Attribute("itemcount");
  63. std::vector<std::string> vecitemcount;
  64. boost::split(vecitemcount, itemcount, boost::is_any_of(","));
  65. int temp;
  66. for (unsigned int i = 0; i < vecitemcount.size(); i++)
  67. {
  68. if (tinyxml2::XMLUtil::ToInt(vecitemcount[i].c_str(), &temp))
  69. {
  70. data.mitemcount.push_back(temp);
  71. }
  72. }
  73. }
  74. {
  75. const char* fixedReward = element->Attribute("fixedReward");
  76. std::vector<std::string> vecfixedReward;
  77. boost::split(vecfixedReward, fixedReward, boost::is_any_of(","));
  78. int temp;
  79. for (unsigned int i = 0; i < vecfixedReward.size(); i++)
  80. {
  81. if (tinyxml2::XMLUtil::ToInt(vecfixedReward[i].c_str(), &temp))
  82. {
  83. data.mfixedReward.push_back(temp);
  84. }
  85. }
  86. }
  87. {
  88. const char* probabilityReward = element->Attribute("probabilityReward");
  89. std::vector<std::string> vecprobabilityReward;
  90. boost::split(vecprobabilityReward, probabilityReward, boost::is_any_of(","));
  91. int temp;
  92. for (unsigned int i = 0; i < vecprobabilityReward.size(); i++)
  93. {
  94. if (tinyxml2::XMLUtil::ToInt(vecprobabilityReward[i].c_str(), &temp))
  95. {
  96. data.mprobabilityReward.push_back(temp);
  97. }
  98. }
  99. }
  100. {
  101. const char* primaryFubao = element->Attribute("primaryFubao");
  102. std::vector<std::string> vecprimaryFubao;
  103. boost::split(vecprimaryFubao, primaryFubao, boost::is_any_of(","));
  104. int temp;
  105. for (unsigned int i = 0; i < vecprimaryFubao.size(); i++)
  106. {
  107. if (tinyxml2::XMLUtil::ToInt(vecprimaryFubao[i].c_str(), &temp))
  108. {
  109. data.mprimaryFubao.push_back(temp);
  110. }
  111. }
  112. }
  113. {
  114. const char* NewGiftIntegral = element->Attribute("NewGiftIntegral");
  115. std::vector<std::string> vecNewGiftIntegral;
  116. boost::split(vecNewGiftIntegral, NewGiftIntegral, boost::is_any_of(","));
  117. int temp;
  118. for (unsigned int i = 0; i < vecNewGiftIntegral.size(); i++)
  119. {
  120. if (tinyxml2::XMLUtil::ToInt(vecNewGiftIntegral[i].c_str(), &temp))
  121. {
  122. data.mNewGiftIntegral.push_back(temp);
  123. }
  124. }
  125. }
  126. {
  127. const char* NewIntegral = element->Attribute("NewIntegral");
  128. std::vector<std::string> vecNewIntegral;
  129. boost::split(vecNewIntegral, NewIntegral, boost::is_any_of(","));
  130. int temp;
  131. for (unsigned int i = 0; i < vecNewIntegral.size(); i++)
  132. {
  133. if (tinyxml2::XMLUtil::ToInt(vecNewIntegral[i].c_str(), &temp))
  134. {
  135. data.mNewIntegral.push_back(temp);
  136. }
  137. }
  138. }
  139. {
  140. const char* GiftIntegral = element->Attribute("GiftIntegral");
  141. std::vector<std::string> vecGiftIntegral;
  142. boost::split(vecGiftIntegral, GiftIntegral, boost::is_any_of(","));
  143. int temp;
  144. for (unsigned int i = 0; i < vecGiftIntegral.size(); i++)
  145. {
  146. if (tinyxml2::XMLUtil::ToInt(vecGiftIntegral[i].c_str(), &temp))
  147. {
  148. data.mGiftIntegral.push_back(temp);
  149. }
  150. }
  151. }
  152. {
  153. const char* ComIntegral = element->Attribute("ComIntegral");
  154. std::vector<std::string> vecComIntegral;
  155. boost::split(vecComIntegral, ComIntegral, boost::is_any_of(","));
  156. int temp;
  157. for (unsigned int i = 0; i < vecComIntegral.size(); i++)
  158. {
  159. if (tinyxml2::XMLUtil::ToInt(vecComIntegral[i].c_str(), &temp))
  160. {
  161. data.mComIntegral.push_back(temp);
  162. }
  163. }
  164. }
  165. {
  166. const char* NorIntegral = element->Attribute("NorIntegral");
  167. std::vector<std::string> vecNorIntegral;
  168. boost::split(vecNorIntegral, NorIntegral, boost::is_any_of(","));
  169. int temp;
  170. for (unsigned int i = 0; i < vecNorIntegral.size(); i++)
  171. {
  172. if (tinyxml2::XMLUtil::ToInt(vecNorIntegral[i].c_str(), &temp))
  173. {
  174. data.mNorIntegral.push_back(temp);
  175. }
  176. }
  177. }
  178. {
  179. const char* OnListWeight = element->Attribute("OnListWeight");
  180. std::vector<std::string> vecOnListWeight;
  181. boost::split(vecOnListWeight, OnListWeight, boost::is_any_of(","));
  182. int temp;
  183. for (unsigned int i = 0; i < vecOnListWeight.size(); i++)
  184. {
  185. if (tinyxml2::XMLUtil::ToInt(vecOnListWeight[i].c_str(), &temp))
  186. {
  187. data.mOnListWeight.push_back(temp);
  188. }
  189. }
  190. }
  191. if (mMapData.find(data.mID) != mMapData.end())std::cout <<"data refind:" << data.mID << std::endl;
  192. CCASSERT(mMapData.find(data.mID) == mMapData.end(), "data.mID is exists");
  193. mMapData.insert(std::make_pair(data.mID, data));
  194. element = element->NextSiblingElement();
  195. }
  196. CCLOG("Fish_IntermediateExchange Loaded. Load Data:%u", mMapData.size());
  197. }
  198. void Fish_IntermediateExchange::LoadLua()
  199. {
  200. LuaEngine::getInstance()->executeScriptFile("config/Fish_IntermediateExchange");
  201. lua_State* L = LuaEngine::getInstance()->getLuaStack()->getLuaState();
  202. lua_getglobal(L, "Fish_IntermediateExchange");
  203. CCASSERT(lua_istable(L, -1) == 1, "is not table");
  204. lua_pushstring(L, "datas");
  205. lua_gettable(L, -2);
  206. CCASSERT(lua_istable(L, -1) == 1, "is not table");
  207. lua_pushnil(L);
  208. while(lua_next(L, 2))
  209. {
  210. CCASSERT(lua_istable(L, -1) == 1, "is not table");
  211. Fish_IntermediateExchangeData data;
  212. LuaCfgHelper::readInt(L, "ID", data.mID);
  213. LuaCfgHelper::readString(L, "name", data.mname);
  214. LuaCfgHelper::readVectorInt(L, "itemId", data.mitemId);
  215. LuaCfgHelper::readVectorInt(L, "itemcount", data.mitemcount);
  216. LuaCfgHelper::readVectorInt(L, "fixedReward", data.mfixedReward);
  217. LuaCfgHelper::readVectorInt(L, "probabilityReward", data.mprobabilityReward);
  218. LuaCfgHelper::readVectorInt(L, "primaryFubao", data.mprimaryFubao);
  219. LuaCfgHelper::readVectorInt(L, "NewGiftIntegral", data.mNewGiftIntegral);
  220. LuaCfgHelper::readVectorInt(L, "NewIntegral", data.mNewIntegral);
  221. LuaCfgHelper::readVectorInt(L, "GiftIntegral", data.mGiftIntegral);
  222. LuaCfgHelper::readVectorInt(L, "ComIntegral", data.mComIntegral);
  223. LuaCfgHelper::readVectorInt(L, "NorIntegral", data.mNorIntegral);
  224. LuaCfgHelper::readVectorInt(L, "OnListWeight", data.mOnListWeight);
  225. if (mMapData.find(data.mID) != mMapData.end())std::cout <<"data refind:" << data.mID << std::endl;
  226. CCASSERT(mMapData.find(data.mID) == mMapData.end(), "data.mID is exists");
  227. mMapData.insert(std::make_pair(data.mID, data));
  228. lua_pop(L, 1);
  229. }
  230. lua_settop(L, 0);
  231. CCLOG("Fish_IntermediateExchange Loaded. Load Data:%u", mMapData.size());
  232. }
  233. void Fish_IntermediateExchange::Reload()
  234. {
  235. mMapData.clear();
  236. Load();
  237. }
  238. Fish_IntermediateExchange* Fish_IntermediateExchange::GetSingleton()
  239. {
  240. if (msSingleton.get() == nullptr)
  241. {
  242. msSingleton.reset(new Fish_IntermediateExchange());
  243. }
  244. return msSingleton.get();
  245. }
  246. void Fish_IntermediateExchange::Release()
  247. {
  248. msSingleton.reset(nullptr);
  249. }