IAPCtlShop.cpp 4.9 KB

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