123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- //
- // IAPRunTimeData.cpp
- // demo
- //
- // Created by Red_mini on 2024/10/25.
- //
- #include "IAPRunTimeData.hpp"
- #include "IAPUserData.hpp"
- #include "IAPConf.hpp"
- #include "cocos2d.h"
- NS_IAPSHOP_BEGIN
- IAPRunTimeData* IAPRunTimeData::_instance = nullptr;
- IAPRunTimeData* IAPRunTimeData::getInstance(){
- if(_instance == nullptr){
- _instance = new IAPRunTimeData();
- }
- return _instance;
- }
- IAPRunTimeData::IAPRunTimeData(){
- _init();
- }
- void IAPRunTimeData::setDeviceLevel(int level){
- _leval = level;
- }
- int IAPRunTimeData::getDeviceLevel(){
- return _leval;
- }
- void IAPRunTimeData::_init(){
- _leval = 1;
- }
- float IAPRunTimeData::getUserBuyMaxAmount(){
- auto conf = IAPConf::getInstance();
- std::vector<std::string> buyInfoIDs = IAPUserData::getInstance()->getBuyInfos();
-
- std::set<std::string> 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;
- }
- UserBuyType IAPRunTimeData::getUserBuyType(){
- auto conf = IAPConf::getInstance();
- // 如果没有购买记录
- auto buyInfos = IAPUserData::getInstance()->getBuyInfos();
- if(buyInfos.size() == 0) return UserBuyType::NoShopping;
-
- 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 = 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;
- }
- NS_IAPSHOP_END
|