Still logging an issue when transitioning Hall <--> Game (button Available/Playing...
authorBenjamin Auder <benjamin.auder@somewhere>
Thu, 6 Feb 2020 00:20:03 +0000 (01:20 +0100)
committerBenjamin Auder <benjamin.auder@somewhere>
Thu, 6 Feb 2020 00:20:03 +0000 (01:20 +0100)
client/src/components/BaseGame.vue
client/src/translations/en.js
client/src/views/Hall.vue
server/sockets.js

index f64c68f..523e0ef 100644 (file)
@@ -81,6 +81,7 @@ export default {
       this.re_setVariables();
     },
     // Received a new move to play:
+    // TODO: error "flush nextTick callbacks" when observer reloads page
     "game.moveToPlay": function(newMove) {
       if (!!newMove) //if stop + launch new game, get undefined move
         this.play(newMove, "receive");
index 4ee1855..3681a4d 100644 (file)
@@ -7,6 +7,7 @@ export const translations =
   "Analyze": "Analyze",
   "Analyze in Dark mode makes no sense!": "Analyze in Dark mode makes no sense!",
   "Apply": "Apply",
+  "Available": "Available",
   "Black": "Black",
   "Black to move": "Black to move",
   "Black win": "Black win",
@@ -55,6 +56,7 @@ export const translations =
   "Opponent action": "Opponent action",
   "Play sounds?": "Play sounds?",
   "Play with?": "Play with?",
+  "Playing": "Playing",
   "Please log in to accept corr challenges": "Please log in to accept corr challenges",
   "Please log in to play corr games": "Please log in to play corr games",
   "Please select a variant": "Please select a variant",
index 8cd330f..e8f7785 100644 (file)
@@ -50,13 +50,13 @@ main
       #people
         h3.text-center {{ st.tr["Who's there?"] }}
         #players
-          p(v-for="p in Object.values(people)" v-if="!!p.name")
-            span {{ p.name }}
+          p(v-for="sid in Object.keys(people)" v-if="!!people[sid].name")
+            span {{ people[sid].name }}
             button.player-action(
-              v-if="p.name != st.user.name"
-              @click="challOrWatch(p,$event)"
+              v-if="people[sid].name != st.user.name"
+              @click="challOrWatch(sid, $event)"
             )
-              | {{ whatPlayerDoes(p) }}
+              | {{ st.tr[!!people[sid].gamer ? 'Playing' : 'Available'] }}
           p.anonymous @nonymous ({{ anonymousCount }})
         #chat
           Chat(:newChat="newChat" @mychat="processChat")
@@ -253,22 +253,6 @@ export default {
       // this.st.variants might be uninitialized (variant == null)
       return (!!variant ? variant.name : "");
     },
-
-// TODO: now that we can distinguish people from Hall or Game,
-// improve this --> the info is already known
-
-    whatPlayerDoes: function(p) {
-      if (this.games.some(g => g.type == "live"
-        && g.players.some(pl => pl.sid == p.sid)))
-      {
-        return "Playing";
-      }
-      return "Challenge"; //player is available
-    },
-
-// Also debug: when from game going to Hall, player appears still connected.
-// when reloading a finished (observed) game, some ghost moveToPlay errors
-
     processChat: function(chat) {
       // When received on server, this will trigger a "notifyRoom"
       this.st.conn.send(JSON.stringify({code:"newchat", chat: chat}));
@@ -281,21 +265,7 @@ export default {
           {target: sid}
         )));
       };
-      if (!!to)
-      {
-        // Challenge with targeted players
-        const targetSid =
-          Object.keys(this.people).find(sid => this.people[sid].name == to);
-        if (!targetSid)
-        {
-          if (!!warnDisconnected)
-            alert(this.st.tr["Warning: target is not connected"]);
-          return false;
-        }
-        else
-          doSend(code, obj, targetSid);
-      }
-      else
+      if (!to || (!to.sid && !to.name))
       {
         // Open challenge: send to all connected players (me excepted)
         Object.keys(this.people).forEach(sid => {
@@ -303,6 +273,27 @@ export default {
             doSend(code, obj, sid);
         });
       }
+      else
+      {
+        let targetSid = "";
+        if (!!to.sid)
+          targetSid = to.sid;
+        else
+        {
+          if (to.name == this.st.user.name)
+            return alert(this.st.tr["Cannot challenge self"]);
+          // Challenge with targeted players
+          targetSid =
+            Object.keys(this.people).find(sid => this.people[sid].name == to.name);
+          if (!targetSid)
+          {
+            if (!!warnDisconnected)
+              alert(this.st.tr["Warning: target is not connected"]);
+            return false;
+          }
+        }
+        doSend(code, obj, targetSid);
+      }
       return true;
     },
     // Messaging center:
