Refactor endgame process, start working on game end (abort)
authorBenjamin Auder <benjamin.auder@somewhere>
Fri, 14 Jun 2019 16:05:23 +0000 (18:05 +0200)
committerBenjamin Auder <benjamin.auder@somewhere>
Fri, 14 Jun 2019 16:05:23 +0000 (18:05 +0200)
client/src/components/BaseGame.vue
client/src/translations/en.js
client/src/views/Game.vue
server/sockets.js

index 2cfa124..442579d 100644 (file)
@@ -85,25 +85,6 @@ export default {
       this.cursor = L-1;
       this.lastMove = (L > 0 ? this.moves[L-1]  : null);
     },
-    setEndgameMessage: function(score) {
-      let eogMessage = "Undefined";
-      switch (score)
-      {
-        case "1-0":
-          eogMessage = translations["White win"];
-          break;
-        case "0-1":
-          eogMessage = translations["Black win"];
-          break;
-        case "1/2":
-          eogMessage = translations["Draw"];
-          break;
-        case "?":
-          eogMessage = "Unfinished";
-          break;
-      }
-      this.endgameMessage = eogMessage;
-    },
     download: function() {
       const content = this.getPgn();
       // Prepare and trigger download link
@@ -137,18 +118,36 @@ export default {
       }
       return pgn + "\n";
     },
-    showScoreMsg: function(score) {
-      this.setEndgameMessage(score);
+    getScoreMessage: function(score) {
+      let eogMessage = "Undefined";
+      switch (score)
+      {
+        case "1-0":
+          eogMessage = this.st.tr["White win"];
+          break;
+        case "0-1":
+          eogMessage = this.st.tr["Black win"];
+          break;
+        case "1/2":
+          eogMessage = this.st.tr["Draw"];
+          break;
+        case "?":
+          eogMessage = this.st.tr["Unfinished"];
+          break;
+      }
+      return eogMessage;
+    },
+    showEndgameMsg: function(message) {
+      this.endgameMessage = message;
       let modalBox = document.getElementById("modalEog");
       modalBox.checked = true;
       setTimeout(() => { modalBox.checked = false; }, 2000);
     },
-
-// TODO: second arg == message
-
-    endGame: function(score) {
+    endGame: function(score, message) {
       this.score = score;
-      this.showScoreMsg(score);
+      if (!message)
+        message = this.getScoreMessage(score);
+      this.showEndgameMsg(score + " . " + message);
       this.$emit("gameover", score);
     },
     animateMove: function(move) {
@@ -230,8 +229,12 @@ export default {
       {
         if (!this.analyze)
           this.endGame(score);
-        else //just show score on screen (allow undo)
-          this.showScoreMsg(score);
+        else
+        {
+          // Just show score on screen (allow undo)
+          const message = this.getScoreMessage(score);
+          this.showEndgameMsg(score + " . " + message);
+        }
       }
       if (!this.analyze)
         this.$emit("newmove", move); //post-processing (e.g. computer play)
index 44e3553..0e3b668 100644 (file)
@@ -98,4 +98,9 @@ export const translations =
     ": unfinished computer game will be erased",
   ": current analysis will be erased":
     ": current analysis will be erased",
+
+  "Terminate game?": "Terminate game?",
+  "Sorry I have to go": "Sorry I have to go",
+  "Game seems over": "Game seems over",
+  "Game is too boring": "Game is too boring",
 };
index 0540fbb..9d755df 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(@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,29 @@ 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?"))
-        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
+    abortGame: function(event) {
+      if (!event)
+      {
+        // First call show options:
+        let modalBox = document.getElementById("modalAbort");
+        modalBox.checked = true;
+      }
+      else
+      {
+        console.log(event);
+        //const message = event.
+        this.gameOver("?");
+        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?"))
index 127b7fa..274d70a 100644 (file)
@@ -114,6 +114,9 @@ module.exports = function(wss) {
         case "resign":
           clients[obj.target].send(JSON.stringify({code:"resign"}));
           break;
+        case "abort":
+          clients[obj.target].send(JSON.stringify({code:"abort",msg:obj.msg}));
+          break;
       }
     });
     socket.on("close", () => {