Fix Berolina en passant
[vchess.git] / client / src / views / Game.vue
index ec25588..8f49c5d 100644 (file)
@@ -14,10 +14,10 @@ main
         span {{ Object.keys(people).length + " " + st.tr["participant(s):"] }} 
         span(
           v-for="p in Object.values(people)"
-          v-if="!!p.name"
+          v-if="p.name"
         )
           | {{ p.name }} 
-        span.anonymous(v-if="Object.values(people).some(p => !p.name)")
+        span.anonymous(v-if="Object.values(people).some(p => !p.name && p.id === 0)")
           | + @nonymous
       Chat(
         :players="game.players"
@@ -240,12 +240,11 @@ export default {
       switch (data.code) {
         case "pollclients":
           data.sockIds.forEach(sid => {
-            this.$set(this.people, sid, { id: 0, name: "" });
             if (sid != this.st.user.sid) {
               this.send("askidentity", { target: sid });
               // Ask potentially missed last state, if opponent and I play
               if (
-                !!this.game.mycolor &&
+                this.game.mycolor &&
                 this.game.type == "live" &&
                 this.game.score == "*" &&
                 this.game.players.some(p => p.sid == sid)
@@ -256,10 +255,7 @@ export default {
           });
           break;
         case "connect":
-          if (!this.people[data.from])
-            // TODO: people array should be init only after identity is known
-            this.$set(this.people, data.from, { name: "", id: 0 });
-          if (!this.people[data.from].name) {
+          if (!this.people[data.from]) {
             this.newConnect[data.from] = true; //for self multi-connects tests
             this.send("askidentity", { target: data.from });
           }
@@ -267,8 +263,7 @@ export default {
         case "disconnect":
           this.$delete(this.people, data.from);
           break;
-        case "mconnect":
-        {
+        case "mconnect": {
           // TODO: from MyGames page : send mconnect message with the list of gid (live and corr)
           // Either me (another tab) or opponent
           const sid = data.from;
@@ -290,7 +285,7 @@ export default {
           alert(this.st.tr["New connexion detected: tab now offline"]);
           break;
         case "askidentity": {
-          // Request for identification (TODO: anonymous shouldn't need to reply)
+          // Request for identification
           const me = {
             // Decompose to avoid revealing email
             name: this.st.user.name,
@@ -301,8 +296,8 @@ export default {
           break;
         }
         case "identity": {
-          // TODO: init people array here.
           const user = data.data;
+          this.$set(this.people, user.sid, { name: user.name, id: user.id });
           if (user.name) {
             // If I multi-connect, kill current connexion if no mark (I'm older)
             if (
@@ -316,13 +311,6 @@ export default {
                 this.killed[this.st.user.sid] = true;
               }
             }
-            if (user.sid != this.st.user.sid) {
-              //I already know my identity...
-              this.$set(this.people, user.sid, {
-                id: user.id,
-                name: user.name
-              });
-            }
           }
           delete this.newConnect[user.sid];
           break;
@@ -399,7 +387,7 @@ export default {
             move.move,
             "received",
             null,
-            {addTime:move.addTime});
+            {addTime: move.addTime});
           break;
         }
         case "resign":
@@ -440,7 +428,7 @@ export default {
           data.lastMove.move,
           "received",
           null,
-          {addTime:data.lastMove.addTime, initime:data.initime});
+          {addTime: data.lastMove.addTime, initime: data.initime});
       }
       if (data.drawSent) this.drawOffer = "received";
       if (data.score != "*") {
@@ -507,24 +495,21 @@ export default {
               game.players[0]
             ];
           }
-          // corr game: need to compute the clocks + initime
           // NOTE: clocks in seconds, initime in milliseconds
-          game.clocks = [tc.mainTime, tc.mainTime];
           game.moves.sort((m1, m2) => m1.idx - m2.idx); //in case of
           const L = game.moves.length;
           if (game.score == "*") {
             // Set clocks + initime
+            game.clocks = [tc.mainTime, tc.mainTime];
             game.initime = [0, 0];
-            if (L >= 3) {
-              let addTime = [0, 0];
-              for (let i = 2; i < L; i++) {
-                addTime[i % 2] +=
-                  tc.increment -
-                  (game.moves[i].played - game.moves[i - 1].played) / 1000;
+            if (L >= 1) {
+              const gameLastupdate = game.moves[L-1].played;
+              game.initime[L % 2] = gameLastupdate;
+              if (L >= 2) {
+                game.clocks[L % 2] =
+                  tc.mainTime - (Date.now() - gameLastupdate) / 1000;
               }
-              for (let i = 0; i <= 1; i++) game.clocks[i] += addTime[i];
             }
-            if (L >= 1) game.initime[L % 2] = game.moves[L - 1].played;
           }
           // Sort chat messages from newest to oldest
           game.chats.sort((c1, c2) => {
@@ -680,19 +665,20 @@ export default {
           var filtered_move = getFilteredMove(move);
         }
         // Send move ("newmove" event) to people in the room (if our turn)
-        let addTime = data ? data.addTime : 0;
+        let addTime = (data && this.game.type == "live") ? data.addTime : 0;
         if (moveCol == this.game.mycolor) {
           if (this.drawOffer == "received")
             // I refuse draw
             this.drawOffer = "";
-          if (this.game.movesCount >= 2) {
+          // 'addTime' is irrelevant for corr games:
+          if (this.game.type == "live" && this.game.movesCount >= 2) {
             const elapsed = Date.now() - this.game.initime[colorIdx];
             // elapsed time is measured in milliseconds
             addTime = this.game.increment - elapsed / 1000;
           }
           const sendMove = {
             move: filtered_move,
-            addTime: addTime,
+            addTime: addTime, //undefined for corr games
             cancelDrawOffer: this.drawOffer == "",
             // Players' SID required for /mygames page
             // TODO: precompute and add this field to game object?
@@ -705,9 +691,13 @@ export default {
         this.game.movesCount++;
         // TODO: notifyTurn
         // (add)Time indication: useful in case of lastate infos requested
-        this.game.moves.push({move:move, addTime:addTime});
+        this.game.moves.push(this.game.type == "live"
+          ? {move:move, addTime:addTime}
+          : move);
         this.game.fen = this.vr.getFen();
-        this.game.clocks[colorIdx] += addTime;
+        if (this.game.type == "live") this.game.clocks[colorIdx] += addTime;
+        // In corr games, just reset clock to mainTime:
+        else this.game.clocks[colorIdx] = extractTime(this.game.cadence).mainTime;
         // data.initime is set only when I receive a "lastate" move from opponent
         this.game.initime[nextIdx] = (data && data.initime) ? data.initime : Date.now();
         this.re_setClocks();