-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.js
40 lines (34 loc) · 1.11 KB
/
handler.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
var chalk = require('chalk');
var log = console.log;
var commands = require('./commands');
var config = require('./config');
var context = require('./context');
var parser = require('./parser');
module.exports = {
connect: function(spinner) {
spinner.stop();
log("\n" + chalk.green("Connected!") + " Websocket at " + chalk.bold(config.url));
this.emit('e', {"msg":"/join lobby", "event":"c"});
this.emit('e', {"username":config.username,"event":"nametaken"});
},
disconnect: function() {
log(chalk.red("Disconnected!"));
},
event: function(data) {
data = data.split('\n');
var _this = this;
data.forEach(function (line) {
if ('cp'.indexOf(line.charAt(0)) < 0) return;
var chat = parser.parseChatEvent(line);
var cmder = parser.parseCommand(chat.msg);
var user = {name: chat.name};
var ctx = context(_this, user.name);
log(chat);
if (!cmder || data.length > 1) return;
log(chalk.inverse(JSON.stringify(cmder)));
if (commands[cmder.cmd]) {
commands[cmder.cmd].call(ctx[chat.ctx], cmder.target, user);
}
});
}
};