Skip to content

Commit

Permalink
remember which levels failed to download
Browse files Browse the repository at this point in the history
  • Loading branch information
Cvolton committed Nov 13, 2024
1 parent da5821c commit 7e55c43
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 11 deletions.
2 changes: 1 addition & 1 deletion mod.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"geode": "4.0.0",
"geode": "4.0.0-alpha.1",
"version": "v4.2.13",
"gd": {
"win": "2.2074",
Expand Down
2 changes: 1 addition & 1 deletion src/layers/LevelFiltering/LevelSearchViewLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ void LevelSearchViewLayer::startLoading(){
void LevelSearchViewLayer::queueLoad(float dt) {
this->retain();
std::string key = m_gjSearchObjLoaded->getKey();
ServerUtils::getOnlineLevels(m_gjSearchObjLoaded, [this, key](auto levels, bool success) {
ServerUtils::getOnlineLevels(m_gjSearchObjLoaded, [this, key](auto levels, bool success, bool explicitError) {
if(success) {
auto array = CCArray::create();
for(auto level : *levels) {
Expand Down
38 changes: 34 additions & 4 deletions src/managers/BetterInfoCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ void BetterInfoCache::validateLoadedData() {
validateIsObject("level-info-dict");
validateIsObject("list-info-dict");
validateIsObject("user-info-dict");
validateIsObject("failed-levels-dict");
}

void BetterInfoCache::establishCachedDicts(){
Expand Down Expand Up @@ -410,6 +411,7 @@ void BetterInfoCache::checkLevelsFromDict(CCDictionary* dict) {
auto levelFromSaved = LevelUtils::getLevelFromSaved(currentLvl->m_levelID);
if(levelFromSaved != nullptr && std::string(levelFromSaved->m_levelName) != "") cacheLevel(levelFromSaved);
else {
if(isLevelFailed(currentLvl->m_levelID)) continue;
if(levelFromSaved && levelFromSaved->m_stars > 0) toDownloadRated.insert(currentLvl->m_levelID);
else toDownloadUnrated.insert(currentLvl->m_levelID);
}
Expand All @@ -426,6 +428,23 @@ void BetterInfoCache::checkLevelsFromDict(CCDictionary* dict) {
}).detach();
}

void BetterInfoCache::markLevelAsFailed(int id) {
auto idString = std::to_string(id);

//std::cout << ("Locking unique_lock cacheLevel") << std::endl;
std::unique_lock guard(m_jsonMutex);
m_json["failed-levels-dict"][idString] = m_json["failed-levels-dict"][idString].asInt().unwrapOr(0) + 1;
//std::cout << ("Unlocking unique_lock cacheLevel") << std::endl;
}

bool BetterInfoCache::isLevelFailed(int id) {
auto idString = std::to_string(id);
if(!objectExists("failed-levels-dict", idString)) return false;

std::shared_lock guard(m_jsonMutex);
return m_json["failed-levels-dict"][idString].asInt().unwrapOr(0) >= 5;
}

void BetterInfoCache::cacheLevel(GJGameLevel* level) {
auto idString = std::to_string(level->m_levelID);

Expand Down Expand Up @@ -481,17 +500,28 @@ void BetterInfoCache::cacheLevels(std::set<int> toDownload, SearchType searchTyp

using namespace std::chrono_literals;
for(const auto& searchObj : *searchObjects) {
auto levelsVector = utils::string::split(searchObj->m_searchQuery, ",");
std::unordered_set<int> levelsSet;

for(auto level : levelsVector) {
levelsSet.insert(BetterInfo::stoi(level));
}

ServerUtils::getOnlineLevels(
searchObj,
[this](auto levels, bool) {
std::thread([this, levels] {
[this, levelsSet = std::move(levelsSet)](auto levels, bool success, bool explicitError) mutable {
std::thread([this, levels, levelsSet = std::move(levelsSet), success, explicitError] mutable {
thread::setName("Level Cache (Inner)");
for(const auto& level : *levels) {
cacheLevel(level);
levelsSet.erase(level->m_levelID.value());
}
if(success || explicitError) for(const auto& id : levelsSet) {
markLevelAsFailed(id);
}
doSave();
Loader::get()->queueInMainThread([levels] {
if(levels->size() > 0) log::debug("Cached {} levels", levels->size());
Loader::get()->queueInMainThread([levels, failed = levelsSet.size()] {
if(levels->size() > 0) log::debug("Cached {} levels; Failed to cache {}", levels->size(), failed);
// this is just a workaround to make sure the vector only gets destroyed in main thread
});
}).detach();
Expand Down
2 changes: 2 additions & 0 deletions src/managers/BetterInfoCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ class BI_DLL BetterInfoCache : public BaseJsonManager {
void removeClaimedLists();

void checkLevelsFromDict(CCDictionary* dict);
void markLevelAsFailed(int id);
bool isLevelFailed(int id);
void cacheLevel(GJGameLevel* level);
void cacheLevels(std::set<int> toDownload, SearchType searchType, int levelsPerRequest);

Expand Down
13 changes: 9 additions & 4 deletions src/utils/ServerUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ void ServerUtils::getLevelLists(GJSearchObject* searchObject, std::function<void
s_requests.emplace(requestKey, task);
}

void ServerUtils::getOnlineLevels(GJSearchObject* searchObject, std::function<void(std::shared_ptr<std::vector<Ref<GJGameLevel>>>, bool)> callback, bool cacheLevels) {
void ServerUtils::getOnlineLevels(GJSearchObject* searchObject, std::function<void(std::shared_ptr<std::vector<Ref<GJGameLevel>>>, bool success, bool explicitError)> callback, bool cacheLevels) {
std::string completedLevels = "";

std::string postString = fmt::format("{}&type={}&str={}&diff={}&len={}&page={}&total={}&uncompleted={}&onlyCompleted={}&featured={}&original={}&twoPlayer={}&coins={}",
Expand Down Expand Up @@ -251,7 +251,7 @@ void ServerUtils::getOnlineLevels(GJSearchObject* searchObject, std::function<vo

auto levels = std::make_shared<std::vector<Ref<GJGameLevel>>>();

callback(levels, false);
callback(levels, false, false);

return *response;
}
Expand All @@ -260,9 +260,14 @@ void ServerUtils::getOnlineLevels(GJSearchObject* searchObject, std::function<vo

auto responseString = response->string().unwrapOr("");

if(responseString == "-1") {
callback(levels, false, true);
return *response;
}

size_t hashes = std::count(responseString.begin(), responseString.end(), '#');
if(hashes < 4) {
callback(levels, false);
callback(levels, false, false);
return *response;
}

Expand Down Expand Up @@ -306,7 +311,7 @@ void ServerUtils::getOnlineLevels(GJSearchObject* searchObject, std::function<vo
else s_cache[key] = levelArray;
}

callback(levels, true);
callback(levels, true, false);

return *response;
});
Expand Down
2 changes: 1 addition & 1 deletion src/utils/ServerUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace ServerUtils {
BI_DLL std::string getBasePostString(bool includeAccount = true);
BI_DLL std::string getSearchObjectKey(GJSearchObject* searchObject);
BI_DLL void getLevelLists(GJSearchObject* searchObject, std::function<void(std::shared_ptr<std::vector<Ref<GJLevelList>>>, bool)> callback, bool cacheLevels = true);
BI_DLL void getOnlineLevels(GJSearchObject* searchObject, std::function<void(std::shared_ptr<std::vector<Ref<GJGameLevel>>>, bool)> callback, bool cacheLevels = true);
BI_DLL void getOnlineLevels(GJSearchObject* searchObject, std::function<void(std::shared_ptr<std::vector<Ref<GJGameLevel>>>, bool success, bool explicitError)> callback, bool cacheLevels = true);
BI_DLL cocos2d::CCArray* getStoredOnlineLevels(const std::string& key);
BI_DLL void resetCache();
BI_DLL bool showCFError(const std::string& data);
Expand Down

0 comments on commit 7e55c43

Please sign in to comment.