// // 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 "IAPRunTimeData.hpp" #include "PBConfigData.hpp" #include "PurchaseBannerView.hpp" 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); } void IAPCtlShop::setDelegate(IAPDelegate *delegate){ _delegate = delegate; } void IAPCtlShop::setCfg4Failure(std::string &cfgFN){ _pbConf = PBConfigData::getInstance(); _pbConf->initData(cfgFN); } void IAPCtlShop::displayFailureCardIn(cocos2d::Node *node, std::vector cardsName){ auto sDot = _pbConf->getViewDot(); PurchaseBannerView::sDotCfg dot; dot.highSp = sDot.highSp; dot.normalSp = sDot.normalSp; dot.sepWidth = sDot.sepWidth; dot.yStart = sDot.yStart; auto visibleSize = Director::getInstance()->getVisibleSize(); float width = visibleSize.width; cocos2d::Size sz = cocos2d::Size(width, 300); PurchaseBannerView* view = PurchaseBannerView::create(sz, dot, true); for(const auto& name : cardsName){ PurchaseBannerCell* tmpNode = PurchaseBannerCell::create(name); view->addBanner(tmpNode); } node->addChild(view); } void IAPCtlShop::setDeviceLevel(int level){ IAPRunTimeData::getInstance()->setDeviceLevel(level); } bool IAPCtlShop::addAPlacement(const IAPPlacement &plInfo){ bool ans =IAPCtlShopUI::getInstance()->addAPlacement(plInfo); return ans; } void IAPCtlShop::removePlacement(const std::string &id){ IAPCtlShopUI::getInstance()->removePlacement(id); } bool IAPCtlShop::addCardToPlacement(const std::string &id, IAPCard *card){ return IAPCtlShopUI::getInstance()->addCardToPlacement(id, card); } bool IAPCtlShop::removeCardToPlacement(const std::string &id, int cardIndex){ return IAPCtlShopUI::getInstance()->removeCardToPlacement(id, cardIndex); } void IAPCtlShop::clearPlacement(){ IAPCtlShopUI::getInstance()->clearPlacement(); } size_t IAPCtlShop::getPlacementCount(){ return IAPCtlShopUI::getInstance()->getPlacementCount(); } void IAPCtlShop::showInNode(cocos2d::Node *pParent, ShopRequirement &requirement){ IAPCtlShopUI::getInstance()->create(pParent, _conf, requirement); } void IAPCtlShop::showSlideCardsByCoinsIn(cocos2d::Node* pParent, int stillNeedCoins){ IAPCtlShopUI::getInstance()->showSlideCardsByCoinsIn(pParent, stillNeedCoins); } UserBuyType IAPCtlShop::getUserBuyType(){ // 如果没有购买记录 auto buyInfos = IAPUserData::getInstance()->getBuyInfos(); if(buyInfos.size() == 0) return UserBuyType::NoShopping; auto conf = IAPConf::getInstance(); vector goodInfos; conf->getAllGoods(goodInfos); // 按照价格排序 , 价格相同按照是否为special商品排序 sort(goodInfos.begin(), goodInfos.end(), [](GoodsInfo a,GoodsInfo b){ if(a.getCostNumber() != b.getCostNumber()) return a.getCostNumber() < b.getCostNumber(); if(a.style == "mostValued")return true; if(b.style == "mostValued")return false; return true; }); bool onlySpecial = true; // 是否仅购买特惠商品 bool isBuyExpensive = false; // 是否购买过最贵商品 float maxAmount = _conf->getMaxAmount(); for(const auto& buyInfo : buyInfos){ for(size_t i = 0; i < goodInfos.size(); i++){ if(buyInfo != goodInfos[i].id)continue; if(goodInfos[i].type != "mostValued") onlySpecial = false; if(goodInfos[i].getCostNumber() == maxAmount) isBuyExpensive = true; } } // 购买过最贵商品 if(isBuyExpensive)return UserBuyType::LotShopping; // 仅购买Special商品 if(onlySpecial)return UserBuyType::LittleShopping; // 正常购物 return UserBuyType::NormalShopping; } void IAPCtlShop::addUserBuyInfo(std::string commodityID){ iap::IAPUserData::getInstance()->addBuyInfo(commodityID); if(_delegate){ std::vector goodInfos; _conf->getAllGoods(goodInfos); for(const auto& goodInfo : goodInfos){ if(goodInfo.id == commodityID){ std::map buyInfo; for(const auto& area : goodInfo.areas){ for(const auto& info : area){ buyInfo[info.name] = info.count; } } // 发送礼包内含有的物品 _delegate->onUserBuySuccess(buyInfo); break; } } } } void IAPCtlShop::clearUserBuyInfo(){ iap::IAPUserData::getInstance()->clearBuyInfo(); } float IAPCtlShop::getUserBuyMaxAmount(){ std::vector buyInfoIDs = IAPUserData::getInstance()->getBuyInfos(); std::set buyInfoID; // 去重 for(const auto& info : buyInfoIDs){ buyInfoID.insert(info); } // 提取最大金额 float maxn = -1; for(const auto& id : buyInfoID){ float coinNum = _conf->getCoinNum(id); maxn = maxn > coinNum ? maxn : coinNum; } return maxn; } NS_IAP_END