Skip to content

Commit

Permalink
start work on downloadGJLevel ratelimit
Browse files Browse the repository at this point in the history
  • Loading branch information
SorkoPiko committed Sep 21, 2024
1 parent 685238a commit b07c142
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 6 deletions.
13 changes: 12 additions & 1 deletion mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
},
"id": "sorkopiko.noratelimit",
"name": "No Rate Limit",
"version": "v1.0.3",
"version": "v1.0.4",
"developer": "SorkoPiko",
"description": "Prevents you from getting rate-limited by Robtop's API",
"links": {
Expand All @@ -33,6 +33,17 @@
"control": {
"slider": true
}
},
"maxDownloadLevelReqs": {
"name": "Max Level Download Requests per Minute",
"description": "The maximum amount of level download requests you can make to Robtop's API per minute (you get banned at 20)",
"type": "int",
"default": 18,
"min": 5,
"max": 19,
"control": {
"slider": true
}
}
},
"incompatibilities": [
Expand Down
19 changes: 18 additions & 1 deletion src/hooks/CCHttpClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,22 @@ class $modify(NRLCCHttpClient, CCHttpClient) {

void send(CCHttpRequest* request) {
if (const std::string url = request->getUrl(); url.find("://www.boomlings.com") != std::string::npos && std::find(handledReqs.begin(), handledReqs.end(), request) == handledReqs.end()) {
auto downloadLevel = false;
if (url.find("://www.boomlings.com/database/downloadGJLevel22.php") != std::string::npos) {
downloadLevel = true;
}
handledReqs.push_back(request);
if (const auto time = RequestStutter::getRequestTime(); time > 0) {
if (const auto time = RequestStutter::getRequestTime(downloadLevel); time > 0) {
log::info("delaying request by {}ms", time);
request->retain();
//CCScheduler::get()->scheduleSelector(
// schedule_selector(NRLCCHttpClient::sendRequest),
// this,
// 1,
// 0,
// time / 1000.0f,
// false
//);
std::thread([this, request, time] {
std::this_thread::sleep_for(std::chrono::milliseconds(time));
Loader::get()->queueInMainThread([this, request] {
Expand All @@ -28,4 +40,9 @@ class $modify(NRLCCHttpClient, CCHttpClient) {
}
CCHttpClient::send(request);
}

//void sendRequest(float dt) {
// CCHttpClient::send(m_fields->m_request);
// m_fields->m_request->release();
//}
};
8 changes: 7 additions & 1 deletion src/hooks/GeodeWeb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,16 @@ static std::vector<size_t> handledReqs;

web::WebTask WebRequest_send(web::WebRequest* request, const std::string_view method, const std::string_view url) {
if (url.find("://www.boomlings.com") != std::string::npos) {
if (const auto time = RequestStutter::getRequestTime(); time > 0 && std::find(handledReqs.begin(), handledReqs.end(), request->getID()) == handledReqs.end()) {
auto downloadLevel = false;
if (url.find("://www.boomlings.com/database/downloadGJLevel22.php") != std::string::npos) {
downloadLevel = true;
}
if (const auto time = RequestStutter::getRequestTime(downloadLevel); time > 0 && std::find(handledReqs.begin(), handledReqs.end(), request->getID()) == handledReqs.end()) {
handledReqs.push_back(request->getID());
log::info("delaying request by {}ms", time);


//Thanks SMJS for the task handler
const auto req = new web::WebRequest(*request);
const auto returnTask = web::WebTask::run([method, url, req, time](auto progress, auto cancelled) -> web::WebTask::Result {
std::unique_ptr<web::WebResponse> response;
Expand Down
20 changes: 19 additions & 1 deletion src/managers/RequestStutter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,36 @@
using namespace geode::prelude;

long long RequestStutter::lastRequestTime = 0;
long long RequestStutter::lastDownloadLevelTime = 0;

long long getCurrentTimestampMillis() {
const auto now = std::chrono::system_clock::now();
const auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch());
return duration.count();
}

long long RequestStutter::getRequestTime() {
long long RequestStutter::getRequestTime(bool downloadLevel) {
const auto maxReqs = Mod::get()->getSettingValue<int64_t>("maxReqs");
const auto delay = 60 * 1000 / maxReqs;
const auto now = getCurrentTimestampMillis();
const auto nextRequestTime = lastRequestTime + delay;
lastRequestTime = std::max(nextRequestTime, now);
if (downloadLevel) {
return getDownloadLevelTime();
}
log::info("{}", lastRequestTime - now);
return lastRequestTime - now;
}

long long RequestStutter::getDownloadLevelTime() {
const auto maxReqs = Mod::get()->getSettingValue<int64_t>("maxDownloadLevelReqs");
const auto delay = 60 * 1000 / maxReqs;
const auto now = getCurrentTimestampMillis();
if (lastDownloadLevelTime < lastRequestTime) {
lastDownloadLevelTime = lastRequestTime;
}
const auto nextRequestTime = lastDownloadLevelTime + delay;
lastDownloadLevelTime = std::max(nextRequestTime, now);
log::info("{}", lastDownloadLevelTime - now);
return lastDownloadLevelTime - now;
}
5 changes: 3 additions & 2 deletions src/managers/RequestStutter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
#define REQUESTSTUTTER_HPP

class RequestStutter {
protected:
static long long lastRequestTime;
static long long lastDownloadLevelTime;
static long long getDownloadLevelTime();

public:
static long long getRequestTime();
static long long getRequestTime(bool downloadLevel);
};

#endif

0 comments on commit b07c142

Please sign in to comment.