Skip to content

Commit

Permalink
Rewrote, simplified and increased the robustness of the Brickadia out…
Browse files Browse the repository at this point in the history
…put parsing mechanism. Breaking API changes.
  • Loading branch information
n42k committed Sep 14, 2019
1 parent cee9360 commit 5e9fd86
Show file tree
Hide file tree
Showing 11 changed files with 160 additions and 272 deletions.
108 changes: 18 additions & 90 deletions brikkit.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
const fs = require('fs');
const stripAnsi = require('strip-ansi');

const Brickadia = require('./brickadia.js');
const Terminal = require('./terminal.js');
const Configuration = require('./data/configuration.js');
const StateMachineJoin = require('./sm_join.js');

const Parser = require('./parsers/parser.js');

const PluginSystem = require('./pluginsystem.js');
const Scraper = require('./scraper.js');
Expand Down Expand Up @@ -35,9 +37,6 @@ class Brikkit {
}

this._playersByName = {};
this._playersByAddress = {};

this._stateMachineJoin = new StateMachineJoin();

this._scraper = new Scraper();
this._pluginSystem = new PluginSystem();
Expand Down Expand Up @@ -68,6 +67,9 @@ class Brikkit {
this._putEvent(new Event.StartEvent(new Date()))
}, 3000);
}, 3000);

this._joinParser = new Parser.JoinParser();
this._chatParser = new Parser.ChatParser();
}

