IAPConf.hpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. //
  2. // IAPConf.hpp
  3. // TileManor
  4. //
  5. // Created by 徐炼新 on 2024/1/16.
  6. //
  7. #ifndef IAPConf_hpp
  8. #define IAPConf_hpp
  9. #include "cocos2d.h"
  10. #include "RUBaseConf.h"
  11. #include "RUUtils.h"
  12. #include <algorithm>
  13. #include <regex>
  14. USING_NS_CC;
  15. typedef struct {
  16. std::string name;
  17. std::string count;
  18. } ItemInGoods;
  19. typedef std::vector<ItemInGoods> AreaInGooods;
  20. struct GoodsInfo{
  21. std::string id;
  22. std::string type;
  23. std::string style;
  24. std::string slogon;
  25. std::string hint;
  26. std::string cost;
  27. cocos2d::Size sz;
  28. bool always;
  29. int idxGT2;
  30. std::vector<AreaInGooods> areas;
  31. // 获取价格(float)类型
  32. float getCostNumber() const{
  33. if(cost == "")return 0.0;
  34. std::regex re(R"(\d+(\.\d{1,2})?)"); // 匹配整数或最多两位小数的数字
  35. std::smatch match;
  36. if (std::regex_search(cost, match, re)) {
  37. return stof(match[0]); // 提取价格
  38. } else {
  39. log("IAPUserData::getUserBuyType : 转换失败");
  40. return 0.0;
  41. }
  42. }
  43. // 获取礼包类型排序权重
  44. int getTypeValue() const{
  45. if(type == "panel")return 1; // 常规礼包
  46. if(type == "bar")return 0; // 金币礼包
  47. return 1000; // 其他礼包默认为置顶
  48. }
  49. // 获取礼包内包含金币数量
  50. int getCoinNumber() const{
  51. int num = 0;
  52. for(const auto area : areas){
  53. for(int i = 0;i < area.size(); i++){
  54. if(area[i].name != "coin")continue;
  55. num += stoi(area[i].count);
  56. }
  57. }
  58. return num;
  59. }
  60. std::string serialize() const {
  61. std::string ret;
  62. ret.append(id).append("|").append(cost).append("|").append(std::to_string(idxGT2));
  63. for (auto& area : areas) {
  64. for (auto& item : area) {
  65. ret.append("|").append(item.name).append("|").append(item.count);
  66. }
  67. ret.append("|");
  68. }
  69. return ret;
  70. }
  71. void deserialize(const std::string& str) {
  72. auto parts = redutils::parseAsStringList(str, "|");
  73. if (parts.size() < 5) {
  74. return;
  75. }
  76. id = parts.at(0);
  77. cost = parts.at(1);
  78. idxGT2 = atoi(parts.at(2).c_str());
  79. int idx = 3;
  80. while (idx < parts.size()) {
  81. AreaInGooods area;
  82. while (idx < parts.size() && parts.at(idx).size() > 0) {
  83. ItemInGoods item;
  84. item.name = parts.at(idx);
  85. item.count = parts.at(idx+1);
  86. area.push_back(item);
  87. idx += 2;
  88. }
  89. areas.push_back(area);
  90. idx++;
  91. }
  92. }
  93. };
  94. class IAPConf {
  95. public:
  96. static IAPConf* getInstance();
  97. void initWith(redutils::BaseConf*);
  98. void getAllGoods(std::vector<GoodsInfo>&);
  99. float getMaxAmount();
  100. float getCoinNum(std::string id);
  101. private:
  102. IAPConf() = default;
  103. private:
  104. static IAPConf* _instance;
  105. redutils::BaseConf* _bc = nullptr;
  106. };
  107. #endif /* IAPConf_hpp */