-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptions.cpp
More file actions
265 lines (232 loc) · 7.19 KB
/
Copy pathoptions.cpp
File metadata and controls
265 lines (232 loc) · 7.19 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
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
#include <iostream>
#include <cxxopts.hpp>
#include "logging.h"
#include "options.h"
/*!
*/
Options::Options(int argc, char* argv[])
{
cxxopts::Options options("elogen", "ELO Generator for CnCNet ladder games");
options.add_options()
("h,help", "Show help.")
("d,dry-run", "Run without writing any results (test mode).")
("s,statistics", "Export additional player and map statistics.")
("cncnet-duplicates", "Use duplicate detection based on IP just like cncnet does.")
("no-duplicates", "No duplicate detection at all. Every user id gets is own elo.")
("e,end", "Set the end date (format: YYYY-MM-DD). Only games before this date are considered.",
cxxopts::value<std::string>())
("i,timeshift", "Add a time shift for all games to fix time zone issues.",
cxxopts::value<int>()->default_value("0"))
("l,log-level", "Set the log level (debug, verbose, info, warning, error, critical, fatal).",
cxxopts::value<std::string>()->default_value("verbose"))
("m,gamemode", "Set the game mode. Every available ladder abbreviation is valid.",
cxxopts::value<std::string>())
("o,output-dir", "Output directory for generated JSON files.",
cxxopts::value<std::string>())
("H,host", "Host name for sql connection. Overrides environment variable MYSQL_HOST. If both not set, localhost is used.",
cxxopts::value<std::string>())
("P,port", "Port for sql connection. Overrides environment variable MYSQL_PORT. If both not set, 3307 is used.",
cxxopts::value<uint32_t>())
("p,password", "Password for sql connection. Overrides environment variable MYSQL_PASSWORD.",
cxxopts::value<std::string>())
("u,user", "User name for sql connection. Overrides environment variable MYSQL_USER.",
cxxopts::value<std::string>())
("t,tournament-games", "Add tournament games from this file.",
cxxopts::value<std::string>());
auto result = options.parse(argc, argv);
if (result.count("help"))
{
std::cout << options.help() << "\n";
setQuitWithErrorCode(0);
return;
}
if (result.count("log-level"))
{
Log::Level level = Log::fromString(result["log-level"].as<std::string>());
if (level == Log::NoLog)
{
std::cerr << "Unknown log level." << std::endl;
setQuitWithErrorCode(1);
return;
}
Log::setGlobalLogLevel(level);
}
outputDirectory = std::filesystem::current_path();
if (result.count("output-dir"))
{
outputDirectory = result["output-dir"].as<std::string>();
if (!std::filesystem::is_directory(outputDirectory))
{
std::cerr << "The directory '" << outputDirectory << "' does not exist." << std::endl;
setQuitWithErrorCode(1);
return;
}
}
if (result.count("tournament-games"))
{
tournamentFile = result["tournament-games"].as<std::string>();
if (!std::filesystem::exists(tournamentFile))
{
std::cerr << "The file '" << tournamentFile << "' does not exist." << std::endl;
setQuitWithErrorCode(1);
return;
}
}
gameMode = gamemodes::Unknown;
if (!result.count("gamemode"))
{
std::cout << "Missing game mode. Use option --gamemode to specify. Fully supported game "
"modes are blitz, ra2, yr, and blitz-2v2, but others might work, too." << std::endl;
setQuitWithErrorCode(1);
}
else
{
ladderAbbreviation = result["gamemode"].as<std::string>();
if (ladderAbbreviation == "ra2-new-maps")
{
std::cout << "Ladder 'ra2-new-maps' is usually integrated in the ra2 ladder, but computing "
<< " elo for ra2-new-maps will work." << std::endl;
}
gameMode = gamemodes::toGameMode(ladderAbbreviation);
}
if (result.count("port") > 0)
{
port = result["port"].as<uint32_t>();
}
if (result.count("host") > 0)
{
host = result["host"].as<std::string>();
}
if (result.count("user") > 0)
{
username = result["user"].as<std::string>();
}
if (result.count("password") > 0)
{
password = result["password"].as<std::string>();
}
dryRun = result["dry-run"].as<bool>();
exportFullStats = result["statistics"].as<bool>();
cncnetDuplicates = result["cncnet-duplicates"].as<bool>();
noDuplicates = result["no-duplicates"].as<bool>();
timeShiftInHours = result["timeshift"].as<int>();
if (result.count("end"))
{
std::string input = result["end"].as<std::string>();
std::istringstream ss(input);
std::tm tm = {};
ss >> std::get_time(&tm, "%Y-%m-%d");
if (ss.fail())
{
std::cerr << "Invalid date format. Use YYYY-MM-DD." << std::endl;
setQuitWithErrorCode(1);
return;
}
std::chrono::year_month_day ymd{
std::chrono::year{tm.tm_year + 1900},
std::chrono::month{tm.tm_mon + 1U},
std::chrono::day{tm.tm_mday}
};
endDate = std::chrono::sys_days{ymd};
}
else
{
// Default end date is today.
endDate = std::chrono::floor<std::chrono::days>(std::chrono::system_clock::now());
}
if (mySqlUser().empty())
{
std::cerr << "No MySql user. Either use --user or set MYSQL_USER." << std::endl;
setQuitWithErrorCode(1);
return;
}
if (mySqlPassword().empty())
{
std::cerr << "No MySql password. Either use --password or set MYSQL_PASSWORD." << std::endl;
setQuitWithErrorCode(1);
return;
}
}
/*!
*/
void Options::setQuitWithErrorCode(int returnValue)
{
_returnValue = returnValue;
_quit = true;
}
/*!
*/
bool Options::quit() const
{
return _quit;
}
/*!
*/
int Options::returnValue() const
{
return _returnValue;
}
/*!
*/
std::string Options::mySqlPassword() const
{
if (!password.empty())
{
return password;
}
else
{
const char *envPassword = std::getenv("MYSQL_PASSWORD");
return (envPassword != nullptr) ? std::string(envPassword) : "";
}
}
/*!
*/
std::string Options::mySqlUser() const
{
if (!username.empty())
{
return username;
}
else
{
const char *envUser = std::getenv("MYSQL_USER");
return (envUser != nullptr) ? std::string(envUser) : "";
}
}
/*!
*/
std::string Options::mySqlHost() const
{
if (!host.empty())
{
return host;
}
else
{
const char *envHost = std::getenv("MYSQL_HOST");
return (envHost != nullptr) ? std::string(envHost) : "localhost";
}
}
/*!
*/
uint32_t Options::mySqlPort() const
{
if (port != std::numeric_limits<uint32_t>::max())
{
return port;
}
else
{
const char *envPort = std::getenv("MYSQL_PORT");
try
{
return (envPort != nullptr) ? static_cast<uint32_t>(std::stoul(std::string(envPort)))
: 3307;
}
catch (const std::exception&)
{
return 3307;
}
}
}