Skip to content

Commit

Permalink
better organized server state
Browse files Browse the repository at this point in the history
  • Loading branch information
btford committed Jul 16, 2012
1 parent 7e1c20d commit 35e9775
Showing 1 changed file with 71 additions and 49 deletions.
120 changes: 71 additions & 49 deletions routes/socket.js
Original file line number Diff line number Diff line change
@@ -1,64 +1,86 @@
// Keep track of which names are used so that there are no duplicates
var userNames = {};

var setUserName = function (name) {
if (!name || userNames[name]) {
return false;
} else {
userNames[name] = true;
return true;
}
};
var userNames = (function () {
var names = {};

var freeUserName = function (name) {
if (userNames[name]) {
delete userNames[name];
}
};
var claim = function (name) {
if (!name || userNames[name]) {
return false;
} else {
userNames[name] = true;
return true;
}
};

// find the lowest unused "guest" name and claim it
var getDefault = function () {
var name,
nextUserId = 1;

do {
name = 'Guest ' + nextUserId;
nextUserId += 1;
} while (!claim(name));

return name;
};

// serialize claimed names as an array
var get = function () {
var res = [];
for (user in userNames) {
res.push(user);
}

return res;
};

// find the lowest unused "guest" name and claim it
var newUserName = function () {
var name,
nextUserId = 1;
var free = function (name) {
if (userNames[name]) {
delete userNames[name];
}
};

do {
name = 'Guest ' + nextUserId;
nextUserId += 1;
} while (!setUserName(name));
return {
claim: claim,
free: free,
get: get,
getDefault: getDefault
};
}());

return name;
};

var getUsers = function () {
var res = [];
for (user in userNames) {
res.push(user);
}
var messages = (function () {
var messages = [];

return res;
};
var add = function (user, text) {
if (messages.length > 10) {
messages.shift();
}
messages.push({
user: user,
text: text
});
};

var messages = [];
var get = function () {
return messages;
};

var addMessage = function (user, text) {
if (messages.length > 10) {
messages.shift();
}
messages.push({
user: user,
text: text
});
}
return {
add: add,
get: get
};
}());

// export function for listening to the socket
module.exports = function (socket) {
var name = newUserName();
var name = userNames.getDefault();

// send the new user their name
socket.emit('set:name', {
name: name,
messages: messages,
users: getUsers()
messages: messages.get(),
users: userNames.get()
});

socket.broadcast.emit('user:join', {
Expand All @@ -71,14 +93,14 @@ module.exports = function (socket) {
user: name,
text: data.message
});
addMessage(name, data.message);
messages.add(name, data.message);
});

// validate a user's name change, and broadcast it on success
socket.on('change:name', function (data, fn) {
if (setUserName(data.name)) {
if (userNames.claim(data.name)) {
var oldName = name;
freeUserName(oldName);
userNames.free(oldName);

name = data.name;

Expand Down Expand Up @@ -107,6 +129,6 @@ module.exports = function (socket) {
socket.broadcast.emit('user:left', {
name: name
});
freeUserName(name);
userNames.free(name);
});
};

0 comments on commit 35e9775

Please sign in to comment.