123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- //
- // 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 <algorithm>
- #include <regex>
- USING_NS_CC;
- typedef struct {
- std::string name;
- std::string count;
- } ItemInGoods;
- typedef std::vector<ItemInGoods> 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<AreaInGooods> 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<GoodsInfo>&);
-
- float getMaxAmount();
-
- float getCoinNum(std::string id);
-
- private:
- IAPConf() = default;
-
- private:
- static IAPConf* _instance;
-
- redutils::BaseConf* _bc = nullptr;
- };
- #endif /* IAPConf_hpp */
|