/
misc.js
237 lines (208 loc) · 6.96 KB
/
misc.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
var fs = require('fs');
var http = require('http');
var misc = {
force: false, //ignore params.extract()
regions: [
"BR-Brazil",
"CN-China",
"EU-London",
"JP-Tokyo",
"RU-Russia",
"SG-Singapore",
"TK-Turkey",
"US-Atlanta"
],
server_types: [
"FFA",
"teams",
"experimental",
"party"
],
params: {
'streamer-port': {
description: 'Port for streamer',
alias: [
'--streamer-port',
'--streamer',
'-s'
],
default: 9158,
extract: function(input) {
var int = parseInt(input);
if(int && int <= 65535) return int;
console.log('[Warning] Streamer port must be 1-65535, port ' + this.default + ' will be used');
}
},
'viewer-port': {
description: 'Port for viewer',
alias: [
'--viewer-port',
'--viewer',
'-v'
],
default: 1400,
extract: function(input) {
var int = parseInt(input);
if(int && int <= 65535) return int;
console.log('[Warning] Viewer port must be 1-65535, port ' + this.default + ' will be used');
}
},
'path': {
description: 'Path with/for .rec files with "/" at the end',
alias: [
'--path',
'-p'
],
default: './records/',
extract: function(input) {
if(fs.existsSync(input)) return input;
console.log('[Warning] Path don\'t exists, ' + this.default + ' will be used');
}
},
'record': {
description: 'Record filename',
alias: [
'--record',
'--file',
'-f'
],
default: 'demo.rec',
extract: function(input) {
if(input.substr(-4) == '.rec') return input;
console.log('[Warning] Filename must have .rec at the end, ' + this.default + ' will be used');
}
},
'agario-server': {
description: 'Agar.io\'s server, starting with ws:// (random = get random server)',
alias: [
'--agario-server',
'--agar-server',
'--server',
'-a'
],
default: 'random',
extract: function(input) {
if(input == 'random') return input;
if(input.substr(0,5) == 'ws://') return input;
if(input.substr(0,6) == 'wss://') return input;
console.log('[Warning] Server must start with ws://, ' + this.default + ' will be used');
}
},
'server-key': {
description: 'Server key for manually specified server',
alias: [
'--server-key',
'--key',
'-k'
],
default: null,
extract: function(input) {
return input;
}
},
'server-region': {
region: true,
description: 'Server region for random server',
alias: [
'--server-region',
'--region',
'-r'
],
default: 'EU-London',
extract: function(input) {
if(misc.regions.indexOf(input) != -1) return input;
console.log('[Warning] Unsupported region, random region will be used');
}
},
'server-type': {
server_type: true,
description: 'Server type',
alias: [
'--server-type',
'--type',
'-t'
],
default: 'FFA',
extract: function(input) {
if(misc.server_types.indexOf(input) != -1) return input;
console.log('[Warning] Unsupported region, random region will be used');
}
},
'force': {
description: 'Disable inspection of param values',
alias: [
'--force'
],
default: false,
extract: function(input) {
return input;
}
},
'help': {
description: 'Display help',
alias: [
'--help',
'-h'
],
default: false,
extract: function(input) {
return input;
}
}
},
exists: function(name) {
var alias = this.params[name].alias;
if(this.rawExtract(alias, true)) return true;
return false;
},
rawExtract: function(alias, exists) {
for(var i=0;i<process.argv.length;i++) {
var alias_pos = alias.indexOf(process.argv[i]);
if(alias_pos == -1) continue;
if(exists) return true;
return process.argv[i+1];
}
},
readParam: function(name) {
var param = this.params[name];
var candidate = this.rawExtract(param.alias);
if((typeof candidate) == 'undefined') return param.default;
if(this.force) return candidate;
var extracted = param.extract(candidate);
if((typeof extracted) == 'undefined') return param.default;
return extracted;
},
help: function(params) {
params.push('force');
params.push('help');
if(!this.exists('help')) return;
console.log('Available params:');
for(var i=0;i<params.length;i++) {
var param_name = params[i];
var param = this.params[param_name];
console.log('');
console.log(' ' + param_name + ': ' + param.description);
console.log(' ' + param.alias.join(', '));
console.log(' Default: ' + param.default);
if(param.region) {
console.log(' Supported:');
for(var j=0;j<misc.regions.length;j++) {
console.log(' ' + misc.regions[j]);
}
}
if(param.server_type) {
console.log(' Supported:');
for(var k=0;k<misc.server_types.length;k++) {
console.log(' ' + misc.server_types[k]);
}
console.log(' If you use "party" then add "--server-key=PARTY_KEY" to connect to existing party');
console.log(' Otherwise new/random party will be created/used');
}
}
process.exit(0);
}
};
if(misc.exists('force')) {
misc.force = true;
}
module.exports = misc;