IAPCtlShop.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. bool ans = false;
  41. if(ans)_placements[plInfo.id] = plInfo;
  42. return ans;
  43. }
  44. void IAPCtlShop::removePlacement(const std::string &id){
  45. _placements.erase(id);
  46. }
  47. size_t IAPCtlShop::getPlacementCount(){
  48. return _placements.size();
  49. }
  50. void IAPCtlShop::showInNode(cocos2d::Node *pParent, ShopRequirement &requirement){
  51. _shopUI = IAPCtlShopUI::getInstance();
  52. _shopUI->create(pParent, _conf, requirement);
  53. }
  54. void IAPCtlShop::showPlacementsInNode(cocos2d::Node *pParent, const vector<std::string> &plIds){
  55. }
  56. UserBuyType IAPCtlShop::getUserBuyType(){
  57. // 如果没有购买记录
  58. auto buyInfos = IAPUserData::getInstance()->getBuyInfos();
  59. if(buyInfos.size() == 0) return UserBuyType::NoShopping;
  60. auto conf = IAPConf::getInstance();
  61. vector<GoodsInfo> goodInfos;
  62. conf->getAllGoods(goodInfos);
  63. // 按照价格排序 , 价格相同按照是否为special商品排序
  64. sort(goodInfos.begin(), goodInfos.end(), [](GoodsInfo a,GoodsInfo b){
  65. if(a.getCostNumber() != b.getCostNumber()) return a.getCostNumber() < b.getCostNumber();
  66. if(a.style == "mostValued")return true;
  67. if(b.style == "mostValued")return false;
  68. return true;
  69. });
  70. bool onlySpecial = true; // 是否仅购买特惠商品
  71. bool isBuyExpensive = false; // 是否购买过最贵商品
  72. float maxAmount = getMaxAmount();
  73. for(const auto& buyInfo : buyInfos){
  74. for(size_t i = 0; i < goodInfos.size(); i++){
  75. if(buyInfo != goodInfos[i].id)continue;
  76. if(goodInfos[i].type != "mostValued") onlySpecial = false;
  77. if(goodInfos[i].getCostNumber() == maxAmount) isBuyExpensive = true;
  78. }
  79. }
  80. // 购买过最贵商品
  81. if(isBuyExpensive)return UserBuyType::LotShopping;
  82. // 仅购买Special商品
  83. if(onlySpecial)return UserBuyType::LittleShopping;
  84. // 正常购物
  85. return UserBuyType::NormalShopping;
  86. }
  87. std::vector<std::string> IAPUserData::getBuyInfos(){
  88. return _buyInfos;
  89. }
  90. void IAPCtlShop::addUserBuyInfo(std::string commodityID){
  91. iap::IAPUserData::getInstance()->addBuyInfo(commodityID);
  92. }
  93. void IAPCtlShop::clearUserBuyInfo(){
  94. iap::IAPUserData::getInstance()->clearBuyInfo();
  95. }
  96. float IAPCtlShop::getMaxAmount(){
  97. float maxn = 0.0;
  98. auto conf = IAPConf::getInstance();
  99. vector<GoodsInfo> goodInfos;
  100. conf->getAllGoods(goodInfos);
  101. for(const auto& goodInfo : goodInfos){
  102. maxn = std::max(maxn, goodInfo.getCostNumber());
  103. }
  104. return maxn;
  105. }
  106. NS_IAP_END