M_ActivityDoubleElevenLotteryTicketShop.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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_ActivityDoubleElevenLotteryTicketShop.h"
  7. std::auto_ptr<M_ActivityDoubleElevenLotteryTicketShop> M_ActivityDoubleElevenLotteryTicketShop::msSingleton(nullptr);
  8. int M_ActivityDoubleElevenLotteryTicketShop::GetCount()
  9. {
  10. return (int)mMapData.size();
  11. }
  12. const M_ActivityDoubleElevenLotteryTicketShopData* M_ActivityDoubleElevenLotteryTicketShop::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, M_ActivityDoubleElevenLotteryTicketShopData>& M_ActivityDoubleElevenLotteryTicketShop::GetMapData()
  22. {
  23. return mMapData;
  24. }
  25. void M_ActivityDoubleElevenLotteryTicketShop::Load()
  26. {
  27. tinyxml2::XMLDocument xmlDoc;
  28. std::string content = FileUtils::getInstance()->getStringFromFile("Config/M_ActivityDoubleElevenLotteryTicketShop.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_ActivityDoubleElevenLotteryTicketShopData data;
  46. data.mID = element->IntAttribute("ID");
  47. data.mLotteryCount = element->IntAttribute("LotteryCount");
  48. data.mNeedCount = element->IntAttribute("NeedCount");
  49. data.mTicketCount = element->IntAttribute("TicketCount");
  50. if (mMapData.find(data.mID) != mMapData.end())std::cout <<"data refind:" << data.mID << std::endl;
  51. CCASSERT(mMapData.find(data.mID) == mMapData.end(), "data.mID is exists");
  52. mMapData.insert(std::make_pair(data.mID, data));
  53. element = element->NextSiblingElement();
  54. }
  55. CCLOG("M_ActivityDoubleElevenLotteryTicketShop Loaded. Load Data:%u", mMapData.size());
  56. }
  57. void M_ActivityDoubleElevenLotteryTicketShop::LoadLua()
  58. {
  59. LuaEngine::getInstance()->executeScriptFile("config/M_ActivityDoubleElevenLotteryTicketShop");
  60. lua_State* L = LuaEngine::getInstance()->getLuaStack()->getLuaState();
  61. lua_getglobal(L, "M_ActivityDoubleElevenLotteryTicketShop");
  62. CCASSERT(lua_istable(L, -1) == 1, "is not table");
  63. lua_pushstring(L, "datas");
  64. lua_gettable(L, -2);
  65. CCASSERT(lua_istable(L, -1) == 1, "is not table");
  66. lua_pushnil(L);
  67. while(lua_next(L, 2))
  68. {
  69. CCASSERT(lua_istable(L, -1) == 1, "is not table");
  70. M_ActivityDoubleElevenLotteryTicketShopData data;
  71. LuaCfgHelper::readInt(L, "ID", data.mID);
  72. LuaCfgHelper::readInt(L, "LotteryCount", data.mLotteryCount);
  73. LuaCfgHelper::readInt(L, "NeedCount", data.mNeedCount);
  74. LuaCfgHelper::readInt(L, "TicketCount", data.mTicketCount);
  75. if (mMapData.find(data.mID) != mMapData.end())std::cout <<"data refind:" << data.mID << std::endl;
  76. CCASSERT(mMapData.find(data.mID) == mMapData.end(), "data.mID is exists");
  77. mMapData.insert(std::make_pair(data.mID, data));
  78. lua_pop(L, 1);
  79. }
  80. lua_settop(L, 0);
  81. CCLOG("M_ActivityDoubleElevenLotteryTicketShop Loaded. Load Data:%u", mMapData.size());
  82. }
  83. void M_ActivityDoubleElevenLotteryTicketShop::Reload()
  84. {
  85. mMapData.clear();
  86. Load();
  87. }
  88. M_ActivityDoubleElevenLotteryTicketShop* M_ActivityDoubleElevenLotteryTicketShop::GetSingleton()
  89. {
  90. if (msSingleton.get() == nullptr)
  91. {
  92. msSingleton.reset(new M_ActivityDoubleElevenLotteryTicketShop());
  93. }
  94. return msSingleton.get();
  95. }
  96. void M_ActivityDoubleElevenLotteryTicketShop::Release()
  97. {
  98. msSingleton.reset(nullptr);
  99. }