-
Notifications
You must be signed in to change notification settings - Fork 18
/
launcher.js
633 lines (540 loc) · 18.2 KB
/
launcher.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
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
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
/**
* # Launcher file for nodeGame Server
* Copyright(c) 2011-2020 Stefano Balietti
* MIT Licensed
*
* Load configuration options and start the server
*
* http://www.nodegame.org
*/
"use strict";
// Modules.
const fs = require('fs');
const path = require('path');
const exec = require('child_process').exec;
const J = require('JSUS').JSUS;
// TODO: refactor, eliminate var; check new Commander options.
// Load commander.
var program = require('commander');
// Load the ServerNode class.
var ServerNode = require('nodegame-server').ServerNode;
// nodeGame version.
var version = require('./package.json').version;
// ServerNode object.
var sn;
// ServerNode options.
var options;
// Other local options.
var confDir, logDir, gamesDir, debug, infoQuery, runTests;
var nClients, clientType, killServer, auth, wait, port;
var codesDb;
var cert, key;
// Warn message.
var ignoredOptions;
// Helper variables.
// Split input parameters.
function list(val) {
return val.split(',');
}
// Go!
ignoredOptions = [];
// Defaults.
confDir = path.resolve(__dirname, 'conf');
logDir = path.resolve(__dirname, 'log');
gamesDir = path.resolve(__dirname, 'games');
debug = undefined;
infoQuery = undefined;
nClients = 4;
clientType = 'autoplay';
runTests = false;
killServer = false;
// Commander.
program
.version(version)
// Specify a configuration file (other inline-options will be ignored).
.option('-C, --config [confFile]',
'Specifies a configuration file to load')
// Specify inline options (more limited than a conf file, but practical).
.option('-c, --confDir <confDir>',
'Sets the configuration directory')
.option('-l, --logDir <logDir>',
'Sets the log directory')
.option('-L, --logLevel <logDir>',
'Sets the log level. Values: error(default)|warn|info|silly')
.option('-g, --gamesDir <gamesDir>',
'Sets the games directory')
.option('-d, --debug',
'Enables the debug mode')
.option('-i, --infoQuery [false]',
'Enables getting information via query-string ?q=')
.option('-b, --build [components]',
'Rebuilds the specified components', list)
.option('-s, --ssl [path-to-ssl-dir]',
'Starts the server with SSL encryption')
.option('-f, --default [channel]',
'Sets the default channel')
.option('-P, --port [port]',
'Sets the port of the server')
// Connect phantoms.
.option('-p, --phantoms <channel>',
'Connect phantoms to the specified channel **DISCONTINUED**')
// .option('-n, --nClients <n>',
// 'Sets the number of clients phantoms to connect (default: 4)')
// .option('-t, --clientType <t>',
// 'Sets the client type of connecting phantoms (default: autoplay)')
// .option('-T, --runTests',
// 'Run tests after all phantoms are game-over ' +
// '(overwrites settings.js in test/)')
// .option('-k, --killServer',
// 'Kill server after all phantoms are game-over')
// .option('-a --auth [option]',
// 'Phantoms auth options. Values: new(default)|createNew|' +
// 'nextAvailable|next|code|id:code&pwd:password|file:path/to/file.')
// .option('-w --wait [milliseconds]',
// 'Waits before connecting the next phantom. Default: 1000')
.parse(process.argv);
// User options (Commander >= 7).
let opts = program.opts();
if (opts.phantoms) {
console.log('***Err: option --phantoms no longer supported. ' +
'PhantomJS support discontinued.');
return false;
}
if (opts.confFile) {
if (!fs.existsSync(opts.confFile)) {
return printErr('--confFile ' + opts.confFile + ' not found.');
}
options = require(opts.confFile);
if ('object' !== typeof options) {
return printErr('--confFile ' + opts.confFile + ' did not return ' +
'a configuration object.');
}
if (opts.confDir) ignoredOptions.push('--confDir');
if (opts.logDir) ignoredOptions.push('--logDir');
if (opts.gamesDir) ignoredOptions.push('--gamesDir');
if (opts.debug) ignoredOptions.push('--debug');
if (opts.infoQuery) ignoredOptions.push('--infoQuery');
}
else {
options = {
// Additional conf directory.
confDir: confDir,
// Log Dir
logDir: logDir,
servernode: function(servernode) {
// Special configuration for the ServerNode object.
// Adds a new game directory (Default is nodegame-server/games).
servernode.gamesDirs.push(gamesDir);
// Sets the debug mode, exceptions will be thrown, if TRUE.
if ('undefined' !== typeof debug) {
servernode.debug = debug;
}
// Can get information from /?q=, if TRUE
if ('undefined' !== typeof infoQuery) {
servernode.enableInfoQuery = infoQuery;
}
// Basepath (without trailing slash).
// servernode.basepath = '/mybasepath';
return true;
},
// http: function(http) {
// // Special configuration for Express goes here.
// return true;
// },
// sio: function(sio) {
// // Special configuration for Socket.Io goes here here.
// // Might not work in Socket.IO 1.x (check).
//
// // sio.set('transports', ['xhr-polling']);
// // sio.set('transports', ['jsonp-polling']);
//
// // sio.set('transports', [
// // 'websocket'
// // , 'flashsocket'
// // , 'htmlfile'
// // , 'xhr-polling'
// // , 'jsonp-polling'
// // ]);
//
// return true;
// }
};
// Validate other options.
if (opts.confDir) {
if (!fs.existsSync(opts.confDir)) {
return printErr('--confDir ' + opts.confDir + ' not found.');
}
confDir = opts.confDir;
}
if (opts.logDir) {
if (!fs.existsSync(opts.logDir)) {
return printErr('--logDir ' + opts.logDir + ' not found.');
}
logDir = opts.logDir;
}
if (opts.gamesDir) {
if (!fs.existsSync(opts.gamesDir)) {
return printErr('--gamesDir ' + opts.gamesDir + ' not found.');
}
gamesDir = opts.gamesDir;
}
if (opts.debug) debug = true;
// Parse infoQuery.
if (opts.infoQuery) {
if ('boolean' === typeof opts.infoQuery) {
infoQuery = opts.infoQuery;
}
else {
let i = opts.infoQuery.toLowerCase();
infoQuery = i === 'f' || i === 'false' || i === '0' ? false : true;
}
}
}
// Validate general options.
if ('boolean' === typeof opts.ssl) {
options.ssl = true;
}
else if ('string' === typeof opts.ssl) {
options.ssl = (function(dir) {
var ssl;
dir = path.resolve(dir) + '/';
if (!fs.existsSync(dir)) {
printErr('ssl directory not found: ' + dir);
return;
}
key = dir + 'private.key';
if (!fs.existsSync(key)) {
printErr('ssl private key not found: ' + key);
return;
}
cert = dir + 'certificate.pem';
if (!fs.existsSync(cert)) {
printErr('ssl certificate not found: ' + cert);
return;
}
ssl = {};
ssl.key = fs.readFileSync(key, 'utf8');
ssl.cert = fs.readFileSync(cert, 'utf8');
return ssl;
})(opts.ssl);
if (!options.ssl) return;
}
if (opts['default']) {
options.defaultChannel = opts['default'];
}
if (opts.port) {
port = J.isInt(opts.port, 0);
if (!port) {
return printErr('--port ' + opts.port +
' is not a positive number.');
}
options.port = port;
}
if (opts.logLevel) {
options.logLevel = opts.logLevel;
}
if (opts.nClients) {
if (!opts.phantoms) ignoredOptions.push('--nClients');
else {
nClients = parseInt(opts.nClients, 10);
if (isNaN(nClients)) {
return printErr('--nClients ' + opts.nClients +
' is invalid.');
}
}
}
if (opts.clientType) {
if (!opts.phantoms) ignoredOptions.push('--clientType');
else clientType = opts.clientType;
}
if (opts.runTests) {
if (!opts.runTests) ignoredOptions.push('--runTests');
else runTests = opts.runTests;
}
if (opts.killServer) {
if (!opts.phantoms) ignoredOptions.push('--killServer');
else killServer = true;
}
if (opts.wait) {
if (!opts.phantoms) {
ignoredOptions.push('--wait');
}
else {
if (true === opts.wait) {
wait = 1000;
}
else {
wait = J.isInt(opts.wait, 0);
if (false === wait) {
printErr('--wait must be a positive number or undefined. ' +
'Found:' + opts.wait);
process.exit();
}
}
}
}
if (opts.auth) {
if (!opts.phantoms) {
ignoredOptions.push('--auth');
}
else if ('string' === typeof opts.auth) {
auth = (function(idIdx, pwdIdx) {
var auth;
idIdx = opts.auth.indexOf('id:');
if (idIdx === 0) {
pwdIdx = opts.auth.indexOf('&pwd:');
if (pwdIdx !== -1) {
auth = {
id: opts.auth.substr(3, (pwdIdx-3)),
pwd: opts.auth.substr(pwdIdx+5)
};
}
else {
printErr('--auth must be a client id or id and ' +
'pwd in the form "id:123&pwd:456"');
process.exit();
}
}
else if (opts.auth === 'new') {
auth = 'createNew';
}
else if (opts.auth === 'next') {
auth = 'nextAvailable';
}
else if (opts.auth.indexOf('file:') === 0) {
const NDDB = require('NDDB').NDDB;
codesDb = new NDDB();
codesDb.loadSync(opts.auth.substr(5));
if (!codesDb.size()) {
printErr('--auth no auth codes found: opts.auth');
process.exit();
}
codesDb = codesDb.db;
}
else {
auth = opts.auth;
}
return auth;
})();
}
else if ('boolean' === typeof opts.auth) {
auth = 'createNew';
}
else if ('number' === typeof opts.auth ||
'object' === typeof opts.auth) {
auth = opts.auth;
}
}
// Rebuild server files as needed.
if (opts.build) {
(function() {
let cssAlso, cssOnly;
let len = opts.build.length;
if (!len) {
opts.build = [ 'client' ];
}
else if (len === 1) {
if (opts.build[0] === 'all') {
// Client will be done anyway.
opts.build = [ 'window', 'widgets', 'JSUS', 'NDDB' ];
}
else if (opts.build[0] === 'css') {
cssOnly = true;
}
}
let info = J.resolveModuleDir('nodegame-server', __dirname);
info = require(path.resolve(info, 'bin', 'info.js'));
if (!cssOnly) {
let out = 'nodegame-full.js';
let modules = {
window: 'window',
client: 'client',
widgets: 'widgets',
JSUS: 'JSUS',
NDDB: 'NDDB',
css: 'css'
};
// Starting build.
let i = -1;
for ( ; ++i < len ; ) {
let module = opts.build[i];
if (!modules[module]) {
throw new Error('unknown build component: ' + module);
}
// Will be done last anyway.
if (module === 'client') {
continue;
}
else if (module === 'NDDB') {
console.log('NDDB does not require build.');
continue;
}
else if (module === 'css') {
cssAlso = true;
continue;
}
info.build[module]({ all: true, clean: true });
console.log('');
}
// Do client last.
info.build.client({
clean: true,
all: true,
output: out
});
J.copyFile(path.resolve(info.modulesDir.client, 'build', out),
path.resolve(info.serverDir.build, out));
console.log(info.serverDir.build + out + ' rebuilt.');
console.log('');
}
if (cssAlso || cssOnly) {
info.build.css(info.serverDir.css, function(err) {
if (!err) {
console.log();
startServer();
}
});
}
else {
startServer();
}
})();
}
else {
startServer();
}
// ## Helper functions.
function startServer() {
// Print warnings, if any.
printIgnoredOptions();
console.log('nodeGame v.' + version);
// Add nodeGame version (might be higher than server version) to options.
options.nodeGameVersion = version;
options.nodeGameRoot = __dirname;
// Start server, options parameter is optional.
sn = new ServerNode(options);
sn.ready(function() {
var channel;
var i, phantoms;
var startPhantom, handleGameover;
var gameName, gameDir, queryString;
var numFinished;
// If there are not bots to add returns.
if (!opts.phantoms) return;
gameName = opts.phantoms;
channel = sn.channels[gameName];
if (!channel) {
printErr('channel ' + gameName + ' was not found.');
if (killServer) process.exit();
return;
}
if (auth && !channel.gameInfo.auth.enabled) {
printErr('auth option enabled, but channel does not support it.');
process.exit();
}
gameDir = sn.channels[gameName].getGameDir();
if (clientType) queryString = '?clientType=' + clientType;
if (killServer || runTests) {
handleGameover = function() {
var command, testProcess;
console.log();
console.log(gameName + ' game has run successfully.');
console.log();
if (runTests) {
command = path.resolve(gameDir, 'node_modules', '.bin', 'mocha');
if (fs.existsSync(command)) {
// Write and backup settings file.
writeSettingsFile(gameDir);
command += ' ' + gameDir + 'test/ --colors';
testProcess = exec(command,
function(err, stdout, stderr) {
if (stdout) console.log(stdout);
if (stderr) console.log(stderr);
testProcess.kill();
if (killServer) process.exit();
});
}
else {
printErr('Cannot run tests, mocha not found: ',
command);
}
}
else if (killServer) process.exit();
};
}
startPhantom = function(i) {
var str, config;
str = 'Connecting phantom #' + (i+1) + '/' + nClients;
if (codesDb) {
config = { queryString: queryString, auth: codesDb[i] };
}
else {
config = { queryString: queryString, auth: auth };
}
if (config.auth) {
if (config.auth.id) {
str += ' id: ' + config.auth.id;
if (config.auth.pwd) str += ' pwd: ' + config.auth.pwd;
}
else {
str += ' ' + config.auth;
}
}
console.log(str);
phantoms[i] = channel.connectPhantom(config);
if ('undefined' !== typeof handleGameover) {
// Wait for all PhantomJS processes to exit, then stop server.
phantoms[i].on('exit', function(code) {
numFinished ++;
if (numFinished === nClients) {
handleGameover();
}
});
}
};
phantoms = [], numFinished = 0;
for (i = 0; i < nClients; ++i) {
if (i > 0 && wait) {
(function(i) {
setTimeout(function() { startPhantom(i); }, wait * i);
})(i);
}
else {
startPhantom(i);
}
}
// TODO: Listen for room creation instead of timeout.
//setTimeout(function() {
// var node;
// node = sn.channels.ultimatum.gameRooms["ultimatum1"].node;
// node.events.ee.ng.on(
// 'GAME_OVER', function() {console.log('The game is over now.');});
//}, 5000);
});
return sn;
}
function printIgnoredOptions() {
if (ignoredOptions.length) {
console.log(' ignored options:', ignoredOptions.join(', '));
console.log();
console.log();
}
}
function printErr(err) {
console.log(' Check the input parameters.');
console.log(' Error: ' + err);
}
function writeSettingsFile(gameDir) {
var settings, settingsFile, bak;
settings = 'module.exports = { numPlayers: ' + nClients + ' };';
settingsFile = path.resolve(gameDir, 'test', 'settings.js');
// Make a backup of existing settings file, if found.
if (fs.existsSync(settingsFile)) {
bak = fs.readFileSync(settingsFile).toString();
fs.writeFileSync(settingsFile + '.bak', bak);
}
// Write updated settings file.
fs.writeFileSync(path.resolve(gameDir, 'test', 'settings.js'), settings);
}
// Exports the ServerNode instance.
module.exports = sn;