From 77f3ff26c1138050191d636d31f44a24ff4f5549 Mon Sep 17 00:00:00 2001 From: Luigi Pinca Date: Fri, 28 Dec 2012 11:17:23 +0100 Subject: [PATCH] refactored slash commands system --- app.js | 19 +++--- lib/room.js | 31 +++++---- package.json | 7 +- public/js/app.js | 173 ++++++++++++++++++++++++++++++++++------------- 4 files changed, 157 insertions(+), 73 deletions(-) diff --git a/app.js b/app.js index 50c8ddd..f950a07 100644 --- a/app.js +++ b/app.js @@ -121,9 +121,9 @@ io.sockets.on('connection', function(socket) { rooms[socket.roomname].guess(socket, guess); } }); - socket.on('ignore', function(baduser, callback) { - if (socket.roomname && typeof baduser === 'string' && typeof callback === 'function') { - rooms[socket.roomname].ignore(baduser, socket.nickname, callback); + socket.on('ignore', function(who, callback) { + if (socket.roomname && typeof who === 'string' && typeof callback === 'function') { + rooms[socket.roomname].ignore(who, socket.nickname, callback); } }); socket.on('joinanonymously', function(nickname, roomname) { @@ -142,9 +142,10 @@ io.sockets.on('connection', function(socket) { rooms[room].joinRoom(socket); } }); - socket.on('kick', function(baduser, callback) { - if (socket.roomname && typeof baduser === 'string' && typeof callback === 'function') { - rooms[socket.roomname].kick(baduser, socket.nickname, callback); + socket.on('kick', function(who, why, callback) { + if (socket.roomname && typeof who === 'string' && typeof why === 'string' && + typeof callback === 'function') { + rooms[socket.roomname].kick(who, why, socket.nickname, callback); } }); socket.on('loggedin', function(callback) { @@ -158,9 +159,9 @@ io.sockets.on('connection', function(socket) { rooms[socket.roomname].sendChatMessage(msg, socket, to); } }); - socket.on('unignore', function(baduser) { - if (socket.roomname && typeof baduser === 'string') { - rooms[socket.roomname].unignore(baduser, socket.nickname); + socket.on('unignore', function(who) { + if (socket.roomname && typeof who === 'string') { + rooms[socket.roomname].unignore(who, socket.nickname); } }); }); diff --git a/lib/room.js b/lib/room.js index ef606a4..3163df2 100644 --- a/lib/room.js +++ b/lib/room.js @@ -244,13 +244,13 @@ function Room(roomname) { } }; - this.ignore = function(baduser, executor, callback) { + this.ignore = function(who, executor, callback) { // Check if the player to be ignored is in the room - if (usersData[baduser]) { + if (usersData[who]) { // Inform the bad player that he/she is being ignored - var recipient = sockets[baduser]; - recipient.emit('chatmsg', executor+' is ignoring you.', 'binb', baduser); - return callback(baduser); + var recipient = sockets[who]; + recipient.emit('chatmsg', executor+' is ignoring you.', 'binb', who); + return callback(who); } callback(false); }; @@ -262,13 +262,16 @@ function Room(roomname) { }; // Kick a user - this.kick = function(baduser, executor, callback) { + this.kick = function(who, why, executor, callback) { usersdb.hget('user:'+executor, 'role', function (err, role) { if (role > 0) { // Check role - if (usersData[baduser]) { - var notice = 'you have been kicked by '+executor+'.'; - var recipient = sockets[baduser]; - recipient.emit('chatmsg', notice, 'binb', baduser); + if (usersData[who]) { + if (why) { + why = ' ('+why+')'; + } + var notice = 'you have been kicked by '+executor+why+'.'; + var recipient = sockets[who]; + recipient.emit('chatmsg', notice, 'binb', who); recipient.disconnect(); } return; @@ -448,12 +451,12 @@ function Room(roomname) { sendLoadTrack(); }; - this.unignore = function(baduser, executor) { - if (usersData[baduser]) { + this.unignore = function(who, executor) { + if (usersData[who]) { // Inform the bad player that he/she is no longer ignored var notice = executor+' has stopped ignoring you.'; - var recipient = sockets[baduser]; - recipient.emit('chatmsg', notice, 'binb', baduser); + var recipient = sockets[who]; + recipient.emit('chatmsg', notice, 'binb', who); } }; } diff --git a/package.json b/package.json index 1d77561..f2e491d 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,8 @@ "socket.io" ], "dependencies": { - "canvas": "0.13.x" + "canvas": "0.13.x", + "ws": "0.4.x" }, "engines": { "node": "0.8.x" @@ -21,5 +22,5 @@ "start": "app.js" }, "subdomain": "binb", - "version": "0.3.5-2" -} \ No newline at end of file + "version": "0.3.5-3" +} diff --git a/public/js/app.js b/public/js/app.js index 2442b36..252da32 100644 --- a/public/js/app.js +++ b/public/js/app.js @@ -1,5 +1,5 @@ (function() { - + var elapsedtime = 0 , DOM = {} , historycursor = 0 @@ -289,8 +289,7 @@ loadFromCookie(); }; - /* Triggered when a logged user tries to join a room from another tab or another browser - and he is already in a room */ + // Called when a registered user already in a room, tries to enter in another room var alreadyInARoom = function() { var html = ''; html += '