123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- //
- // IAPConf.cpp
- // TileManor
- //
- // Created by 徐炼新 on 2024/1/16.
- //
- #include "IAPConf.hpp"
- IAPConf* IAPConf::_instance = nullptr;
- IAPConf* IAPConf::getInstance() {
- if (_instance == nullptr) {
- _instance = new IAPConf();
- }
- return _instance;
- }
- void IAPConf::initWith(redutils::BaseConf* bc) {
- _bc = 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;
- std::string key = "goods_" + Value(i).asString();
- gi.id = _bc->getConf("goods", key+":id");
- gi.type = _bc->getConf("goods", key+":type");
- gi.style = _bc->getConf("goods", key+":style");
- gi.slogon = _bc->getConf("goods", key+":slogon");
- gi.hint = _bc->getConf("goods", key+":hint");
- gi.cost = _bc->getConf("goods", key+":cost");
- gi.always = _bc->getConf("goods", key+":always") == "1";
- gi.idxGT2 = Value(_bc->getConf("goods", key+":idxGT2")).asInt();
-
- {
- const auto& szStr = _bc->getConf("goods", key+":size");
- int nPos = szStr.find(",");
- gi.sz.width = atoi(szStr.substr(0, nPos).c_str());
- gi.sz.height = atoi(szStr.substr(nPos+1).c_str());
- }
-
- // areas
- int areaCnt = atoi(_bc->getConf("goods", key+":areas:count").c_str());
- for (int j=1; j<=areaCnt; j++) {
- auto& rewards = _bc->getConf("goods", key+":areas:area_"+Value(j).asString());
- // 解析得到各个奖励信息
- auto parseItem = [](std::string str, AreaInGooods& aig) {
- int nPos = str.find(":");
- std::string name = str.substr(0, nPos);
- std::string cnt = str.substr(nPos+1);
- aig.push_back({name, cnt});
- };
-
- AreaInGooods aig;
- int startPos = 0;
- int nPos = rewards.find(",");
- string conf = rewards.substr(startPos, nPos);
- while (nPos != string::npos) {
- parseItem(conf, aig);
- startPos = nPos + 1;
- nPos = rewards.find(",", startPos);
- conf = rewards.substr(startPos, nPos-startPos);
- }
- parseItem(conf, aig);
-
- gi.areas.push_back(aig);
- }
- goods.push_back(gi);
- }
- }
- float IAPConf::getMaxAmount(){
- if(_bc == nullptr) return 0;
- float maxn = 0.0;
-
- vector<GoodsInfo> goodInfos;
- getAllGoods(goodInfos);
-
- for(const auto& goodInfo : goodInfos){
- maxn = std::max(maxn, goodInfo.getCostNumber());
- }
-
- return maxn;
- }
- float IAPConf::getCoinNum(std::string id){
- if(_bc == nullptr) return -1;
- float num = -1;
- int cnt = atoi(_bc->getConf("goods", "count").c_str());
- for (int i=1; i<=cnt; i++) {
- GoodsInfo gi;
- std::string key = "goods_" + Value(i).asString();
- gi.id = _bc->getConf("goods", key+":id");
- if(gi.id == id){
- gi.cost = _bc->getConf("goods", key+":cost");
- num = gi.getCostNumber();
- break;
- }
- }
- return num;
- }
|