Skip to content

Commit

Permalink
#18 - Server side check for duplicate usernames
Browse files Browse the repository at this point in the history
  • Loading branch information
tracend committed May 26, 2012
1 parent f18c342 commit 8cf945d
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 7 deletions.
5 changes: 2 additions & 3 deletions lib/game.js
Expand Up @@ -69,14 +69,13 @@ function start(){
.leave('arena')
.leave('lobby')
// login action
.on('login', function(email){
.on('login', function( name ){

// get username
//var user = io.sockets.sockets[sid];
var sid = user.id;
var player = players[sid];
// save username
player.name = email;
player.setName( name, players );
players[sid] = player;

// emit the id back to the user
Expand Down
24 changes: 24 additions & 0 deletions lib/player.js
Expand Up @@ -19,6 +19,30 @@ var Player = function(io){
// broadcast message
this.socket.emit("died", this.score);
this.socket.broadcast.emit("dead-invader", this.name);
},
setName: function( name, players )
{
// save the name only if it is unique for the current player list
var valid = true;
// check against the players array
for(i in players){
if( players[i].name == name ){
valid = false;
break;
}
}
// if it exists add three random characters at the end
if( !valid ) {
name += "_";
var chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
for(x=0;x<3;x++)
{
i = Math.floor(Math.random() * 62);
name += chars.charAt(i);
}
}
// save new name
this.name = name;
}

});
Expand Down
1 change: 1 addition & 0 deletions public/assets/js/logic/player.js
Expand Up @@ -20,6 +20,7 @@ return $.extend({}, (new User()), {
// events
socket.on('id', function( user ) {
self.set( user );
lobby.init();
});

socket.on('died', function( score ) {
Expand Down
6 changes: 2 additions & 4 deletions public/assets/js/rooms/login.js
Expand Up @@ -10,11 +10,9 @@ Login = function() {
var input = $(this).find("input[type='text']");
if( input.val() != "" ){
//if( validateEmail(input.val() ) ){
player.name = noSpecialChars( input.val() );
socket.emit('login', player.name );
var name = noSpecialChars( input.val() );
socket.emit('login', name );
self.remove();
// move this to a socket response
lobby.init();
} else {
//alert("A valid email is needed to Paypal the top score");
alert("Enter your username - any text string will do...");
Expand Down

0 comments on commit 8cf945d

Please sign in to comment.