// // IAPConf.hpp // TileManor // // Created by 徐炼新 on 2024/1/16. // #ifndef IAPConf_hpp #define IAPConf_hpp #include "cocos2d.h" #include "RUBaseConf.h" #include "RUUtils.h" #include #include USING_NS_CC; typedef struct { std::string name; std::string count; } ItemInGoods; typedef std::vector AreaInGooods; struct GoodsInfo{ std::string id; std::string type; std::string style; std::string slogon; std::string hint; std::string cost; cocos2d::Size sz; bool always; int idxGT2; std::vector areas; // 获取价格(float)类型 float getCostNumber() const{ if(cost == "")return 0.0; std::regex re(R"(\d+(\.\d{1,2})?)"); // 匹配整数或最多两位小数的数字 std::smatch match; if (std::regex_search(cost, match, re)) { return stof(match[0]); // 提取价格 } else { log("IAPUserData::getUserBuyType : 转换失败"); return 0.0; } } // 获取礼包类型排序权重 int getTypeValue() const{ if(type == "panel")return 1; // 常规礼包 if(type == "bar")return 0; // 金币礼包 return 1000; // 其他礼包默认为置顶 } // 获取礼包内包含金币数量 int getCoinNumber() const{ int num = 0; for(const auto area : areas){ for(int i = 0;i < area.size(); i++){ if(area[i].name != "coin")continue; num += stoi(area[i].count); } } return num; } std::string serialize() const { std::string ret; ret.append(id).append("|").append(cost).append("|").append(std::to_string(idxGT2)); for (auto& area : areas) { for (auto& item : area) { ret.append("|").append(item.name).append("|").append(item.count); } ret.append("|"); } return ret; } void deserialize(const std::string& str) { auto parts = redutils::parseAsStringList(str, "|"); if (parts.size() < 5) { return; } id = parts.at(0); cost = parts.at(1); idxGT2 = atoi(parts.at(2).c_str()); int idx = 3; while (idx < parts.size()) { AreaInGooods area; while (idx < parts.size() && parts.at(idx).size() > 0) { ItemInGoods item; item.name = parts.at(idx); item.count = parts.at(idx+1); area.push_back(item); idx += 2; } areas.push_back(area); idx++; } } }; class IAPConf { public: static IAPConf* getInstance(); void initWith(redutils::BaseConf*); void getAllGoods(std::vector&); float getMaxAmount(); float getCoinNum(std::string id); private: IAPConf() = default; private: static IAPConf* _instance; redutils::BaseConf* _bc = nullptr; }; #endif /* IAPConf_hpp */