IAPConf.hpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. typedef struct {
  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. std::string serialize() const {
  44. std::string ret;
  45. ret.append(id).append("|").append(cost).append("|").append(std::to_string(idxGT2));
  46. for (auto& area : areas) {
  47. for (auto& item : area) {
  48. ret.append("|").append(item.name).append("|").append(item.count);
  49. }
  50. ret.append("|");
  51. }
  52. return ret;
  53. }
  54. void deserialize(const std::string& str) {
  55. auto parts = redutils::parseAsStringList(str, "|");
  56. if (parts.size() < 5) {
  57. return;
  58. }
  59. id = parts.at(0);
  60. cost = parts.at(1);
  61. idxGT2 = atoi(parts.at(2).c_str());
  62. int idx = 3;
  63. while (idx < parts.size()) {
  64. AreaInGooods area;
  65. while (idx < parts.size() && parts.at(idx).size() > 0) {
  66. ItemInGoods item;
  67. item.name = parts.at(idx);
  68. item.count = parts.at(idx+1);
  69. area.push_back(item);
  70. idx += 2;
  71. }
  72. areas.push_back(area);
  73. idx++;
  74. }
  75. }
  76. } GoodsInfo;
  77. class IAPConf {
  78. public:
  79. static IAPConf* getInstance();
  80. void initWith(redutils::BaseConf*);
  81. void getAllGoods(std::vector<GoodsInfo>&);
  82. private:
  83. IAPConf() = default;
  84. private:
  85. static IAPConf* _instance;
  86. redutils::BaseConf* _bc = nullptr;
  87. };
  88. #endif /* IAPConf_hpp */