@@ -326,7 +317,7 @@ export default {
           // NOTE: we could make a difference between people in hall
           // and gamers, but is it necessary?
           data.sockIds.forEach(sid => {
-            this.$set(this.people, sid, {id:0, name:""});
+            this.$set(this.people, sid, {id:0, name:"", gamer:true});
             this.st.conn.send(JSON.stringify({code:"askidentity", target:sid}));
           });
           // Also ask current games to all playing peers (TODO: some design issue)
@@ -351,7 +342,11 @@ export default {
         case "identity":
         {
           this.$set(this.people, data.user.sid,
-            {id: data.user.id, name: data.user.name});
+            {
+              id: data.user.id,
+              name: data.user.name,
+              gamer: this.people[data.user.sid].gamer,
+            });
           break;
         }
         case "askchallenge":
@@ -452,7 +447,8 @@ export default {
         }
         case "connect":
         case "gconnect":
-          this.$set(this.people, data.from, {name:"", id:0});
+console.log(data.code + " " + data.from);
+          this.$set(this.people, data.from, {name:"", id:0, gamer:data.code[0]=='g'});
           this.st.conn.send(JSON.stringify({code:"askidentity", target:data.from}));
           if (data.code == "connect")
             this.st.conn.send(JSON.stringify({code:"askchallenge", target:data.from}));
@@ -460,6 +456,8 @@ export default {
             this.st.conn.send(JSON.stringify({code:"askgame", target:data.from}));
           break;
         case "disconnect":
+        case "pdisconnect":
+console.log(data.code + " " + data.from);
           this.$delete(this.people, data.from);
           if (data.code == "disconnect")
           {
@@ -477,22 +475,22 @@ export default {
       }
     },
     // Challenge lifecycle:
-    tryChallenge: function(player) {
-      if (player.id == 0)
+    tryChallenge: function(sid) {
+      if (this.people[sid].id == 0)
         return; //anonymous players cannot be challenged
-      this.newchallenge.to = player.name;
+      // TODO: SID is available, so we could use it instead of searching from name
+      this.newchallenge.to = this.people[sid].name;
       doClick("modalNewgame");
     },
-    challOrWatch: function(p, e) {
+    challOrWatch: function(sid, e) {
       switch (e.target.innerHTML)
       {
         case "Challenge":
-          this.tryChallenge(p);
+          this.tryChallenge(sid);
           break;
         case "Playing":
-          // NOTE: this search for game was already done for rendering
           this.showGame(this.games.find(
-            g => g.type=="live" && g.players.some(pl => pl.sid == p.sid)));
+            g => g.type=="live" && g.players.some(pl => pl.sid == sid)));
           break;
       };
     },
@@ -515,7 +513,7 @@ export default {
       const finishAddChallenge = (cid,warnDisconnected) => {
         chall.id = cid || "c" + getRandString();
         // Send challenge to peers (if connected)
-        const isSent = this.sendSomethingTo(chall.to, "challenge",
+        const isSent = this.sendSomethingTo({name:chall.to}, "challenge",
           {chall:chall}, !!warnDisconnected);
         if (!isSent)
           return;
@@ -525,7 +523,7 @@ export default {
         if (cIdx >= 0)
         {
           // Delete current challenge (will be replaced now)
-          this.sendSomethingTo(this.challenges[cIdx].to,
+          this.sendSomethingTo({name:this.challenges[cIdx].to},
             "deletechallenge", {cid:this.challenges[cIdx].id});
           if (ctype == "corr")
           {
@@ -599,14 +597,7 @@ export default {
             code: "refusechallenge",
             cid: c.id, target: c.from.sid}));
         }
-        // TODO: refactor the "sendSomethingTo()" function
-        if (!c.to)
-          this.sendSomethingTo(null, "deletechallenge", {cid:c.id});
-        else
-        {
-          this.st.conn.send(JSON.stringify({
-            code:"deletechallenge", target: c.from.sid, cid: c.id}));
-        }
+        this.sendSomethingTo(!!c.to ? {sid:c.from.sid} : null, "deletechallenge", {cid:c.id});
       }
       else //my challenge
       {
@@ -620,7 +611,7 @@ export default {
         }
         else //live
           localStorage.removeItem("challenge");
-        this.sendSomethingTo(c.to, "deletechallenge", {cid:c.id});
+        this.sendSomethingTo({name:c.to}, "deletechallenge", {cid:c.id});
       }
       // In all cases, the challenge is consumed:
       ArrayFun.remove(this.challenges, ch => ch.id == c.id);
index d296efa..795ed43 100644 (file)
@@ -38,10 +38,6 @@ module.exports = function(wss) {
       let obj = JSON.parse(objtxt);
       if (!!obj.target && !clients[obj.target])
         return; //receiver not connected, nothing we can do
-
-// TODO: debug
-console.log(obj.code + " " + clients[sid].page);
-
       switch (obj.code)
       {
         case "connect":
@@ -59,15 +55,14 @@ console.log(obj.code + " " + clients[sid].page);
           break;
         }
         case "pollgamers":
-        {
-          const curPage = clients[sid].page;
           socket.send(JSON.stringify({code:"pollgamers",
             sockIds: Object.keys(clients).filter(k =>
               k != sid && clients[k].page.indexOf("/game/") >= 0
             )}));
           break;
-        }
         case "pagechange":
+          // page change clients[sid].page --> obj.page
+console.log("page change: " + clients[sid].page + " " + obj.page + " " + sid);
           notifyRoom(clients[sid].page, "disconnect");
           if (clients[sid].page.indexOf("/game/") >= 0)
             notifyRoom("/", "gdisconnect");