/*
Expand All @@ -91,11 +93,6 @@ class Brikkit {
return player === undefined ? null : player;
}

getPlayerFromAddress(address) {
const player = this._playersByAddress[address];
return player === undefined ? null : player;
}

say(message) {
const messages = message.split('\n');

Expand Down Expand Up @@ -139,18 +136,6 @@ class Brikkit {
this._brickadia.write(`travel ${mapName}\n`);
}

getPlayerList() {
const players = [];

for(const username in this._playersByName) {
const player = this._playersByName[username];
if(player.isReady())
players.push(player);
}

return players;
}

getScraper() {
return this._scraper;
}
Expand All @@ -166,6 +151,8 @@ class Brikkit {
}

_handleBrickadiaLine(line) {
line = stripAnsi(line);

const matches = /^\[(.*?)\]\[.*?\](.*?): (.*)$/.exec(line);

if(matches === undefined || matches === null)
Expand All @@ -188,62 +175,18 @@ class Brikkit {

const restOfLine = matches[3];

if(generator === 'LogChat') {
const [_1, username, msg] = /^(.*?): (.*?)$/.exec(restOfLine);
const joinedPlayer = this._joinParser.parse(generator, restOfLine);
if(joinedPlayer !== null) {
this._addPlayer(joinedPlayer);
this._putEvent(new Event.JoinEvent(date, joinedPlayer));
}

const chatParserResult = this._chatParser.parse(generator, restOfLine);
if(chatParserResult !== null) {
const [username, message] = chatParserResult;
const player = this.getPlayerFromUsername(username);

this._putEvent(new Event.ChatEvent(date, player, msg));
} else if(generator === 'LogServerList') {
this._stateMachineJoinHandleLine(restOfLine);
} else if(generator === 'LogNet') {
const closeString = 'UChannel::Close: Sending CloseBunch.';
if(restOfLine.startsWith(closeString)) {
const [_1, argString] = /^.*?\. .*?\. (.*)$/.exec(restOfLine);

const args = argString.split(', ').map(arg => {
const [_, key, value] = /^(.*?): (.*?)$/.exec(arg);
return [key, value];
});

let closeString = null;
for(const [key, value] of args)
if(key === 'Closing')
closeString = value;

if(closeString === null)
return;

const [_2, address] = /^.*?RemoteAddr: (.*)$/.exec(closeString);
const player = this.getPlayerFromAddress(address);

if(player === null) {
// must be a player that cancelled joining midway
console.warn('WARNING: player cancelled joining midway');
}

this._removePlayer(player);
this._putEvent(new Event.LeaveEvent(date, player));
}

const [key, value] = restOfLine.split(': ', 2);

if(key === 'Join succeeded') {
const player = this.getPlayerFromUsername(value);

this.getScraper().getProfile(player.getUserId(), profile => {
player._setProfile(profile);
this._putEvent(new Event.JoinEvent(date, player));
});
return;
}

const validKeys = [
'Server accepting post-challenge connection from',
'NotifyAcceptingConnection accepted from'
];

if(validKeys.indexOf(key) !== -1)
this._stateMachineJoinHandleLine(restOfLine);
this._putEvent(new Event.ChatEvent(date, player, message));
}
}

Expand All @@ -254,21 +197,6 @@ class Brikkit {

_addPlayer(player) {
this._playersByName[player.getUsername()] = player;
this._playersByAddress[player.getAddress()] = player;
}

_removePlayer(player) {
delete this._playersByName[player.getUsername()];
delete this._playersByAddress[player.getAddress()];
}

_stateMachineJoinHandleLine(line) {
const player = this._stateMachineJoin.parseLine(line);

if(player === null)
return;

this._addPlayer(player);
}
}

Expand Down
53 changes: 4 additions & 49 deletions data/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const net = require('net');
const isValidUUID = require('uuid-validate');

class Player {
constructor(username, userId, handleId, ip, port) {
constructor(username, userId, handleId) {
if(username === undefined)
throw new Error('Invalid player: undefined username');

Expand All @@ -12,13 +12,6 @@ class Player {
if(handleId === undefined)
throw new Error('Invalid player: undefined handleId');

if(ip === undefined)
throw new Error('Invalid player: undefined ip');

if(port === undefined)
throw new Error('Invalid player: undefined port');


if(typeof username !== 'string')
throw new Error('Invalid player: username not a string');

Expand All @@ -28,29 +21,15 @@ class Player {
if(typeof handleId !== 'string')
throw new Error('Invalid player: handleId not a string');

if(typeof ip !== 'string')
throw new Error('Invalid player: ip not a string')

if(typeof port !== 'number')
throw new Error('Invalid player: port not a number');


if(!isValidUUID(userId))
throw new Error('Invalid profile: bad userId');

if(!net.isIP(ip))
throw new Error('Invalid player: invalid ip address');

if(port < 1 || port > 65335)
throw new Error('Invalid player: invalid port');
throw new Error('Invalid player: bad userId ("' + userId + '")');

if(!isValidUUID(handleId))
throw new Error('Invalid player: bad handleId ("' + handleId + '")');

this._username = username;
this._userId = userId;
this._handleId = handleId;
this._ip = ip;
this._port = port;
this._profile = null;
}

getUsername() {
Expand All @@ -64,30 +43,6 @@ class Player {
getHandleId() {
return this._handleId;
}

getIp() {
return this._ip;
}

getPort() {
return this._port;
}

getAddress() {
return this._ip + ':' + this._port;
}

isReady() {
return this._profile !== null;
}

getProfile() {
return this._profile;
}

_setProfile(profile) {
this._profile = profile;
}
}

module.exports = Player;
1 change: 0 additions & 1 deletion events/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ const Event = {};

Event.ChatEvent = require('./chat.js');
Event.JoinEvent = require('./join.js');
Event.LeaveEvent = require('./leave.js');
Event.StartEvent = require('./start.js');

module.exports = Event;
24 changes: 0 additions & 24 deletions events/leave.js

This file was deleted.

13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"dotenv": "^8.0.0",
"moment": "^2.24.0",
"request": "^2.88.0",
"strip-ansi": "^5.2.0",
"tmp": "^0.1.0",
"uuid-validate": "0.0.3"
}
Expand Down
9 changes: 9 additions & 0 deletions parsers/baseparser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class BaseParser {
// returns null OR
// if something useful was parsed, something that varies per parser
parse(generator, line) {
throw new Error('Not implemented!');
}
}

module.exports = BaseParser;
20 changes: 20 additions & 0 deletions parsers/chat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const BaseParser = require('./baseparser.js');

const Player = require('../data/player.js');
/*
This line is related to player joins:
[2019.09.14-18.34.41:930][443]LogChat: n: hello
*/

class ChatParser extends BaseParser {
parse(generator, line) {
if(generator !== 'LogChat')
return null;

const [username, ...messageParts] = line.split(': ');
const message = messageParts.join(': ');
return [username, message];
}
}

module.exports = ChatParser;
Loading

0 comments on commit 5e9fd86

Please sign in to comment.