Browse Source

fix delete game with only one player on disconnect; update dependencies

romanmatiasko 4 years ago
parent
commit
f8735db00c
6 changed files with 61 additions and 51 deletions
  1. 12 12
      dist/js/play.js
  2. 16 22
      io.js
  3. 0 5
      jest-preprocessor.js
  4. 10 9
      package.json
  5. 2 2
      routes/routes.js
  6. 21 1
      spec/io-spec.js

File diff suppressed because it is too large
+ 12 - 12
dist/js/play.js


+ 16 - 22
io.js

@@ -68,7 +68,6 @@ io.sockets.on('connection', socket => {
 
     _games = _games.updateIn([data.token, 'players'], players =>
       players.push(Map({
-        id: socket.id,
         socket: socket,
         color: color,
         time: data.time - data.inc + 1,
@@ -79,7 +78,7 @@ io.sockets.on('connection', socket => {
     socket.emit('joined', {color: color});
 
     if (nOfPlayers === 1) {
-      io.sockets.in(data.token).emit('both-joined');
+      io.to(data.token).emit('both-joined');
     }
   });
 
@@ -99,7 +98,7 @@ io.sockets.on('connection', socket => {
     if (!_games.has(data.token)) return;
     clearInterval(_games.getIn([data.token, 'interval']));
 
-    io.sockets.in(data.token).emit('player-resigned', {
+    io.to(data.token).emit('player-resigned', {
       color: data.color
     });
   });
@@ -119,27 +118,17 @@ io.sockets.on('connection', socket => {
         .set('inc', data.inc)
         .update('color', color => color === 'black' ? 'white' : 'black')));
 
-    io.sockets.in(data.token).emit('rematch-accepted');
+    io.to(data.token).emit('rematch-accepted');
   });
 
-  socket.on('disconnect', data => {
-    let tokenToDelete;
+  socket.on('disconnect', () => {
+    const token = findToken(socket);
 
-    _games.forEach((game, token) => {
-      const opponent = getOpponent(token, socket);
+    if (!token) return;
 
-      if (opponent) {
-        opponent.get('socket').emit('opponent-disconnected');
-        clearInterval(game.get('interval'));
-        tokenToDelete = token;
-
-        return false;
-      }
-    });
-
-    if (tokenToDelete) {
-      _games = _games.delete(tokenToDelete);
-    }
+    maybeEmit('opponent-disconnected', {}, token, socket);
+    clearInterval(_games.getIn([token, 'interval']));
+    _games = _games.delete(token);
   });
 
 });
@@ -153,6 +142,11 @@ function maybeEmit(event, data, token, socket) {
   }
 }
 
+function findToken(socket) {
+  return _games.findKey((game, token) =>
+    game.get('players').some(player => player.get('socket') === socket));
+}
+
 function runClock(color, token, socket) {
   if (!_games.has(token)) return;
 
@@ -171,12 +165,12 @@ function runClock(color, token, socket) {
           });
 
           if (timeLeft >= 0) {
-            io.sockets.in(token).emit('countdown', {
+            io.to(token).emit('countdown', {
               time: timeLeft,
               color: color
             });
           } else {
-            io.sockets.in(token).emit('countdown-gameover', {
+            io.to(token).emit('countdown-gameover', {
               color: color
             });
             clearInterval(_games.getIn([token, 'interval']));

+ 0 - 5
jest-preprocessor.js

@@ -1,5 +0,0 @@
-const babel = require('babel');
-
-module.exports.process = function(sourceText, sourcePath) {
-  return babel.transform(sourceText).code;
-};

+ 10 - 9
package.json

@@ -1,6 +1,6 @@
 {
   "name": "retichess",
-  "version": "0.3.0",
+  "version": "0.3.1",
   "scripts": {
     "start": "node --harmony ./bin/www",
     "test": "node --harmony ./node_modules/jasmine/bin/jasmine.js && jest",
@@ -8,18 +8,18 @@
     "deploy": "NODE_ENV=production gulp"
   },
   "dependencies": {
-    "babel": "^4.7.13",
-    "body-parser": "^1.12.0",
+    "babel": "^4.7.16",
+    "body-parser": "^1.12.2",
     "chess.js": "jhlywa/chess.js",
     "classnames": "^1.2.0",
     "debug": "^2.1.3",
     "es6-shim": "^0.27.1",
     "eventemitter2": "^0.4.14",
-    "express": "^4.12.2",
+    "express": "^4.12.3",
     "flux": "^2.0.1",
-    "immutable": "^3.6.4",
+    "immutable": "^3.7.0",
     "jade": "^1.9.2",
-    "lodash.omit": "^3.0.0",
+    "lodash.omit": "^3.1.0",
     "morgan": "^1.5.2",
     "react": "^0.13.1",
     "serve-favicon": "^2.2.0",
@@ -28,6 +28,7 @@
     "winston": "^0.9.0"
   },
   "devDependencies": {
+    "babel-jest": "^4.0.0",
     "babelify": "^5.0.4",
     "browserify": "^9.0.3",
     "gulp": "^3.8.11",
@@ -37,20 +38,20 @@
     "gulp-imagemin": "^2.2.1",
     "gulp-notify": "^2.2.0",
     "gulp-sass": "^1.3.3",
-    "gulp-sourcemaps": "^1.5.0",
+    "gulp-sourcemaps": "^1.5.1",
     "gulp-streamify": "0.0.5",
     "gulp-uglify": "^1.1.0",
     "gulp-util": "^3.0.4",
     "jasmine": "^2.2.1",
     "jest-cli": "^0.4.0",
     "vinyl-source-stream": "^1.1.0",
-    "watchify": "2.4.0"
+    "watchify": "2.6.2"
   },
   "engines": {
     "node": "^0.12.0"
   },
   "jest": {
-    "scriptPreprocessor": "./jest-preprocessor.js",
+    "scriptPreprocessor": "<rootDir>/node_modules/babel-jest",
     "unmockedModulePathPatterns": [
       "eventemmiter2",
       "immutable",

+ 2 - 2
routes/routes.js

@@ -32,9 +32,9 @@ router.get('/play/:token/:time/:inc', (req, res) => {
 });
 
 router.get('/logs', (req, res) => {
-  fs.readFile(path.join(__dirname, 'logs/games.log'), (err, data) => {
+  fs.readFile(path.join(__dirname, '../logs/games.log'), (err, data) => {
     if (err) {
-      res.redirect('/');
+      return res.redirect('/');
     }
     res.set('Content-Type', 'text/plain');
     res.send(data);

+ 21 - 1
spec/io-spec.js

@@ -14,7 +14,7 @@ function getGames() {
   games = server.getGames();
 }
 
-describe('Socket.IO', () => {
+describe('io', () => {
   
   it('should create new game', done => {
     p1 = client.connect(url, options);
@@ -206,4 +206,24 @@ describe('Socket.IO', () => {
     });
   });
 
+  it('should delete game with only one player on disconnect', done => {
+    p3 = client.connect(url, options);
+    p3.on('connect', () => p3.emit('start'));
+    p3.on('created', data => p3.emit('join', {
+      token: data.token,
+      time: 10 * 60,
+      inc: 5
+    }));
+
+    p3.on('joined', () => {
+      getGames();
+      expect(games.size).toBe(1);
+      p3.disconnect();
+      setTimeout(() => {
+        getGames();
+        expect(games.size).toBe(0);
+        done();
+      }, 0);
+    });
+  });
 });