M_LotteryChipCFG.cpp 3.1 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_LotteryChipCFG.h"
  7. std::auto_ptr<M_LotteryChipCFG> M_LotteryChipCFG::msSingleton(nullptr);
  8. int M_LotteryChipCFG::GetCount()
  9. {
  10. return (int)mMapData.size();
  11. }
  12. const M_LotteryChipCFGData* M_LotteryChipCFG::GetData(int Recharge)
  13. {
  14. auto it = mMapData.find(Recharge);
  15. if (it != mMapData.end())
  16. {
  17. return &it->second;
  18. }
  19. return NULL;
  20. }
  21. const std::map<int, M_LotteryChipCFGData>& M_LotteryChipCFG::GetMapData()
  22. {
  23. return mMapData;
  24. }
  25. void M_LotteryChipCFG::Load()
  26. {
  27. tinyxml2::XMLDocument xmlDoc;
  28. std::string content = FileUtils::getInstance()->getStringFromFile("Config/M_LotteryChipCFG.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_LotteryChipCFGData data;
  46. data.mRecharge = element->IntAttribute("Recharge");
  47. data.mRechargeUp = element->IntAttribute("RechargeUp");
  48. data.mRetChipRate = element->IntAttribute("RetChipRate");
  49. data.mQuickDeduction = element->IntAttribute("QuickDeduction");
  50. if (mMapData.find(data.mRecharge) != mMapData.end())std::cout <<"data refind:" << data.mRecharge << std::endl;
  51. CCASSERT(mMapData.find(data.mRecharge) == mMapData.end(), "data.mRecharge is exists");
  52. mMapData.insert(std::make_pair(data.mRecharge, data));
  53. element = element->NextSiblingElement();
  54. }
  55. CCLOG("M_LotteryChipCFG Loaded. Load Data:%u", mMapData.size());
  56. }
  57. void M_LotteryChipCFG::LoadLua()
  58. {
  59. LuaEngine::getInstance()->executeScriptFile("config/M_LotteryChipCFG");
  60. lua_State* L = LuaEngine::getInstance()->getLuaStack()->getLuaState();
  61. lua_getglobal(L, "M_LotteryChipCFG");
  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_LotteryChipCFGData data;
  71. LuaCfgHelper::readInt(L, "Recharge", data.mRecharge);
  72. LuaCfgHelper::readInt(L, "RechargeUp", data.mRechargeUp);
  73. LuaCfgHelper::readInt(L, "RetChipRate", data.mRetChipRate);
  74. LuaCfgHelper::readInt(L, "QuickDeduction", data.mQuickDeduction);
  75. if (mMapData.find(data.mRecharge) != mMapData.end())std::cout <<"data refind:" << data.mRecharge << std::endl;
  76. CCASSERT(mMapData.find(data.mRecharge) == mMapData.end(), "data.mRecharge is exists");
  77. mMapData.insert(std::make_pair(data.mRecharge, data));
  78. lua_pop(L, 1);
  79. }
  80. lua_settop(L, 0);
  81. CCLOG("M_LotteryChipCFG Loaded. Load Data:%u", mMapData.size());
  82. }
  83. void M_LotteryChipCFG::Reload()
  84. {
  85. mMapData.clear();
  86. Load();
  87. }
  88. M_LotteryChipCFG* M_LotteryChipCFG::GetSingleton()
  89. {
  90. if (msSingleton.get() == nullptr)
  91. {
  92. msSingleton.reset(new M_LotteryChipCFG());
  93. }
  94. return msSingleton.get();
  95. }
  96. void M_LotteryChipCFG::Release()
  97. {
  98. msSingleton.reset(nullptr);
  99. }