Odds2_HitCoefficient.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #include "stdafx.h"
  2. #include "tinyxml2/tinyxml2.h"
  3. #include "LuaCfgHelper.h"
  4. #include <iostream>
  5. #include <boost/algorithm/string.hpp>
  6. #include "Odds2_HitCoefficient.h"
  7. std::auto_ptr<Odds2_HitCoefficient> Odds2_HitCoefficient::msSingleton(nullptr);
  8. int Odds2_HitCoefficient::GetCount()
  9. {
  10. return (int)mMapData.size();
  11. }
  12. const Odds2_HitCoefficientData* Odds2_HitCoefficient::GetData(int BuffID)
  13. {
  14. auto it = mMapData.find(BuffID);
  15. if (it != mMapData.end())
  16. {
  17. return &it->second;
  18. }
  19. return NULL;
  20. }
  21. const std::map<int, Odds2_HitCoefficientData>& Odds2_HitCoefficient::GetMapData()
  22. {
  23. return mMapData;
  24. }
  25. void Odds2_HitCoefficient::Load()
  26. {
  27. tinyxml2::XMLDocument xmlDoc;
  28. std::string content = FileUtils::getInstance()->getStringFromFile("Config/Odds2_HitCoefficient.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. Odds2_HitCoefficientData data;
  46. data.mBuffID = element->IntAttribute("BuffID");
  47. data.mCaptureNumber = element->IntAttribute("CaptureNumber");
  48. data.mBuffEffectMin = element->IntAttribute("BuffEffectMin");
  49. {
  50. const char* BossId = element->Attribute("BossId");
  51. std::vector<std::string> vecBossId;
  52. boost::split(vecBossId, BossId, boost::is_any_of(","));
  53. int temp;
  54. for (unsigned int i = 0; i < vecBossId.size(); i++)
  55. {
  56. if (tinyxml2::XMLUtil::ToInt(vecBossId[i].c_str(), &temp))
  57. {
  58. data.mBossId.push_back(temp);
  59. }
  60. }
  61. }
  62. if (mMapData.find(data.mBuffID) != mMapData.end())std::cout <<"data refind:" << data.mBuffID << std::endl;
  63. CCASSERT(mMapData.find(data.mBuffID) == mMapData.end(), "data.mBuffID is exists");
  64. mMapData.insert(std::make_pair(data.mBuffID, data));
  65. element = element->NextSiblingElement();
  66. }
  67. CCLOG("Odds2_HitCoefficient Loaded. Load Data:%u", mMapData.size());
  68. }
  69. void Odds2_HitCoefficient::LoadLua()
  70. {
  71. LuaEngine::getInstance()->executeScriptFile("config/Odds2_HitCoefficient");
  72. lua_State* L = LuaEngine::getInstance()->getLuaStack()->getLuaState();
  73. lua_getglobal(L, "Odds2_HitCoefficient");
  74. CCASSERT(lua_istable(L, -1) == 1, "is not table");
  75. lua_pushstring(L, "datas");
  76. lua_gettable(L, -2);
  77. CCASSERT(lua_istable(L, -1) == 1, "is not table");
  78. lua_pushnil(L);
  79. while(lua_next(L, 2))
  80. {
  81. CCASSERT(lua_istable(L, -1) == 1, "is not table");
  82. Odds2_HitCoefficientData data;
  83. LuaCfgHelper::readInt(L, "BuffID", data.mBuffID);
  84. LuaCfgHelper::readInt(L, "CaptureNumber", data.mCaptureNumber);
  85. LuaCfgHelper::readInt(L, "BuffEffectMin", data.mBuffEffectMin);
  86. LuaCfgHelper::readVectorInt(L, "BossId", data.mBossId);
  87. if (mMapData.find(data.mBuffID) != mMapData.end())std::cout <<"data refind:" << data.mBuffID << std::endl;
  88. CCASSERT(mMapData.find(data.mBuffID) == mMapData.end(), "data.mBuffID is exists");
  89. mMapData.insert(std::make_pair(data.mBuffID, data));
  90. lua_pop(L, 1);
  91. }
  92. lua_settop(L, 0);
  93. CCLOG("Odds2_HitCoefficient Loaded. Load Data:%u", mMapData.size());
  94. }
  95. void Odds2_HitCoefficient::Reload()
  96. {
  97. mMapData.clear();
  98. Load();
  99. }
  100. Odds2_HitCoefficient* Odds2_HitCoefficient::GetSingleton()
  101. {
  102. if (msSingleton.get() == nullptr)
  103. {
  104. msSingleton.reset(new Odds2_HitCoefficient());
  105. }
  106. return msSingleton.get();
  107. }
  108. void Odds2_HitCoefficient::Release()
  109. {
  110. msSingleton.reset(nullptr);
  111. }