Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Bringing so much awesome

  • Loading branch information...
commit f5d4e87f62a5177b340d3482a61b48ec5da74a8c 2 parents 1d31ef7 + 3db8103
@teleject teleject authored
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  public/css/style.css
@@ -450,6 +450,7 @@ section.myboard {
}
+
.gameboard_frame .gameboard_frame {
}
View
6 public/index.html
@@ -20,8 +20,14 @@
<a href="#" onClick="APPVIEW.next_page();return false;"><img src="images/main-screen.png" alt="Man the GUNS! Your crew needs you!"></a>
</section>
<section class="buttons">
+<<<<<<< HEAD
<button class="large" onclick="APPVIEW.goto('rules.html');">Rules</button>
<button class="large orangellow" onClick="APPVIEW.next_page();">Play Now!</button>
+=======
+ <!--<button class="large">Rules</button> No rules brother. -->
+ <!--<button class="large orangellow" onClick="APPVIEW.next_page();">Play Now!</button>-->
+ <button class="large orangellow">The Battle Begins at Midnight</button>
+>>>>>>> 3db8103c1f34edba09d43f091ac4a2174dc45a08
</section>
<section class="mod about">
<p>Welcome to the crazy party boat, the HMS Dreadnode. We are a team in the 2010 Node Knockout competition.</p>
View
6 public/js/socket.js
@@ -89,6 +89,12 @@
global.APPVIEW.win_state = true;
global.APPVIEW.next_page();
},
+ userlist : function(message) {
+ var userlist = message.msg || [];
+ console.log(JSON.stringify(userlist));
+ alert(JSON.stringify(userlist));
+ // TODO: Update player listing.
+ },
yourturn : function(message) {
var msg = message.msg || "";
console.log(msg);
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.