Skip to content

Commit

Permalink
load lists from bi server instead if possible
Browse files Browse the repository at this point in the history
  • Loading branch information
Cvolton committed Nov 17, 2024
1 parent 49608d8 commit 3c7650e
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 9 deletions.
56 changes: 52 additions & 4 deletions src/managers/BetterInfoCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ bool BetterInfoCache::init(){
void BetterInfoCache::finishLoading(){
auto now = BetterInfo::timeInMs();

checkClaimableLists();
cacheRatedLists();
//checkClaimableLists();
//cacheRatedLists();

cacheFollowedCreators();

Expand Down Expand Up @@ -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;
Expand All @@ -183,6 +223,7 @@ void BetterInfoCache::cacheRatedLists(int page) {
found = true;
break;
}
m_updatedCachedLists.emplace(list->m_listID, list);
cacheList(list);
}

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions src/managers/BetterInfoCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
class BI_DLL BetterInfoCache : public BaseJsonManager {
std::unordered_set<int> m_attemptedUsernames;
std::unordered_set<int> m_attemptedLevelDates;
std::unordered_map<int, Ref<GJLevelList>> m_updatedCachedLists;
std::unordered_map<int, Ref<GJLevelList>> m_claimableLists;
std::unordered_map<int, int> m_coinCounts;
std::unordered_map<std::string, std::string> m_vaultCodes;
Expand Down Expand Up @@ -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);
Expand Down
16 changes: 11 additions & 5 deletions src/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -653,8 +653,11 @@ void BetterInfo::loadImportantNotices(Ref<CCLayer> 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;
}

Expand All @@ -665,22 +668,25 @@ void BetterInfo::loadImportantNotices(Ref<CCLayer> 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<std::string>(
"last_dialog_version",
Expand Down

0 comments on commit 3c7650e

Please sign in to comment.