Browse Source

优化了一些设计

MoYuWang 9 months ago
parent
commit
7e48a2553c
3 changed files with 33 additions and 48 deletions
  1. 17 0
      Classes/IAP/Conf/IAPConf.hpp
  2. 15 47
      Classes/IAP/IAPCtlShop.cpp
  3. 1 1
      Classes/IAP/IAPCtlShop.hpp

+ 17 - 0
Classes/IAP/Conf/IAPConf.hpp

@@ -12,6 +12,9 @@
 #include "RUBaseConf.h"
 #include "RUUtils.h"
 
+#include <algorithm>
+#include <regex>
+
 USING_NS_CC;
 
 typedef struct {
@@ -32,6 +35,20 @@ typedef struct {
     bool always;
     int idxGT2;
     std::vector<AreaInGooods> areas;
+    
+    // 获取价格(float)类型
+    float getCostNumber() const{
+        if(cost == "")return 0.0;
+        
+        std::regex re(R"(\d+(\.\d{1,2})?)");  // 匹配整数或最多两位小数的数字
+        std::smatch match;
+        if (std::regex_search(cost, match, re)) {
+            return stof(match[0]);    // 提取价格
+        } else {
+            log("IAPUserData::getUserBuyType : 转换失败");
+            return 0.0;
+        }
+    }
 
     std::string serialize() const {
         std::string ret;

+ 15 - 47
Classes/IAP/IAPCtlShop.cpp

@@ -16,10 +16,6 @@
 #include "IAPCtlShopUI.hpp"
 #include "IAPUserData.hpp"
 
-#include <algorithm>
-#include <string>
-#include <regex>
-
 NS_IAP_BEGIN
 
 IAPCtlShop* IAPCtlShop::_instance = nullptr;
@@ -60,22 +56,21 @@ bool IAPCtlShop::addAPlacement(const IAPPlacement &plInfo){
     bool ans = false;
     
     
-    if(ans)placementIDs.insert(plInfo.id);
+    if(ans)_placements[plInfo.id] = plInfo;
     return ans;
 }
 
 void IAPCtlShop::removePlacement(const std::string &id){
-    
-    placementIDs.erase(id);
+    _placements.erase(id);
 }
 
 size_t IAPCtlShop::getPlacementCount(){
-    return placementIDs.size();
+    return _placements.size();
 }
 
 void IAPCtlShop::showInNode(cocos2d::Node *pParent, ShopRequirement &requirement){
     _shopUI = IAPCtlShopUI::getInstance();
-    _shopUI->create(pParent, _conf, requirement,1);
+    _shopUI->create(pParent, _conf, requirement);
 }
 
 void IAPCtlShop::showPlacementsInNode(cocos2d::Node *pParent, const vector<std::string> &plIds){
@@ -91,44 +86,23 @@ UserBuyType IAPCtlShop::getUserBuyType(){
     vector<GoodsInfo> goodInfos;
     conf->getAllGoods(goodInfos);
     
-    struct SortVec{
-        std::string id;
-        std::string price;
-        bool isSpecial;
-    };
-    vector<SortVec> vec;
-    
-    std::regex re(R"(\d+(\.\d{1,2})?)");  // 匹配整数或最多两位小数的数字
-    for(const auto& goodInfo : goodInfos){
-        bool isSpecial = goodInfo.style == "mostValued" ? true : false;
-        std::smatch match;
-        if (std::regex_search(goodInfo.cost, match, re)) {
-            SortVec sv;
-            sv.id = goodInfo.id;
-            sv.price = match[0];    // 提取价格
-            sv.isSpecial = isSpecial;
-            vec.push_back(sv);
-        } else {
-            log("IAPUserData::getUserBuyType : 转换失败");
-        }
-    }
     // 按照价格排序 , 价格相同按照是否为special商品排序
-    sort(vec.begin(), vec.end(), [](SortVec a,SortVec b){
-        if(a.price != b.price) return stof(a.price) < stof(b.price);
-        if(a.isSpecial)return true;
-        if(b.isSpecial)return false;
+    sort(goodInfos.begin(), goodInfos.end(), [](GoodsInfo a,GoodsInfo b){
+        if(a.getCostNumber() != b.getCostNumber()) return a.getCostNumber() < b.getCostNumber();
+        if(a.style == "mostValued")return true;
+        if(b.style == "mostValued")return false;
         return true;
     });
     
-    bool onlySpecial = true;
-    bool isBuyExpensive = false;
+    bool onlySpecial = true;    // 是否仅购买特惠商品
+    bool isBuyExpensive = false;    // 是否购买过最贵商品
     float maxAmount = getMaxAmount();
     for(const auto& buyInfo : buyInfos){
         
-        for(size_t i = 0; i < vec.size(); i++){
-            if(buyInfo != vec[i].id)continue;
-            if(!vec[i].isSpecial) onlySpecial = false;
-            if(stof(vec[i].price) == maxAmount) isBuyExpensive = true;
+        for(size_t i = 0; i < goodInfos.size(); i++){
+            if(buyInfo != goodInfos[i].id)continue;
+            if(goodInfos[i].type != "mostValued") onlySpecial = false;
+            if(goodInfos[i].getCostNumber() == maxAmount) isBuyExpensive = true;
         }
         
     }
@@ -161,14 +135,8 @@ float IAPCtlShop::getMaxAmount(){
     vector<GoodsInfo> goodInfos;
     conf->getAllGoods(goodInfos);
     
-    std::regex re(R"(\d+(\.\d{1,2})?)");  // 匹配整数或最多两位小数的数字
     for(const auto& goodInfo : goodInfos){
-        std::smatch match;
-        if (std::regex_search(goodInfo.cost, match, re)) {
-            maxn = std::max(maxn, stof(match[0]));    // 提取价格
-        } else {
-            log("IAPUserData::getUserBuyType : 转换失败");
-        }
+        maxn = std::max(maxn, goodInfo.getCostNumber());
     }
     
     return maxn;

+ 1 - 1
Classes/IAP/IAPCtlShop.hpp

@@ -88,7 +88,7 @@ private:
     IAPCtlShopUI* _shopUI;
     IAPDelegate* _delegate;
     
-    std::set<std::string> placementIDs;
+    std::map<std::string, IAPPlacement> _placements;
     int _level;
 };