123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- //
- // 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"
- 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::setDeviceLevel(int level){
- IAPRunTimeData::getInstance()->setDeviceLevel(level);
- if(_delegate){
- _delegate->setDeviceLevel(level);
- }
- }
- bool IAPCtlShop::addAPlacement(const IAPPlacement &plInfo){
- bool ans =IAPCtlShopUI::getInstance()->addAPlacement(plInfo);
- if(_delegate){
- _delegate->addAPlacement(plInfo);
- }
- return ans;
- }
- void IAPCtlShop::removePlacement(const std::string &id){
- IAPCtlShopUI::getInstance()->removePlacement(id);
- if(_delegate){
- _delegate->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::showPlacementsInNode(cocos2d::Node *pParent, const vector<std::string> &plIds){
- IAPCtlShopUI::getInstance()->showPlacementsInNode(pParent, 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);
-
- // 按照价格排序 , 价格相同按照是否为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 = 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){
- _delegate->onUserBuySuccess(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);
-
- for(const auto& goodInfo : goodInfos){
- maxn = std::max(maxn, goodInfo.getCostNumber());
- }
-
- return maxn;
- }
- NS_IAP_END
|