forked from wonderingabout/gtp2ogs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.js
373 lines (306 loc) · 13.6 KB
/
config.js
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
// vim: tw=120 softtabstop=4 shiftwidth=4
// see: https://github.com/online-go/gtp2ogs/blob/devel/docs/DEV.md#no-console-eslint-rule-in-configjs
/* eslint-disable no-console */
const { getArgNamesGRU } = require('./options/getArgNamesGRU');
const { getOptionName } = require('./options/getOptionName');
const { getRankedUnranked } = require('./options/getRankedUnranked');
const { getRankedUnrankedUnderscored } = require('./options/getRankedUnrankedUnderscored');
const { droppedOptions, ogsPvAIs, rankedUnrankedOptions } = require('./constants');
exportInitialConfig();
exports.updateFromArgv = function(argv) {
// console messages
// A- greeting and debug status
const debugStatus = argv.debug ? "DEBUG: ON\n" : "";
console.log(`\ngtp2ogs version 6.0.1\n--------------------\n- For changelog or latest devel updates, please visit https://github.com/online-go/gtp2ogs/tree/devel\n${debugStatus}`);
// B - test unsupported argv
testBotCommandArgvIsValid(argv);
testDroppedArgv(droppedOptions, argv);
testConflictingOptions("rankedonly", "unrankedonly", argv);
testConflictingOptions("persist", "persistnoncorr", argv);
testConflictingOptions("rejectnew", "rejectnewfile", argv);
ensureSupportedOgspvAI(argv.ogspv, ogsPvAIs);
testRankedUnrankedOptions(rankedUnrankedOptions, argv);
// C - set general/ranked/unranked options defaults
// For general/ranked/unranked options, do not add a default using .default of yargs, add it later in the
// code if no ranked arg nor unranked arg are used, else we would be force using the general arg regardless of
// botadmin using the ranked and/or unranked arg(s), triggering the no 3 args at the same time error.
setRankedUnrankedOptionsDefaults(rankedUnrankedOptions, argv);
// EXPORTS FROM ARGV
// 0) Export everything in argv first
for (const k in argv) {
exports[k] = argv[k];
}
// 1) Add and Modify/Adjust exports
if (argv.debug) {
exports.DEBUG = true;
}
if (argv.logfile !== undefined) {
exportLogfileFilename(argv.logfile, argv.debug);
}
exportSecondsAsMilliseconds("timeout", argv);
exportSecondsAsMilliseconds("startupbuffer", argv);
if (argv.beta) {
exports.host = 'beta.online-go.com';
}
exportNoAutoHandicapsIfMinHandicapsArePositive(argv);
if (argv.ogspv) {
exports.ogspv = argv.ogspv.toUpperCase();
}
if (argv.rejectnew) {
// A bot never accepting new games shouldn't be appearing in the dropdown, polite to our users.
exports.hidden = true;
}
exports.bot_command = argv._;
// 2) specific ranked/unranked options exports
exportMinMaxRanks(argv);
exportBans(argv);
exportAllowedGroups(argv);
// console messages
// C - test exports warnings
warnIfNopauseIsMissing("ranked");
warnIfNopauseIsMissing("unranked");
console.log("\n");
}
function exportInitialConfig() {
exportInitialConfigUnderscored("");
exportInitialConfigUnderscored("_ranked");
exportInitialConfigUnderscored("_unranked");
exports.start_date = new Date();
}
function exportInitialConfigUnderscored(rankedUnrankedUnderscored) {
exports[`banned_users${rankedUnrankedUnderscored}`] = {};
exports[`allow_all_boardsizes${rankedUnrankedUnderscored}`] = false;
exports[`allowed_boardsizes${rankedUnrankedUnderscored}`] = [];
exports[`allow_all_komis${rankedUnrankedUnderscored}`] = false;
exports[`allowed_komis${rankedUnrankedUnderscored}`] = [];
exports[`allowed_speeds${rankedUnrankedUnderscored}`] = {};
exports[`allow_all_speeds${rankedUnrankedUnderscored}`] = false;
exports[`allowed_timecontrols${rankedUnrankedUnderscored}`] = {};
exports[`allow_all_timecontrols${rankedUnrankedUnderscored}`] = false;
}
function testRankedUnrankedOptions(rankedUnrankedOptions, argv) {
for (const option of rankedUnrankedOptions) {
const [general, ranked, unranked] = getArgNamesGRU(option.name);
// check undefined specifically to handle valid values such as 0 or null which are tested false
if (argv[general] !== undefined) {
if (argv[ranked] !== undefined) {
throw `Cannot use --${general} and --${ranked} at the same time.\nFor ranked games, use either --${general} or --${ranked}, or do not use anything to switch to default settings for this option.`;
}
if (argv[unranked] !== undefined) {
throw `Cannot use --${general} and --${unranked} at the same time.\nFor unranked games, use either --${general} or --${unranked}, or do not use anything to switch to default settings for this option.`;
}
}
}
}
function testBotCommandArgvIsValid(argv) {
if (argv._ === undefined) {
throw "Missing bot command.";
}
const parsedBotCommand = JSON.stringify(argv._);
if (!Array.isArray(argv._)) {
throw `Bot command (detected as ${parsedBotCommand}) was not correctly parsed as an array of parameters, please check your syntax ( -- ).`;
}
if (argv._.length === 0) {
throw `Bot command (detected as ${parsedBotCommand}) cannot be empty, please use at least one element in your bot command which should be the AI executable (ex: lz.exe).`;
}
}
function testDroppedArgv(droppedOptions, argv) {
for (const [oldNames, newName] of droppedOptions) {
for (const oldName of oldNames) {
if (argv[oldName]) {
if (newName !== undefined) throw `Dropped: --${oldName} is no longer supported, use --${newName} instead.`;
throw `Dropped: --${oldName} is no longer supported.`;
}
}
}
for (const argName of getArgNamesGRU("komis")) {
if (argv[argName]) {
for (const komi of ["auto","null"]) {
if (argv[argName].split(",").includes(komi)) {
throw `Dropped: --${argName} ${komi} is no longer supported, use --${argName} automatic instead`;
}
}
}
}
}
function testConflictingOptions(optionNameFirst, optionNameSecond, argv) {
if (argv[optionNameFirst] && argv[optionNameSecond]) {
throw `Please choose either --${optionNameFirst} or --${optionNameSecond}, not both.`;
}
}
function ensureSupportedOgspvAI(ogspv, ogsPvAIs) {
// being case tolerant
if (!ogspv) return;
const upperCaseOgsPv = ogspv.toUpperCase();
const upperCaseAIs = ogsPvAIs.map(e => e.toUpperCase());
if (!upperCaseAIs.includes(upperCaseOgsPv)) {
throw `Unsupported --ogspv option ${ogspv}.\nSupported options are ${ogsPvAIs.join(', ')}`;
}
}
function setRankedUnrankedOptionsDefaults(rankedUnrankedOptions, argv) {
for (const option of rankedUnrankedOptions) {
if (!("default" in option)) continue;
const [general, ranked, unranked] = getArgNamesGRU(option.name);
if (argv[general] === undefined) {
if ((argv[ranked] === undefined) && (argv[unranked] === undefined)) {
argv[general] = option.default;
} else if (argv[ranked] !== undefined && argv[unranked] === undefined) {
// also set the opposite arg currently undefined to the default, more convenient for botadmin
argv[unranked] = option.default;
} else if (argv[ranked] === undefined && argv[unranked] !== undefined) {
// also set the opposite arg currently undefined to the default, more convenient for botadmin
argv[ranked] = option.default;
}
}
}
}
function getLogfileFilename(argvLogfile) {
return (argvLogfile === "" ? `gtp2ogs-logfile-${exports.start_date.toISOString()}` : argvLogfile);
}
function getValidFilename(filename) {
// convert any other character than letters (A-Z a-z), numbers (0-9), hypens (-), underscore (_),
// space ( ), dot (.) to a hyphen (-)
return filename.replace(/[^\w\-. ]/g, "-");
}
function exportSecondsAsMilliseconds(optionName, argv) {
if (argv[optionName] !== undefined) {
// Convert some times in seconds to milliseconds once here so we don't need
// to do it each time it is used later.
exports[optionName] = argv[optionName] * 1000;
}
}
function exportNoAutoHandicapIfMinHandicapIsPositive(rankedUnranked, argv){
// Setting minimum handicap higher than -1 has the consequence of rejecting
// challenge that have automatic handicap in connection.js (-1 lower than
// minimum handicap 0 or higher, rejecting challenge)
if (argv[`minhandicap${rankedUnranked}`] > -1) {
exports[`noautohandicap${rankedUnranked}`] = true;
}
}
function exportNoAutoHandicapsIfMinHandicapsArePositive(argv) {
exportNoAutoHandicapIfMinHandicapIsPositive("", argv);
exportNoAutoHandicapIfMinHandicapIsPositive("ranked", argv);
exportNoAutoHandicapIfMinHandicapIsPositive("unranked", argv);
}
function exportLogfileFilename(argvLogfile, argvDebug) {
const filename = getLogfileFilename(argvLogfile);
const validFilename = getValidFilename(filename);
if (argvDebug && filename !== validFilename) {
console.log (`Logfile name "${filename}" has been automatically renamed to "${validFilename}".\nValid logfile name can only be composed of letters (A-Z a-z), numbers (0-9), hyphens (-), underscores (_), spaces ( ), dots (.).\n`);
}
exports.logfile = validFilename;
}
function parseRank(arg) {
if (arg) {
const re = /(\d+)([kdp])/;
const results = arg.toLowerCase().match(re);
if (results) {
if (results[2] === "k") {
return (30 - parseInt(results[1]));
} else if (results[2] === "d") {
return (30 - 1 + parseInt(results[1]));
} else if (results[2] === "p") {
return (36 + parseInt(results[1]));
}
} else {
throw `error: could not parse rank -${arg}-`;
}
}
}
function processRankExport(argName, argv) {
const arg = argv[argName];
if (arg) {
exports[argName] = parseRank(arg);
}
}
function exportMinMaxRanks(argv) {
processRankExport("minrank", argv);
processRankExport("minrankranked", argv);
processRankExport("minrankunranked", argv);
processRankExport("maxrank", argv);
processRankExport("maxrankranked", argv);
processRankExport("maxrankunranked", argv);
}
function processBansExport(argName, argv) {
const arg = argv[argName];
const rankedUnrankedUnderscored = getRankedUnrankedUnderscored(argName);
if (arg) {
const bans = arg.split(',');
for (const bannedUser of bans) {
exports[`banned_users${rankedUnrankedUnderscored}`][bannedUser] = true;
}
}
}
function exportBans(argv) {
processBansExport("bans", argv);
processBansExport("bansranked", argv);
processBansExport("bansunranked", argv);
}
function processBoardsizesExport(argName, argv) {
const arg = argv[argName];
if (arg) {
const rankedUnranked = getRankedUnranked(argName);
const rankedUnrankedUnderscored = getRankedUnrankedUnderscored(rankedUnranked);
const boardsizes = arg.split(',');
for (const boardsize of boardsizes) {
if (boardsize === "all") {
exports[`allow_all_boardsizes${rankedUnrankedUnderscored}`] = true;
} else {
exports[`allowed_boardsizes${rankedUnrankedUnderscored}`][boardsize] = true;
}
}
}
}
function processKomisExport(argName, argv) {
const arg = argv[argName];
if (arg) {
const rankedUnranked = getRankedUnranked(argName);
const rankedUnrankedUnderscored = getRankedUnrankedUnderscored(rankedUnranked);
const komis = arg.split(',');
for (const komi of komis) {
if (komi === "all") {
exports[`allow_all_komis${rankedUnrankedUnderscored}`] = true;
} else if (komi === "automatic") {
exports[`allowed_komis${rankedUnrankedUnderscored}`][null] = true;
} else {
exports[`allowed_komis${rankedUnrankedUnderscored}`][komi] = true;
}
}
}
}
function processAllowedGroupExport(argName, argv) {
const arg = argv[argName];
if (arg) {
const optionName = getOptionName(argName);
const rankedUnranked = getRankedUnranked(argName);
const rankedUnrankedUnderscored = getRankedUnrankedUnderscored(rankedUnranked);
const allowedValues = arg.split(',');
for (const allowedValue of allowedValues) {
if (allowedValue === "all") {
exports[`allow_all_${optionName}${rankedUnrankedUnderscored}`] = true;
} else {
exports[`allowed_${optionName}${rankedUnrankedUnderscored}`][allowedValue] = true;
}
}
}
}
function exportAllowedGroups(argv) {
processBoardsizesExport("boardsizes", argv);
processBoardsizesExport("boardsizesranked", argv);
processBoardsizesExport("boardsizesunranked", argv);
processKomisExport("komis", argv);
processKomisExport("komisranked", argv);
processKomisExport("komisunranked", argv);
processAllowedGroupExport("speeds", argv);
processAllowedGroupExport("speedsranked", argv);
processAllowedGroupExport("speedsunranked", argv);
processAllowedGroupExport("timecontrols", argv);
processAllowedGroupExport("timecontrolsranked", argv);
processAllowedGroupExport("timecontrolsunranked", argv);
}
function warnIfNopauseIsMissing(rankedUnranked) {
// avoid infinite games
if (!exports.nopause && !exports[`nopause${rankedUnranked}`]) {
console.log(`Warning: No --nopause nor --nopause${rankedUnranked}, ${rankedUnranked} games are likely to last forever`);
}
}