IAPCtlShop.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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->getCardNameWhenFailed();
  50. for(const auto& name : names){
  51. PurchaseBannerCell* tmpNode = PurchaseBannerCell::create(name);
  52. view->addBanner(tmpNode);
  53. }
  54. // // 测试暂用
  55. // PurchaseBannerCell* tmpNode1 = PurchaseBannerCell::create("礼包1");
  56. // view->addBanner(tmpNode1);
  57. // PurchaseBannerCell* tmpNode2 = PurchaseBannerCell::create("tilefavor");
  58. // view->addBanner(tmpNode2);
  59. // PurchaseBannerCell* tmpNode3 = PurchaseBannerCell::create("tilepack");
  60. // view->addBanner(tmpNode3);
  61. // PurchaseBannerCell* tmpNode4 = PurchaseBannerCell::create("tilepass");
  62. // view->addBanner(tmpNode4);
  63. node->addChild(view);
  64. }
  65. void IAPCtlShop::setDeviceLevel(int level){
  66. IAPRunTimeData::getInstance()->setDeviceLevel(level);
  67. }
  68. bool IAPCtlShop::addAPlacement(const IAPPlacement &plInfo){
  69. bool ans =IAPCtlShopUI::getInstance()->addAPlacement(plInfo);
  70. return ans;
  71. }
  72. void IAPCtlShop::removePlacement(const std::string &id){
  73. IAPCtlShopUI::getInstance()->removePlacement(id);
  74. }
  75. bool IAPCtlShop::addCardToPlacement(const std::string &id, IAPCard *card){
  76. return IAPCtlShopUI::getInstance()->addCardToPlacement(id, card);
  77. }
  78. bool IAPCtlShop::removeCardToPlacement(const std::string &id, int cardIndex){
  79. return IAPCtlShopUI::getInstance()->removeCardToPlacement(id, cardIndex);
  80. }
  81. void IAPCtlShop::clearPlacement(){
  82. IAPCtlShopUI::getInstance()->clearPlacement();
  83. }
  84. size_t IAPCtlShop::getPlacementCount(){
  85. return IAPCtlShopUI::getInstance()->getPlacementCount();
  86. }
  87. void IAPCtlShop::showInNode(cocos2d::Node *pParent, ShopRequirement &requirement){
  88. IAPCtlShopUI::getInstance()->create(pParent, _conf, requirement);
  89. }
  90. UserBuyType IAPCtlShop::getUserBuyType(){
  91. // 如果没有购买记录
  92. auto buyInfos = IAPUserData::getInstance()->getBuyInfos();
  93. if(buyInfos.size() == 0) return UserBuyType::NoShopping;
  94. auto conf = IAPConf::getInstance();
  95. vector<GoodsInfo> goodInfos;
  96. conf->getAllGoods(goodInfos);
  97. // 按照价格排序 , 价格相同按照是否为special商品排序
  98. sort(goodInfos.begin(), goodInfos.end(), [](GoodsInfo a,GoodsInfo b){
  99. if(a.getCostNumber() != b.getCostNumber()) return a.getCostNumber() < b.getCostNumber();
  100. if(a.style == "mostValued")return true;
  101. if(b.style == "mostValued")return false;
  102. return true;
  103. });
  104. bool onlySpecial = true; // 是否仅购买特惠商品
  105. bool isBuyExpensive = false; // 是否购买过最贵商品
  106. float maxAmount = _conf->getMaxAmount();
  107. for(const auto& buyInfo : buyInfos){
  108. for(size_t i = 0; i < goodInfos.size(); i++){
  109. if(buyInfo != goodInfos[i].id)continue;
  110. if(goodInfos[i].type != "mostValued") onlySpecial = false;
  111. if(goodInfos[i].getCostNumber() == maxAmount) isBuyExpensive = true;
  112. }
  113. }
  114. // 购买过最贵商品
  115. if(isBuyExpensive)return UserBuyType::LotShopping;
  116. // 仅购买Special商品
  117. if(onlySpecial)return UserBuyType::LittleShopping;
  118. // 正常购物
  119. return UserBuyType::NormalShopping;
  120. }
  121. void IAPCtlShop::addUserBuyInfo(std::string commodityID){
  122. iap::IAPUserData::getInstance()->addBuyInfo(commodityID);
  123. if(_delegate){
  124. std::vector<GoodsInfo> goodInfos;
  125. _conf->getAllGoods(goodInfos);
  126. for(const auto& goodInfo : goodInfos){
  127. if(goodInfo.id == commodityID){
  128. std::map<string, string> buyInfo;
  129. for(const auto& area : goodInfo.areas){
  130. for(const auto& info : area){
  131. buyInfo[info.name] = info.count;
  132. }
  133. }
  134. // 发送礼包内含有的物品
  135. _delegate->onUserBuySuccess(buyInfo);
  136. break;
  137. }
  138. }
  139. }
  140. }
  141. void IAPCtlShop::clearUserBuyInfo(){
  142. iap::IAPUserData::getInstance()->clearBuyInfo();
  143. }
  144. float IAPCtlShop::getUserBuyMaxAmount(){
  145. std::vector<std::string> buyInfoIDs = IAPUserData::getInstance()->getBuyInfos();
  146. std::set<std::string> buyInfoID;
  147. // 去重
  148. for(const auto& info : buyInfoIDs){
  149. buyInfoID.insert(info);
  150. }
  151. // 提取最大金额
  152. float maxn = -1;
  153. for(const auto& id : buyInfoID){
  154. float coinNum = _conf->getCoinNum(id);
  155. maxn = maxn > coinNum ? maxn : coinNum;
  156. }
  157. return maxn;
  158. }
  159. NS_IAP_END