IAPConf.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. //
  2. // IAPConf.cpp
  3. // TileManor
  4. //
  5. // Created by 徐炼新 on 2024/1/16.
  6. //
  7. #include "IAPConf.hpp"
  8. IAPConf* IAPConf::_instance = nullptr;
  9. IAPConf* IAPConf::getInstance() {
  10. if (_instance == nullptr) {
  11. _instance = new IAPConf();
  12. }
  13. return _instance;
  14. }
  15. void IAPConf::initWith(redutils::BaseConf* bc) {
  16. _bc = bc;
  17. }
  18. void IAPConf::getAllGoods(std::vector<GoodsInfo>& goods) {
  19. // 清空
  20. goods.clear();
  21. // 获取
  22. int cnt = atoi(_bc->getConf("goods", "count").c_str());
  23. for (int i=1; i<=cnt; i++) {
  24. GoodsInfo gi;
  25. std::string key = "goods_" + Value(i).asString();
  26. gi.id = _bc->getConf("goods", key+":id");
  27. gi.type = _bc->getConf("goods", key+":type");
  28. gi.style = _bc->getConf("goods", key+":style");
  29. gi.slogon = _bc->getConf("goods", key+":slogon");
  30. gi.hint = _bc->getConf("goods", key+":hint");
  31. gi.cost = _bc->getConf("goods", key+":cost");
  32. gi.always = _bc->getConf("goods", key+":always") == "1";
  33. gi.idxGT2 = Value(_bc->getConf("goods", key+":idxGT2")).asInt();
  34. {
  35. const auto& szStr = _bc->getConf("goods", key+":size");
  36. int nPos = szStr.find(",");
  37. gi.sz.width = atoi(szStr.substr(0, nPos).c_str());
  38. gi.sz.height = atoi(szStr.substr(nPos+1).c_str());
  39. }
  40. // areas
  41. int areaCnt = atoi(_bc->getConf("goods", key+":areas:count").c_str());
  42. for (int j=1; j<=areaCnt; j++) {
  43. auto& rewards = _bc->getConf("goods", key+":areas:area_"+Value(j).asString());
  44. // 解析得到各个奖励信息
  45. auto parseItem = [](std::string str, AreaInGooods& aig) {
  46. int nPos = str.find(":");
  47. std::string name = str.substr(0, nPos);
  48. std::string cnt = str.substr(nPos+1);
  49. aig.push_back({name, cnt});
  50. };
  51. AreaInGooods aig;
  52. int startPos = 0;
  53. int nPos = rewards.find(",");
  54. string conf = rewards.substr(startPos, nPos);
  55. while (nPos != string::npos) {
  56. parseItem(conf, aig);
  57. startPos = nPos + 1;
  58. nPos = rewards.find(",", startPos);
  59. conf = rewards.substr(startPos, nPos-startPos);
  60. }
  61. parseItem(conf, aig);
  62. gi.areas.push_back(aig);
  63. }
  64. goods.push_back(gi);
  65. }
  66. }
  67. float IAPConf::getMaxAmount(){
  68. if(_bc == nullptr) return 0;
  69. float maxn = 0.0;
  70. vector<GoodsInfo> goodInfos;
  71. getAllGoods(goodInfos);
  72. for(const auto& goodInfo : goodInfos){
  73. maxn = std::max(maxn, goodInfo.getCostNumber());
  74. }
  75. return maxn;
  76. }
  77. float IAPConf::getCoinNum(std::string id){
  78. if(_bc == nullptr) return -1;
  79. float num = -1;
  80. int cnt = atoi(_bc->getConf("goods", "count").c_str());
  81. for (int i=1; i<=cnt; i++) {
  82. GoodsInfo gi;
  83. std::string key = "goods_" + Value(i).asString();
  84. gi.id = _bc->getConf("goods", key+":id");
  85. if(gi.id == id){
  86. gi.cost = _bc->getConf("goods", key+":cost");
  87. num = gi.getCostNumber();
  88. break;
  89. }
  90. }
  91. return num;
  92. }