Some fixes. TODO: game.mode must be analyze when score is not '*' ==> simplify Board...
[vchess.git] / client / src / views / Game.vue
index 0540fbb..9be8806 100644 (file)
@@ -1,11 +1,19 @@
 <template lang="pug">
 .row
   .col-sm-12.col-md-10.col-md-offset-1.col-lg-8.col-lg-offset-2
+    input#modalAbort.modal(type="checkbox")
+    div(role="dialog" aria-labelledby="abortBoxTitle")
+      .card.smallpad.small-modal.text-center
+        label.modal-close(for="modalAbort")
+        h3#abortBoxTitle.section {{ st.tr["Terminate game?"] }}
+        button(@click="abortGame") {{ st.tr["Sorry I have to go"] }}
+        button(@click="abortGame") {{ st.tr["Game seems over"] }}
+        button(@click="abortGame") {{ st.tr["Game is too boring"] }}
     BaseGame(:game="game" :vr="vr" ref="basegame"
       @newmove="processMove" @gameover="gameOver")
-    .button-group(v-if="game.mode!='analyze'")
+    .button-group(v-if="game.mode!='analyze' && game.score=='*'")
       button(@click="offerDraw") Draw
-      button(@click="abortGame") Abort
+      button(@click="() => abortGame()") Abort
       button(@click="resign") Resign
     div(v-if="game.mode=='corr'")
       textarea(v-show="score=='*' && vr.turn==game.mycolor" v-model="corrMsg")
@@ -75,8 +83,6 @@ export default {
       switch (data.code)
       {
         case "newmove":
-          // TODO: observer on dark games must see all board ? Or alternate ? (seems better)
-          // ...or just see nothing as on buho21
           // NOTE: next call will trigger processMove()
           this.$refs["basegame"].play(data.move,
             "receive", this.game.vname!="Dark" ? "animate" : null);
@@ -203,16 +209,31 @@ export default {
       // TODO: ignore if preventDrawOffer is set; otherwise show modal box with option "prevent future offers"
       // if accept: send message "draw"
     },
-    abortGame: function() {
-      if (!confirm("Abort the game?"))
+    abortGame: function(event) {
+      if (!event)
+      {
+        // First call show options:
+        let modalBox = document.getElementById("modalAbort");
+        modalBox.checked = true;
+      }
+      else
+      {
+        console.log(event);
         return;
-      
-// Abort possible à tout moment avec message
-// Sorry I have to go / Game seems over / Game is not interesting
-
-      //+ bouton "abort" avec score == "?" + demander confirmation pour toutes ces actions,
-      //send message: "gameOver" avec score "?"
-      // ==> BaseGame must listen to game.score change, and call "endgame(score)" in this case
+        //const message = event.
+        // Next line will trigger a "gameover" event, bubbling up till here
+        this.$refs["basegame"].endGame("?");
+        this.game.players.forEach(p => {
+          if (!!p.sid && p.sid != this.st.user.sid)
+          {
+            this.st.conn.send(JSON.stringify({
+              code: "abort",
+              msg: message,
+              target: p.sid,
+            }));
+          }
+        });
+      }
     },
     resign: function(e) {
       if (!confirm("Resign the game?"))
@@ -236,25 +257,15 @@ export default {
     //  - from remote peer (one live game I don't play, finished or not)
     loadGame: function() {
       GameStorage.get(this.gameRef, async (game) => {
+        const vModule = await import("@/variants/" + game.vname + ".js");
+        window.V = vModule.VariantRules;
+        this.vr = new V(game.fenStart);
         this.game = Object.assign({},
           game,
           // NOTE: assign mycolor here, since BaseGame could also bs VS computer
           {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 => {
-          // NOTE: this is doing manually what BaseGame.play() achieve...
-          // but in a lighter "fast-forward" way
-          move.color = this.vr.turn;
-          this.vr.play(move);
-          move.fen = this.vr.getFen();
-        });
-        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)