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"
|
"url": "https://github.com/spiretf/webrcon"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"bluebird": "^3.0.5",
|
"ws": "^2.2.3"
|
||||||
"ws": "^0.8.0"
|
},
|
||||||
|
"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) {
|
handler (response) {
|
||||||
var regex = '"' + this.name + '" = "([^"]*)"';
|
const regex = '"' + this.name + '" = "([^"]*)"';
|
||||||
var matches = response.match(new RegExp(regex));
|
const matches = response.match(new RegExp(regex));
|
||||||
if (matches) {
|
if (matches) {
|
||||||
return matches[1];
|
return matches[1];
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
|
|
@ -2,19 +2,19 @@ export default class Status {
|
||||||
command = 'status';
|
command = 'status';
|
||||||
|
|
||||||
handler (response) {
|
handler (response) {
|
||||||
var status = {
|
const status = {
|
||||||
name: '',
|
name: '',
|
||||||
map: '',
|
map: '',
|
||||||
players: []
|
players: []
|
||||||
};
|
};
|
||||||
var playerIds = {};
|
const playerIds = {};
|
||||||
var lines = response.split("\n");
|
const lines = response.split("\n");
|
||||||
for (var i = 0; i < lines.length; i++) {
|
for (let i = 0; i < lines.length; i++) {
|
||||||
let parts;
|
let parts;
|
||||||
var line = lines[i].trim();
|
const line = lines[i].trim();
|
||||||
if (line[0] !== '#' && line.indexOf(':')) {
|
if (line[0] !== '#' && line.indexOf(':')) {
|
||||||
parts = line.split(':');
|
parts = line.split(':');
|
||||||
parts = parts.map((part)=> {
|
parts = parts.map((part) => {
|
||||||
return part.trim()
|
return part.trim()
|
||||||
});
|
});
|
||||||
if (parts[0] === 'hostname') {
|
if (parts[0] === 'hostname') {
|
||||||
|
|
@ -23,23 +23,23 @@ export default class Status {
|
||||||
status.map = parts[1].substr(0, parts[1].indexOf(' '));
|
status.map = parts[1].substr(0, parts[1].indexOf(' '));
|
||||||
}
|
}
|
||||||
} else if (line[0] === '#' && line.substr(0, 8) !== '# userid') {
|
} 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 = playerLine.split('"');
|
||||||
parts = parts.map((part)=> {
|
parts = parts.map((part) => {
|
||||||
return part.trim()
|
return part.trim()
|
||||||
});
|
});
|
||||||
var id = parseInt(parts[0], 10);
|
const id = parseInt(parts[0], 10);
|
||||||
if (playerIds[id]) {
|
if (playerIds[id]) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
playerIds[id] = true;
|
playerIds[id] = true;
|
||||||
var name = parts[1];
|
const name = parts[1];
|
||||||
if (!parts[2]) {
|
if (!parts[2]) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
parts = parts[2].replace(/\s+/g, ' ').split(' ');
|
parts = parts[2].replace(/\s+/g, ' ').split(' ');
|
||||||
var steamId = parts[0];
|
const steamId = parts[0];
|
||||||
var player = {
|
const player = {
|
||||||
id: id,
|
id: id,
|
||||||
name: name,
|
name: name,
|
||||||
steamId: steamId
|
steamId: steamId
|
||||||
|
|
@ -47,7 +47,7 @@ export default class Status {
|
||||||
if (parts.length > 2 && steamId !== 'BOT') {
|
if (parts.length > 2 && steamId !== 'BOT') {
|
||||||
player.ping = parseInt(parts[2], 10);
|
player.ping = parseInt(parts[2], 10);
|
||||||
player.ip = parts[5];
|
player.ip = parts[5];
|
||||||
var timeParts = parts[1].split(':');
|
const timeParts = parts[1].split(':');
|
||||||
if (timeParts.length === 2) {
|
if (timeParts.length === 2) {
|
||||||
player.connected = parseInt(timeParts[0], 10) * 60 + parseInt(timeParts[1], 10);
|
player.connected = parseInt(timeParts[0], 10) * 60 + parseInt(timeParts[1], 10);
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,3 @@
|
||||||
import Promise from 'bluebird';
|
|
||||||
import WebRcon from './webrcon.js';
|
import WebRcon from './webrcon.js';
|
||||||
|
|
||||||
export default class Connection {
|
export default class Connection {
|
||||||
|
|
@ -36,11 +35,11 @@ export default class Connection {
|
||||||
this.receivedBuffer += response;
|
this.receivedBuffer += response;
|
||||||
// buffer the response
|
// buffer the response
|
||||||
this.receivedTimer = setTimeout(() => {
|
this.receivedTimer = setTimeout(() => {
|
||||||
var response = this.receivedBuffer;
|
const response = this.receivedBuffer;
|
||||||
this.receivedBuffer = '';
|
this.receivedBuffer = '';
|
||||||
if (this.messageHandlers.length) {
|
if (this.messageHandlers.length) {
|
||||||
while (this.messageHandlers.length > 0) {
|
while (this.messageHandlers.length > 0) {
|
||||||
var handler = this.messageHandlers.shift();
|
const handler = this.messageHandlers.shift();
|
||||||
handler(response);
|
handler(response);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -50,14 +49,13 @@ export default class Connection {
|
||||||
}, 100);
|
}, 100);
|
||||||
});
|
});
|
||||||
this.rcon.on('error', (e) => {
|
this.rcon.on('error', (e) => {
|
||||||
if (e == 'not authenticated') {
|
if (e === 'not authenticated') {
|
||||||
for (let listener of this.errorListeners) {
|
for (let listener of this.errorListeners) {
|
||||||
listener(e);
|
listener(e);
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.errorCount++;
|
this.errorCount++;
|
||||||
console.log('failed to connect ' + this.errorCount + ' times (' + e + ')');
|
|
||||||
Promise.delay(2500).then(() => {
|
Promise.delay(2500).then(() => {
|
||||||
if (this.connectPromise) {
|
if (this.connectPromise) {
|
||||||
this.rcon.connect();
|
this.rcon.connect();
|
||||||
|
|
@ -90,7 +88,7 @@ export default class Connection {
|
||||||
await this.waitFor(() => {
|
await this.waitFor(() => {
|
||||||
return this.messageHandlers.length === 0;
|
return this.messageHandlers.length === 0;
|
||||||
}, 500);
|
}, 500);
|
||||||
var promise = new Promise((resolve) => {
|
const promise = new Promise((resolve) => {
|
||||||
this.messageHandlers.push(resolve);
|
this.messageHandlers.push(resolve);
|
||||||
});
|
});
|
||||||
this.rcon.send(command);
|
this.rcon.send(command);
|
||||||
|
|
@ -98,7 +96,7 @@ export default class Connection {
|
||||||
}
|
}
|
||||||
|
|
||||||
async send (command) {
|
async send (command) {
|
||||||
var response = await this.sendString(command.command);
|
const response = await this.sendString(command.command);
|
||||||
return command.handler(response);
|
return command.handler(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -107,14 +105,22 @@ export default class Connection {
|
||||||
}
|
}
|
||||||
|
|
||||||
async waitFor (cb, timeout) {
|
async waitFor (cb, timeout) {
|
||||||
var waited = 0;
|
let waited = 0;
|
||||||
while (waited < timeout) {
|
while (waited < timeout) {
|
||||||
if (cb()) {
|
if (cb()) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
await Promise.delay(250);
|
await this.delay(250);
|
||||||
waited += 250;
|
waited += 250;
|
||||||
}
|
}
|
||||||
return false;
|
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.connected = true;
|
||||||
this.emit('connect');
|
this.emit('connect');
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
for (var message of this.queue) {
|
for (let message of this.queue) {
|
||||||
this.socket.send(message);
|
this.socket.send(message);
|
||||||
}
|
}
|
||||||
this.queue = [];
|
this.queue = [];
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue