Skip to content

Commit

Permalink
v1.0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
SergeyMC9730 committed May 1, 2024
1 parent b451214 commit a988373
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 11 deletions.
2 changes: 1 addition & 1 deletion about.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Look through <cy>deleted and outdated levels</c> using <cp>GDHistory</c> or other sources!

## As of `v1.0.1` GDHistory is supported only.
## As of `v1.0.3` GDHistory is supported only.

# How To Use

Expand Down
2 changes: 1 addition & 1 deletion mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"android": "2.205",
"win": "2.204"
},
"version": "v1.0.2",
"version": "v1.0.3",
"id": "dogotrigger.level_history",
"name": "Level History",
"developer": "dogotrigger",
Expand Down
62 changes: 60 additions & 2 deletions src/GDHistoryProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,25 @@ void GDHistoryProvider::downloadLevel(std::function<void(LevelProvider *, GJGame
.fetch(fmt::format("{}/api/v1/level/{}", _baseUrl, std::get<int>(_params[LPFeatures::QueryID])))
.text()
.then([this, onComplete](std::string const& catgirl) {
if (catgirl.find("Server Error") != std::string::npos) {
this->_currentError = "-7";
onComplete(this, nullptr);

return;
}

nlohmann::json data = nlohmann::json::parse(catgirl);

if (!data.contains("records")) {
this->_currentError = "-8";
onComplete(this, nullptr);

return;
}

nlohmann::json records = data.at("records");
if (!records.is_array()) {
this->_currentError = "-9";
onComplete(this, nullptr);

return;
Expand All @@ -36,6 +45,7 @@ void GDHistoryProvider::downloadLevel(std::function<void(LevelProvider *, GJGame
int levels = records.size();

if (levels == 0) {
this->_currentError = "-10";
onComplete(this, nullptr);

return;
Expand Down Expand Up @@ -89,6 +99,8 @@ void GDHistoryProvider::downloadLevel(std::function<void(LevelProvider *, GJGame
level->m_dislikes = 0;
}

this->makeLevelCopyable(level);

level->retain();

this->_serverResponseParsed.push_back(level);
Expand All @@ -111,6 +123,14 @@ void GDHistoryProvider::downloadLevel(std::function<void(LevelProvider *, GJGame
onComplete(this, this->_serverResponseParsed.at(0));
})
.expect([this, onComplete](std::string const& error) {
if (error.find("Server Error") != std::string::npos) {
this->_currentError = "-7";
onComplete(this, nullptr);

return;
}

this->_currentError = "-1";
onComplete(this, nullptr);
});
}
Expand All @@ -137,14 +157,23 @@ void GDHistoryProvider::downloadLevel(std::function<void(LevelProvider *, GJGame
.then([this, onComplete](std::string const& catgirl) {
nlohmann::json data = nlohmann::json::parse(catgirl);

if (catgirl.find("Server Error") != std::string::npos) {
this->_currentError = "-7";
onComplete(this, nullptr);

return;
}

if (!data.contains("hits")) {
this->_currentError = "-8";
onComplete(this, nullptr);

return;
}

nlohmann::json records = data.at("hits");
if (!records.is_array()) {
this->_currentError = "-9";
onComplete(this, nullptr);

return;
Expand All @@ -153,6 +182,7 @@ void GDHistoryProvider::downloadLevel(std::function<void(LevelProvider *, GJGame
int levels = records.size();

if (levels == 0) {
this->_currentError = "-10";
onComplete(this, nullptr);

return;
Expand Down Expand Up @@ -187,12 +217,16 @@ void GDHistoryProvider::downloadLevel(std::function<void(LevelProvider *, GJGame
bool has_leveldata = false;
PARSE_BOOL(has_leveldata, leveljson["cache_level_string_available"]);
if (has_leveldata) {
log::info("(GDHistoryProvider) level {} by {} has leveldata", level->m_levelName, level->m_creatorName);
level->m_dislikes = 1;
level->m_likes++;
} else {
log::info("(GDHistoryProvider) level {} by {} does not has leveldata", level->m_levelName, level->m_creatorName);
level->m_dislikes = 0;
}

this->makeLevelCopyable(level);

level->retain();

this->_serverResponseParsed.push_back(level);
Expand All @@ -201,6 +235,14 @@ void GDHistoryProvider::downloadLevel(std::function<void(LevelProvider *, GJGame
onComplete(this, this->_serverResponseParsed.at(0));
})
.expect([this, onComplete](std::string const& error) {
if (error.find("Server Error") != std::string::npos) {
this->_currentError = "-7";
onComplete(this, nullptr);

return;
}

this->_currentError = "-1";
onComplete(this, nullptr);
});
}
Expand Down Expand Up @@ -274,7 +316,8 @@ void GDHistoryProvider::getLevelData(int id, std::function<void(LevelProvider *,
this->cleanupLevels(true);
this->_serverResponseParsed = old_vec;

onComplete(this, "-4", info);
// this->_currentError = "-4";
onComplete(this, this->getErrorCode(), info);

return;
}
Expand All @@ -294,6 +337,7 @@ void GDHistoryProvider::getLevelData(int id, std::function<void(LevelProvider *,
this->cleanupLevels(true);
this->_serverResponseParsed = old_vec;

this->_currentError = "-5";
onComplete(this, "-5", info);

return;
Expand All @@ -305,6 +349,7 @@ void GDHistoryProvider::getLevelData(int id, std::function<void(LevelProvider *,
this->cleanupLevels(true);
this->_serverResponseParsed = old_vec;

this->_currentError = "-3";
onComplete(this, "-3", info);

return;
Expand All @@ -329,12 +374,21 @@ void GDHistoryProvider::getLevelData(int id, std::function<void(LevelProvider *,
.then([this, onComplete](std::string const& catgirl) {
struct LevelProvider::BasicLevelInformation info;

if (catgirl.find("Server Error") != std::string::npos) {
this->_currentError = "-7";
onComplete(this, "-7", info);

return;
}

if (catgirl.find("This record does not contain any level data.") != std::string::npos) {
this->_currentError = "-5";
onComplete(this, "-5", info);

return;
}
if (catgirl.find("You do not have the rights to download this record") != std::string::npos) {
this->_currentError = "-6";
onComplete(this, "-6", info);

return;
Expand Down Expand Up @@ -400,7 +454,11 @@ std::string GDHistoryProvider::getErrorCodeDescription(std::string err) {
{"-3", "invalid record id."},
{"-4", "level not found."},
{"-5", "level data cannot be downloaded for this level. Note that this issue will be fixed if level would have downloadable link for it in the future."},
{"-6", "insufficient rights to download this level."}
{"-6", "insufficient rights to download this level."},
{"-7", "gd history's api is down."},
{"-8", "api did not return record data."},
{"-9", "api returned record data with invalid data type."},
{"-10", "api returned record array with zero levels in it."}
};

if (errors.count(err)) {
Expand Down
11 changes: 11 additions & 0 deletions src/LevelProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,15 @@ std::string LevelProvider::url_encode(const std::string value) {
}

return escaped.str();
}

std::string LevelProvider::getErrorCode() {
return _currentError;
}

#include <Geode/binding/GJGameLevel.hpp>

void LevelProvider::makeLevelCopyable(GJGameLevel *level) {
level->m_isEditable = true;
level->m_password = 1;
}
5 changes: 5 additions & 0 deletions src/LevelProvider.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ class LevelProvider {
std::unordered_map<enum LPFeatures, std::variant<std::string, int>> _params;

std::string url_encode(const std::string value);

std::string _currentError;

void makeLevelCopyable(GJGameLevel *level);
public:
struct BasicLevelInformation {
int musicID = 0;
Expand All @@ -44,4 +48,5 @@ class LevelProvider {
virtual void getLevelData(int id, std::function<void(LevelProvider *, std::string, struct BasicLevelInformation)> onComplete) = 0;

virtual std::string getErrorCodeDescription(std::string err) = 0;
virtual std::string getErrorCode();
};
45 changes: 38 additions & 7 deletions src/ProviderPopup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ using namespace geode::prelude;

#include "LevelProvider.hpp"

// #include <geode/binding/LevelInfoLayer.hpp>

void ProviderPopup::onToggler1PressMaybe(CCObject *sender) {
LevelProvider *provider = static_cast<LevelProvider *>(((CCNode *)sender)->getUserData());

Expand Down Expand Up @@ -168,6 +170,10 @@ void ProviderPopup::update(float delta) {
// _levelPageStr = lpi->getString();
// log::info("lps: {}", _levelPageStr);
// }

for (auto cell : _levelPage._cells) {
removeThumbnailForCell(cell);
}
}

ProviderPopup* ProviderPopup::create(std::vector<std::shared_ptr<LevelProvider>> providers) {
Expand Down Expand Up @@ -630,7 +636,7 @@ LevelCell *ProviderPopup::createLevelCell(GJGameLevel *level, CCLayer *page) {

CCLayer *base = dynamic_cast<CCLayer *>(cell->getChildByID("main-layer"));
base->setAnchorPoint({0, 0});
base->setPositionX(csz.width / 3);
base->setPositionX(csz.width / 4);
base->setScale(0.65f);

std::vector<CCNode *> to_lower;
Expand Down Expand Up @@ -659,6 +665,9 @@ LevelCell *ProviderPopup::createLevelCell(GJGameLevel *level, CCLayer *page) {

cell->addChild(dateLabel);

CCMenuItemSpriteExtra *viewBtn = dynamic_cast<CCMenuItemSpriteExtra *>(cell->getChildByIDRecursive("view-button"));
viewBtn->setVisible(false);

return cell;
}

Expand Down Expand Up @@ -775,7 +784,8 @@ void ProviderPopup::lambdaOnDownloadLevel(SearchInstance *si, LoadingCircleLayer
existingCircle->m_pCircle->setOpacity(0);

if (level == nullptr) {
FLAlertLayer::create("Error", "<cr>No such level!</c>", "OK")->show();
auto error = fmt::format("<cr>No levels cannot be found: </c><cy>{}</c>", prov->getErrorCodeDescription(prov->getErrorCode()));
FLAlertLayer::create("Error", error, "OK")->show();
return;
}

Expand Down Expand Up @@ -1091,7 +1101,9 @@ void ProviderPopup::lambdaOnDownloadLevelList(SearchInstance *si, LoadingCircleL
existingCircle->m_pCircle->setOpacity(0);

if (level == nullptr) {
FLAlertLayer::create("Error", "<cr>No levels were found!</c>", "OK")->show();
auto error = fmt::format("<cr>No levels cannot be found: </c><cy>{}</c>", prov->getErrorCodeDescription(prov->getErrorCode()));

FLAlertLayer::create("Error", error, "OK")->show();
return;
}

Expand Down Expand Up @@ -1306,7 +1318,20 @@ void ProviderPopup::onPlayLevelDownload(CCObject *sender) {
}

if (data[0] == '-') {
FLAlertLayer::create("Error", fmt::format("<cr>Error while getting level data: </c><cy>{}</c>", prov->getErrorCodeDescription(data)), "OK")->show();
float transition_time = 0.5f;

auto clevel = newpopup->_levelPage._currentLevels[newpopup->_levelPage._currentLevelsIndex];
auto scene = LevelInfoLayer::scene(clevel, false);
auto transition = CCTransitionFade::create(transition_time, scene);

CCDirector::sharedDirector()->pushScene(transition);

std::string err = fmt::format("<cr>Error while getting level data: </c><cy>{}</c>", prov->getErrorCodeDescription(data));
ProviderPopup::_currentError = err;

scene->scheduleUpdate();
scene->scheduleOnce(schedule_selector(ProviderPopup::scenePrintError), transition_time + 0.1f);

return;
}

Expand All @@ -1318,7 +1343,7 @@ void ProviderPopup::onPlayLevelDownload(CCObject *sender) {

// log::info("level string: {}", data);

auto scene = PlayLayer::scene(level, false, false);
auto scene = LevelInfoLayer::scene(level, false);

auto transition = CCTransitionFade::create(0.5f, scene);

Expand Down Expand Up @@ -1349,7 +1374,7 @@ void ProviderPopup::applyBottomButtons(CCLayer *page) {
}

{
auto spr4 = ButtonSprite::create("Play Level");
auto spr4 = ButtonSprite::create("Go to Level");
spr4->setScale(0.6f);
spr4->setID("play-level-spr");

Expand Down Expand Up @@ -1419,4 +1444,10 @@ void ProviderPopup::removeThumbnailForCell(LevelCell *cell) {
}

// children->release();
}
}

void ProviderPopup::scenePrintError(float delta) {
FLAlertLayer::create("Error", ProviderPopup::_currentError, "OK")->show();
}

std::string ProviderPopup::_currentError = "";
4 changes: 4 additions & 0 deletions src/ProviderPopup.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ class ProviderPopup : public FLAlertLayer {
std::string _enterLevelID = "";
std::string _enterQuery = "";

static std::string _currentError;

LevelCell *createLevelCell(GJGameLevel *level, CCLayer *page);

void lambdaOnDownloadLevel(SearchInstance *si, LoadingCircleLayer *existingCircle, ProviderPopup *popup, LevelProvider *prov, GJGameLevel *level);
Expand Down Expand Up @@ -86,6 +88,8 @@ class ProviderPopup : public FLAlertLayer {

static void removeThumbnailForCell(LevelCell *cell);
void removeLevelRatings();

void scenePrintError(float delta);
// static void setButtonSpriteColor(ButtonSprite)
// bool ccTouchBegan(cocos2d::CCTouch *touch, cocos2d::CCEvent *event) override;
};

0 comments on commit a988373

Please sign in to comment.