Browse Source

do not redirect after game is over, allow chatting until one player disconnects

romanmatiasko 10 years ago
parent
commit
f4f3b5c6c0
2 changed files with 45 additions and 45 deletions
  1. 25 11
      public/javascripts/play.js
  2. 20 34
      server.js

+ 25 - 11
public/javascripts/play.js

@@ -1,6 +1,7 @@
 var $side  = 'w';
 var $piece = null;
 var $chess = new Chess();
+var $gameOver = false;
 
 function selectPiece(el) {
   el.addClass('selected');
@@ -21,7 +22,7 @@ function movePiece(from, to, promotion, rcvd) {
     promotion: promotion
   });
 
-  if (move) {
+  if (move && !$gameOver) {
     var tdFrom = $('td.' + from.toUpperCase());
     var tdTo = $('td.' + to.toUpperCase());
 
@@ -203,13 +204,24 @@ $(document).ready(function () {
   });
 
   $socket.on('opponent-disconnected', function (data) {
-    alert("Your opponent has disconnected.");
-    window.location = '/';
+    $('.resign').remove();
+    $('.chess_board a').off();
+    $('#sendMessage').off();
+    $('#sendMessage').submit(function(e) {
+      e.preventDefault();
+      alert("Your opponent has diconnected. You can't send messages.");
+    });
   });
 
-  $socket.on('opponent-resigned', function (data) {
-    alert("Your opponent has resigned. You won!");
-    window.location = '/';
+  $socket.on('player-resigned', function (data) {
+    $('.resign').remove();
+    $('.chess_board a').off();
+    var winner = data.color === 'w' ? 'Black' : 'White';
+    var loser = data.color === 'w' ? 'White' : 'Black';
+    var message = loser + ' resigned. ' + winner + ' wins.';
+    alert(message);
+    $('.feedback-move').text('');
+    $('.feedback-status').text(message);
   });
 
   $socket.on('full', function (data) {
@@ -248,11 +260,14 @@ $(document).ready(function () {
   });
 
   $socket.on('countdown-gameover', function (data) {
+    $('.chess_board a').off();
     var loser = data.color === 'black' ? 'Black' : 'White';
     var winner = data.color === 'black' ? 'White' : 'Black';
+    var message = loser + "'s time is out. " + winner + " won.";
     $('.resign').hide();
-    alert(loser + "'s time is out. " + winner + " won.");
-    window.location = '/';
+    alert(message);
+    $('.feedback-move').text('');
+    $('.feedback-status').text(message);
   });
 
 });
@@ -310,10 +325,9 @@ $(document).ready(function () {
 
   $('.resign').click(function (e) {
     $socket.emit('resign', {
-      'token': $token
+      'token': $token,
+      'color': $side
     });
-    alert('You resigned.');
-    window.location = '/';
   });
 
   $('a.chat').click(function (e) {

+ 20 - 34
server.js

@@ -152,11 +152,28 @@ io.sockets.on('connection', function (socket) {
   });
 
   socket.on('resign', function (data) {
-    cancelGame('opponent-resigned', data.token, socket);
+    clearInterval(games[data.token].interval);
+    io.sockets.in(data.token).emit('player-resigned', {
+      'color': data.color
+    });
   });
 
   socket.on('disconnect', function (data) {
-    cancelGame('opponent-disconnected', null, socket);
+    var player, opponent, game;
+    for (var token in games) {
+    game = games[token];
+
+      for (var j in game.players) {
+        player = game.players[j];
+
+        if (player.socket === socket) {
+          opponent = game.players[Math.abs(j - 1)];
+          opponent.socket.emit('opponent-disconnected');
+          clearInterval(games[token].interval);
+          delete games[token];
+        }
+      }
+    }
   });
 
   socket.on('send-message', function (data) {
@@ -166,6 +183,7 @@ io.sockets.on('connection', function (socket) {
 });
 
 function runTimer(color, token, socket) {
+  console.log(games);
   var player, time_left, game = games[token];
 
   for (var i in game.players) {
@@ -190,7 +208,6 @@ function runTimer(color, token, socket) {
             'color': color
           });
           clearInterval(games[token].interval);
-          delete games[token];
         }
       }, 1000);
     }
@@ -209,35 +226,4 @@ function getOpponent(token, socket) {
       return opponent;
     }
   }
-}
-
-function cancelGame(event, token, socket) {
-  var player, game, opponent;
-
-  function removeGame(game, opponent) {    
-    clearInterval(game.interval);
-
-    opponent.socket.emit(event);
-    delete games[token];
-  }
-
-  if (token) {
-    game = games[token];
-    opponent = getOpponent(token, socket);
-    removeGame(game, opponent);
-  } else {
-
-    for (var token in games) {
-    game = games[token];
-
-      for (var j in game.players) {
-        player = game.players[j];
-
-        if (player.socket === socket) {
-          opponent = game.players[Math.abs(j - 1)];
-          removeGame(game, opponent);
-        }
-      }
-    }
-  }
 }