IAPCtlShop.cpp 3.8 KB

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