IAPCtlShop.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 <algorithm>
  15. #include <string>
  16. #include <regex>
  17. NS_IAP_BEGIN
  18. IAPCtlShop* IAPCtlShop::_instance = nullptr;
  19. IAPCtlShop* IAPCtlShop::createWith(){
  20. if (_instance == nullptr) {
  21. _instance = new IAPCtlShop();
  22. }
  23. return _instance;
  24. }
  25. void IAPCtlShop::init(std::string &cfgFN){
  26. redutils::BaseConf* baseConf = new redutils::BaseConf(cfgFN.c_str());
  27. _conf = IAPConf::getInstance();
  28. _conf->initWith(baseConf);
  29. _shopUI = IAPCtlShopUI::getInstance();
  30. _delegate = IAPDelegate::getInstance();
  31. _level = 1;
  32. }
  33. void IAPCtlShop::setDelegate(IAPDelegate *delegate){
  34. _delegate = delegate;
  35. }
  36. void IAPCtlShop::setDeviceLevel(int level){
  37. _level = level;
  38. }
  39. int IAPCtlShop::getDeviceLevel(){
  40. return _level;
  41. }
  42. bool IAPCtlShop::addAPlacement(const IAPPlacement &plInfo){
  43. bool ans = false;
  44. if(ans)placementIDs.insert(plInfo.id);
  45. return ans;
  46. }
  47. void IAPCtlShop::removePlacement(const std::string &id){
  48. placementIDs.erase(id);
  49. }
  50. size_t IAPCtlShop::getPlacementCount(){
  51. return placementIDs.size();
  52. }
  53. void IAPCtlShop::showInNode(cocos2d::Node *pParent, ShopRequirement &requirement){
  54. _shopUI = IAPCtlShopUI::getInstance();
  55. _shopUI->create(pParent, _conf, requirement,1);
  56. }
  57. void IAPCtlShop::showPlacementsInNode(cocos2d::Node *pParent, const vector<std::string> &plIds){
  58. }
  59. UserBuyType IAPCtlShop::getUserBuyType(){
  60. // 如果没有购买记录
  61. auto buyInfos = IAPUserData::getInstance()->getBuyInfos();
  62. if(buyInfos.size() == 0) return UserBuyType::NoShopping;
  63. auto conf = IAPConf::getInstance();
  64. vector<GoodsInfo> goodInfos;
  65. conf->getAllGoods(goodInfos);
  66. struct SortVec{
  67. std::string id;
  68. std::string price;
  69. bool isSpecial;
  70. };
  71. vector<SortVec> vec;
  72. std::regex re(R"(\d+(\.\d{1,2})?)"); // 匹配整数或最多两位小数的数字
  73. for(const auto& goodInfo : goodInfos){
  74. bool isSpecial = goodInfo.style == "mostValued" ? true : false;
  75. std::smatch match;
  76. if (std::regex_search(goodInfo.cost, match, re)) {
  77. SortVec sv;
  78. sv.id = goodInfo.id;
  79. sv.price = match[0]; // 提取价格
  80. sv.isSpecial = isSpecial;
  81. vec.push_back(sv);
  82. } else {
  83. log("IAPUserData::getUserBuyType : 转换失败");
  84. }
  85. }
  86. // 按照价格排序 , 价格相同按照是否为special商品排序
  87. sort(vec.begin(), vec.end(), [](SortVec a,SortVec b){
  88. if(a.price != b.price) return stof(a.price) < stof(b.price);
  89. if(a.isSpecial)return true;
  90. if(b.isSpecial)return false;
  91. return true;
  92. });
  93. bool onlySpecial = true;
  94. bool isBuyExpensive = false;
  95. float maxAmount = getMaxAmount();
  96. for(const auto& buyInfo : buyInfos){
  97. for(size_t i = 0; i < vec.size(); i++){
  98. if(buyInfo != vec[i].id)continue;
  99. if(!vec[i].isSpecial) onlySpecial = false;
  100. if(stof(vec[i].price) == maxAmount) isBuyExpensive = true;
  101. }
  102. }
  103. // 购买过最贵商品
  104. if(isBuyExpensive)return UserBuyType::LotShopping;
  105. // 仅购买Special商品
  106. if(onlySpecial)return UserBuyType::LittleShopping;
  107. // 正常购物
  108. return UserBuyType::NormalShopping;
  109. }
  110. std::vector<std::string> IAPUserData::getBuyInfos(){
  111. return _buyInfos;
  112. }
  113. void IAPCtlShop::addUserBuyInfo(std::string commodityID){
  114. iap::IAPUserData::getInstance()->addBuyInfo(commodityID);
  115. }
  116. void IAPCtlShop::clearUserBuyInfo(){
  117. iap::IAPUserData::getInstance()->clearBuyInfo();
  118. }
  119. float IAPCtlShop::getMaxAmount(){
  120. float maxn = 0.0;
  121. auto conf = IAPConf::getInstance();
  122. vector<GoodsInfo> goodInfos;
  123. conf->getAllGoods(goodInfos);
  124. std::regex re(R"(\d+(\.\d{1,2})?)"); // 匹配整数或最多两位小数的数字
  125. for(const auto& goodInfo : goodInfos){
  126. std::smatch match;
  127. if (std::regex_search(goodInfo.cost, match, re)) {
  128. maxn = std::max(maxn, stof(match[0])); // 提取价格
  129. } else {
  130. log("IAPUserData::getUserBuyType : 转换失败");
  131. }
  132. }
  133. return maxn;
  134. }
  135. NS_IAP_END