Fix moves sending. TODO: fix double opp move + clocks update
[vchess.git] / client / src / views / Game.vue
index 920e9c8..8389bde 100644 (file)
@@ -97,19 +97,8 @@ export default {
           // TODO: observer on dark games must see all board ? Or alternate ? (seems better)
           // ...or just see nothing as on buho21
           this.$refs["basegame"].play(
-            data.move, this.game.vname!="Dark" ? "animate" : null);
+            data.move, this.game.vname!="Dark" ? "animate" : null); //TODO: arg to know if opponent move (or comp) in case of dark variant...
           this.processMove(data.move);
-
-
-
-// TODO:
-// send filtered_move + elapsed time
-// receive same. (update clock) + update (our) initime if it's my turn
-// + update fen (using vr.getFen())
-
-
-
-
           break;
         case "pong": //received if we sent a ping (game still alive on our side)
           if (this.gameRef.id != data.gameId)
@@ -261,8 +250,9 @@ export default {
         // Post-processing: decorate each move with current FEN:
         // (to be able to jump to any position quickly)
         game.moves.forEach(move => {
+          // TODO: this is doing manually what BaseGame.play() achieve...
           move.color = this.vr.turn;
-          vr.play(move);
+          this.vr.play(move);
           move.fen = this.vr.getFen();
         });
         this.vr.re_init(game.fen);
@@ -278,9 +268,9 @@ export default {
 //    }
     },
     // TODO: refactor this old "oppConnected" logic
-    oppConnected: function(uid) {
-      return this.opponents.some(o => o.id == uid && o.online);
-    },
+//    oppConnected: function(uid) {
+//      return this.opponents.some(o => o.id == uid && o.online);
+//    },
     // Post-process a move (which was just played)
     processMove: function(move) {
       if (!this.game.mycolor)
@@ -292,7 +282,7 @@ export default {
       const filtered_move = Object.keys(move)
         .filter(key => allowed_fields.includes(key))
         .reduce((obj, key) => {
-          obj[key] = raw[key];
+          obj[key] = move[key];
           return obj;
         }, {});
       // Send move ("newmove" event) to opponent(s) (if ours)
@@ -302,11 +292,11 @@ export default {
         const elapsed = Date.now() - GameStorage.getInitime();
         this.game.players.forEach(p => {
           if (p.sid != this.st.user.sid)
-            this.st.conn.send("newmove",
-            {
+            this.st.conn.send(JSON.stringify({
+              code: "newmove",
               target: p.sid,
               move: Object.assign({}, filtered_move, {elapsed: elapsed}),
-            });
+            }));
         });
         move.elapsed = elapsed;
       }
@@ -316,7 +306,7 @@ export default {
         fen: move.fen,
         elapsed: move.elapsed,
         increment: this.game.increment, //redundant but faster
-        initime: (this.vr.turn == this.game.mycolor), //it's my turn
+        initime: (this.vr.turn == this.game.mycolor), //is it my turn?
       });
     },
     // NOTE: this update function should also work for corr games