mirror of
https://codeberg.org/spire/webrcon.git
synced 2026-06-03 17:24:07 +02:00
minor js fixes, update build system, minimize dependencies
This commit is contained in:
parent
7ebfdddf2c
commit
ef059b1af3
7 changed files with 68 additions and 43 deletions
8
.babelrc
8
.babelrc
|
|
@ -1,3 +1,9 @@
|
|||
{
|
||||
"stage": 0
|
||||
"presets": [
|
||||
"es2015"
|
||||
],
|
||||
"plugins": [
|
||||
"transform-class-properties",
|
||||
"async-to-promises"
|
||||
]
|
||||
}
|
||||
|
|
|
|||
8
Makefile
Normal file
8
Makefile
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
all: build
|
||||
|
||||
node_modules: package.json
|
||||
npm install
|
||||
|
||||
.PHONY: build
|
||||
build: node_modules
|
||||
node node_modules/.bin/babel src --out-dir lib
|
||||
|
|
@ -13,7 +13,12 @@
|
|||
"url": "https://github.com/spiretf/webrcon"
|
||||
},
|
||||
"dependencies": {
|
||||
"bluebird": "^3.0.5",
|
||||
"ws": "^0.8.0"
|
||||
"ws": "^2.2.3"
|
||||
},
|
||||
"devDependencies": {
|
||||
"babel-cli": "^6.24.1",
|
||||
"babel-plugin-async-to-promises": "^1.0.5",
|
||||
"babel-plugin-transform-class-properties": "^6.24.1",
|
||||
"babel-preset-es2015": "^6.24.1"
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,8 +5,8 @@ export default class GetSVar {
|
|||
}
|
||||
|
||||
handler (response) {
|
||||
var regex = '"' + this.name + '" = "([^"]*)"';
|
||||
var matches = response.match(new RegExp(regex));
|
||||
const regex = '"' + this.name + '" = "([^"]*)"';
|
||||
const matches = response.match(new RegExp(regex));
|
||||
if (matches) {
|
||||
return matches[1];
|
||||
} else {
|
||||
|
|
|
|||
|
|
@ -2,19 +2,19 @@ export default class Status {
|
|||
command = 'status';
|
||||
|
||||
handler (response) {
|
||||
var status = {
|
||||
const status = {
|
||||
name: '',
|
||||
map: '',
|
||||
players: []
|
||||
};
|
||||
var playerIds = {};
|
||||
var lines = response.split("\n");
|
||||
for (var i = 0; i < lines.length; i++) {
|
||||
const playerIds = {};
|
||||
const lines = response.split("\n");
|
||||
for (let i = 0; i < lines.length; i++) {
|
||||
let parts;
|
||||
var line = lines[i].trim();
|
||||
const line = lines[i].trim();
|
||||
if (line[0] !== '#' && line.indexOf(':')) {
|
||||
parts = line.split(':');
|
||||
parts = parts.map((part)=> {
|
||||
parts = parts.map((part) => {
|
||||
return part.trim()
|
||||
});
|
||||
if (parts[0] === 'hostname') {
|
||||
|
|
@ -23,23 +23,23 @@ export default class Status {
|
|||
status.map = parts[1].substr(0, parts[1].indexOf(' '));
|
||||
}
|
||||
} else if (line[0] === '#' && line.substr(0, 8) !== '# userid') {
|
||||
var playerLine = line.substr(2).trim();
|
||||
const playerLine = line.substr(2).trim();
|
||||
parts = playerLine.split('"');
|
||||
parts = parts.map((part)=> {
|
||||
parts = parts.map((part) => {
|
||||
return part.trim()
|
||||
});
|
||||
var id = parseInt(parts[0], 10);
|
||||
const id = parseInt(parts[0], 10);
|
||||
if (playerIds[id]) {
|
||||
continue;
|
||||
}
|
||||
playerIds[id] = true;
|
||||
var name = parts[1];
|
||||
const name = parts[1];
|
||||
if (!parts[2]) {
|
||||
return;
|
||||
}
|
||||
parts = parts[2].replace(/\s+/g, ' ').split(' ');
|
||||
var steamId = parts[0];
|
||||
var player = {
|
||||
const steamId = parts[0];
|
||||
const player = {
|
||||
id: id,
|
||||
name: name,
|
||||
steamId: steamId
|
||||
|
|
@ -47,7 +47,7 @@ export default class Status {
|
|||
if (parts.length > 2 && steamId !== 'BOT') {
|
||||
player.ping = parseInt(parts[2], 10);
|
||||
player.ip = parts[5];
|
||||
var timeParts = parts[1].split(':');
|
||||
const timeParts = parts[1].split(':');
|
||||
if (timeParts.length === 2) {
|
||||
player.connected = parseInt(timeParts[0], 10) * 60 + parseInt(timeParts[1], 10);
|
||||
} else {
|
||||
|
|
|
|||
|
|
@ -1,4 +1,3 @@
|
|||
import Promise from 'bluebird';
|
||||
import WebRcon from './webrcon.js';
|
||||
|
||||
export default class Connection {
|
||||
|
|
@ -36,11 +35,11 @@ export default class Connection {
|
|||
this.receivedBuffer += response;
|
||||
// buffer the response
|
||||
this.receivedTimer = setTimeout(() => {
|
||||
var response = this.receivedBuffer;
|
||||
const response = this.receivedBuffer;
|
||||
this.receivedBuffer = '';
|
||||
if (this.messageHandlers.length) {
|
||||
while (this.messageHandlers.length > 0) {
|
||||
var handler = this.messageHandlers.shift();
|
||||
const handler = this.messageHandlers.shift();
|
||||
handler(response);
|
||||
}
|
||||
} else {
|
||||
|
|
@ -50,14 +49,13 @@ export default class Connection {
|
|||
}, 100);
|
||||
});
|
||||
this.rcon.on('error', (e) => {
|
||||
if (e == 'not authenticated') {
|
||||
if (e === 'not authenticated') {
|
||||
for (let listener of this.errorListeners) {
|
||||
listener(e);
|
||||
}
|
||||
return;
|
||||
}
|
||||
this.errorCount++;
|
||||
console.log('failed to connect ' + this.errorCount + ' times (' + e + ')');
|
||||
Promise.delay(2500).then(() => {
|
||||
if (this.connectPromise) {
|
||||
this.rcon.connect();
|
||||
|
|
@ -90,7 +88,7 @@ export default class Connection {
|
|||
await this.waitFor(() => {
|
||||
return this.messageHandlers.length === 0;
|
||||
}, 500);
|
||||
var promise = new Promise((resolve) => {
|
||||
const promise = new Promise((resolve) => {
|
||||
this.messageHandlers.push(resolve);
|
||||
});
|
||||
this.rcon.send(command);
|
||||
|
|
@ -98,7 +96,7 @@ export default class Connection {
|
|||
}
|
||||
|
||||
async send (command) {
|
||||
var response = await this.sendString(command.command);
|
||||
const response = await this.sendString(command.command);
|
||||
return command.handler(response);
|
||||
}
|
||||
|
||||
|
|
@ -107,14 +105,22 @@ export default class Connection {
|
|||
}
|
||||
|
||||
async waitFor (cb, timeout) {
|
||||
var waited = 0;
|
||||
let waited = 0;
|
||||
while (waited < timeout) {
|
||||
if (cb()) {
|
||||
return true;
|
||||
}
|
||||
await Promise.delay(250);
|
||||
await this.delay(250);
|
||||
waited += 250;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
delay (delay) {
|
||||
return new Promise(function (resolve) {
|
||||
setTimeout(function () {
|
||||
resolve()
|
||||
}, delay)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -24,7 +24,7 @@ export default class WebRcon extends EventEmitter {
|
|||
this.connected = true;
|
||||
this.emit('connect');
|
||||
setTimeout(() => {
|
||||
for (var message of this.queue) {
|
||||
for (let message of this.queue) {
|
||||
this.socket.send(message);
|
||||
}
|
||||
this.queue = [];
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue