Modularized event listeners

This commit is contained in:
AlexKvazos
2015-04-29 11:46:09 -05:00
parent fcb2236d12
commit 5046fd49ba
10 changed files with 75 additions and 64 deletions
+8
View File
@@ -0,0 +1,8 @@
module.exports = function(socket) {
socket.mcbot.on('end', function() {
socket.emit('bot:disconnect');
socket.mcbot = null;
});
};
+13
View File
@@ -0,0 +1,13 @@
module.exports = function(socket) {
// login event
socket.mcbot.on('login', function() {
socket.emit('buffer:success', 'Successfully logged in as ' + socket.mcbot.username + ' with entity id ' + socket.mcbot.entity.id);
socket.emit('bot:connect', {
host: socket.connectionParams.hostname,
port: socket.connectionParams.port,
username: socket.connectionParams.username
});
});
};
@@ -1,59 +1,10 @@
function stringToCode(string) {
var dictionary = {
'black': 0,
'dark_blue': 1,
'dark_green': 2,
'dark_aqua': 3,
'dark_red': 4,
'dark_purple': 5,
'gold': 6,
'gray': 7,
'dark_gray': 8,
'indigo': 9,
'green': 'a',
'aqua': 'b',
'red': 'c',
'light_purple': 'd',
'yellow': 'e',
'white': 'f'
};
return dictionary[string] || 'f';
}
function escapeHtml(unsafe) {
return unsafe
.replace(/&/g, "&")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;")
.replace(/"/g, "&quot;")
.replace(/'/g, "&#039;");
}
var stringToCode = require('../../utils').stringToCode;
var escapeHtml = require('../../utils').escapeHtml;
module.exports = function(socket) {
var bot = socket.mcbot;
// login event
bot.on('login', function() {
socket.emit('buffer:success', 'Successfully logged in as ' + bot.username + ' with entity id ' + bot.entity.id);
socket.emit('bot:connect', {
host: socket.connectionParams.hostname,
port: socket.connectionParams.port,
username: socket.connectionParams.username
});
});
// spawn event
bot.on('spawn', function() {
var pos = bot.entity.position;
socket.emit('buffer:info', 'Spawned at X:' + pos.x + ', Y:' + pos.y + ', Z:' + pos.z);
});
// message event
bot.on('message', function(message) {
socket.mcbot.on('message', function(message) {
// empty buffer
var buffer = '';
@@ -142,9 +93,4 @@ module.exports = function(socket) {
});
bot.on('end', function() {
socket.emit('bot:disconnect');
socket.mcbot = null;
});
};
};
+9
View File
@@ -0,0 +1,9 @@
module.exports = function(socket) {
// spawn event
socket.mcbot.on('spawn', function() {
var pos = socket.mcbot.entity.position;
socket.emit('buffer:info', 'Spawned at X:' + pos.x + ', Y:' + pos.y + ', Z:' + pos.z);
});
};
+5 -1
View File
@@ -1,5 +1,9 @@
module.exports = function(socket) {
require('./events')(socket);
// bind all listeners to the bot
require('./events/login')(socket);
require('./events/spawn')(socket);
require('./events/message')(socket);
require('./events/end')(socket);
};
+1 -1
View File
@@ -43,4 +43,4 @@ server.listen(app.get('port'), function() {
// handle exceptions
process.on('uncaughtException', function(ex) {
console.error(ex.stack);
});
});
+1 -4
View File
@@ -1,11 +1,8 @@
var mineflayer = require('mineflayer');
module.exports = function(io) {
io.on('connection', function(socket) {
// bind all events to the socket
// bind all listeners to the socket
require('./events/connection')(socket);
require('./events/disconnection')(socket);
require('./events/chat')(socket);
+8
View File
@@ -0,0 +1,8 @@
module.exports = function escapeHtml(unsafe) {
return unsafe
.replace(/&/g, "&amp;")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;")
.replace(/"/g, "&quot;")
.replace(/'/g, "&#039;");
};
+2
View File
@@ -0,0 +1,2 @@
exports.escapeHtml = require('./escapeHtml');
exports.stringToCode = require('./stringToCode');
+24
View File
@@ -0,0 +1,24 @@
module.exports = function stringToCode(string) {
var dictionary = {
'black': 0,
'dark_blue': 1,
'dark_green': 2,
'dark_aqua': 3,
'dark_red': 4,
'dark_purple': 5,
'gold': 6,
'gray': 7,
'dark_gray': 8,
'indigo': 9,
'green': 'a',
'aqua': 'b',
'red': 'c',
'light_purple': 'd',
'yellow': 'e',
'white': 'f'
};
return dictionary[string] || 'f';
};