IAPRunTimeData.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. //
  2. // IAPRunTimeData.cpp
  3. // demo
  4. //
  5. // Created by Red_mini on 2024/10/25.
  6. //
  7. #include "IAPRunTimeData.hpp"
  8. #include "IAPUserData.hpp"
  9. #include "IAPConf.hpp"
  10. #include "cocos2d.h"
  11. NS_IAPSHOP_BEGIN
  12. IAPRunTimeData* IAPRunTimeData::_instance = nullptr;
  13. IAPRunTimeData* IAPRunTimeData::getInstance(){
  14. if(_instance == nullptr){
  15. _instance = new IAPRunTimeData();
  16. }
  17. return _instance;
  18. }
  19. IAPRunTimeData::IAPRunTimeData(){
  20. _init();
  21. }
  22. void IAPRunTimeData::setDeviceLevel(int level){
  23. _leval = level;
  24. }
  25. int IAPRunTimeData::getDeviceLevel(){
  26. return _leval;
  27. }
  28. void IAPRunTimeData::_init(){
  29. _leval = 1;
  30. }
  31. float IAPRunTimeData::getUserBuyMaxAmount(){
  32. auto conf = IAPConf::getInstance();
  33. std::vector<std::string> buyInfoIDs = IAPUserData::getInstance()->getBuyInfos();
  34. std::set<std::string> buyInfoID;
  35. // 去重
  36. for(const auto& info : buyInfoIDs){
  37. buyInfoID.insert(info);
  38. }
  39. // 提取最大金额
  40. float maxn = -1;
  41. for(const auto& id : buyInfoID){
  42. float coinNum = conf->getCoinNum(id);
  43. maxn = maxn > coinNum ? maxn : coinNum;
  44. }
  45. return maxn;
  46. }
  47. UserBuyType IAPRunTimeData::getUserBuyType(){
  48. auto conf = IAPConf::getInstance();
  49. // 如果没有购买记录
  50. auto buyInfos = IAPUserData::getInstance()->getBuyInfos();
  51. if(buyInfos.size() == 0) return UserBuyType::NoShopping;
  52. vector<GoodsInfo> goodInfos;
  53. conf->getAllGoods(goodInfos);
  54. // 按照价格排序 , 价格相同按照是否为special商品排序
  55. sort(goodInfos.begin(), goodInfos.end(), [](GoodsInfo a,GoodsInfo b){
  56. if(a.getCostNumber() != b.getCostNumber()) return a.getCostNumber() < b.getCostNumber();
  57. if(a.style == "mostValued")return true;
  58. if(b.style == "mostValued")return false;
  59. return true;
  60. });
  61. bool onlySpecial = true; // 是否仅购买特惠商品
  62. bool isBuyExpensive = false; // 是否购买过最贵商品
  63. float maxAmount = conf->getMaxAmount();
  64. for(const auto& buyInfo : buyInfos){
  65. for(size_t i = 0; i < goodInfos.size(); i++){
  66. if(buyInfo != goodInfos[i].id)continue;
  67. if(goodInfos[i].type != "mostValued") onlySpecial = false;
  68. if(goodInfos[i].getCostNumber() == maxAmount) isBuyExpensive = true;
  69. }
  70. }
  71. // 购买过最贵商品
  72. if(isBuyExpensive)return UserBuyType::LotShopping;
  73. // 仅购买Special商品
  74. if(onlySpecial)return UserBuyType::LittleShopping;
  75. // 正常购物
  76. return UserBuyType::NormalShopping;
  77. }
  78. NS_IAPSHOP_END