IAPCtlShop.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. //
  2. // IAPCtlShop.cpp
  3. // TileManor
  4. //
  5. // Created by 徐炼新 on 2024/9/30.
  6. //
  7. #include "IAPCtlShop.hpp"
  8. #include "cocos2d.h"
  9. #include "RUBaseConf.h"
  10. #include "RUUtils.h"
  11. #include "IAPConf.hpp"
  12. #include "IAPCtlShopUI.hpp"
  13. #include "IAPUserData.hpp"
  14. #include "IAPRunTimeData.hpp"
  15. #include "PBConfigData.hpp"
  16. #include "PurchaseBannerView.hpp"
  17. NS_IAP_BEGIN
  18. IAPCtlShop* IAPCtlShop::_instance = nullptr;
  19. IAPCtlShop* IAPCtlShop::createWith(){
  20. if (_instance == nullptr) {
  21. _instance = new IAPCtlShop();
  22. }
  23. return _instance;
  24. }
  25. void IAPCtlShop::init(std::string &cfgFN){
  26. redutils::BaseConf* baseConf = new redutils::BaseConf(cfgFN.c_str());
  27. _conf = IAPConf::getInstance();
  28. _conf->initWith(baseConf);
  29. }
  30. void IAPCtlShop::setDelegate(IAPDelegate *delegate){
  31. _delegate = delegate;
  32. }
  33. void IAPCtlShop::setCfg4Failure(std::string &cfgFN){
  34. _pbConf = PBConfigData::getInstance();
  35. _pbConf->initData(cfgFN);
  36. }
  37. void IAPCtlShop::displayFailureCardIn(cocos2d::Node *node){
  38. auto sDot = _pbConf->getViewDot();
  39. PurchaseBannerView::sDotCfg dot;
  40. dot.highSp = sDot.highSp;
  41. dot.normalSp = sDot.normalSp;
  42. dot.sepWidth = sDot.sepWidth;
  43. dot.yStart = sDot.yStart;
  44. auto visibleSize = Director::getInstance()->getVisibleSize();
  45. float width = visibleSize.width;
  46. cocos2d::Size sz = cocos2d::Size(width, 300);
  47. PurchaseBannerView* view = PurchaseBannerView::create(sz, dot, true);
  48. std::vector<std::string> names;
  49. if(_delegate)names = _delegate->getCardsNameWhenFailed();
  50. for(const auto& name : names){
  51. PurchaseBannerCell* tmpNode = PurchaseBannerCell::create(name);
  52. view->addBanner(tmpNode);
  53. }
  54. node->addChild(view);
  55. }
  56. void IAPCtlShop::setDeviceLevel(int level){
  57. IAPRunTimeData::getInstance()->setDeviceLevel(level);
  58. }
  59. bool IAPCtlShop::addAPlacement(const IAPPlacement &plInfo){
  60. bool ans =IAPCtlShopUI::getInstance()->addAPlacement(plInfo);
  61. return ans;
  62. }
  63. void IAPCtlShop::removePlacement(const std::string &id){
  64. IAPCtlShopUI::getInstance()->removePlacement(id);
  65. }
  66. bool IAPCtlShop::addCardToPlacement(const std::string &id, IAPCard *card){
  67. return IAPCtlShopUI::getInstance()->addCardToPlacement(id, card);
  68. }
  69. bool IAPCtlShop::removeCardToPlacement(const std::string &id, int cardIndex){
  70. return IAPCtlShopUI::getInstance()->removeCardToPlacement(id, cardIndex);
  71. }
  72. void IAPCtlShop::clearPlacement(){
  73. IAPCtlShopUI::getInstance()->clearPlacement();
  74. }
  75. size_t IAPCtlShop::getPlacementCount(){
  76. return IAPCtlShopUI::getInstance()->getPlacementCount();
  77. }
  78. void IAPCtlShop::showInNode(cocos2d::Node *pParent, ShopRequirement &requirement){
  79. IAPCtlShopUI::getInstance()->create(pParent, _conf, requirement);
  80. }
  81. void IAPCtlShop::showSlideCardsByCoinsIn(cocos2d::Node* pParent, int stillNeedCoins){
  82. IAPCtlShopUI::getInstance()->showSlideCardsByCoinsIn(pParent, stillNeedCoins);
  83. }
  84. UserBuyType IAPCtlShop::getUserBuyType(){
  85. // 如果没有购买记录
  86. auto buyInfos = IAPUserData::getInstance()->getBuyInfos();
  87. if(buyInfos.size() == 0) return UserBuyType::NoShopping;
  88. auto conf = IAPConf::getInstance();
  89. vector<GoodsInfo> goodInfos;
  90. conf->getAllGoods(goodInfos);
  91. // 按照价格排序 , 价格相同按照是否为special商品排序
  92. sort(goodInfos.begin(), goodInfos.end(), [](GoodsInfo a,GoodsInfo b){
  93. if(a.getCostNumber() != b.getCostNumber()) return a.getCostNumber() < b.getCostNumber();
  94. if(a.style == "mostValued")return true;
  95. if(b.style == "mostValued")return false;
  96. return true;
  97. });
  98. bool onlySpecial = true; // 是否仅购买特惠商品
  99. bool isBuyExpensive = false; // 是否购买过最贵商品
  100. float maxAmount = _conf->getMaxAmount();
  101. for(const auto& buyInfo : buyInfos){
  102. for(size_t i = 0; i < goodInfos.size(); i++){
  103. if(buyInfo != goodInfos[i].id)continue;
  104. if(goodInfos[i].type != "mostValued") onlySpecial = false;
  105. if(goodInfos[i].getCostNumber() == maxAmount) isBuyExpensive = true;
  106. }
  107. }
  108. // 购买过最贵商品
  109. if(isBuyExpensive)return UserBuyType::LotShopping;
  110. // 仅购买Special商品
  111. if(onlySpecial)return UserBuyType::LittleShopping;
  112. // 正常购物
  113. return UserBuyType::NormalShopping;
  114. }
  115. void IAPCtlShop::addUserBuyInfo(std::string commodityID){
  116. iap::IAPUserData::getInstance()->addBuyInfo(commodityID);
  117. if(_delegate){
  118. std::vector<GoodsInfo> goodInfos;
  119. _conf->getAllGoods(goodInfos);
  120. for(const auto& goodInfo : goodInfos){
  121. if(goodInfo.id == commodityID){
  122. std::map<string, string> buyInfo;
  123. for(const auto& area : goodInfo.areas){
  124. for(const auto& info : area){
  125. buyInfo[info.name] = info.count;
  126. }
  127. }
  128. // 发送礼包内含有的物品
  129. _delegate->onUserBuySuccess(buyInfo);
  130. break;
  131. }
  132. }
  133. }
  134. }
  135. void IAPCtlShop::clearUserBuyInfo(){
  136. iap::IAPUserData::getInstance()->clearBuyInfo();
  137. }
  138. float IAPCtlShop::getUserBuyMaxAmount(){
  139. std::vector<std::string> buyInfoIDs = IAPUserData::getInstance()->getBuyInfos();
  140. std::set<std::string> buyInfoID;
  141. // 去重
  142. for(const auto& info : buyInfoIDs){
  143. buyInfoID.insert(info);
  144. }
  145. // 提取最大金额
  146. float maxn = -1;
  147. for(const auto& id : buyInfoID){
  148. float coinNum = _conf->getCoinNum(id);
  149. maxn = maxn > coinNum ? maxn : coinNum;
  150. }
  151. return maxn;
  152. }
  153. NS_IAP_END