-
Notifications
You must be signed in to change notification settings - Fork 2
/
game-server-query-wrapper.js
51 lines (39 loc) · 1.4 KB
/
game-server-query-wrapper.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
var gamedig = require("gamedig");
var gsq = function(options, callback) {
gamedig.query(options)
.then(function(state) {
if (state.players.length > 0 && state.players.every(p => !p.name)) {
throw new Error("Missing all player names");
}
callback(Object.assign({query: options}, state));
})
.catch(function(error) {
callback({query: options, error: error});
});
};
var MAX_PARALLEL_QUERY_COUNT = 10;
var endpoint_to_gsq_param = function( server ) {
var host = server.split(':')[0];
var port = parseInt(server.split(':')[1]);
return { type: "quakelive", host: host, port: port, requestRules: true };
};
var query = function(servers, callback) {
if ( servers.length == 0 ) return;
var index = 0;
return new Promise((resolve, reject) => {
var inner_callback = function(state) {
var server = state.query.host + ":" + state.query.port.toString();
callback(server, state);
index++;
if( servers[index] ) {
gsq( endpoint_to_gsq_param ( servers[index] ), inner_callback );
} else if (index == servers.length + Math.min( MAX_PARALLEL_QUERY_COUNT - 1, servers.length ) ) {
resolve();
}
};
for ( ; index<Math.min( MAX_PARALLEL_QUERY_COUNT, servers.length ); index++ ) {
gsq( endpoint_to_gsq_param ( servers[index] ), inner_callback );
}
});
};
module.exports = query;