Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Users should gracefully be removed from the game

  • Loading branch information...
commit 9c6239728f30f5ce9533003880ac214a10903951 1 parent d59ec52
@rhussmann rhussmann authored
Showing with 83 additions and 1 deletion.
  1. +82 −1 lib/gamemanager.js
  2. +1 −0  server.js
View
83 lib/gamemanager.js
@@ -104,6 +104,86 @@ GameManager.prototype.addUser = function(client, username) {
return {success: true};
};
+GameManager.prototype.removeUser = function(client) {
+
+ var manager = this;
+ var clientConnection = manager.connections[client.sessionId]
+
+ // Check to see if this user is even in the cache
+ // if not, there's nothing to do
+ if (!clientConnection) {
+ console.log("Removed a user that hasn't authenticated.");
+ return;
+ }
+
+ var game = clientConnection.game;
+ var playa = clientConnection.playa;
+
+ // Remove the player from the connections cache
+ delete manager.connections[client.sessionId];
+ delete manager.connections[playa.username];
+
+ var playaIndex = game.playas.indexOf(playa);
+ if (playaIndex === -1) {
+ // Something weird happened
+ console.log("ATTEMPTING TO REMOVE USER THAT ISN'T IN GAME!");
+ return;
+ }
+
+ game.playas.splice(playaIndex, 1);
+
+ // Send out the new userlist
+ // Create the list of users in this game
+ var userlist = [];
+ game.playas.forEach(function(element) {
+ userlist.push(element.username);
+ });
+
+ // Send all users in the game the updated userlist
+ userlist.forEach(function(currentUsername) {
+ var client = manager.connections[currentUsername].client;
+ userlistmsg = { type: "userlist", msg: userlist };
+ client.send(JSON.stringify(userlistmsg));
+ });
+
+ // If game is not in progress, check to see if it's
+ // ready to start
+ if (game.inProgress === false) {
+
+ var isAllRemainingPlayersReady = true;
+ game.playas.forEach(function(element){
+ if (element.ready === false) {
+ isAllRemainingPlayersReady = false;
+ }
+ });
+
+ if (isAllRemainingPlayersReady && game.playas.length > 1) {
+ // TODO: Alert all players in the game that
+ // game can start and assign first player
+ console.log("Player that left was holding up game. Starting...");
+
+ // Send the 'gamestart' message to the team
+ var startGameMsg = {type: "gamestart", msg: "FRUIT!!!"};
+ game.playas.forEach(function(element) {
+ console.log("Alerting " + element.username + " game is starting");
+ game.inProgress = true;
+ var aConnection = manager.connections[element.username];
+ aConnection.client.send(JSON.stringify(startGameMsg));
+ });
+
+ // Send the first player a 'yourturn' message
+ console.log("Determining first player...");
+ var nextPlaya = game.getNextPlaya();
+ console.log("First player will be: " + nextPlaya.username);
+ var nextPlayaConnection = manager.connections[nextPlaya.username];
+ var yourTurnMsg = {type: "yourturn",
+ msg: "It's your turn " + nextPlaya.username};
+
+ nextPlayaConnection.client.send(JSON.stringify(yourTurnMsg));
+ }
+ }
+};
+
GameManager.prototype.getUsers = function() {
return this.usernames;
};
@@ -278,7 +358,8 @@ GameManager.prototype.placeShips = function(client, locations) {
});
// If not everyone is ready, we're done here
- if(!everyoneReady) {
+ if(!everyoneReady || game.playas.length === 1) {
+ console.log("Not everyone is ready, or there is only one player");
return;
}
View
1  server.js
@@ -106,6 +106,7 @@ io.on("connection", function(client) {
client.on("disconnect", function() {
console.log("Socket.IO Client Disconnected");
+ manager.removeUser(client);
});
manager.on("winner", function (connection) {
Please sign in to comment.
Something went wrong with that request. Please try again.