diff --git a/.gitignore b/.gitignore index 0965ced..7464d87 100644 --- a/.gitignore +++ b/.gitignore @@ -13,4 +13,5 @@ build/ .DS_Store .vscode/settings.json -.idea/ \ No newline at end of file +.idea/ +tempCodeRunnerFile.bat diff --git a/src/MoreLeaderboards/MoreLeaderboards.cpp b/src/MoreLeaderboards/MoreLeaderboards.cpp index 376a872..46d4ad4 100644 --- a/src/MoreLeaderboards/MoreLeaderboards.cpp +++ b/src/MoreLeaderboards/MoreLeaderboards.cpp @@ -3,6 +3,8 @@ static StatsListType g_tab = StatsListType::Diamonds; +static std::unordered_map RUNNING_REQUESTS {}; + std::vector MoreLeaderboards::getWords(std::string s, std::string delim) { std::vector res; std::string token = ""; @@ -98,17 +100,19 @@ bool MoreLeaderboards::init(std::string type) { this->addChild(background); // Change region - /*auto menu = CCMenu::create(); + auto menu_region = CCMenu::create(); - auto regionSpr = CCSprite::createWithSpriteFrameName("GJ_plusBtn_001.png"); + auto regionSpr = CCSprite::create(Mod::get()->expandSpriteName("earth_btn.png")); regionSpr->setScale(.8f); auto regionBtn = CCMenuItemSpriteExtra::create( regionSpr, this, menu_selector(MoreLeaderboards::onMoreLeaderboards) ); - regionBtn->setPosition(239, 20); - menu->addChild(regionBtn);*/ + regionBtn->setPosition(239, 0); + menu_region->addChild(regionBtn); + + this->addChild(menu_region); // Corners CCSprite* corner_left = CCSprite::createWithSpriteFrameName("GJ_sideArt_001.png"); @@ -248,7 +252,32 @@ void MoreLeaderboards::startLoadingMods() { loading_circle->setParentLayer(this); loading_circle->show(); - web::AsyncWebRequest() + const geode::utils::MiniFunction then = [this](std::string const& data) { + fadeLoadingCircle(); + handle_request_mods(data); + }; + const geode::utils::MiniFunction expect = [this](std::string const& error) { + fadeLoadingCircle(); + }; + + geode::utils::web::WebRequest request = web::WebRequest(); + RUNNING_REQUESTS.emplace( + "@loaderModListCheck", + request.get("https://clarifygdps.com/gdutils/modslist.php").map( + [expect = std::move(expect), then = std::move(then)](web::WebResponse* response) { + if (response->ok()) { + then(response->string().value()); + } else { + expect("An error occured while sending a request on our server. Please try again later."); + } + + RUNNING_REQUESTS.erase("@loaderModListCheck"); + return *response; + } + ) + ); + + /*web::AsyncWebRequest() .fetch("https://clarifygdps.com/gdutils/modslist.php") .text() .then([this](std::string const& data) { @@ -258,7 +287,7 @@ void MoreLeaderboards::startLoadingMods() { }) .expect([this](std::string const& error) { fadeLoadingCircle(); - }); + });*/ }; void MoreLeaderboards::loadPageMods() { @@ -293,7 +322,86 @@ void MoreLeaderboards::startLoadingMore() { } this->retain(); - web::AsyncWebRequest() + + geode::utils::web::WebRequest request = web::WebRequest(); + const geode::utils::MiniFunction then = [this](std::string const& data) { + loading = false; + auto scene = CCDirector::sharedDirector()->getRunningScene(); + auto layer = scene->getChildren()->objectAtIndex(0); + if (layer == nullptr) return this->release(); + if (misc::getNodeName(layer) != "MoreLeaderboards") return this->release(); // prevent le crash, even though the layer shouldve already been destructed + if (data == "-1" || data.length() < 2) { + fadeLoadingCircle(); + geode::createQuickPopup( + "Error", + "An error occured while sending a request on our server. Please try again later.", + "OK", nullptr, + [this](auto, bool btn2) { + if (!btn2) { + g_tab = StatsListType::Diamonds; + keyBackClicked(); + } + } + ); + this->release(); + return; + } + fadeLoadingCircle(); + + handle_request_more(data); + loading = false; + + m_diamondsTabBtn->setEnabled(true); + m_usercoinsTabBtn->setEnabled(true); + m_demonsTabBtn->setEnabled(true); + m_moonsTabBtn->setEnabled(true); + m_creatorsTabBtn->setEnabled(true); + this->release(); + }; + const geode::utils::MiniFunction expect = [this](std::string const& error) { + loading = false; + auto scene = CCDirector::sharedDirector()->getRunningScene(); + auto layer = scene->getChildren()->objectAtIndex(0); + if (layer == nullptr) return this->release(); + if (misc::getNodeName(layer) != "MoreLeaderboards") return this->release(); + geode::createQuickPopup( + "Error", + "An error occured while sending a request on our server. Please try again later.", + "OK", nullptr, + [this](auto, bool btn2) { + if (!btn2) { + g_tab = StatsListType::Diamonds; + keyBackClicked(); + } + } + ); + fadeLoadingCircle(); + loading = false; + m_diamondsTabBtn->setEnabled(true); + m_usercoinsTabBtn->setEnabled(true); + m_demonsTabBtn->setEnabled(true); + m_moonsTabBtn->setEnabled(true); + m_creatorsTabBtn->setEnabled(true); + this->release(); + }; + + RUNNING_REQUESTS.emplace( + "@loaderMoreLeaderboardCheck", + request.bodyString(fmt::format("type={}", type)).post("https://clarifygdps.com/gdutils/moreleaderboards.php").map( + [expect = std::move(expect), then = std::move(then)](web::WebResponse* response) { + if (response->ok()) { + then(response->string().value()); + } else { + expect("An error occured while sending a request on our server. Please try again later."); + } + + RUNNING_REQUESTS.erase("@loaderMoreLeaderboardCheck"); + return *response; + } + ) + ); + + /*web::AsyncWebRequest() .postRequest() .bodyRaw(fmt::format("type={}", type)) .fetch("https://clarifygdps.com/gdutils/moreleaderboards.php") @@ -357,7 +465,7 @@ void MoreLeaderboards::startLoadingMore() { m_moonsTabBtn->setEnabled(true); m_creatorsTabBtn->setEnabled(true); this->release(); - }); + });*/ }; void MoreLeaderboards::handle_request_more(std::string const& data) { diff --git a/src/MoreLeaderboards/MoreLeaderboards.h b/src/MoreLeaderboards/MoreLeaderboards.h index 10c1596..fc059d3 100644 --- a/src/MoreLeaderboards/MoreLeaderboards.h +++ b/src/MoreLeaderboards/MoreLeaderboards.h @@ -1,6 +1,6 @@ #pragma once #include "../includes.h" -#include +#include #include "MoreLeaderboardsListView.h" #include diff --git a/src/Notifications/EventsPush.cpp b/src/Notifications/EventsPush.cpp index e825b8e..431ec86 100644 --- a/src/Notifications/EventsPush.cpp +++ b/src/Notifications/EventsPush.cpp @@ -2,7 +2,9 @@ #include "../Settings/CustomSettings.hpp" #include "Geode/utils/general.hpp" #include -#include +#include + +static std::unordered_map RUNNING_REQUESTS {}; std::queue eventQueue; bool processingEvents = false; @@ -279,9 +281,51 @@ void EventsPush::onClickBtn(CCObject* ret) { #else // mac os int level_id = events_layer->levelId; #endif - //std::string const& url = "https://clarifygdps.com/getGJLevels21.php"; std::string const& fields = "secret=Wmfd2893gb7&gameVersion=22&type=0&binaryVersion=35&gdw=0&diff=-&len=-&count=1&str=" + std::to_string(level_id); - web::AsyncWebRequest() + + geode::utils::web::WebRequest request = web::WebRequest(); + RUNNING_REQUESTS.emplace( + "@loaderEventRateNotification", + request.bodyString(fields).post(url).map( + [](web::WebResponse* response) { + if (response->ok()) { + if (response->data().empty()) { + FLAlertLayer::create(nullptr, + "Error", + "An server error happened.", + "OK", + nullptr, + 180.0F + )->show(); + } else { + auto data_result = response->string().value(); + if (data_result != "-1") { + auto scene = CCScene::create(); + auto layer = LevelInfoLayer::create(EventsPush::convertLevelToJSON(data_result), false); + layer->downloadLevel(); + scene->addChild(layer); + CCDirector::sharedDirector()->pushScene(cocos2d::CCTransitionFade::create(0.5f, scene)); + } else { + log::info("Level not found. (-1)"); + } + } + } else { + FLAlertLayer::create(nullptr, + "Error", + "An server error happened.", + "OK", + nullptr, + 180.0F + )->show(); + } + + RUNNING_REQUESTS.erase("@loaderEventRateNotification"); + return *response; + } + ) + ); + + /*web::AsyncWebRequest() .bodyRaw(fields) .postRequest() .fetch(url).text() @@ -297,7 +341,7 @@ void EventsPush::onClickBtn(CCObject* ret) { } }).expect([](std::string const& error) { log::error("Error occured while doing a web request: {}", error); - }); + });*/ } else { // copy to clipboard #ifndef GEODE_IS_MACOS clipboard::write(std::to_string(events_layer->level->m_levelID)); diff --git a/src/main.cpp b/src/main.cpp index 74106fe..b3cb968 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7,7 +7,7 @@ #include #include #include -#include +#include #include "includes.h" #include "Settings/CustomSettings.hpp" #include "Notifications/EventsPush.h" @@ -24,6 +24,8 @@ #include #include +static std::unordered_map RUNNING_REQUESTS {}; + int reconnectionDelay = 1000; int reconnectionDelayMax = 5000; int reconnectionAttempts = 1000; @@ -215,7 +217,58 @@ class $modify(CCScheduler) { // GD Protocol part std::string const& url = "https://www.boomlings.com/database/getGJLevels21.php"; std::string const& fields = "secret=Wmfd2893gb7&type=0&str=" + levelName; - std::cout << fields << std::endl; + geode::utils::web::WebRequest request = web::WebRequest(); + RUNNING_REQUESTS.emplace( + "@loaderLevelProtocolURL", + request.bodyString(fields).post(url).map( + [](web::WebResponse* response) { + if (response->ok()) { + if (response->data().empty()) { + FLAlertLayer::create(nullptr, + "Error", + "An server error happened.", + "OK", + nullptr, + 180.0F + )->show(); + } else { + auto data = response->string().value(); + if (data != "-1") { + auto scene = CCScene::create(); + + GJGameLevel* gjgl = EventsPush::convertLevelToJSON(data); + + auto layer = LevelInfoLayer::create(gjgl, false); + layer->downloadLevel(); + scene->addChild(layer); + CCDirector::sharedDirector()->pushScene(cocos2d::CCTransitionFade::create(0.5f, scene)); + } else { + FLAlertLayer::create(nullptr, + "Error", + "Level not found.", + "OK", + nullptr, + 180.0F + )->show(); + } + } + } else { + FLAlertLayer::create(nullptr, + "Error", + "An server error happened.", + "OK", + nullptr, + 180.0F + )->show(); + } + + RUNNING_REQUESTS.erase("@loaderLevelProtocolURL"); + return *response; + } + ) + ); + + /* web::AsyncWebRequest() .bodyRaw(fields) .postRequest() @@ -247,7 +300,7 @@ class $modify(CCScheduler) { // GD Protocol part nullptr, 350.0F )->show(); - }); + });*/ } } } catch (const std::exception& e) {}