Permalink
Browse files

Implemented player cap of 5

  • Loading branch information...
1 parent 48a9682 commit c49c99522fc02c181641d3a85ebe6aa3e84f9eca @rhussmann rhussmann committed Aug 29, 2010
Showing with 15 additions and 5 deletions.
  1. +12 −2 lib/gamemanager.js
  2. +3 −3 server.js
View
@@ -78,10 +78,15 @@ GameManager.prototype.addUser = function(client, username) {
// Check if username is already in use
if (this.usernames.indexOf(username) !== -1) {
- return false;
+ return {success: false, reason: "User exists"};
}
var existingGame = this.getCurrentGame();
+ // Is the game full?
+ if (existingGame.isFull()) {
+ return {success: false, reason: "Game is full"};
+ }
+
this.usernames.push(username);
var playa = new Playa(username, existingGame);
existingGame.addPlayer(playa);
@@ -91,7 +96,7 @@ GameManager.prototype.addUser = function(client, username) {
this.connections[username] = connection;
this.emit("newuser", connection);
- return true;
+ return {success: true};
};
GameManager.prototype.getUsers = function() {
@@ -307,6 +312,7 @@ function Game() {
this.name = this.battleNames[Math.floor(Math.random()*this.battleNames.length)];
this.playas = [];
this.currentPlaya = 0;
+ this.maxPlayers = 5;
// listeners
this.on("winner", function () {
@@ -316,6 +322,10 @@ function Game() {
Game.prototype = new process.EventEmitter();
+Game.prototype.isFull = function () {
+ return this.playas.length >= this.maxPlayers;
+};
+
Game.prototype.addPlayer = function (player) {
this.playas.push(player);
};
View
@@ -55,15 +55,15 @@ var dispatch = {
response.msg = "NOOOOOOOOOOO!";
} else {
var username = message.msg;
- var success = manager.addUser(client, username);
- if (success) {
+ var couldAddUser = manager.addUser(client, username);
+ if (couldAddUser["success"]) {
response.type = "auth";
response.status = "success";
response.msg = "Welcome "+username;
} else {
response.type = "auth";
response.status = "ufail";
- response.msg = "Username taken";
+ response.msg = couldAddUser["reason"];
}
}
client.send(JSON.stringify(response));

0 comments on commit c49c995

Please sign in to comment.