Skip to content
Permalink
Browse files

Emotes, pony, anon users and stuff like that with commands

  • Loading branch information...
Depado committed May 15, 2014
1 parent 9953962 commit d50fcca9651f1b0f6382beaba5594aec11674d4a
64 app.js
@@ -18,18 +18,61 @@ app.use(express.static(__dirname + '/bower_components'));
app.use('/custom', express.static(__dirname + '/static'));

var usernames = {};
var anon_users = 0;
var emotes = {
':angry:': {
'image': 'angry.gif'
},
':hi:': {
'image': 'hi.gif'
},
':sigh:': {
'image': 'sigh.gif'
},
':smirk:': {
'image': 'smirk.gif'
}
};

function generate_img_string (arg) {
var str = "<img src='/custom/images/" + emotes[arg]['image'] + "'";
if('height' in emotes[arg]) {
str += "style='height:" + emotes[arg]['height'] + "px; padding-bottom:" +
emotes[arg]['height']/10 + "px;'";
}
str += ">";
return str;
}

io.set('log level', 1);
io.sockets.on('connection', function (socket) {

io.sockets.emit('updateusers', usernames);
anon_users += 1;
io.sockets.emit('updateusers', {'usernames':usernames, 'anon': anon_users});

// Broadcasts the message sent to all users after escaping it
socket.on('sendchat', function (data) {
if(socket.username == null) {
socket.emit('server-message', 'You are not connected.');
} else {
io.sockets.emit('updatechat', socket.username, validator.escape(data));
} else if (data == '/help') {
var help_message = 'This is the help message. Only you can see this.<br />Emotes :<br />';
for (var key in emotes) {
help_message += generate_img_string(key) + " " + key + "&nbsp;&nbsp;";
};
help_message += '<br />Commands :<br />/dash : Launches a wild pony over the screen of everyone !';
socket.emit('server-message', help_message);
} else if (data == '/dash') {
io.sockets.emit('dash');
io.sockets.emit('server-message', socket.username + ' launches a wild pony !');
}else {
var data = validator.escape(data);
for(var key in emotes) {
if(data.indexOf(key) != -1) {
var re = new RegExp(key,"g");
data = data.replace(re, generate_img_string(key));
}
};
io.sockets.emit('updatechat', socket.username, data);
}
});

@@ -46,21 +89,26 @@ io.sockets.on('connection', function (socket) {
socket.emit('server-message', 'Now known as ' + new_username);
socket.broadcast.emit('server-message', socket.username + ' is now known as ' + new_username);
} else {
socket.emit('server-message', 'Now connected as ' + new_username);
socket.emit('server-message', 'Now connected as ' + new_username + ' (/help for commands)');
socket.emit('connection-success');
socket.broadcast.emit('server-message', new_username + ' is now connected');
anon_users -= 1;
}
socket.username = new_username;
usernames[new_username] = new_username;
io.sockets.emit('updateusers', usernames);
io.sockets.emit('updateusers', {'usernames':usernames, 'anon': anon_users});
}
});

// Removing the user in case the websocket is closed
socket.on('disconnect', function(){
delete usernames[socket.username];
io.sockets.emit('updateusers', usernames);
socket.broadcast.emit('server-message', socket.username + ' has disconnected');
if(typeof socket.username != 'undefined') {
delete usernames[socket.username];
io.sockets.emit('updateusers', {'usernames':usernames, 'anon': anon_users});
socket.broadcast.emit('server-message', socket.username + ' has disconnected');
} else {
anon_users -= 1;
}
});
});

@@ -51,4 +51,5 @@ body {
font-smooth: never;
-webkit-font-smoothing : none;
font-size: 8px;
overflow: hidden;
}
@@ -0,0 +1,92 @@
function scrollDown () {
var objDiv = document.getElementById('toscroll');
objDiv.scrollTop = objDiv.scrollHeight;
}
var isActive;
window.onfocus = function () { isActive = true; document.title = "Unicorn Chat"; };
window.onblur = function () { isActive = false; };

var socket = io.connect(window.location.protocol+'//'+window.location.hostname+':'+window.location.port);

socket.on('updatechat', function (username, data) {
var d = new Date();
$('#conversation').append(
'<li class="left"><div class="chat-body"><p>['
+ d.getHours() + ':' + (d.getMinutes()<10?'0':'') + d.getMinutes() + '] ' + username + ' > ' + data
+ '</p></div></li>'
);
if(!isActive && document.title != "[New] Unicorn Chat") document.title = "[New] Unicorn Chat";
scrollDown();
});

