Skip to content

Commit

Permalink
Merge #32 v1.1.5
Browse files Browse the repository at this point in the history
# v1.1.5
- Fixed a bug that would cause the game to crash if the level cache was for some reason not a valid json
- The "couldn't cache" popup shouldn't pop up every single time when there's no connection anymore on the title screen
- Fixed a bug which caused the weekly full cache refresh to not work due to overwriting the initial cache time value
  • Loading branch information
B1rtek authored Mar 23, 2024
2 parents 945aa20 + 40c74ae commit 6bea034
Show file tree
Hide file tree
Showing 11 changed files with 104 additions and 39 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ If you encounter an <cy>issue</c> with the mod, [open an issue in the mod's GitH

<cb>*Code contributions:*</c> <cy>**[Diversion](https://github.com/B1rtek/Geode-GDDLIntegration/pull/3)**</c>
<cg>*Feature suggestions:*</c> <cy>[MasterGamerY](https://github.com/B1rtek/Geode-GDDLIntegration/issues/1), [Weebifying](https://github.com/B1rtek/Geode-GDDLIntegration/pull/2), [averiee_](https://github.com/B1rtek/Geode-GDDLIntegration/milestone/4), [TheSuperJepphyKiller](https://github.com/B1rtek/Geode-GDDLIntegration/issues/6), [hoshinekosanti](https://github.com/B1rtek/Geode-GDDLIntegration/issues/11), [epicmushroom.](https://github.com/B1rtek/Geode-GDDLIntegration/issues/15), [Koble](https://github.com/B1rtek/Geode-GDDLIntegration/issues/21), [Diversion](https://github.com/B1rtek/Geode-GDDLIntegration/issues/25)</c>
<cr>*Bug reporters:*</c> <cy>croozington, bllue, [Fleeym](https://github.com/geode-sdk/indexer/issues/557#issuecomment-1913780380), [matcool](https://github.com/geode-sdk/indexer/issues/557#issuecomment-1913780699), [Lexicon](https://github.com/B1rtek/Geode-GDDLIntegration/issues/7), [patrick61804](https://github.com/B1rtek/Geode-GDDLIntegration/issues/16), [epicmushroom.](https://github.com/B1rtek/Geode-GDDLIntegration/issues/20), [GlassesCatandsteve](https://github.com/B1rtek/Geode-GDDLIntegration/issues/27), [HotKentang, wangko5383, dshifter, Ev212](https://github.com/B1rtek/Geode-GDDLIntegration/issues/29)</c>
<cr>*Bug reporters:*</c> <cy>croozington, bllue, [Fleeym](https://github.com/geode-sdk/indexer/issues/557#issuecomment-1913780380), [matcool](https://github.com/geode-sdk/indexer/issues/557#issuecomment-1913780699), [Lexicon](https://github.com/B1rtek/Geode-GDDLIntegration/issues/7), [patrick61804](https://github.com/B1rtek/Geode-GDDLIntegration/issues/16), [epicmushroom.](https://github.com/B1rtek/Geode-GDDLIntegration/issues/20), [GlassesCatandsteve](https://github.com/B1rtek/Geode-GDDLIntegration/issues/27), [HotKentang, wangko5383, dshifter, Ev212](https://github.com/B1rtek/Geode-GDDLIntegration/issues/29), Cvolton</c>
2 changes: 1 addition & 1 deletion about.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ If you encounter an <cy>issue</c> with the mod, [open an issue in the mod's GitH

<cb>*Code contributions:*</c> <cy>**[Diversion](https://github.com/B1rtek/Geode-GDDLIntegration/pull/3)**</c>
<cg>*Feature suggestions:*</c> <cy>[MasterGamerY](https://github.com/B1rtek/Geode-GDDLIntegration/issues/1), [Weebifying](https://github.com/B1rtek/Geode-GDDLIntegration/pull/2), [averiee_](https://github.com/B1rtek/Geode-GDDLIntegration/milestone/4), [TheSuperJepphyKiller](https://github.com/B1rtek/Geode-GDDLIntegration/issues/6), [hoshinekosanti](https://github.com/B1rtek/Geode-GDDLIntegration/issues/11), [epicmushroom.](https://github.com/B1rtek/Geode-GDDLIntegration/issues/15), [Koble](https://github.com/B1rtek/Geode-GDDLIntegration/issues/21), [Diversion](https://github.com/B1rtek/Geode-GDDLIntegration/issues/25)</c>
<cr>*Bug reporters:*</c> <cy>croozington, bllue, [Fleeym](https://github.com/geode-sdk/indexer/issues/557#issuecomment-1913780380), [matcool](https://github.com/geode-sdk/indexer/issues/557#issuecomment-1913780699), [Lexicon](https://github.com/B1rtek/Geode-GDDLIntegration/issues/7), [patrick61804](https://github.com/B1rtek/Geode-GDDLIntegration/issues/16), [epicmushroom.](https://github.com/B1rtek/Geode-GDDLIntegration/issues/20), [GlassesCatandsteve](https://github.com/B1rtek/Geode-GDDLIntegration/issues/27), [HotKentang, wangko5383, dshifter, Ev212](https://github.com/B1rtek/Geode-GDDLIntegration/issues/29)</c>
<cr>*Bug reporters:*</c> <cy>croozington, bllue, [Fleeym](https://github.com/geode-sdk/indexer/issues/557#issuecomment-1913780380), [matcool](https://github.com/geode-sdk/indexer/issues/557#issuecomment-1913780699), [Lexicon](https://github.com/B1rtek/Geode-GDDLIntegration/issues/7), [patrick61804](https://github.com/B1rtek/Geode-GDDLIntegration/issues/16), [epicmushroom.](https://github.com/B1rtek/Geode-GDDLIntegration/issues/20), [GlassesCatandsteve](https://github.com/B1rtek/Geode-GDDLIntegration/issues/27), [HotKentang, wangko5383, dshifter, Ev212](https://github.com/B1rtek/Geode-GDDLIntegration/issues/29), Cvolton</c>
5 changes: 5 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# v1.1.5
- Fixed a bug that would cause the game to crash if the level cache was for some reason not a valid json
- The "couldn't cache" popup shouldn't pop up every single time when there's no connection anymore on the title screen
- Fixed a bug which caused the weekly full cache refresh to not work due to overwriting the initial cache time value

# v1.1.4
- Fixed [the bug that prevented players from using the simple search UI](https://github.com/B1rtek/Geode-GDDLIntegration/issues/29)
- The local ratings cache updates downloaded from the API are now saved, so the GDDL Split should be even more accurate now
Expand Down
2 changes: 1 addition & 1 deletion mod.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"geode": "2.0.0",
"version": "v1.1.4",
"version": "v1.1.5",
"gd": {
"android": "2.205",
"win": "2.204"
Expand Down
10 changes: 10 additions & 0 deletions src/GDDLRating.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,13 @@ GDDLRating::GDDLRating(json levelData) {

this->roundedRating = static_cast<int>(round(this->rating));
}

GDDLRating GDDLRating::createInvalid() {
auto rating = GDDLRating();
rating.ratingCount = -1;
return rating;
}

bool GDDLRating::isInvalid() const {
return ratingCount == -1;
}
4 changes: 4 additions & 0 deletions src/GDDLRating.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ class GDDLRating {
int ratingCount;
int enjoymentCount;
int submissionCount;

static GDDLRating createInvalid();

bool isInvalid() const;
};


Expand Down
26 changes: 15 additions & 11 deletions src/GDDLSearchLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -509,19 +509,23 @@ std::string GDDLSearchLayer::formSearchRequest() {

std::vector<int> GDDLSearchLayer::parseResponse(const std::string& response) {
std::vector<int> results;
json responseJson = json::parse(response);
const int total = responseJson["total"];
totalOnlineResults = std::max(totalOnlineResults, total); // so it never grabs 0 if a bad request is made
json levelList = responseJson["levels"];
for (auto element: levelList) {
const int levelID = element["LevelID"];
if (levelID > 3) { // to avoid official demons
results.push_back(element["LevelID"]);
if(!element["Rating"].is_null()) {
const float rating = element["Rating"];
RatingsManager::updateCacheFromSearch(levelID, rating);
try {
json responseJson = json::parse(response);
const int total = responseJson["total"];
totalOnlineResults = std::max(totalOnlineResults, total); // so it never grabs 0 if a bad request is made
json levelList = responseJson["levels"];
for (auto element: levelList) {
const int levelID = element["LevelID"];
if (levelID > 3) { // to avoid official demons
results.push_back(element["LevelID"]);
if(!element["Rating"].is_null()) {
const float rating = element["Rating"];
RatingsManager::updateCacheFromSearch(levelID, rating);
}
}
}
} catch (json::exception &error) {
// well nothing really can be done here
}
return results;
}
Expand Down
67 changes: 46 additions & 21 deletions src/RatingsManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,12 @@ std::vector<int> RatingsManager::tierColors = {
std::map<int, int> RatingsManager::ratingsCache;

GDDLRating RatingsManager::parseJson(const std::string& response) {
const json levelData = json::parse(response);
return GDDLRating(levelData);
try {
const json levelData = json::parse(response);
return GDDLRating(levelData);
} catch (json::exception &error) {
return GDDLRating::createInvalid();
}
}

cocos2d::ccColor3B RatingsManager::convertToColor(const int hexColor) {
Expand All @@ -72,25 +76,39 @@ cocos2d::ccColor3B RatingsManager::convertToColor(const int hexColor) {
* If the list is over a week old, don't load data from it
*/
void RatingsManager::populateFromSave() {
if (!Utils::fileExists(cachedListPath))
if (!Utils::fileExists(cachedListPath)) {
return;
}
std::ifstream f(cachedListPath);
json data = json::parse(f);
const unsigned int cachedTimestamp = data["cached"];
// ReSharper disable once CppTooWideScopeInitStatement
const unsigned int currentTimestamp = Utils::getCurrentTimestamp();
if (currentTimestamp - cachedTimestamp < 86400 * 7) { // list less than 7 days old, load it
for (auto idRatingPair: data["list"]) {
const int id = idRatingPair["ID"];
const int rating = idRatingPair["Rating"];
ratingsCache[id] = rating;
if (Utils::fileIsEmpty(f)) {
return;
}
try {
json data = json::parse(f);
cacheTimestamp = data["cached"];
// ReSharper disable once CppTooWideScopeInitStatement
const unsigned int currentTimestamp = Utils::getCurrentTimestamp();
if (currentTimestamp - cacheTimestamp < 86400 * 7) { // list less than 7 days old, load it
for (auto idRatingPair: data["list"]) {
const int id = idRatingPair["ID"];
const int rating = idRatingPair["Rating"];
ratingsCache[id] = rating;
}
}
} catch (json::exception &error) {
// just do nothing, the user will be notified that stuff happened
}
}

void RatingsManager::cacheList() {
void RatingsManager::cacheList(bool onQuit) {
json cachedList;
cachedList["cached"] = Utils::getCurrentTimestamp();
if (!onQuit) {
// update the timestamp because we're saving a new list
cacheTimestamp = Utils::getCurrentTimestamp(); // NOLINT(*-narrowing-conversions)
// else: if it was 0 then the request failed and will be tried again on the next game launch
// otherwise we retain the last value to keep track of the 7 days cache age
}
cachedList["cached"] = cacheTimestamp;
std::vector<json> idRatingPairs;
json element;
for (auto [id, rating]: ratingsCache) {
Expand Down Expand Up @@ -131,20 +149,27 @@ bool RatingsManager::addRatingFromResponse(const int id, const std::string &resp
if (response.empty())
return false;
const GDDLRating rating = parseJson(response);
if (rating.isInvalid()) {
return false;
}
demonMap[id] = rating;
return true;
}

void RatingsManager::cacheRatings(const std::string &response) {
// ReSharper disable once CppTooWideScopeInitStatement
json ratingsData = json::parse(response);
for (auto element: ratingsData) {
const int id = element["ID"];
const float rating = element["Rating"].is_null() ? -1.0f : static_cast<float>(element["Rating"]);
const int roundedRating = static_cast<int>(round(rating));
ratingsCache[id] = roundedRating;
try {
json ratingsData = json::parse(response);
for (auto element: ratingsData) {
const int id = element["ID"];
const float rating = element["Rating"].is_null() ? -1.0f : static_cast<float>(element["Rating"]);
const int roundedRating = static_cast<int>(round(rating));
ratingsCache[id] = roundedRating;
}
cacheList(false);
} catch (json::exception &error) {
// just do nothing, the user will be notified that stuff happened
}
cacheList();
}

std::map<int, int> RatingsManager::getTierStats() {
Expand Down
5 changes: 4 additions & 1 deletion src/RatingsManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class RatingsManager {
static std::map<int, GDDLRating> demonMap;
static std::vector<int> tierColors;
static std::map<int, int> ratingsCache;
inline static int cacheTimestamp = 0;
inline static std::string cachedListPath = Mod::get()->getSaveDir().string() + "/gddlcache.json";

static GDDLRating parseJson(const std::string& response);
Expand All @@ -22,6 +23,8 @@ class RatingsManager {
static void populateFromSave();

public:
inline static bool triedToCache = false;

static int getDemonTier(int id);

static cocos2d::ccColor3B getTierColor(int tier);
Expand All @@ -42,7 +45,7 @@ class RatingsManager {

static int getCachedTier(int levelID);

static void cacheList();
static void cacheList(bool onQuit);
};


Expand Down
6 changes: 6 additions & 0 deletions src/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ class Utils {
return stat(name.c_str(), &buffer) == 0;
}

// https://stackoverflow.com/questions/2390912/checking-for-an-empty-file-in-c
static bool fileIsEmpty(std::ifstream& pFile)
{
return pFile.peek() == std::ifstream::traits_type::eof();
}

template<typename T>
static std::vector<T> copySetToVector(std::set<T> setToCopy) {
std::vector<T> vec;
Expand Down
14 changes: 11 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,30 @@ class $modify(MenuLayer) {
GDDLSearchLayer::stopSearch();
GDDLSearchLayer::restoreValuesAfterSplit();
GDDLSearchLayer::saveSettings();
if (!RatingsManager::alreadyCached()) {
if (!RatingsManager::alreadyCached() && !RatingsManager::triedToCache) {
RatingsManager::triedToCache = true;
web::AsyncWebRequest()
.fetch("https://gdladder.com/api/theList")
.text()
.then([](std::string const& response) {
RatingsManager::cacheRatings(response);
if(!RatingsManager::alreadyCached()) {
somethingWentWrong();
}
})
.expect([](std::string const& error) {
FLAlertLayer::create("GDDL Integration", "Could not cache ratings from gdladder.com! Check your internet connection and restart the game.", "OK")->show();
somethingWentWrong();
});
}
return true;
}

static void somethingWentWrong() {
FLAlertLayer::create("GDDL Integration", "Could not cache ratings from gdladder.com! Check your internet connection and restart the game.", "OK")->show();
}

void onQuit(cocos2d::CCObject* sender) {
MenuLayer::onQuit(sender);
RatingsManager::cacheList(); // cache modified list on every exit
RatingsManager::cacheList(true); // cache modified list on every exit
}
};

0 comments on commit 6bea034

Please sign in to comment.