|
@@ -16,6 +16,10 @@
|
|
|
#include "IAPCtlShopUI.hpp"
|
|
|
#include "IAPUserData.hpp"
|
|
|
|
|
|
+#include <algorithm>
|
|
|
+#include <string>
|
|
|
+#include <regex>
|
|
|
+
|
|
|
NS_IAP_BEGIN
|
|
|
|
|
|
IAPCtlShop* IAPCtlShop::_instance = nullptr;
|
|
@@ -78,8 +82,97 @@ void IAPCtlShop::showPlacementsInNode(cocos2d::Node *pParent, const vector<std::
|
|
|
|
|
|
}
|
|
|
|
|
|
+UserBuyType IAPCtlShop::getUserBuyType(){
|
|
|
+ // 如果没有购买记录
|
|
|
+ auto buyInfos = IAPUserData::getInstance()->getBuyInfos();
|
|
|
+ if(buyInfos.size() == 0) return UserBuyType::NoShopping;
|
|
|
+
|
|
|
+ auto conf = IAPConf::getInstance();
|
|
|
+ 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;
|
|
|
+ return true;
|
|
|
+ });
|
|
|
+
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ // 购买过最贵商品
|
|
|
+ if(isBuyExpensive)return UserBuyType::LotShopping;
|
|
|
+ // 仅购买Special商品
|
|
|
+ if(onlySpecial)return UserBuyType::LittleShopping;
|
|
|
+ // 正常购物
|
|
|
+ return UserBuyType::NormalShopping;
|
|
|
+}
|
|
|
+
|
|
|
+std::vector<std::string> IAPUserData::getBuyInfos(){
|
|
|
+ return _buyInfos;
|
|
|
+}
|
|
|
+
|
|
|
+void IAPCtlShop::addUserBuyInfo(std::string commodityID){
|
|
|
+ iap::IAPUserData::getInstance()->addBuyInfo(commodityID);
|
|
|
+}
|
|
|
+
|
|
|
void IAPCtlShop::clearUserBuyInfo(){
|
|
|
iap::IAPUserData::getInstance()->clearBuyInfo();
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+float IAPCtlShop::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
|