-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTankiCodeBackupCpp.cpp
More file actions
190 lines (149 loc) · 5.42 KB
/
TankiCodeBackupCpp.cpp
File metadata and controls
190 lines (149 loc) · 5.42 KB
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
#include <iostream>
#include <fstream>
#include <string>
#include <regex>
#include <thread>
#include <filesystem>
#include <syncstream>
#include <barrier>
#include <semaphore>
#include <cpr/cpr.h>
#define sout std::osyncstream(std::cout)
#define serr std::osyncstream(std::cerr)
using namespace std;
namespace fs = filesystem;
constexpr auto MAIN_JS_PATTERN = R"(src="([^"]*main\.[\w\d]{8}\.js)\")";
constexpr int MAX_PARALLEL_DOWNLOADS = 2;
string baseUrlForServer(int s) {
return s == 0 ? "https://tankionline.com"
: "https://public-deploy" + to_string(s) + ".test-eu.tankionline.com";
}
optional<string> fetchIndexHtml(int server, const string& baseUrl) {
string url = baseUrl + (server == 0 ? "/play/index.html" : "/browser-public/index.html");
auto resp = cpr::Get(cpr::Url{ url });
if (resp.status_code == 200) {
return resp.text;
}
return nullopt;
}
// currently unused (TODO: implement downloadStatusJs)
optional<string> fetchBalancer(int server, const string& baseUrl) {
string url = baseUrl + (server == 0 ? "/s/status.js" : "/balancer");
auto resp = cpr::Get(cpr::Url{ url });
if (resp.status_code == 200) {
return resp.text;
}
return nullopt;
}
optional<string> extractMainJSUrl(const string& html) {
static const regex pattern(MAIN_JS_PATTERN);
smatch match;
if (regex_search(html, match, pattern)) {
return match[1].str();
}
return nullopt;
}
pair<string, string> buildLocalPath(int server, const string& mainJS) {
string serverDir = to_string(server);
fs::create_directories(serverDir);
string fileName = fs::path(mainJS).filename().string();
string localPath = "./" + serverDir + "/" + fileName;
return { localPath, fileName };
}
counting_semaphore<MAX_PARALLEL_DOWNLOADS> semaphore1{ MAX_PARALLEL_DOWNLOADS };
void downloadMap(const string& url, const string& path, const string& fileName) {
semaphore1.acquire();
cpr::Response resp = cpr::Get(cpr::Url{ url });
semaphore1.release();
if (resp.status_code != 403 && resp.status_code != 404) {
ofstream file(path, ios::binary);
file.write(resp.text.data(), resp.text.size());
sout << "MAP FOUND!!! (" << fileName << "): " << resp.status_code << "\n";
}
else {
sout << "No Map (" << fileName << "): " << resp.status_code << "\n";
}
}
void downloadFile(const string& url, const string& path) {
semaphore1.acquire();
cpr::Response resp = cpr::Get(cpr::Url{ url });
semaphore1.release();
if (resp.status_code == 200) {
ofstream file(path, ios::binary);
file.write(resp.text.data(), resp.text.size());
sout << "Finished " << path << "\n";
}
else {
serr << "Download failed: " << url << " (" << resp.status_code << ")\n";
}
}
int phase = 1;
barrier barrier1(11, []() noexcept {
switch (phase) {
case 1: cout << "\nDownloading missing js files...\n"; break;
case 2: cout << "\nTrying to download maps...\n"; break;
}
phase++;
});
void downloadServerMainJS(int server) {
string baseUrl = baseUrlForServer(server);
auto indexHtmlOpt = fetchIndexHtml(server, baseUrl);
if (!indexHtmlOpt) return barrier1.arrive_and_drop();;
auto mainJsUrlOpt = extractMainJSUrl(*indexHtmlOpt);
if (!mainJsUrlOpt) {
serr << "JS not found in response from " << baseUrl << "\n";
return barrier1.arrive_and_drop();;
}
// stupid ahh logic because tanki is updating rn, so gotta support
// old (/static/js/...) and new version (https://absolutepath...)
// so please delete this workaround when it's on new version everywhere
bool isNewVersion = mainJsUrlOpt->starts_with("http");
string realMainJsUrl = isNewVersion ? *mainJsUrlOpt : baseUrl + *mainJsUrlOpt;
auto [localPath, fileName] = buildLocalPath(server, realMainJsUrl);
string mapUrl = realMainJsUrl + ".map";
string mapPath = localPath + ".map";
string mapFilename = fileName + ".map";
// new version text is a temporary feature so you can see which
// servers have been updated (see above comment)
string newVersionText = isNewVersion ? " (new version)" : " (old version)";
if (fs::exists(localPath)) {
sout << baseUrl << ": " << fileName << newVersionText << "\nNo Changes\n";
barrier1.arrive_and_wait();
}
else {
sout << baseUrl << ": " << fileName << newVersionText << "\nUpdate!\n";
barrier1.arrive_and_wait();
downloadFile(realMainJsUrl, localPath);
}
barrier1.arrive_and_wait();
if (!fs::exists(mapPath)) {
downloadMap(mapUrl, mapPath, mapFilename);
}
else {
sout << "Map already downloaded. (" << mapFilename << ")\n";
}
}
// TODO: implement downloadStatusJs
void downloadStatusJs() {
int server = 0;
string baseUrl = baseUrlForServer(server);
auto balancerOpt = fetchBalancer(server, baseUrl);
if (!balancerOpt) {
serr << "Balancer not found from " << baseUrl << "\n";
return;
}
}
int main() {
{
vector<jthread> threads;
for (int i = 0; i <= 10; ++i) {
// server 0 is alias for /play server
// servers 1-10 are /browser-public test servers
threads.emplace_back(downloadServerMainJS, i);
}
// wait for threads to join (jthread joins automatically when going out of scope)
}
sout << "\nDone.\n";
this_thread::sleep_for(chrono::seconds(10));
return 0;
}