Left some TODOs, some bugs
[vchess.git] / client / src / views / Game.vue
index a7096e5..ddd5e59 100644 (file)
@@ -5,13 +5,13 @@ main
     #chat.card
       label.modal-close(for="modalChat")
       #participants
-        span {{ Object.keys(people).length }} st.tr["participant(s):"] 
+        span {{ Object.keys(people).length + " " + st.tr["participant(s):"] }} 
         span(v-for="p in Object.values(people)" v-if="!!p.name")
           | {{ p.name }} 
         span.anonymous(v-if="Object.values(people).some(p => !p.name)")
           | + @nonymous
       Chat(:players="game.players" :pastChats="game.chats"
-        @newchat-sent="finishSendChat" @newchat-received="processChat")
+        :newChat="newChat" @mychat="processChat")
   .row
     #aboveBoard.col-sm-12.col-md-9.col-md-offset-3.col-lg-10.col-lg-offset-2
       span.variant-info
@@ -68,6 +68,7 @@ export default {
       people: {}, //players + observers
       lastate: undefined, //used if opponent send lastate before game is ready
       repeat: {}, //detect position repetition
+      newChat: "",
     };
   },
   watch: {
@@ -223,25 +224,34 @@ export default {
           break;
         }
         case "askgame":
-          // Send current (live) game if not asked by opponent (!)
-          if (this.game.players.some(p => p.sid == data.from))
-            return;
-          const myGame =
+          // Send current (live) game if I play in (not an observer),
+          // and not asked by opponent (!)
+          if (this.game.type == "live"
+            && this.game.players.some(p => p.sid == this.st.user.sid)
+            && this.game.players.every(p => p.sid != data.from))
           {
-            // Minimal game informations:
-            id: this.game.id,
-            players: this.game.players,
-            vid: this.game.vid,
-            timeControl: this.game.timeControl,
-          };
-          this.st.conn.send(JSON.stringify({code:"game",
-            game:myGame, target:data.from}));
+            const myGame =
+            {
+              // Minimal game informations:
+              id: this.game.id,
+              players: this.game.players,
+              vid: this.game.vid,
+              timeControl: this.game.timeControl,
+            };
+            this.st.conn.send(JSON.stringify({code:"game",
+              game:myGame, target:data.from}));
+          }
           break;
         case "newmove":
           if (!!data.move.cancelDrawOffer) //opponent refuses draw
             this.drawOffer = "";
           this.$set(this.game, "moveToPlay", data.move);
           break;
+        case "newchat":
+          this.newChat = data.chat;
+          if (!document.getElementById("modalChat").checked)
+            document.getElementById("chatBtn").style.backgroundColor = "#c5fefe";
+          break;
         case "lastate": //got opponent infos about last move
         {
           this.lastate = data;
@@ -612,15 +622,12 @@ export default {
       // TODO: this is called twice, once on opening an once on closing
       document.getElementById("chatBtn").style.backgroundColor = "#e2e2e2";
     },
-    finishSendChat: function(chat) {
+    processChat: function(chat) {
+      this.st.conn.send(JSON.stringify({code:"newchat", chat:chat}));
       // NOTE: anonymous chats in corr games are not stored on server (TODO?)
       if (this.game.type == "corr" && this.st.user.id > 0)
         GameStorage.update(this.gameRef.id, {chat: chat});
     },
-    processChat: function() {
-      if (!document.getElementById("modalChat").checked)
-        document.getElementById("chatBtn").style.backgroundColor = "#c5fefe";
-    },
     gameOver: function(score, scoreMsg) {
       this.game.score = score;
       this.game.scoreMsg = this.st.tr[(!!scoreMsg
@@ -662,7 +669,6 @@ export default {
   margin-top: 10px
   button
     display: inline-block
-    width: 33%
     margin: 0
 
 @media screen and (max-width: 767px)