node-gamedig/protocols/nadeo.js

90 lines
2.9 KiB
JavaScript
Raw Normal View History

const gbxremote = require('gbxremote'),
async = require('async'),
Core = require('./core');
2014-10-29 08:02:03 +01:00
class Nadeo extends Core {
2017-08-09 12:32:09 +02:00
constructor() {
super();
this.options.port = 2350;
this.options.port_query = 5000;
this.gbxclient = false;
}
2017-08-09 12:32:09 +02:00
reset() {
super.reset();
if(this.gbxclient) {
this.gbxclient.terminate();
this.gbxclient = false;
}
}
2014-10-29 08:02:03 +01:00
2017-08-09 12:32:09 +02:00
run(state) {
const cmds = [
['Connect'],
['Authenticate', this.options.login,this.options.password],
2018-02-26 23:52:42 +01:00
['GetStatus'], // 1
['GetPlayerList',10000,0], // 2
2018-02-26 23:52:42 +01:00
['GetServerOptions'], // 3
['GetCurrentMapInfo'], // 4
['GetCurrentGameInfo'], // 5
['GetNextMapInfo'] // 6
2017-08-09 12:32:09 +02:00
];
const results = [];
2014-10-29 08:02:03 +01:00
2017-08-09 12:32:09 +02:00
async.eachSeries(cmds, (cmdset,c) => {
const cmd = cmdset[0];
const params = cmdset.slice(1);
2014-10-29 08:02:03 +01:00
2017-08-09 12:32:09 +02:00
if(cmd === 'Connect') {
const client = this.gbxclient = gbxremote.createClient(this.options.port_query,this.options.host, (err) => {
2017-08-09 12:32:09 +02:00
if(err) return this.fatal('GBX error '+JSON.stringify(err));
c();
});
client.on('error',() => {});
} else {
this.gbxclient.methodCall(cmd, params, (err, value) => {
if(err) return this.fatal('XMLRPC error '+JSON.stringify(err));
results.push(value);
c();
});
}
}, () => {
let gamemode = '';
const igm = results[5].GameMode;
2017-08-09 12:32:09 +02:00
if(igm === 0) gamemode="Rounds";
if(igm === 1) gamemode="Time Attack";
if(igm === 2) gamemode="Team";
if(igm === 3) gamemode="Laps";
if(igm === 4) gamemode="Stunts";
if(igm === 5) gamemode="Cup";
2014-10-29 08:02:03 +01:00
2017-08-09 12:32:09 +02:00
state.name = this.stripColors(results[3].Name);
state.password = (results[3].Password !== 'No password');
state.maxplayers = results[3].CurrentMaxPlayers;
2018-03-18 11:42:44 +01:00
state.raw.maxspectators = results[3].CurrentMaxSpectators;
state.map = this.stripColors(results[4].Name);
state.raw.mapUid = results[4].UId;
state.raw.gametype = gamemode;
state.raw.players = results[2];
state.raw.mapcount = results[5].NbChallenge;
state.raw.nextmapName = this.stripColors(results[6].Name);
state.raw.nextmapUid = results[6].UId;
for (const player of state.raw.players) {
state.players.push({
name:this.stripColors(player.Name || player.NickName)
});
}
2014-10-29 08:02:03 +01:00
2017-08-09 12:32:09 +02:00
this.finish(state);
});
}
2017-08-09 12:32:09 +02:00
stripColors(str) {
2018-03-18 11:42:44 +01:00
return str.replace(/\$([0-9a-f]{3}|[a-z])/gi,'');
2017-08-09 12:32:09 +02:00
}
2018-03-01 09:34:30 +01:00
}
module.exports = Nadeo;