123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- //
- // IAPUserData.cpp
- // TileManor
- //
- // Created by 徐炼新 on 2024/9/30.
- //
- #include "IAPUserData.hpp"
- #include "IAPConf.hpp"
- #include <algorithm>
- #include <string>
- #include <regex>
- NS_IAP_BEGIN
- IAPUserData* IAPUserData::_instance = nullptr;
- IAPUserData* IAPUserData::getInstance(){
- if(!_instance){
- _instance = new IAPUserData();
- }
- return _instance;
- }
- void IAPUserData::init(){
-
-
-
- }
- void IAPUserData::addBuyInfo(std::string commodityID){
- buyInfos.push_back(commodityID);
- }
- UserBuyType IAPUserData::getUserBuyType(){
- // 如果没有购买记录
- 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;
- }
- void IAPUserData::clearBuyInfo(){
- buyInfos.clear();
- }
- 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
|