123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- //
- // 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 <algorithm>
- #include <string>
- #include <regex>
- 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<std::string> &plIds){
-
- }
- UserBuyType IAPCtlShop::getUserBuyType(){
- // 如果没有购买记录
- auto buyInfos = IAPUserData::getInstance()->getBuyInfos();
- if(buyInfos.size() == 0) return UserBuyType::NoShopping;
-
- auto conf = IAPConf::getInstance();
- vector<GoodsInfo> goodInfos;
- conf->getAllGoods(goodInfos);
-
- struct SortVec{
- std::string id;
- std::string price;
- bool isSpecial;
- };
- vector<SortVec> 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<std::string> 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<GoodsInfo> 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
|