Styling, adjustments
[vchess.git] / client / src / views / Game.vue
index 5199154..00414d9 100644 (file)
@@ -1,17 +1,21 @@
 <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" @gameover="gameOver")
-    div Names: {{ game.players[0].name }} - {{ game.players[1].name }}
-    div(v-if="game.score=='*'") Time: {{ virtualClocks[0] }} - {{ virtualClocks[1] }}
-    .button-group(v-if="game.mode!='analyze' && game.score=='*'")
-      button(@click="offerDraw") Draw
-      button(@click="abortGame") Abort
-      button(@click="resign") Resign
-    div(v-if="game.type=='corr'") {{ game.corrMsg }}
-    textarea(v-if="game.score=='*'" v-model="corrMsg")
-    Chat(:players="game.players")
+main
+  .row
+    .col-sm-12.col-md-3
+      Chat(:players="game.players")
+    .col-sm-12.col-md-9
+      BaseGame(:game="game" :vr="vr" ref="basegame"
+        @newmove="processMove" @gameover="gameOver")
+  .row
+    .col-sm-12.col-md-9.col-md-offset-3
+      .button-group(v-if="game.mode!='analyze' && game.score=='*'")
+        button(@click="offerDraw") Draw
+        button(@click="abortGame") Abort
+        button(@click="resign") Resign
+      div Names: {{ game.players[0].name }} - {{ game.players[1].name }}
+      div(v-if="game.score=='*'") Time: {{ virtualClocks[0] }} - {{ virtualClocks[1] }}
+      div(v-if="game.type=='corr'") {{ game.corrMsg }}
+      textarea(v-if="game.score=='*'" v-model="corrMsg")
 </template>
 
 <script>
@@ -44,6 +48,7 @@ export default {
       drawOffer: "", //TODO: use for button style
       people: [], //players + observers
       lastate: undefined, //used if opponent send lastate before game is ready
+      repeat: {}, //detect position repetition
     };
   },
   watch: {
@@ -195,7 +200,7 @@ export default {
           break;
         case "newmove":
           this.corrMsg = data.move.message; //may be empty
-          this.game.moveToPlay = data.move;
+          this.$set(this.game, "moveToPlay", data.move); //TODO: Vue3...
           break;
         case "lastate": //got opponent infos about last move
         {
@@ -243,7 +248,7 @@ export default {
       if (data.movesCount > L)
       {
         // Just got last move from him
-        this.game.moveToPlay = data.lastMove;
+        this.$set(this.game, "moveToPlay", data.lastMove);
         if (data.score != "*" && this.game.score == "*")
         {
           // Opponent resigned or aborted game, or accepted draw offer
@@ -257,7 +262,7 @@ export default {
     },
     setScore: function(score, message) {
       this.game.scoreMsg = message;
-      this.game.score = score;
+      this.$set(this.game, "score", score); //TODO: Vue3...
     },
     offerDraw: function() {
       if (this.drawOffer == "received")
@@ -403,6 +408,7 @@ export default {
             oppid: (myIdx < 0 ? undefined : game.players[1-myIdx].uid),
           }
         );
+        this.repeat = {}; //reset
         if (!!this.lastate) //lastate arrived before game was loaded:
           this.processLastate();
         callback();
@@ -501,10 +507,21 @@ export default {
       //TODO: (Vue3) just this.game.clocks[colorIdx] += addTime;
       this.$set(this.game.clocks, colorIdx, this.game.clocks[colorIdx] + addTime);
       this.game.initime[nextIdx] = Date.now();
+      // If repetition detected, consider that a draw offer was received:
+      const fenObj = V.ParseFen(move.fen);
+      let repIdx = fenObj.position + "_" + fenObj.turn;
+      if (!!fenObj.flags)
+        repIdx += "_" + fenObj.flags;
+      this.repeat[repIdx] = (!!this.repeat[repIdx]
+        ? this.repeat[repIdx]+1
+        : 1);
+      if (this.repeat[repIdx] >= 3)
+        this.drawOffer = "received"; //TODO: will print "mutual agreement"...
     },
     gameOver: function(score) {
       this.game.mode = "analyze";
-      this.game.score = score;
+      this.game.score = score; //until Vue3, this property change isn't seen
+                               //by child (and doesn't need to be)
       const myIdx = this.game.players.findIndex(p => {
         return p.sid == this.st.user.sid || p.uid == this.st.user.id;
       });
@@ -516,5 +533,15 @@ export default {
 </script>
 
 <style lang="sass">
-// TODO
+.connected
+  background-color: green
+
+.disconnected
+  background-color: red
+
+.white-turn
+  background-color: white
+
+.black-turn
+  background-color: black
 </style>