socket.on('server-message', function(data) {
$('#conversation').append('<p>'+ data + '</p>');
if(!isActive && document.title != "[New] Unicorn Chat") document.title = "[New] Unicorn Chat";
scrollDown();
});

socket.on('connection-success', function() {
$('#nickname-send').html('Change');
$('#data').show();
$('#activate-browser-notifications').show();
});

socket.on('error', function (data) {
$('#error-message').text(data);
$('#error-message').show();
setTimeout(function() {
$('#error-message').fadeOut(500);
}, 2000);
});

socket.on('updateusers', function(data) {
$('#users').empty();
$.each(data['usernames'], function(key, value) {
$('#users').append('<div>' + key + '</div>');
});
$('#users').append('<div>Anonymous users : ' + data['anon'] + '</div>');
});

$(function(){
$('#data').keypress(function(e) {
if(e.which == 13) {
var message = $('#data').val();
if(message != '') {
$('#data').val('');
socket.emit('sendchat', message);
}
}
});

$('#nickname-send').click(function() {
socket.emit('adduser', $('#nickname-input').val());
$('#nickname-input').val('');
});

$('#nickname-input').keypress(function(e) {
if(e.which == 13) {
socket.emit('adduser', $('#nickname-input').val());
$('#nickname-input').val('');
}
});

$('#data').hide();
$('#activate-browser-notifications').hide();

var windowWidth = $(document).width();
var windowHeight = $(document).height();
var current_dash = 0;
socket.on('dash', function() {
current_dash += 1
var this_dash = current_dash;
$('body').append('<img id="dash_'+ this_dash +
'" src="/custom/images/rdash.gif" style="position: absolute; bottom:' +
Math.floor((Math.random() * windowHeight) + 1) + 'px;">');
$("#dash_" + this_dash).animate({
left: windowWidth
}, 5000, function() {
$('#dash_' + this_dash).remove();
});
});

});
Binary file not shown.
BIN +1.18 KB static/images/hi.gif
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN +802 Bytes static/images/smirk.gif
Binary file not shown.
@@ -8,84 +8,7 @@
<link href='//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css' rel='stylesheet'>
<link rel='stylesheet' type='text/css' href='/custom/custom.css'>
<script src='/socket.io/socket.io.js'></script>

<script>
function scrollDown () {
var objDiv = document.getElementById('toscroll');
objDiv.scrollTop = objDiv.scrollHeight;
}
var isActive;
window.onfocus = function () { isActive = true; document.title = "Unicorn Chat"; };
window.onblur = function () { isActive = false; };
var socket = io.connect(window.location.protocol+'//'+window.location.hostname+':'+window.location.port);
socket.on('updatechat', function (username, data) {
var d = new Date();
$('#conversation').append(
'<li class="left"><div class="chat-body"><p>['
+ d.getHours() + ':' + d.getMinutes() + '] ' + username + ' > ' + data
+ '</p></div></li>'
);
if(!isActive && document.title != "[New] Unicorn Chat") document.title = "[New] Unicorn Chat";
scrollDown();
});
socket.on('server-message', function(data) {
$('#conversation').append('<p>'+ data + '</p>');
if(!isActive && document.title != "[New] Unicorn Chat") document.title = "[New] Unicorn Chat";
scrollDown();
});
socket.on('connection-success', function() {
$('#nickname-send').html('Change');
$('#data').show();
$('#activate-browser-notifications').show();
});
socket.on('error', function (data) {
$('#error-message').text(data);
$('#error-message').show();
setTimeout(function() {
$('#error-message').fadeOut(500);
}, 2000);
});
socket.on('updateusers', function(data) {
$('#users').empty();
$.each(data, function(key, value) {
$('#users').append('<div>' + key + '</div>');
});
});
$(function(){
$('#data').keypress(function(e) {
if(e.which == 13) {
var message = $('#data').val();
if(message != '') {
$('#data').val('');
socket.emit('sendchat', message);
}
}
});
$('#nickname-send').click(function() {
socket.emit('adduser', $('#nickname-input').val());
$('#nickname-input').val('');
});
$('#nickname-input').keypress(function(e) {
if(e.which == 13) {
socket.emit('adduser', $('#nickname-input').val());
$('#nickname-input').val('');
}
});
$('#data').hide();
$('#activate-browser-notifications').hide();
});
</script>
<script src='/custom/custom.js'></script>
</head>
<body>
<div class="container-fluid">

0 comments on commit d50fcca

Please sign in to comment.
You can’t perform that action at this time.