node-gamedig/lib/index.js

49 lines
1.4 KiB
JavaScript
Raw Normal View History

2019-01-12 11:43:36 +01:00
const QueryRunner = require('./QueryRunner'),
GlobalUdpSocket = require('./GlobalUdpSocket');
2014-10-29 08:02:03 +01:00
2019-01-12 11:43:36 +01:00
let singleton = null;
2014-10-29 08:02:03 +01:00
class Gamedig {
2019-01-12 11:43:36 +01:00
constructor() {
this.udpSocket = new GlobalUdpSocket();
this.queryRunner = new QueryRunner(this.udpSocket);
this._debug = false;
}
2014-10-29 08:02:03 +01:00
2019-01-12 11:43:36 +01:00
setDebug(on) {
this.udpSocket.debug = on;
this._debug = on;
this.queryRunner.debug = on;
}
2017-03-14 09:40:02 +01:00
2019-01-12 11:43:36 +01:00
async query(userOptions) {
userOptions.debug |= this._debug;
return await this.queryRunner.run(userOptions);
}
2014-10-29 08:02:03 +01:00
2019-01-12 11:43:36 +01:00
static getInstance() {
if (!singleton) {
singleton = new Gamedig();
}
return singleton;
}
static query(userOptions, callback) {
const promise = Gamedig.getInstance().query(userOptions);
2017-08-09 12:32:09 +02:00
if (callback && callback instanceof Function) {
2019-01-12 11:43:36 +01:00
if (callback.length === 2) {
2017-08-09 12:32:09 +02:00
promise
2019-01-12 11:43:36 +01:00
.then((state) => callback(null, state))
2017-08-09 12:32:09 +02:00
.catch((error) => callback(error));
} else if (callback.length === 1) {
promise
.then((state) => callback(state))
2019-01-12 11:43:36 +01:00
.catch((error) => callback({error: error}));
2017-08-09 12:32:09 +02:00
}
}
return promise;
}
}
2019-01-12 11:43:36 +01:00
Object.defineProperty(Gamedig, "debug", { set: on => Gamedig.getInstance().setDebug(on) });
2014-10-29 08:02:03 +01:00
module.exports = Gamedig;