Prechádzať zdrojové kódy

实现了多设备不同商店显示

MoYuWang 10 mesiacov pred
rodič
commit
f74d6f6bcc

+ 3 - 0
Classes/IAP/Conf/IAPConf.cpp

@@ -21,6 +21,9 @@ void IAPConf::initWith(redutils::BaseConf* bc) {
 }
 
 void IAPConf::getAllGoods(std::vector<GoodsInfo>& goods) {
+    // 清空
+    goods.clear();
+    // 获取
     int cnt = atoi(_bc->getConf("goods", "count").c_str());
     for (int i=1; i<=cnt; i++) {
         GoodsInfo gi;

+ 8 - 0
Classes/IAP/Ctl/IAPProcess.cpp

@@ -12,6 +12,9 @@
 #include "RUReboltLayer.h"
 #include "IAPUtils.h"
 
+#include "IAPUserData.hpp"
+#include <cocos2d.h>
+
 IAPProcess* IAPProcess::_instance = nullptr;
 
 IAPProcess* IAPProcess::getInstance() {
@@ -113,6 +116,11 @@ void IAPProcess::onProductPurchaseSuccess(const std::string& pid) {
     if (_cbIfSuccess) {
         _cbIfSuccess();
     }
+    // 记录用户购买信息
+    iap::IAPUserData::getInstance()->addBuyInfo(pid);
+    printf("购买%s成功\n", pid.c_str());
+    printf("用户类型为: %d\n", iap::IAPUserData::getInstance()->getUserBuyType());
+    
 //    // 判断当前是否在家装场景,如果在游戏场景,则等待
 //    if (bNeedAcceptNow) {
 //        // 像tilepass的内购,就不要有结算,结算是放在其活动本身的

+ 12 - 1
Classes/IAP/IAPCtlShop.cpp

@@ -32,7 +32,7 @@ void IAPCtlShop::init(std::string &cfgFN){
     
     _shopUI = IAPCtlShopUI::getInstance();
     _delegate = IAPDelegate::getInstance();
-
+    _level = 1;
 }
 
 void IAPCtlShop::setDelegate(IAPDelegate *delegate){
@@ -43,15 +43,26 @@ void IAPCtlShop::setDeviceLevel(int level){
     _level = level;
 }
 
+int IAPCtlShop::getDeviceLevel(){
+    return _level;
+}
+
+
 bool IAPCtlShop::addAPlacement(const IAPPlacement &plInfo){
     bool ans = false;
     
+    
+    if(ans)placementIDs.insert(plInfo.id);
     return ans;
 }
 
 void IAPCtlShop::removePlacement(const std::string &id){
     
+    placementIDs.erase(id);
+}
 
+size_t IAPCtlShop::getPlacementCount(){
+    return placementIDs.size();
 }
 
 void IAPCtlShop::showInNode(cocos2d::Node *pParent, ShopRequirement &requirement){

+ 7 - 0
Classes/IAP/IAPCtlShop.hpp

@@ -34,6 +34,9 @@ public:
     // 设置当前设备的等级
     // level 等级: 1 表示初级, 2 表示中级, 3 表示高级
     void setDeviceLevel(int level);
+    
+    // 获取当前设备等级
+    int getDeviceLevel();
 
     // 添加一个商店版位
     // plInfo 版位信息
@@ -43,6 +46,9 @@ public:
     // id 卡片id(添加版位时的id)
     void removePlacement(const std::string& id);
     
+    // 获取活动礼包总数
+    size_t getPlacementCount();
+    
     // 在某个节点中显示
     // pParent 父节点
     // requirement 对商店的要求,比如说最低金币数
@@ -60,6 +66,7 @@ private:
     IAPCtlShopUI* _shopUI;
     IAPDelegate* _delegate;
     
+    std::set<std::string> placementIDs;
     int _level;
 };
 

+ 1 - 3
Classes/IAP/Shop/IAPCtlShopItem.cpp

@@ -109,9 +109,7 @@ void IAPCtlShopItem::onNotifyDevelopment(const redutils::ReboltNotifyData& data)
 }
 
 void IAPCtlShopItem::accepted() {
-    // 退出购买界面,展示成功界面,记录购买信息
-    iap::IAPUserData::getInstance()->addBuyInfo(_gi.id);
-    auto type = iap::IAPUserData::getInstance()->getUserBuyType();
+    // 退出购买界面,展示成功界面
     IAPCtlShopUI::getInstance()->quit();
 }
 

+ 109 - 9
Classes/IAP/Shop/IAPCtlShopUI.cpp

@@ -10,6 +10,10 @@
 #include "RUUtils.h"
 #include "RUPlatform.h"
 
+#include "IAPCtlShop.hpp"
+#include "IAPUserData.hpp"
+#include <regex>
+
 IAPCtlShopUI* IAPCtlShopUI::_instance = nullptr;
 
 
@@ -98,7 +102,8 @@ bool IAPCtlShopUI::create(Node* pNode, IAPConf* cfg, iap::ShopRequirement& requi
     if (_goodsInfo.size() == 0) {
         IAPConf::getInstance()->getAllGoods(_goodsInfo);
     }
-    sortGoods(requirement);
+    // 过滤掉不符合的金币
+    filterGoods(requirement);
     
     // 构建 list 区域
     constructShopItem(false);
@@ -158,9 +163,71 @@ void IAPCtlShopUI::constructShopItem(bool bShowAll) {
         height += h;
     }
     
+    // 重新获取
+    if(bShowAll)IAPConf::getInstance()->getAllGoods(_goodsInfo);
+    // 根据用户类型进行礼包排序
+    switch (iap::IAPUserData::getInstance()->getUserBuyType()) {
+        case iap::UserBuyType::NoShopping:
+            sortGoods(true);
+            break;
+        case iap::UserBuyType::LittleShopping:
+            sortGoods(true);
+            break;
+        case iap::UserBuyType::NormalShopping:
+            sortGoods(true);
+            break;
+        case iap::UserBuyType::LotShopping:
+            sortGoods(false);
+            break;
+        default:
+            break;
+    }
     // 当前有效的区域
+    int bigPackCount,SmallPackCount; // 需要显示的礼包
+    auto iapCtlShop = iap::IAPCtlShop::createWith();
+    int leval = iapCtlShop->getDeviceLevel();
+    size_t placementCount = iapCtlShop->getPlacementCount();
+    switch(leval){
+        case 1:
+            // 低等级
+            bigPackCount = 2;
+            SmallPackCount = 1;
+            break;
+        case 2:
+            // 中等级
+            break;
+        case 3:
+            if(placementCount >= 2){
+                bigPackCount = 3;
+                SmallPackCount = 0;
+            }else{
+                bigPackCount = 2;
+                SmallPackCount = 2;
+            }
+            // 高等级
+            break;
+        default:
+            break;
+    }
+    
+    // 显示商品
     for (const auto& gi : _goodsInfo) {
-        if (bShowAll || gi.always) {
+        // 判断是否显示
+        bool isShow = false;
+        if (bShowAll) {
+            isShow = true;
+        }else{
+            if(gi.type == "panel" && bigPackCount > 0){
+                isShow = true;
+                bigPackCount--;
+            }
+            if(gi.type == "bar" && SmallPackCount > 0){
+                isShow = true;
+                SmallPackCount--;
+            }
+        }
+        // 商品显示
+        if (isShow) {
             bool bNeedAnim = height <= size.height;
             Node* nd = Node::create();
             nd->setCascadeOpacityEnabled(true);
@@ -184,7 +251,7 @@ void IAPCtlShopUI::constructShopItem(bool bShowAll) {
             }
         }
     }
-    
+    // 添加展示所有按钮
     if (!bShowAll) {
         // 加入show All btn, 这个是一定会有动画的
         auto sz = cocos2d::Size(584, 130);
@@ -246,7 +313,7 @@ void IAPCtlShopUI::constructShopItem(bool bShowAll) {
     }, 0.05, "SCH_Update_Cells");
 }
 
-void IAPCtlShopUI::sortGoods(iap::ShopRequirement &requirement){
+void IAPCtlShopUI::filterGoods(iap::ShopRequirement &requirement){
     std::map<int, bool> m;
     int cnt = 0;
     for(const auto& goodsInfo : _goodsInfo){
@@ -276,14 +343,47 @@ void IAPCtlShopUI::sortGoods(iap::ShopRequirement &requirement){
         }
     }
     // 添加不符合条件的礼包(不清楚是不是需要显示,暂放到列表末尾)
-    for(int i = 0;i < _goodsInfo.size(); i++){
-        if(!m[i]){
-            newGoodsInfo.push_back(_goodsInfo[i]);
-        }
-    }
+//    for(int i = 0;i < _goodsInfo.size(); i++){
+//        if(!m[i]){
+//            newGoodsInfo.push_back(_goodsInfo[i]);
+//        }
+//    }
     _goodsInfo = newGoodsInfo;
 }
 
+void IAPCtlShopUI::sortGoods(bool flag){
+    
+    sort(_goodsInfo.begin(), _goodsInfo.end(), [flag](GoodsInfo a, GoodsInfo b){
+        if(a.type != b.type){
+            if(a.type == "panel"){
+                return true;
+            }else if(b.type == "panel"){
+                return false;
+            }
+        }
+        float val_a,val_b;
+        
+        std::regex re(R"(\d+(\.\d{1,2})?)");  // 匹配整数或最多两位小数的数字
+        std::smatch match;
+        
+        if (std::regex_search(a.cost, match, re)) {
+            val_a = stof(match[0]);    // 提取价格
+        } else {
+            log("IAPUserData::getUserBuyType : 转换失败");
+        }
+        
+        if (std::regex_search(b.cost, match, re)) {
+            val_b = stof(match[0]);    // 提取价格
+        } else {
+            log("IAPUserData::getUserBuyType : 转换失败");
+        }
+        
+        if(flag) return val_a < val_b;
+        else return val_a > val_b;
+    });
+    
+}
+
 void IAPCtlShopUI::quit() {
     if (_layer) {
         _layer->runBehaviacWhitFunName("退出");

+ 4 - 2
Classes/IAP/Shop/IAPCtlShopUI.hpp

@@ -46,8 +46,10 @@ private:
     
     void constructShopItem(bool bShowAll = false);
     
-    // 根据所需要的金币进行筛选
-    void sortGoods(iap::ShopRequirement& requirement);
+    // 根据所需要的金币进行排序筛选
+    void filterGoods(iap::ShopRequirement& requirement);
+    
+    void sortGoods(bool flag);
 
 private:
     static IAPCtlShopUI* _instance;

+ 29 - 5
Classes/IAP/User/IAPUserData.cpp

@@ -25,6 +25,8 @@ IAPUserData* IAPUserData::getInstance(){
 
 void IAPUserData::init(){
     
+    
+    
 }
 
 void IAPUserData::addBuyInfo(std::string commodityID){
@@ -41,19 +43,19 @@ UserBuyType IAPUserData::getUserBuyType(){
     
     struct SortVec{
         std::string id;
-        float price;
+        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 == "special" ? true : false;
+        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 = std::stof(match[0]);    // 提取价格
+            sv.price = match[0];    // 提取价格
             sv.isSpecial = isSpecial;
             vec.push_back(sv);
         } else {
@@ -62,7 +64,7 @@ UserBuyType IAPUserData::getUserBuyType(){
     }
     // 按照价格排序 , 价格相同按照是否为special商品排序
     sort(vec.begin(), vec.end(), [](SortVec a,SortVec b){
-        if(std::fabs(a.price - b.price) < 1e-9) return a.price < b.price;
+        if(a.price != b.price) return stof(a.price) < stof(b.price);
         if(a.isSpecial)return true;
         if(b.isSpecial)return false;
         return true;
@@ -70,12 +72,13 @@ UserBuyType IAPUserData::getUserBuyType(){
     
     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(i == vec.size() - 1) isBuyExpensive = true;
+            if(stof(vec[i].price) == maxAmount) isBuyExpensive = true;
         }
         
     }
@@ -88,4 +91,25 @@ UserBuyType IAPUserData::getUserBuyType(){
     return UserBuyType::NormalShopping;
 }
 
+float IAPUserData::getMaxAmount(){
+    float maxn = 0.0;
+    
+    auto conf = IAPConf::getInstance();
+    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 : 转换失败");
+        }
+    }
+    
+    return maxn;
+}
+
+
 NS_IAP_END

+ 1 - 0
Classes/IAP/User/IAPUserData.hpp

@@ -35,6 +35,7 @@ public:
 
 private:
     
+    float getMaxAmount();
     
 private:
     static IAPUserData* _instance;

+ 6 - 5
Classes/TestScene.cpp

@@ -70,11 +70,11 @@ void TestScene::onNotifyDevelopment(const redutils::ReboltNotifyData& data){
         
         _iapShop->showInNode(this, shopRequirement);
     }else if(data.notify == "点击设备1"){
-        log("点击设备等级1按钮");
+        _iapShop->setDeviceLevel(1);
+        log("设置为低等级设备");
     }else if(data.notify == "点击设备2"){
-        log("点击设备等级2按钮");
-    }else if(data.notify == "点击设备3"){
-        log("点击设备等级3按钮");
+        _iapShop->setDeviceLevel(3);
+        log("设置为高等级设备");
     }else if(data.notify == "点击失败"){
         log("点击失败时金币不够按钮");
         
@@ -83,7 +83,8 @@ void TestScene::onNotifyDevelopment(const redutils::ReboltNotifyData& data){
         
         _iapShop->showInNode(this, shopRequirement);
     }else if(data.notify == "点击重置"){
-        log("重置");
+        _iapShop->setDeviceLevel(1);
+        log("重置为低等级设备");
     }
 }
 

+ 13 - 230
Resources/resources/ccb/lyMap.red

@@ -176,7 +176,7 @@
                             <key>value</key>
                             <array>
                                 <real>50.0000000</real>
-                                <real>80.0000000</real>
+                                <real>50.3345184</real>
                                 <integer>0</integer>
                                 <integer>2</integer>
                                 <integer>2</integer>
@@ -352,7 +352,7 @@
                                             <key>type</key>
                                             <string>Text</string>
                                             <key>value</key>
-                                            <string>设备等级1</string>
+                                            <string>低等级设备</string>
                                         </dict>
                                         <dict>
                                             <key>name</key>
@@ -364,7 +364,7 @@
                                                 <false/>
                                                 <false/>
                                                 <string></string>
-                                                <string>设备等级1</string>
+                                                <string>低等级设备</string>
                                             </array>
                                         </dict>
                                     </array>
@@ -399,7 +399,7 @@
                                     <string>Position</string>
                                     <key>value</key>
                                     <array>
-                                        <real>-150.0000000</real>
+                                        <real>-124.0000305</real>
                                         <real>0.0000000</real>
                                         <integer>0</integer>
                                         <integer>0</integer>
@@ -569,7 +569,7 @@
                                             <key>type</key>
                                             <string>Text</string>
                                             <key>value</key>
-                                            <string>设备等级2</string>
+                                            <string>高等级设备</string>
                                         </dict>
                                         <dict>
                                             <key>name</key>
@@ -581,7 +581,7 @@
                                                 <false/>
                                                 <false/>
                                                 <string></string>
-                                                <string>设备等级2</string>
+                                                <string>高等级设备</string>
                                             </array>
                                         </dict>
                                     </array>
@@ -616,8 +616,8 @@
                                     <string>Position</string>
                                     <key>value</key>
                                     <array>
-                                        <integer>0</integer>
-                                        <integer>0</integer>
+                                        <real>164.9998779</real>
+                                        <real>0.0000000</real>
                                         <integer>0</integer>
                                         <integer>0</integer>
                                         <integer>0</integer>
@@ -680,223 +680,6 @@
                             <key>uniqueNodeId</key>
                             <integer>234332132</integer>
                         </dict>
-                        <dict>
-                            <key>animatedProperties</key>
-                            <dict/>
-                            <key>baseClass</key>
-                            <string>REDNodeButton</string>
-                            <key>children</key>
-                            <array>
-                                <dict>
-                                    <key>animatedProperties</key>
-                                    <dict/>
-                                    <key>baseClass</key>
-                                    <string>CCLabelTTF</string>
-                                    <key>children</key>
-                                    <array/>
-                                    <key>customClass</key>
-                                    <string></string>
-                                    <key>customProperties</key>
-                                    <array/>
-                                    <key>displayName</key>
-                                    <string>CCLabelTTF</string>
-                                    <key>expand</key>
-                                    <false/>
-                                    <key>hidden</key>
-                                    <false/>
-                                    <key>locked</key>
-                                    <false/>
-                                    <key>memberVarAssignmentName</key>
-                                    <string></string>
-                                    <key>memberVarAssignmentType</key>
-                                    <integer>0</integer>
-                                    <key>properties</key>
-                                    <array>
-                                        <dict>
-                                            <key>name</key>
-                                            <string>position</string>
-                                            <key>type</key>
-                                            <string>Position</string>
-                                            <key>value</key>
-                                            <array>
-                                                <real>50.0000000</real>
-                                                <real>50.0000000</real>
-                                                <integer>0</integer>
-                                                <integer>2</integer>
-                                                <integer>2</integer>
-                                            </array>
-                                        </dict>
-                                        <dict>
-                                            <key>name</key>
-                                            <string>opacity</string>
-                                            <key>type</key>
-                                            <string>Byte</string>
-                                            <key>value</key>
-                                            <integer>255</integer>
-                                        </dict>
-                                        <dict>
-                                            <key>name</key>
-                                            <string>color</string>
-                                            <key>type</key>
-                                            <string>Color3</string>
-                                            <key>value</key>
-                                            <array>
-                                                <integer>255</integer>
-                                                <integer>255</integer>
-                                                <integer>255</integer>
-                                            </array>
-                                        </dict>
-                                        <dict>
-                                            <key>name</key>
-                                            <string>fontName</string>
-                                            <key>type</key>
-                                            <string>FontTTF</string>
-                                            <key>value</key>
-                                            <string>Helvetica</string>
-                                        </dict>
-                                        <dict>
-                                            <key>name</key>
-                                            <string>fontSize</string>
-                                            <key>type</key>
-                                            <string>FloatScale</string>
-                                            <key>value</key>
-                                            <array>
-                                                <real>24.0000000</real>
-                                                <integer>0</integer>
-                                            </array>
-                                        </dict>
-                                        <dict>
-                                            <key>name</key>
-                                            <string>dimensions</string>
-                                            <key>type</key>
-                                            <string>Size</string>
-                                            <key>value</key>
-                                            <array>
-                                                <integer>0</integer>
-                                                <integer>0</integer>
-                                                <integer>0</integer>
-                                                <integer>0</integer>
-                                                <false/>
-                                                <false/>
-                                            </array>
-                                        </dict>
-                                        <dict>
-                                            <key>name</key>
-                                            <string>string</string>
-                                            <key>type</key>
-                                            <string>Text</string>
-                                            <key>value</key>
-                                            <string>设备等级3</string>
-                                        </dict>
-                                        <dict>
-                                            <key>name</key>
-                                            <string>localization</string>
-                                            <key>type</key>
-                                            <string>Localization</string>
-                                            <key>value</key>
-                                            <array>
-                                                <false/>
-                                                <false/>
-                                                <string></string>
-                                                <string>设备等级3</string>
-                                            </array>
-                                        </dict>
-                                    </array>
-                                    <key>seqExpanded</key>
-                                    <false/>
-                                    <key>uniqueNodeId</key>
-                                    <integer>434754243</integer>
-                                </dict>
-                            </array>
-                            <key>customClass</key>
-                            <string></string>
-                            <key>customProperties</key>
-                            <array/>
-                            <key>displayName</key>
-                            <string>设备3</string>
-                            <key>expand</key>
-                            <true/>
-                            <key>hidden</key>
-                            <false/>
-                            <key>locked</key>
-                            <false/>
-                            <key>memberVarAssignmentName</key>
-                            <string></string>
-                            <key>memberVarAssignmentType</key>
-                            <integer>0</integer>
-                            <key>properties</key>
-                            <array>
-                                <dict>
-                                    <key>name</key>
-                                    <string>position</string>
-                                    <key>type</key>
-                                    <string>Position</string>
-                                    <key>value</key>
-                                    <array>
-                                        <real>150.0000000</real>
-                                        <real>0.0000000</real>
-                                        <integer>0</integer>
-                                        <integer>0</integer>
-                                        <integer>0</integer>
-                                    </array>
-                                </dict>
-                                <dict>
-                                    <key>name</key>
-                                    <string>opacity</string>
-                                    <key>type</key>
-                                    <string>Byte</string>
-                                    <key>value</key>
-                                    <integer>255</integer>
-                                </dict>
-                                <dict>
-                                    <key>name</key>
-                                    <string>color</string>
-                                    <key>type</key>
-                                    <string>Color3</string>
-                                    <key>value</key>
-                                    <array>
-                                        <integer>255</integer>
-                                        <integer>255</integer>
-                                        <integer>255</integer>
-                                    </array>
-                                </dict>
-                                <dict>
-                                    <key>name</key>
-                                    <string>ccControl</string>
-                                    <key>type</key>
-                                    <string>BlockCCControl</string>
-                                    <key>value</key>
-                                    <array>
-                                        <string></string>
-                                        <integer>0</integer>
-                                        <integer>32</integer>
-                                    </array>
-                                </dict>
-                                <dict>
-                                    <key>name</key>
-                                    <string>preferedSize</string>
-                                    <key>type</key>
-                                    <string>Size</string>
-                                    <key>value</key>
-                                    <array>
-                                        <integer>120</integer>
-                                        <integer>40</integer>
-                                        <integer>0</integer>
-                                        <integer>0</integer>
-                                        <false/>
-                                        <false/>
-                                    </array>
-                                </dict>
-                            </array>
-                            <key>reboltId</key>
-                            <string>dtNFL8iGlwHx</string>
-                            <key>reboltName</key>
-                            <string>设备3</string>
-                            <key>seqExpanded</key>
-                            <false/>
-                            <key>uniqueNodeId</key>
-                            <integer>780628770</integer>
-                        </dict>
                     </array>
                     <key>customClass</key>
                     <string></string>
@@ -1010,8 +793,8 @@
                                     <string>Position</string>
                                     <key>value</key>
                                     <array>
-                                        <real>50.0000000</real>
-                                        <real>50.0000000</real>
+                                        <real>47.6803589</real>
+                                        <real>37.4999619</real>
                                         <integer>0</integer>
                                         <integer>2</integer>
                                         <integer>2</integer>
@@ -1077,7 +860,7 @@
                                     <key>type</key>
                                     <string>Text</string>
                                     <key>value</key>
-                                    <string>失败时金币不够</string>
+                                    <string>失败时金币不够跳转商店</string>
                                 </dict>
                                 <dict>
                                     <key>name</key>
@@ -1089,7 +872,7 @@
                                         <false/>
                                         <false/>
                                         <string></string>
-                                        <string>失败时金币不够</string>
+                                        <string>失败时金币不够跳转商店</string>
                                     </array>
                                 </dict>
                             </array>
@@ -1125,7 +908,7 @@
                             <key>value</key>
                             <array>
                                 <real>50.0000000</real>
-                                <real>60.0000000</real>
+                                <real>42.7464828</real>
                                 <integer>0</integer>
                                 <integer>2</integer>
                                 <integer>2</integer>

BIN
Resources/resources/ccbi/lyMap.redream