IAPCtlShop.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. NS_IAP_BEGIN
  16. IAPCtlShop* IAPCtlShop::_instance = nullptr;
  17. IAPCtlShop* IAPCtlShop::createWith(){
  18. if (_instance == nullptr) {
  19. _instance = new IAPCtlShop();
  20. }
  21. return _instance;
  22. }
  23. void IAPCtlShop::init(std::string &cfgFN){
  24. redutils::BaseConf* baseConf = new redutils::BaseConf(cfgFN.c_str());
  25. _conf = IAPConf::getInstance();
  26. _conf->initWith(baseConf);
  27. }
  28. void IAPCtlShop::setDelegate(IAPDelegate *delegate){
  29. _delegate = delegate;
  30. }
  31. void IAPCtlShop::setDeviceLevel(int level){
  32. IAPRunTimeData::getInstance()->setDeviceLevel(level);
  33. if(_delegate){
  34. _delegate->setDeviceLevel(level);
  35. }
  36. }
  37. bool IAPCtlShop::addAPlacement(const IAPPlacement &plInfo){
  38. bool ans =IAPCtlShopUI::getInstance()->addAPlacement(plInfo);
  39. if(_delegate){
  40. _delegate->addAPlacement(plInfo);
  41. }
  42. return ans;
  43. }
  44. void IAPCtlShop::removePlacement(const std::string &id){
  45. IAPCtlShopUI::getInstance()->removePlacement(id);
  46. if(_delegate){
  47. _delegate->removePlacement(id);
  48. }
  49. }
  50. bool IAPCtlShop::addCardToPlacement(const std::string &id, IAPCard *card){
  51. return IAPCtlShopUI::getInstance()->addCardToPlacement(id, card);
  52. }
  53. bool IAPCtlShop::removeCardToPlacement(const std::string &id, int cardIndex){
  54. return IAPCtlShopUI::getInstance()->removeCardToPlacement(id, cardIndex);
  55. }
  56. void IAPCtlShop::clearPlacement(){
  57. IAPCtlShopUI::getInstance()->clearPlacement();
  58. }
  59. size_t IAPCtlShop::getPlacementCount(){
  60. return IAPCtlShopUI::getInstance()->getPlacementCount();
  61. }
  62. void IAPCtlShop::showInNode(cocos2d::Node *pParent, ShopRequirement &requirement){
  63. IAPCtlShopUI::getInstance()->create(pParent, _conf, requirement);
  64. }
  65. void IAPCtlShop::showPlacementsInNode(cocos2d::Node *pParent, const vector<std::string> &plIds){
  66. IAPCtlShopUI::getInstance()->showPlacementsInNode(pParent, plIds);
  67. }
  68. UserBuyType IAPCtlShop::getUserBuyType(){
  69. // 如果没有购买记录
  70. auto buyInfos = IAPUserData::getInstance()->getBuyInfos();
  71. if(buyInfos.size() == 0) return UserBuyType::NoShopping;
  72. auto conf = IAPConf::getInstance();
  73. vector<GoodsInfo> goodInfos;
  74. conf->getAllGoods(goodInfos);
  75. // 按照价格排序 , 价格相同按照是否为special商品排序
  76. sort(goodInfos.begin(), goodInfos.end(), [](GoodsInfo a,GoodsInfo b){
  77. if(a.getCostNumber() != b.getCostNumber()) return a.getCostNumber() < b.getCostNumber();
  78. if(a.style == "mostValued")return true;
  79. if(b.style == "mostValued")return false;
  80. return true;
  81. });
  82. bool onlySpecial = true; // 是否仅购买特惠商品
  83. bool isBuyExpensive = false; // 是否购买过最贵商品
  84. float maxAmount = _conf->getMaxAmount();
  85. for(const auto& buyInfo : buyInfos){
  86. for(size_t i = 0; i < goodInfos.size(); i++){
  87. if(buyInfo != goodInfos[i].id)continue;
  88. if(goodInfos[i].type != "mostValued") onlySpecial = false;
  89. if(goodInfos[i].getCostNumber() == maxAmount) isBuyExpensive = true;
  90. }
  91. }
  92. // 购买过最贵商品
  93. if(isBuyExpensive)return UserBuyType::LotShopping;
  94. // 仅购买Special商品
  95. if(onlySpecial)return UserBuyType::LittleShopping;
  96. // 正常购物
  97. return UserBuyType::NormalShopping;
  98. }
  99. void IAPCtlShop::addUserBuyInfo(std::string commodityID){
  100. iap::IAPUserData::getInstance()->addBuyInfo(commodityID);
  101. if(_delegate){
  102. _delegate->onUserBuySuccess(commodityID);
  103. }
  104. }
  105. void IAPCtlShop::clearUserBuyInfo(){
  106. iap::IAPUserData::getInstance()->clearBuyInfo();
  107. if(_delegate){
  108. _delegate->clearUserBuyInfo();
  109. }
  110. }
  111. float IAPCtlShop::getUserBuyMaxAmount(){
  112. std::vector<std::string> buyInfoIDs = IAPUserData::getInstance()->getBuyInfos();
  113. std::set<std::string> buyInfoID;
  114. // 去重
  115. for(const auto& info : buyInfoIDs){
  116. buyInfoID.insert(info);
  117. }
  118. // 提取最大金额
  119. float maxn = -1;
  120. for(const auto& id : buyInfoID){
  121. float coinNum = _conf->getCoinNum(id);
  122. maxn = maxn > coinNum ? maxn : coinNum;
  123. }
  124. return maxn;
  125. }
  126. NS_IAP_END