diff --git a/src/managers/BetterInfoCache.cpp b/src/managers/BetterInfoCache.cpp index 01e1e14..48f0821 100644 --- a/src/managers/BetterInfoCache.cpp +++ b/src/managers/BetterInfoCache.cpp @@ -16,8 +16,8 @@ bool BetterInfoCache::init(){ void BetterInfoCache::finishLoading(){ auto now = BetterInfo::timeInMs(); - checkClaimableLists(); - cacheRatedLists(); + //checkClaimableLists(); + //cacheRatedLists(); cacheFollowedCreators(); @@ -163,6 +163,46 @@ GJUserScore* BetterInfoCache::getCachedScore(int accountID) { return score; } +void BetterInfoCache::cacheRatedListsFromMegaResponse(const std::string& megaResponse) { + if(megaResponse.empty() || ServerUtils::isGDPS()) return cacheRatedLists(); + + auto GLM = GameLevelManager::sharedState(); + + size_t hashes = std::count(megaResponse.begin(), megaResponse.end(), '#'); + if(hashes < 3) return cacheRatedLists(); + + std::stringstream responseStream(megaResponse); + std::string levelData; + std::string userData; + + getline(responseStream, levelData, '#'); + getline(responseStream, userData, '#'); + + std::stringstream userStream(userData); + std::string currentUser; + while(getline(userStream, currentUser, '|')) { + auto info = utils::string::split(currentUser, ":"); + + int userID = std::stoi(info[0]); + int accountID = std::stoi(info[2]); + + if(userID > 0) GLM->storeUserName(userID, accountID, info[1]); + } + + std::stringstream levelStream(levelData); + std::string currentLevel; + while(getline(levelStream, currentLevel, '|')) { + auto level = GJLevelList::create(BetterInfo::responseToDict(currentLevel)); + GLM->updateSavedLevelList(level); + cacheListAsync(level); + m_updatedCachedLists.emplace(level->m_listID, level); + } + + log::debug("Cached rated lists from mega response"); + + checkClaimableLists(); +} + void BetterInfoCache::cacheRatedLists(int page) { auto searchObj = GJSearchObject::create(SearchType::Awarded); searchObj->m_searchMode = 1; @@ -183,6 +223,7 @@ void BetterInfoCache::cacheRatedLists(int page) { found = true; break; } + m_updatedCachedLists.emplace(list->m_listID, list); cacheList(list); } @@ -250,10 +291,16 @@ void BetterInfoCache::checkClaimableLists() { if(GameStatsManager::sharedState()->hasClaimedListReward(list)) continue; if(m_claimableLists.contains(listID)) continue; - m_claimableLists.emplace(listID,nullptr); + if(m_updatedCachedLists.contains(listID)) { + m_claimableLists.emplace(listID, m_updatedCachedLists[listID]); + m_updatedCachedLists.erase(listID); + } else { + m_claimableLists.emplace(listID, nullptr); + } log::info("Can claim reward for list {}", listID); } + m_updatedCachedLists.clear(); downloadClaimableLists(); }); }).detach(); @@ -328,7 +375,8 @@ void BetterInfoCache::downloadClaimableLists() { log::debug("Downloaded list {}", lists->at(0)->m_listID); }, false - );log::debug("Downloading list {}", listID); + ); + log::debug("Downloading list {}", listID); }); using namespace std::chrono_literals; diff --git a/src/managers/BetterInfoCache.h b/src/managers/BetterInfoCache.h index 55d05d0..a040756 100644 --- a/src/managers/BetterInfoCache.h +++ b/src/managers/BetterInfoCache.h @@ -7,6 +7,7 @@ class BI_DLL BetterInfoCache : public BaseJsonManager { std::unordered_set m_attemptedUsernames; std::unordered_set m_attemptedLevelDates; + std::unordered_map> m_updatedCachedLists; std::unordered_map> m_claimableLists; std::unordered_map m_coinCounts; std::unordered_map m_vaultCodes; @@ -38,6 +39,7 @@ class BI_DLL BetterInfoCache : public BaseJsonManager { GJUserScore* getCachedOrPlaceholderScore(int accountID); GJUserScore* getCachedScore(int accountID); + void cacheRatedListsFromMegaResponse(const std::string& megaResponse); void cacheRatedLists(int page = 0); void cacheList(GJLevelList* list); void cacheListAsync(GJLevelList* list); diff --git a/src/utils.cpp b/src/utils.cpp index f3cd95b..1792628 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -653,8 +653,11 @@ void BetterInfo::loadImportantNotices(Ref layer) { s_requestTask = web::WebRequest().get(url).map( [layer](web::WebResponse* response) { + auto biCache = BetterInfoCache::sharedState(); + if(!response->ok() || response->json().isErr()) { log::warn("Fetching important notices failed: {} - {}", response->code(), response->string().unwrapOr("No response")); + biCache->cacheRatedLists(); return *response; } @@ -665,22 +668,25 @@ void BetterInfo::loadImportantNotices(Ref layer) { alert->show(); } - auto biCache = BetterInfoCache::sharedState(); - for(auto& value : info["additional"]["vault5"]) { if(!value.isString()) continue; biCache->cacheVaultCode(value.getKey().value_or(""), value.asString().unwrap()); } + if(auto res = info["additional"]["rated_lists"]["content"].asString()) { + log::debug("Attempting to cache rated lists from megaresponse"); + biCache->cacheRatedListsFromMegaResponse(res.unwrap()); + } else { + log::debug("Attempting to cache rated lists from server"); + biCache->cacheRatedLists(); + } + return *response; }); } FLAlertLayer* BetterInfo::createUpdateDialog() { - //TODO: enable before real 420 release - //return nullptr; - auto versionResult = VersionInfo::parse( Mod::get()->getSavedValue( "last_dialog_version",