forked from CTCC1/Pokemon-Showdown-Node-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
getserver.js
146 lines (136 loc) · 4.12 KB
/
getserver.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
/*
Server configuration help
*/
try {
require('sugar');
require('colors');
} catch (e) {
console.log('Installing dependencies...');
require('child_process').spawnSync('sh', ['-c', 'npm install --production'], {stdio: 'inherit'});
}
require('sugar');
var colors = require('colors');
var readline = require('readline');
var sys = require('sys');
var url = require('url');
var http = require('http');
var fs = require('fs');
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
function askUrl () {
rl.question("Client url for server (example.psim.us): ", function (serverUrl) {
if (serverUrl.indexOf('://') !== -1) {
serverUrl = url.parse(serverUrl).host;
}
if (serverUrl.slice(-1) === '/') {
serverUrl = serverUrl.slice(0, -1);
}
console.log('Getting data for ' + serverUrl + '...');
console.log('This may take some time, depending on Showdown\'s speed.');
var received = false;
var requestOptions = {
hostname: 'play.pokemonshowdown.com',
port: 80,
path: '/crossdomain.php?host=' + serverUrl + '&path=',
method: 'GET'
};
var req = http.request(requestOptions, function (res) {
res.setEncoding('utf8');
var str = '';
res.on('data', function (chunk) {
str += chunk;
});
res.on('end', function () {
if (received) {
return;
}
received = true;
var search = 'var config = ';
var index = str.indexOf(search);
if (index !== -1) {
var data = str.substr(index);
data = data.substr(search.length, data.indexOf(';') - search.length);
while (typeof data === "string") {
try {
data = JSON.parse(data);
} catch (e) {
console.log(e.message);
console.log(e.stack);
break;
}
}
console.log('---------------');
console.log('server: ' + data.host);
console.log('port: ' + data.port);
console.log('serverid: ' + data.id);
console.log('---------------\n');
writeConfig(data.host, data.port, data.id);
} else {
console.log('ERROR: failed to get data!');
rl.close();
process.exit(-1);
}
});
res.on('error', function (err) {
console.log('ERROR: ' + sys.inspect(err));
rl.close();
process.exit(-1);
});
});
req.on('error', function (err) {
console.log('ERROR: ' + sys.inspect(err));
rl.close();
process.exit(-1);
});
req.end();
});
}
function writeConfig (server, port, serverid) {
rl.question("Do you want to write this in config.js? (yes, no): ", function (answer) {
answer = answer.toLowerCase().replace(/[^a-z0-9]/g, '');
if (answer in {'no': 1, 'n': 1}) {
rl.close();
return process.exit();
}
if (answer in {'yes': 1, 'y': 1}) {
if (!fs.existsSync('./config.js')) {
console.log("config.js does not exist - creating one with default settings...");
fs.writeFileSync('./config.js', fs.readFileSync('./config-example.js'));
}
var conf = fs.readFileSync('./config.js').toString().split('\n');
var status = {
server: 0,
port: 0,
serverid: 0
};
for (var i = 0; i < conf.length; i++) {
if (conf[i].substr(0, "exports.serverid".length) === "exports.serverid") {
conf[i] = "exports.serverid = '" + serverid + "';";
status.serverid = 1;
} else if (conf[i].substr(0, "exports.server".length) === "exports.server") {
conf[i] = "exports.server = '" + server + "';";
status.server = 1;
} else if (conf[i].substr(0, "exports.port".length) === "exports.port") {
conf[i] = "exports.port = " + port + ";";
status.port = 1;
}
}
if (!status.server) conf.unshift("exports.server = '" + server + "';");
if (!status.port) conf.unshift("exports.port = " + port + ";");
if (!status.serverid) conf.unshift("exports.serverid = '" + serverid + "';");
fs.writeFileSync('./config.js', conf.join('\n'));
console.log("Done!".green + "\t" + "successfully created default config");
rl.close();
return process.exit();
}
writeConfig(server, port, serverid);
});
}
console.log((
"\n-------------------------------------------\n" +
" Pokemon-Showdown Servers Helper \n" +
"-------------------------------------------\n"
).yellow);
askUrl();