node-gamedig/protocols/nadeo.js

89 lines
3.1 KiB
JavaScript
Raw Normal View History

const gbxremote = require('gbxremote'),
Core = require('./core'),
Promises = require('../lib/Promises');
2014-10-29 08:02:03 +01:00
class Nadeo extends Core {
2019-01-10 13:03:07 +01:00
async run(state) {
await this.withClient(async client => {
const start = Date.now();
2019-10-15 22:03:11 +02:00
await this.query(client, 'Authenticate', this.options.login, this.options.password);
this.registerRtt(Date.now()-start);
2019-01-10 13:03:07 +01:00
//const data = this.methodCall(client, 'GetStatus');
2014-10-29 08:02:03 +01:00
2019-01-10 13:03:07 +01:00
{
2019-10-15 22:03:11 +02:00
const results = await this.query(client, 'GetServerOptions');
2019-01-10 13:03:07 +01:00
state.name = this.stripColors(results.Name);
state.password = (results.Password !== 'No password');
state.maxplayers = results.CurrentMaxPlayers;
state.raw.maxspectators = results.CurrentMaxSpectators;
}
2014-10-29 08:02:03 +01:00
2019-01-10 13:03:07 +01:00
{
2019-10-15 22:03:11 +02:00
const results = await this.query(client, 'GetCurrentMapInfo');
2019-01-10 13:03:07 +01:00
state.map = this.stripColors(results.Name);
state.raw.mapUid = results.UId;
}
2014-10-29 08:02:03 +01:00
2019-01-10 13:03:07 +01:00
{
2019-10-15 22:03:11 +02:00
const results = await this.query(client, 'GetCurrentGameInfo');
2019-01-10 13:03:07 +01:00
let gamemode = '';
const igm = results.GameMode;
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";
state.raw.gametype = gamemode;
state.raw.mapcount = results.NbChallenge;
2017-08-09 12:32:09 +02:00
}
2014-10-29 08:02:03 +01:00
2019-01-10 13:03:07 +01:00
{
2019-10-15 22:03:11 +02:00
const results = await this.query(client, 'GetNextMapInfo');
2019-01-10 13:03:07 +01:00
state.raw.nextmapName = this.stripColors(results.Name);
state.raw.nextmapUid = results.UId;
}
2018-03-18 11:42:44 +01:00
2019-01-12 12:45:09 +01:00
if (this.options.port === 5000) {
state.gamePort = 2350;
}
2019-10-15 22:03:11 +02:00
state.raw.players = await this.query(client, 'GetPlayerList', 10000, 0);
2018-03-18 11:42:44 +01:00
for (const player of state.raw.players) {
state.players.push({
name:this.stripColors(player.Name || player.NickName)
});
}
2017-08-09 12:32:09 +02:00
});
}
2019-01-10 13:03:07 +01:00
async withClient(fn) {
2019-10-15 22:03:11 +02:00
const socket = new gbxremote.Client(this.options.port, this.options.host);
2019-01-10 13:03:07 +01:00
try {
2019-10-15 22:03:11 +02:00
const connectPromise = socket.connect();
const timeoutPromise = Promises.createTimeout(this.options.socketTimeout, 'GBX Remote Opening');
2019-10-15 22:03:11 +02:00
await Promise.race([connectPromise, timeoutPromise, this.abortedPromise]);
2019-01-10 13:03:07 +01:00
return await fn(socket);
} finally {
socket.terminate();
}
}
2019-10-15 22:03:11 +02:00
async query(client, ...cmdset) {
2019-01-10 13:03:07 +01:00
const cmd = cmdset[0];
const params = cmdset.slice(1);
2019-10-15 22:03:11 +02:00
const sentPromise = client.query(cmd, params);
const timeoutPromise = Promises.createTimeout(this.options.socketTimeout, 'GBX Method Call');
return await Promise.race([sentPromise, timeoutPromise, this.abortedPromise]);
2019-01-10 13:03:07 +01:00
}
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;