12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- //
- // 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;
- float 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;
- std::smatch match;
- if (std::regex_search(goodInfo.cost, match, re)) {
- SortVec sv;
- sv.id = goodInfo.id;
- sv.price = std::stof(match[0]); // 提取价格
- sv.isSpecial = isSpecial;
- vec.push_back(sv);
- } else {
- log("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.isSpecial)return true;
- if(b.isSpecial)return false;
- return true;
- });
-
- bool onlySpecial = true;
- bool isBuyExpensive = false;
- 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(isBuyExpensive)return UserBuyType::LotShopping;
- // 仅购买Special商品
- if(onlySpecial)return UserBuyType::LittleShopping;
- // 正常购物
- return UserBuyType::NormalShopping;
- }
- NS_IAP_END
|