// // IAPCtlShop.cpp // TileManor // // Created by 徐炼新 on 2024/9/30. // #include "IAPCtlShop.hpp" #include "cocos2d.h" #include "RUBaseConf.h" #include "RUUtils.h" #include "IAPConf.hpp" #include "IAPCtlShopUI.hpp" #include "IAPUserData.hpp" #include #include #include NS_IAP_BEGIN IAPCtlShop* IAPCtlShop::_instance = nullptr; IAPCtlShop* IAPCtlShop::createWith(){ if (_instance == nullptr) { _instance = new IAPCtlShop(); } return _instance; } void IAPCtlShop::init(std::string &cfgFN){ redutils::BaseConf* baseConf = new redutils::BaseConf(cfgFN.c_str()); _conf = IAPConf::getInstance(); _conf->initWith(baseConf); _shopUI = IAPCtlShopUI::getInstance(); _delegate = IAPDelegate::getInstance(); _level = 1; } void IAPCtlShop::setDelegate(IAPDelegate *delegate){ _delegate = delegate; } void IAPCtlShop::setDeviceLevel(int level){ _level = level; } int IAPCtlShop::getDeviceLevel(){ return _level; } bool IAPCtlShop::addAPlacement(const IAPPlacement &plInfo){ bool ans = false; if(ans)placementIDs.insert(plInfo.id); return ans; } void IAPCtlShop::removePlacement(const std::string &id){ placementIDs.erase(id); } size_t IAPCtlShop::getPlacementCount(){ return placementIDs.size(); } void IAPCtlShop::showInNode(cocos2d::Node *pParent, ShopRequirement &requirement){ _shopUI = IAPCtlShopUI::getInstance(); _shopUI->create(pParent, _conf, requirement,1); } void IAPCtlShop::showPlacementsInNode(cocos2d::Node *pParent, const vector &plIds){ } UserBuyType IAPCtlShop::getUserBuyType(){ // 如果没有购买记录 auto buyInfos = IAPUserData::getInstance()->getBuyInfos(); if(buyInfos.size() == 0) return UserBuyType::NoShopping; auto conf = IAPConf::getInstance(); vector goodInfos; conf->getAllGoods(goodInfos); struct SortVec{ std::string id; std::string price; bool isSpecial; }; vector vec; std::regex re(R"(\d+(\.\d{1,2})?)"); // 匹配整数或最多两位小数的数字 for(const auto& goodInfo : goodInfos){ bool isSpecial = goodInfo.style == "mostValued" ? true : false; std::smatch match; if (std::regex_search(goodInfo.cost, match, re)) { SortVec sv; sv.id = goodInfo.id; sv.price = match[0]; // 提取价格 sv.isSpecial = isSpecial; vec.push_back(sv); } else { log("IAPUserData::getUserBuyType : 转换失败"); } } // 按照价格排序 , 价格相同按照是否为special商品排序 sort(vec.begin(), vec.end(), [](SortVec a,SortVec b){ if(a.price != b.price) return stof(a.price) < stof(b.price); if(a.isSpecial)return true; if(b.isSpecial)return false; return true; }); bool onlySpecial = true; bool isBuyExpensive = false; float maxAmount = getMaxAmount(); for(const auto& buyInfo : buyInfos){ for(size_t i = 0; i < vec.size(); i++){ if(buyInfo != vec[i].id)continue; if(!vec[i].isSpecial) onlySpecial = false; if(stof(vec[i].price) == maxAmount) isBuyExpensive = true; } } // 购买过最贵商品 if(isBuyExpensive)return UserBuyType::LotShopping; // 仅购买Special商品 if(onlySpecial)return UserBuyType::LittleShopping; // 正常购物 return UserBuyType::NormalShopping; } std::vector IAPUserData::getBuyInfos(){ return _buyInfos; } void IAPCtlShop::addUserBuyInfo(std::string commodityID){ iap::IAPUserData::getInstance()->addBuyInfo(commodityID); } void IAPCtlShop::clearUserBuyInfo(){ iap::IAPUserData::getInstance()->clearBuyInfo(); } float IAPCtlShop::getMaxAmount(){ float maxn = 0.0; auto conf = IAPConf::getInstance(); vector goodInfos; conf->getAllGoods(goodInfos); std::regex re(R"(\d+(\.\d{1,2})?)"); // 匹配整数或最多两位小数的数字 for(const auto& goodInfo : goodInfos){ std::smatch match; if (std::regex_search(goodInfo.cost, match, re)) { maxn = std::max(maxn, stof(match[0])); // 提取价格 } else { log("IAPUserData::getUserBuyType : 转换失败"); } } return maxn; } NS_IAP_END