Browse Source

start game when both players are connected

romanmatiasko 9 years ago
parent
commit
a136109b31
2 changed files with 21 additions and 14 deletions
  1. 4 0
      io.js
  2. 17 14
      src/js/components/GameInterface.js

+ 4 - 0
io.js

@@ -76,6 +76,10 @@ io.sockets.on('connection', socket => {
 
     game.get('creator').emit('ready');
     socket.emit('joined', {color: color});
+
+    if (nOfPlayers === 1) {
+      io.sockets.in(data.token).emit('both-joined');
+    }
   });
 
   socket.on('clock-run', data => runClock(data.color, data.token, socket));

+ 17 - 14
src/js/components/GameInterface.js

@@ -37,12 +37,6 @@ const GameInterface = React.createClass({
   componentDidMount() {
     const {io, params} = this.props;
 
-    io.emit('join', {
-      token: params[0],
-      time: params[1] * 60,
-      inc: params[2]
-    });
-
     io.on('token-invalid', () => this.setState({
       modal: this.state.modal
         .set('open', true)
@@ -50,19 +44,28 @@ const GameInterface = React.createClass({
         .set('type', 'info')
     }));
 
+    io.emit('join', {
+      token: params[0],
+      time: params[1] * 60,
+      inc: params[2]
+    });
+
     io.on('joined', data => {
-      if (data.color === 'white') {
-        io.emit('clock-run', {
-          token: params[0],
-          color: 'white'
-        });
-      } else {
+      if (data.color === 'black') {
         this.setState({color: 'black'});
       }
-
-      this.setState({isOpponentAvailable: true});
     });
 
+    io.on('both-joined', () =>
+      this.setState({isOpponentAvailable: true}, () => {
+        if (this.state.color === 'white') {
+          io.emit('clock-run', {
+            token: params[0],
+            color: 'white'
+          });
+        }
+      }));
+
     io.on('full', () => {
       window.alert(
         'This game already has two players. You have to create a new one.');