M_un_lock_battle.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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_un_lock_battle.h"
  7. std::auto_ptr<M_un_lock_battle> M_un_lock_battle::msSingleton(nullptr);
  8. int M_un_lock_battle::GetCount()
  9. {
  10. return (int)mMapData.size();
  11. }
  12. const M_un_lock_battleData* M_un_lock_battle::GetData(int64_t 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<int64_t, M_un_lock_battleData>& M_un_lock_battle::GetMapData()
  22. {
  23. return mMapData;
  24. }
  25. void M_un_lock_battle::Load()
  26. {
  27. tinyxml2::XMLDocument xmlDoc;
  28. std::string content = FileUtils::getInstance()->getStringFromFile("Config/M_un_lock_battle.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_un_lock_battleData data;
  46. data.mbattle_type = element->IntAttribute("battle_type");
  47. data.mun_lock_magic = element->IntAttribute("un_lock_magic");
  48. data.mdemand = element->IntAttribute("demand");
  49. data.mprohibit = element->IntAttribute("prohibit");
  50. {
  51. const char* use_magic = element->Attribute("use_magic");
  52. std::vector<std::string> vecuse_magic;
  53. boost::split(vecuse_magic, use_magic, boost::is_any_of(","));
  54. int temp;
  55. for (unsigned int i = 0; i < vecuse_magic.size(); i++)
  56. {
  57. if (tinyxml2::XMLUtil::ToInt(vecuse_magic[i].c_str(), &temp))
  58. {
  59. data.muse_magic.push_back(temp);
  60. }
  61. }
  62. }
  63. {
  64. const char* fish_wave = element->Attribute("fish_wave");
  65. std::vector<std::string> vecfish_wave;
  66. boost::split(vecfish_wave, fish_wave, boost::is_any_of(","));
  67. int temp;
  68. for (unsigned int i = 0; i < vecfish_wave.size(); i++)
  69. {
  70. if (tinyxml2::XMLUtil::ToInt(vecfish_wave[i].c_str(), &temp))
  71. {
  72. data.mfish_wave.push_back(temp);
  73. }
  74. }
  75. }
  76. data.mfish_scheme = element->Attribute("fish_scheme");
  77. if (mMapData.find(data.mID) != mMapData.end())std::cout <<"data refind:" << data.mID << std::endl;
  78. CCASSERT(mMapData.find(data.mID) == mMapData.end(), "data.mID is exists");
  79. mMapData.insert(std::make_pair(data.mID, data));
  80. element = element->NextSiblingElement();
  81. }
  82. CCLOG("M_un_lock_battle Loaded. Load Data:%u", mMapData.size());
  83. }
  84. void M_un_lock_battle::LoadLua()
  85. {
  86. LuaEngine::getInstance()->executeScriptFile("config/M_un_lock_battle");
  87. lua_State* L = LuaEngine::getInstance()->getLuaStack()->getLuaState();
  88. lua_getglobal(L, "M_un_lock_battle");
  89. CCASSERT(lua_istable(L, -1) == 1, "is not table");
  90. lua_pushstring(L, "datas");
  91. lua_gettable(L, -2);
  92. CCASSERT(lua_istable(L, -1) == 1, "is not table");
  93. lua_pushnil(L);
  94. while(lua_next(L, 2))
  95. {
  96. CCASSERT(lua_istable(L, -1) == 1, "is not table");
  97. M_un_lock_battleData data;
  98. LuaCfgHelper::readInt(L, "battle_type", data.mbattle_type);
  99. LuaCfgHelper::readInt(L, "un_lock_magic", data.mun_lock_magic);
  100. LuaCfgHelper::readInt(L, "demand", data.mdemand);
  101. LuaCfgHelper::readInt(L, "prohibit", data.mprohibit);
  102. LuaCfgHelper::readVectorInt(L, "use_magic", data.muse_magic);
  103. LuaCfgHelper::readVectorInt(L, "fish_wave", data.mfish_wave);
  104. LuaCfgHelper::readString(L, "fish_scheme", data.mfish_scheme);
  105. if (mMapData.find(data.mID) != mMapData.end())std::cout <<"data refind:" << data.mID << std::endl;
  106. CCASSERT(mMapData.find(data.mID) == mMapData.end(), "data.mID is exists");
  107. mMapData.insert(std::make_pair(data.mID, data));
  108. lua_pop(L, 1);
  109. }
  110. lua_settop(L, 0);
  111. CCLOG("M_un_lock_battle Loaded. Load Data:%u", mMapData.size());
  112. }
  113. void M_un_lock_battle::Reload()
  114. {
  115. mMapData.clear();
  116. Load();
  117. }
  118. M_un_lock_battle* M_un_lock_battle::GetSingleton()
  119. {
  120. if (msSingleton.get() == nullptr)
  121. {
  122. msSingleton.reset(new M_un_lock_battle());
  123. }
  124. return msSingleton.get();
  125. }
  126. void M_un_lock_battle::Release()
  127. {
  128. msSingleton.reset(nullptr);
  129. }