On update + clocks thinking
[vchess.git] / client / src / views / Game.vue
index 22cf5bf..5a64b79 100644 (file)
@@ -16,7 +16,8 @@ pareil quand quelqu'un reco.
 <template lang="pug">
 .row
   .col-sm-12.col-md-10.col-md-offset-1.col-lg-8.col-lg-offset-2
-    BaseGame(:game="game" :vr="vr" ref="basegame" @newmove="processMove")
+    BaseGame(:game="game" :vr="vr" ref="basegame"
+      @newmove="processMove" @gameover="gameOver")
     .button-group(v-if="game.mode!='analyze'")
       button(@click="offerDraw") Draw
       button(@click="abortGame") Abort
@@ -81,6 +82,7 @@ export default {
 
     // --> doivent être enregistrés comme observers au niveau du serveur...
     // non: poll users + events startObserving / stopObserving
+    // (à faire au niveau du routeur ?)
 
 
     // TODO: also handle "draw accepted" (use opponents array?)
@@ -238,11 +240,18 @@ export default {
         this.game = Object.assign({},
           game,
           // NOTE: assign mycolor here, since BaseGame could also bs VS computer
-          {mycolor: ["w","b"][game.players.findIndex(p => p.sid == this.st.user.sid)]},
+          {mycolor: [undefined,"w","b"][1 + game.players.findIndex(
+            p => p.sid == this.st.user.sid)]},
         );
         const vModule = await import("@/variants/" + game.vname + ".js");
         window.V = vModule.VariantRules;
         this.vr = new V(game.fen);
+        // Post-processing: decorate each move with current FEN:
+        // (to be able to jump to any position quickly)
+        game.moves.forEach(move => {
+          vr.play(move); //side-effect: set move.fen
+        });
+        this.vr.re_init(game.fen);
       });
 //    // Poll all players except me (if I'm playing) to know online status.
 //    // --> Send ping to server (answer pong if players[s] are connected)
@@ -254,12 +263,42 @@ export default {
 //      });
 //    }
     },
+    // TODO: refactor this old "oppConnected" logic
     oppConnected: function(uid) {
       return this.opponents.some(o => o.id == uid && o.online);
     },
     processMove: function(move) {
-      // TODO: process some opponent's move
+      if (!this.game.mycolor)
+        return; //I'm just an observer
+      // TODO: update storage (corr or live),
+      GameStorage.update({
+        fen: move: clocks: ................ // TODO
+      });
+      // Send move ("newmove" event) to opponent(s) (if ours)
+      if (move.disappear[0].c == this.game.mycolor)
+      {
+        this.game.players.forEach(p => {
+          if (p.sid != this.st.user.sid)
+            this.st.conn.send("newmove", {target:p.sid, move:move});
+        });
+      }
+    },
+    gameOver: function(score) {
+      // TODO: GameStorage.update with score
+      // NOTE: this update function should also work for corr games
     },
   },
 };
 </script>
+
+// TODO: utiliser "started" (renommer) pour se souvenir du timestamp où un user a commencé à réfléchir à un coup. Le sauvegarder dans update aussi...
+
+// variable initime à récupérer de game aussi (et pas "started")
+// si initime à -1 ou undefined alors pas commencé. Abort possible à tout moment avec message
+// Sorry I have to go / Game seems over / Game is not interesting
+
+move.clock à màj avec (current) clock - temps de réflexion (now() - initime) + increment
+après chaque coup dans une partie live ou corr non terminée.
+--> donc à Game.update on passe directement clock
+
+code "T" pour score "perte au temps" ?