-
Notifications
You must be signed in to change notification settings - Fork 1
/
NodeRunnerLegacy.cpp
560 lines (447 loc) · 22.5 KB
/
NodeRunnerLegacy.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
/**
* LevelAPI - Geometry Dash level cacher with search functionality and more.
Copyright (C) 2023 Sergei Baigerov
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "GDServer_BoomlingsLike22.h"
#include "curl_backend.h"
#include "DatabaseControllerThreads.h"
#include "StringSplit.h"
#include "json/single_include/nlohmann/json.hpp"
#include <chrono>
#include <cstdio>
#include <filesystem>
#include <fstream>
#include <optional>
#include <stdexcept>
#include <string>
#include <thread>
#include <iostream>
#include <vector>
#include <dirent.h>
#include <sys/statvfs.h>
#include "GDServer.h"
#include "Level.h"
#include "lapi_database.h"
#ifdef _DPP_ENABLED_
#include "message.h"
#include "restresults.h"
#endif
#include "Translation.h"
using namespace LevelAPI;
using namespace LevelAPI::Frontend;
using namespace std::chrono_literals;
void DatabaseController::node_runner_recount_task(Node *nd) {
goto lp;
lp:
auto start = std::chrono::high_resolution_clock::now();
nd->m_nTableLevels = nd->_sqliteObject->countTable("levels");
nd->m_nCachedLevels = nd->m_nTableLevels;
if(!nd->m_pPolicy->m_bNoOutput) {
std::cout << Translation::getByKey("lapi.noderunner.recount.complete", nd->m_sInternalName, nd->m_nTableLevels) << std::endl;
}
auto stop = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(stop - start);
std::cout << Translation::getByKey("lapi.noderunner.recount.time", nd->m_sInternalName, duration.count()) << std::endl;
std::this_thread::sleep_for(5s);
goto lp;
}
void DatabaseController::node_runner_recentBot(Node *nd) {
if(!nd->m_pPolicy->m_bEnableRecentTab) return;
start:
std::this_thread::sleep_for(std::chrono::milliseconds((int)(nd->m_pPolicy->m_nQueueProcessingInterval * 1000.f)));
if(nd->m_uQueue->m_vCommandQueue.size() == 0) {
nd->m_uQueue->m_vCommandQueue.push_back({NC_RECENT, "-"});
nd->m_uQueue->save();
}
goto start;
}
void DatabaseController::node_runner_waitResolverRL(Node *nd, int rate_limit_length) {
if(nd->m_bRateLimitApplied) return;
nd->m_bRateLimitApplied = true;
if(!nd->m_pPolicy->m_bNoOutput) {
//std::cout << "[LevelAPI resolver " << nd->m_sInternalName << "] Waiting " << rate_limit_length << "s" << std::endl;
std::cout << Translation::getByKey("lapi.noderunner.resolver.wait.start", nd->m_sInternalName, rate_limit_length) << std::endl;
}
std::this_thread::sleep_for(std::chrono::seconds(rate_limit_length + (int)nd->m_pPolicy->m_nResolverInterval));
if(!nd->m_pPolicy->m_bNoOutput) {
//std::cout << "[LevelAPI resolver " << nd->m_sInternalName << "] Rate limit end!" << std::endl;
std::cout << Translation::getByKey("lapi.noderunner.resolver.wait.end", nd->m_sInternalName) << std::endl;
}
nd->m_bRateLimitApplied = false;
return;
}
#ifdef _DPP_ENABLED_
void DatabaseController::node_runner_wait_level(Node *nd, dpp::message message, int id) {}
#else
void DatabaseController::node_runner_wait_level(Node *nd, int id) {}
#endif
void DatabaseController::node_runner_resolve_level(Node *nd, NodeCommandQueue *q, Backend::GDServer *server) {}
void DatabaseController::node_runner(Node *nd) {
int prev_q = NC_NONE;
std::cout << Translation::getByKey("lapi.noderunner.start", nd->m_sInternalName) << std::endl;
std::vector<LevelAPI::Backend::CurlProxy> proxies = {};
int i = 0;
while(i < nd->m_pProxy->m_vProxies.size()) {
proxies.push_back(nd->m_pProxy->m_vProxies[i]);
i++;
}
Backend::GDServer *server = nd->createServer();
server->setDebug(false);
std::thread rcbt(DatabaseController::node_runner_recentBot, nd);
rcbt.detach();
std::thread rtt(DatabaseController::node_runner_recount_task, nd);
rtt.detach();
Level *llevel;
auto resolver_job = [&](Node *this_node, Backend::GDServer *this_server, std::vector<LevelAPI::Backend::CurlProxy> proxy_list) {
if (!this_node->m_pPolicy->m_bEnableResolver) return;
while (true) {
if (this_node->m_uQueue->m_vResolverQueuedLevels.size() != 0) {
printf("qsize %lu\n", this_node->m_uQueue->m_vResolverQueuedLevels.size());
int i = 0;
bool dont_stop = false;
while (i < proxy_list.size() && (i < this_node->m_uQueue->m_vResolverQueuedLevels.size() || dont_stop)) {
int id = this_node->m_uQueue->m_vResolverQueuedLevels[0];
if (id == 0) {
this_node->m_uQueue->m_vResolverQueuedLevels.erase(this_node->m_uQueue->m_vResolverQueuedLevels.begin());
dont_stop = false;
break;
}
auto selected_proxy = proxy_list[i];
std::string datapath = "database/nodes/" + this_node->m_sInternalName + "/" + this_node->m_sLevelDataPath + "/" + this_node->getLevelPathRepresentation(id) + "/data.gmd2";
if (!std::filesystem::exists(datapath)) {
std::cout << "Selecting proxy " << selected_proxy.getURL() << std::endl;
std::cout << Translation::getByKey("lapi.noderunner.resolver.event.download.progress", this_node->m_sInternalName, id) << std::endl;
auto level = this_server->downloadLevel(id, selected_proxy);
if (!level || this_server->_rateLimitLength > 0) {
std::cout << "failed to download the level (proxy " << selected_proxy.getURL() << ")" << std::endl;;
dont_stop = true;
if (this_server->_rateLimitLength > 0) {
std::cout << "reason: rate limit " << this_server->_rateLimitLength << " seconds" << std::endl;
}
} else {
if (this_server->_rateLimitLength < 0) {
std::cout << "failed to download the level (proxy " << selected_proxy.getURL() << ") : level does not exist" << std::endl;;
} else {
level->m_bHasLevelString = true;
// std::cout << "level string: " << level->m_sLevelString << std::endl;;
bool exists = this_node->levelExists(id);
this_node->initLevel(level);
level->save(exists);
std::cout << Translation::getByKey("lapi.noderunner.resolver.event.download.complete", this_node->m_sInternalName, id) << std::endl;
}
delete level;
level = nullptr;
this_node->m_uQueue->m_vResolverQueuedLevels.erase(this_node->m_uQueue->m_vResolverQueuedLevels.begin());
dont_stop = false;
}
} else {
this_node->m_uQueue->m_vResolverQueuedLevels.erase(this_node->m_uQueue->m_vResolverQueuedLevels.begin());
}
i++;
}
if (this_node->m_uQueue->m_vResolverQueuedLevels.size() != 0 && !this_node->m_pPolicy->m_bUseProxyOnly) {
int id = this_node->m_uQueue->m_vResolverQueuedLevels[0];
std::string datapath = "database/nodes/" + this_node->m_sInternalName + "/" + this_node->m_sLevelDataPath + "/Level_" + std::to_string(id) + "/data.gmd2";
if (!std::filesystem::exists(datapath)) {
std::cout << Translation::getByKey("lapi.noderunner.resolver.event.download.progress", this_node->m_sInternalName, id) << std::endl;
auto level = this_server->downloadLevel(id);
if (!level || this_server->_rateLimitLength > 0) {
std::cout << "failed to download the level" << std::endl;
if (level != nullptr) {
std::cout << "reason: rate limit " << this_server->_rateLimitLength << " seconds" << std::endl;
}
} else {
if (level == nullptr) {
std::cout << "failed to download the level : level does not exist" << std::endl;;
} else {
level->m_bHasLevelString = true;
// std::cout << "level string: " << level->m_sLevelString << std::endl;;
bool exists = this_node->levelExists(id);
this_node->initLevel(level);
level->save(exists);
std::cout << Translation::getByKey("lapi.noderunner.resolver.event.download.complete", this_node->m_sInternalName, id) << std::endl;
}
delete level;
level = nullptr;
this_node->m_uQueue->m_vResolverQueuedLevels.erase(this_node->m_uQueue->m_vResolverQueuedLevels.begin());
}
} else {
this_node->m_uQueue->m_vResolverQueuedLevels.erase(this_node->m_uQueue->m_vResolverQueuedLevels.begin());
}
}
}
std::this_thread::sleep_for(std::chrono::seconds((int)this_node->m_pPolicy->m_nResolverInterval));
}
};
std::thread resolver_thread(resolver_job, nd, server, proxies);
resolver_thread.detach();
auto user_job = [=](std::string uid) {
if(!nd->m_pPolicy->m_bNoOutput) {
std::cout << Translation::getByKey("lapi.noderunner.downloader.user.fetch", nd->m_sInternalName) << std::endl;
}
int pages = 6553; //65530 levels max
int i = 0;
while(i < pages) {
auto levels = server->getLevelsBySearchType(5, uid, i);
std::cout << "Got response for " << uid << std::endl;
if(levels.size() == 0) {
i = pages + 1;
} else {
int q = 0;
int skipped = 0;
while(q < levels.size()) {
int levelid;
std::string levelname;
levelid = levels[q]->m_nLevelID;
levelname = levels[q]->m_sLevelName;
levels[q]->m_sLinkedNode = nd->m_sInternalName;
if(!nd->levelExists(levelid)) {
// nd->m_uQueue->m_vResolverQueuedLevels.push_back(levelid);
nd->initLevel(levels[q]);
levels[q]->m_bHasLevelString = false;
levels[q]->save();
if (nd->m_pPolicy->m_bEnableResolver) {
nd->m_uQueue->m_vResolverQueuedLevels.push_back(levels[q]->m_nLevelID);
}
#ifdef _DPP_ENABLED_
if (!DatabaseController::database->m_sRegisteredCID2.empty() && DatabaseController::database->m_bBotReady && DatabaseController::database->m_pLinkedBot->m_pBot != nullptr) {
DatabaseController::database->m_pLinkedBot->m_pBot->message_create(dpp::message(
dpp::snowflake(DatabaseController::database->m_sRegisteredCID2), levels[q]->getAsEmbed(E_REGISTERED)
));
}
#endif
if(!nd->m_pPolicy->m_bNoOutput) {
std::cout << Translation::getByKey("lapi.noderunner.downloader.event.complete.noresolver", nd->m_sInternalName, levelid, levelname) << std::endl;
}
} else {
skipped++;
}
delete levels[q];
levels[q] = nullptr;
q++;
}
if (levels.size() < 10) {
i = pages + 1;
}
std::cout << Translation::getByKey("lapi.noderunner.downloader.skipped", nd->m_sInternalName, skipped) << std::endl;
levels.clear();
}
i++;
std::this_thread::sleep_for(
std::chrono::milliseconds((int)(nd->m_pPolicy->m_nQueueProcessingInterval * 1000.f))
);
}
};
goto run_again;
error_ignore:
std::this_thread::sleep_for(100ms);
if(!nd->m_pPolicy->m_bNoOutput) {
prev_q = NC_NONE;
//std::cout << Translation::getByKey("lapi.noderunner.downloader.error.queue.empty", nd->m_sInternalName) << std::endl;
}
goto run_again;
loop_readonly2:
if(!nd->m_pPolicy->m_bNoOutput) {
std::cout << Translation::getByKey("lapi.noderunner.readonly", nd->m_sInternalName) << std::endl;
}
//delete server;
loop_readonly:
if(!nd->m_pPolicy->m_bNoOutput) {
std::cout << Translation::getByKey("lapi.noderunner.readonly", nd->m_sInternalName) << std::endl;
}
while(true) {
std::this_thread::sleep_for(std::chrono::seconds(2s));
}
run_again:
nd->m_bRateLimitApplied = false;
if(nd->m_bRateLimitApplied) {
printf("rate limit happened. waiting for %d seconds\n", server->_rateLimitLength);
std::this_thread::sleep_for(
std::chrono::seconds(server->_rateLimitLength)
);
std::thread rlt(DatabaseController::node_runner_waitResolverRL, nd, server->_rateLimitLength);
rlt.detach();
}
start:
if(nd->m_uQueue->m_vCommandQueue.size() == 0) goto error_ignore;
const NodeCommandQueue &q = nd->m_uQueue->m_vCommandQueue.at(0);
nd->m_uQueue->currentState = q.m_nCommand;
prev_q = q.m_nCommand;
bool wait_after_command = true;
switch(q.m_nCommand) {
case NC_EXPERIMENT1: { // downloads levels from all gd users (or, atleast, tries to do so)
break;
if(nd->m_nExperiment1Value < 0) nd->m_nExperiment1Value = 0;
int n = nd->m_nExperiment1Value;
for(;;) {
user_job(std::to_string(n));
n++;
nd->m_nExperiment1Value = n;
}
break;
}
case NC_USER: {
if(server->m_eStatus == Backend::GSS_PERMANENT_BAN) break;
user_job(q.m_sText);
break;
}
case NC_RECENT: {
if(!nd->m_pPolicy->m_bEnableRecentTab) break;
int page = atoi(q.m_sText.c_str());
if(!nd->m_pPolicy->m_bNoOutput) {
std::cout << Translation::getByKey("lapi.noderunner.downloader.recenttab.fetch", nd->m_sInternalName) << std::endl;
}
auto levels = server->getLevelsBySearchType(4, "", page);
int i = 0;
if(server->m_eStatus == LevelAPI::Backend::GSS_PERMANENT_BAN) {
while(i < 32) {
std::cout << Translation::getByKey("lapi.noderunner.error.pban", nd->m_sInternalName) << std::endl;
i++;
}
std::cout << Translation::getByKey("lapi.noderunner.error.pban.halt", nd->m_sInternalName) << std::endl;
for(;;);
}
while(i < levels.size()) {
int levelid = levels[i]->m_nLevelID;
std::string levelname = levels[i]->m_sLevelName;
levels[i]->m_sLinkedNode = nd->m_sInternalName;
if(!nd->levelExists(levelid)) {
bool userIDExists = nd->userIDExists(levels[i]->m_nPlayerID);
auto identifier = server->getServerIdentifier();
if(!userIDExists) {
nd->m_uQueue->m_vCommandQueue.push_back({NC_USER, std::to_string(levels[i]->m_nPlayerID)});
}
nd->initLevel(levels[i]);
levels[i]->m_bHasLevelString = false;
levels[i]->save();
// nd->m_jLastDownloadedLevel = levels[i]->_jsonObject;
// recent_downloadedids.push_back(levelid);
// if(!nd->m_bRateLimitApplied && nd->m_pPolicy->m_bEnableResolver) {
// nd->m_uQueue->m_vCommandQueue.push_back(new NodeCommandQueue(NC_ID, std::to_string(levels[i]->m_nLevelID)));
// }
#ifdef _DPP_ENABLED_
if (!DatabaseController::database->m_sRegisteredCID.empty() && DatabaseController::database->m_bBotReady && DatabaseController::database->m_pLinkedBot != nullptr && DatabaseController::database->m_pLinkedBot->m_pBot != nullptr) {
DatabaseController::database->m_pLinkedBot->m_pBot->message_create(dpp::message(
dpp::snowflake(DatabaseController::database->m_sRegisteredCID), levels[i]->getAsEmbed(E_RECENT)
));
}
#endif
if(!nd->m_pPolicy->m_bNoOutput) {
std::cout << Translation::getByKey("lapi.noderunner.downloader.event.complete.noresolver", nd->m_sInternalName, levelid, levelname) << std::endl;
}
// printf("level %d does not exist\n", levelid);
if(nd->m_pPolicy->m_bEnableResolver) nd->m_uQueue->m_vResolverQueuedLevels.push_back(levelid);
} else {
if(nd->m_pPolicy->m_bEnableResolver && !levels[i]->m_bHasLevelString) {
// nd->m_uQueue->m_vCommandQueue.push_back(new NodeCommandQueue(NC_ID, std::to_string(levels[i]->m_nLevelID)));
}
}
delete levels[i];
levels[i] = nullptr;
// if(!nd->m_pPolicy->m_bNoOutput) std::cout << "[LevelAPI downloader " << nd->m_sInternalName << "] Level saved without level string: " << levelid << " \"" << levelname << "\"" << std::endl;
i++;
}
levels.clear();
break;
}
case NC_ID: {
if (std::stoi(q.m_sText) == 0) break;
nd->m_uQueue->m_vResolverQueuedLevels.push_back(std::stoi(q.m_sText));
}
default:
case NC_NONE:
case NC_IDLE: {
wait_after_command = false;
break;
}
case NC_EXPERIMENT2: {
std::cout << "performing experiment 2\n";
std::vector<std::string> objects = splitString(q.m_sText.c_str(), ':');
int pages = 0;
int offset = 0;
if (objects.size() >= 1) {
try {
pages = std::stoi(objects[0]);
} catch (std::invalid_argument &e) {
printf("inv arg 0: %s\n", e.what());
}
}
if (objects.size() >= 2) {
try {
offset = std::stoi(objects[1]);
} catch (std::invalid_argument &e) {
printf("inv arg 1: %s\n", e.what());
}
}
for (int i = 0; i < pages; i++) {
nd->m_uQueue->m_vCommandQueue.push_back({NC_22REGION_META, std::to_string(i + offset)});
}
std::cout << "performed experiment 2: " << pages << " NC_22REGION_META commands with offset " << offset << " were added\n";
break;
}
case NC_22REGION_META: {
if (server->getGameVersion() < 22) {
std::cout << "NC_22REGION_META command is not supported on " << server->getGameVersion() << " server\n";
break;
}
auto srv22 = dynamic_cast<Backend::GDServer_BoomlingsLike22 *>(server);
if (!srv22) {
std::cout << "NC_22REGION_META command is not supported on " << server->getServerIdentifier() << " server\n";
break;
}
int offset = 0;
try {
offset = std::stoi(q.m_sText);
} catch (std::invalid_argument &e) {
std::cout << "NC_22REGION_META: invalid offset " << q.m_sText << ": " << e.what() << "\n";
}
std::vector<int> vlist;
int max = 100;
for (int i = 0; i < max; i++) {
vlist.push_back(i + (max * offset));
}
std::optional<Backend::CurlProxy> proxy = std::nullopt;
if (nd->m_pProxy && nd->m_pProxy->m_vProxies.size() != 0) {
std::cout << "NC_22REGION_META selected proxy " << nd->m_pProxy->m_vProxies[0] << "\n";
proxy = Backend::CurlProxy(nd->m_pProxy->m_vProxies[0]);
}
auto list = srv22->fetchListOfLevels(vlist, 0, proxy);
if (server->m_eStatus == Backend::GDServerStatus::GSS_OFFLINE) {
std::cout << "NC_22REGION_META cannot reach server\n";
break;
} else if (server->m_eStatus == Backend::GDServerStatus::GSS_PERMANENT_BAN) {
std::cout << "NC_22REGION_META cannot reach server because host ip is permabanned\n";
while (1) {
std::this_thread::sleep_for(5s);
}
}
for (Level *lvl : list) {
if (nd->levelExists(lvl->m_nLevelID)) {
continue;
}
nd->initLevel(lvl);
lvl->m_bHasLevelString = false;
lvl->save();
if(!nd->m_pPolicy->m_bNoOutput) {
std::cout << Translation::getByKey("lapi.noderunner.downloader.event.complete.noresolver", nd->m_sInternalName, lvl->m_nLevelID, lvl->m_sLevelName) << std::endl;
}
}
srv22->destroyLevelVector(list);
break;
}
}
nd->m_uQueue->m_vCommandQueue.erase(nd->m_uQueue->m_vCommandQueue.begin());
// nd->m_uQueue->save();
nd->m_uQueue->currentState = NC_NONE;
goto run_again;
}