Fish_LuckyDrawCFG.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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_LuckyDrawCFG.h"
  7. std::auto_ptr<Fish_LuckyDrawCFG> Fish_LuckyDrawCFG::msSingleton(nullptr);
  8. int Fish_LuckyDrawCFG::GetCount()
  9. {
  10. return (int)mMapData.size();
  11. }
  12. const Fish_LuckyDrawCFGData* Fish_LuckyDrawCFG::GetData(int Index)
  13. {
  14. auto it = mMapData.find(Index);
  15. if (it != mMapData.end())
  16. {
  17. return &it->second;
  18. }
  19. return NULL;
  20. }
  21. const std::map<int, Fish_LuckyDrawCFGData>& Fish_LuckyDrawCFG::GetMapData()
  22. {
  23. return mMapData;
  24. }
  25. void Fish_LuckyDrawCFG::Load()
  26. {
  27. tinyxml2::XMLDocument xmlDoc;
  28. std::string content = FileUtils::getInstance()->getStringFromFile("Config/Fish_LuckyDrawCFG.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_LuckyDrawCFGData data;
  46. data.mIndex = element->IntAttribute("Index");
  47. {
  48. const char* PinValue = element->Attribute("PinValue");
  49. std::vector<std::string> vecPinValue;
  50. boost::split(vecPinValue, PinValue, boost::is_any_of(","));
  51. int temp;
  52. for (unsigned int i = 0; i < vecPinValue.size(); i++)
  53. {
  54. if (tinyxml2::XMLUtil::ToInt(vecPinValue[i].c_str(), &temp))
  55. {
  56. data.mPinValue.push_back(temp);
  57. }
  58. }
  59. }
  60. data.mStarLvl = element->IntAttribute("StarLvl");
  61. data.mItemType = element->IntAttribute("ItemType");
  62. data.mItemCount = element->IntAttribute("ItemCount");
  63. data.mItemValue = element->IntAttribute("ItemValue");
  64. {
  65. const char* NormalProb = element->Attribute("NormalProb");
  66. std::vector<std::string> vecNormalProb;
  67. boost::split(vecNormalProb, NormalProb, boost::is_any_of(","));
  68. int temp;
  69. for (unsigned int i = 0; i < vecNormalProb.size(); i++)
  70. {
  71. if (tinyxml2::XMLUtil::ToInt(vecNormalProb[i].c_str(), &temp))
  72. {
  73. data.mNormalProb.push_back(temp);
  74. }
  75. }
  76. }
  77. {
  78. const char* NoChipProb = element->Attribute("NoChipProb");
  79. std::vector<std::string> vecNoChipProb;
  80. boost::split(vecNoChipProb, NoChipProb, boost::is_any_of(","));
  81. int temp;
  82. for (unsigned int i = 0; i < vecNoChipProb.size(); i++)
  83. {
  84. if (tinyxml2::XMLUtil::ToInt(vecNoChipProb[i].c_str(), &temp))
  85. {
  86. data.mNoChipProb.push_back(temp);
  87. }
  88. }
  89. }
  90. if (mMapData.find(data.mIndex) != mMapData.end())std::cout <<"data refind:" << data.mIndex << std::endl;
  91. CCASSERT(mMapData.find(data.mIndex) == mMapData.end(), "data.mIndex is exists");
  92. mMapData.insert(std::make_pair(data.mIndex, data));
  93. element = element->NextSiblingElement();
  94. }
  95. CCLOG("Fish_LuckyDrawCFG Loaded. Load Data:%u", mMapData.size());
  96. }
  97. void Fish_LuckyDrawCFG::LoadLua()
  98. {
  99. LuaEngine::getInstance()->executeScriptFile("config/Fish_LuckyDrawCFG");
  100. lua_State* L = LuaEngine::getInstance()->getLuaStack()->getLuaState();
  101. lua_getglobal(L, "Fish_LuckyDrawCFG");
  102. CCASSERT(lua_istable(L, -1) == 1, "is not table");
  103. lua_pushstring(L, "datas");
  104. lua_gettable(L, -2);
  105. CCASSERT(lua_istable(L, -1) == 1, "is not table");
  106. lua_pushnil(L);
  107. while(lua_next(L, 2))
  108. {
  109. CCASSERT(lua_istable(L, -1) == 1, "is not table");
  110. Fish_LuckyDrawCFGData data;
  111. LuaCfgHelper::readInt(L, "Index", data.mIndex);
  112. LuaCfgHelper::readVectorInt(L, "PinValue", data.mPinValue);
  113. LuaCfgHelper::readInt(L, "StarLvl", data.mStarLvl);
  114. LuaCfgHelper::readInt(L, "ItemType", data.mItemType);
  115. LuaCfgHelper::readInt(L, "ItemCount", data.mItemCount);
  116. LuaCfgHelper::readInt(L, "ItemValue", data.mItemValue);
  117. LuaCfgHelper::readVectorInt(L, "NormalProb", data.mNormalProb);
  118. LuaCfgHelper::readVectorInt(L, "NoChipProb", data.mNoChipProb);
  119. if (mMapData.find(data.mIndex) != mMapData.end())std::cout <<"data refind:" << data.mIndex << std::endl;
  120. CCASSERT(mMapData.find(data.mIndex) == mMapData.end(), "data.mIndex is exists");
  121. mMapData.insert(std::make_pair(data.mIndex, data));
  122. lua_pop(L, 1);
  123. }
  124. lua_settop(L, 0);
  125. CCLOG("Fish_LuckyDrawCFG Loaded. Load Data:%u", mMapData.size());
  126. }
  127. void Fish_LuckyDrawCFG::Reload()
  128. {
  129. mMapData.clear();
  130. Load();
  131. }
  132. Fish_LuckyDrawCFG* Fish_LuckyDrawCFG::GetSingleton()
  133. {
  134. if (msSingleton.get() == nullptr)
  135. {
  136. msSingleton.reset(new Fish_LuckyDrawCFG());
  137. }
  138. return msSingleton.get();
  139. }
  140. void Fish_LuckyDrawCFG::Release()
  141. {
  142. msSingleton.reset(nullptr);
  143. }