Implement basic observing logic
authorBenjamin Auder <benjamin.auder@somewhere>
Tue, 21 Jan 2020 12:46:35 +0000 (13:46 +0100)
committerBenjamin Auder <benjamin.auder@somewhere>
Tue, 21 Jan 2020 12:46:35 +0000 (13:46 +0100)
client/src/translations/en.js
client/src/views/Game.vue
client/src/views/Hall.vue
server/sockets.js

index b4af338..cbc3a63 100644 (file)
@@ -86,5 +86,4 @@ export const translations =
   "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 cadda01..984a66b 100644 (file)
@@ -8,7 +8,6 @@
         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")
     div Names: {{ game.players[0].name }} - {{ game.players[1].name }}
     Chat(:players="game.players")
 </template>
 
-<!--
-// ==> après, implémenter/vérifier les passages de challenges + parties en cours
-// observer,
--->
-
 <script>
 import BaseGame from "@/components/BaseGame.vue";
 import Chat from "@/components/Chat.vue";
@@ -103,22 +97,23 @@ export default {
     // Always add myself to players' list
     const my = this.st.user;
     this.people.push({sid:my.sid, id:my.id, name:my.name});
-    if (!!this.$route.params["id"])
-    {
-      this.gameRef.id = this.$route.params["id"];
-      this.gameRef.rid = this.$route.query["rid"];
-      this.loadGame();
-    }
+    this.gameRef.id = this.$route.params["id"];
+    this.gameRef.rid = this.$route.query["rid"]; //may be undefined
+    if (!this.gameRef.rid)
+      this.loadGame(); //local or corr: can load from now
     // TODO: mode analyse (/analyze/Atomic/rn
     // ... fen = query[], vname=params[] ...
     // 0.1] Ask server for room composition:
-    const funcPollClients = () => {
+    const initialize = () => {
+      // Poll clients + load game if stored remotely
       this.st.conn.send(JSON.stringify({code:"pollclients"}));
+      if (!!this.gameRef.rid)
+        this.loadGame();
     };
     if (!!this.st.conn && this.st.conn.readyState == 1) //1 == OPEN state
-      funcPollClients();
+      initialize();
     else //socket not ready yet (initial loading)
-      this.st.conn.onopen = funcPollClients;
+      this.st.conn.onopen = initialize;
     this.st.conn.onmessage = this.socketMessageListener;
     const socketCloseListener = () => {
       store.socketCloseListener(); //reinitialize connexion (in store.js)
@@ -239,12 +234,10 @@ export default {
           this.drawOffer = "received";
           break;
         case "askfullgame":
-          // TODO: just give game; observers are listed here anyway:
-          // ==> mark request SID as someone to send moves to
-          // NOT to all people array: our opponent can send moves too!
+          // TODO: use data.id to retrieve game in indexedDB (but for now only one running game so OK)
+          this.st.conn.send(JSON.stringify({code:"fullgame", game:this.game, target:data.from}));
           break;
         case "fullgame":
-          // and when receiving answer just call loadGame(received_game)
           this.loadGame(data.game);
           break;
         // TODO: drawaccepted (click draw button before sending move
@@ -409,10 +402,12 @@ export default {
         );
       };
       if (!!game)
-        return afterRetrival(game);
+        return afterRetrieval(game);
       if (!!this.gameRef.rid)
       {
         // Remote live game
+        // (TODO: send game ID as well, and receiver would pick the corresponding
+        // game in his current games; if allowed to play several)
         this.st.conn.send(JSON.stringify(
           {code:"askfullgame", target:this.gameRef.rid}));
         // (send moves updates + resign/abort/draw actions)
@@ -437,7 +432,7 @@ export default {
           obj[key] = move[key];
           return obj;
         }, {});
-      // Send move ("newmove" event) to opponent(s) (if ours)
+      // Send move ("newmove" event) to people in the room (if our turn)
       let addTime = 0;
       if (move.color == this.game.mycolor)
       {
@@ -451,14 +446,16 @@ export default {
         if (this.game.type == "corr")
           sendMove.message = this.corrMsg;
         const oppsid = this.getOppSid();
-        if (!!oppsid)
-        {
-          this.st.conn.send(JSON.stringify({
-            code: "newmove",
-            target: oppsid,
-            move: sendMove,
-          }));
-        }
+        this.people.forEach(p => {
+          if (p.sid != this.st.user.sid)
+          {
+            this.st.conn.send(JSON.stringify({
+              code: "newmove",
+              target: p.sid,
+              move: sendMove,
+            }));
+          }
+        });
         if (this.game.type == "corr" && this.corrMsg != "")
         {
           // Add message to last move in BaseGame:
index e0910c0..7b90318 100644 (file)
@@ -221,11 +221,7 @@ export default {
       // ==> Moves sent by connected remote player(s) if live game
       let url = "/game/" + g.id;
       if (g.type == "live")
-      {
-        const remotes = g.players.filter(p => this.people.some(pl => pl.sid == p.sid));
-        const rIdx = (remotes.length == 1 ? 0 : Math.floor(Math.random()*2));
-        url += "?rid=" + remotes[rIdx].sid;
-      }
+        url += "?rid=" + g.rid;
       this.$router.push(url);
     },
     getVname: function(vid) {
index 3b16653..b51a978 100644 (file)
@@ -68,14 +68,40 @@ module.exports = function(wss) {
             {code:"askchallenge",from:sid}));
           break;
         case "askgames":
+        {
           // Check all clients playing, and send them a "askgame" message
+          let gameSids = {}; //game ID --> [sid1, sid2]
+          const regexpGid = /\/[a-zA-Z0-9]+$/;
           Object.keys(clients).forEach(k => {
             if (k != sid && clients[k].page.indexOf("/game/") >= 0)
             {
-              clients[k].sock.send(JSON.stringify(
-                {code:"askgame", from: sid}));
+              const gid = clients[k].page.match(regexpGid)[0];
+              if (!gameSids[gid])
+                gameSids[gid] = [k];
+              else
+                gameSids[gid].push(k);
             }
           });
+          // Request only one client out of 2 (TODO: this is a bit heavy)
+          // Alt: ask game to all, and filter later?
+          Object.keys(gameSids).forEach(gid => {
+            const L = gameSids[gid].length;
+            const idx = L > 1
+              ? Math.floor(Math.random() * Math.floor(L))
+              : 0;
+            const rid = gameSids[gid][idx];
+            clients[rid].sock.send(JSON.stringify(
+              {code:"askgame", from: rid}));
+          });
+          break;
+        }
+        case "askfullgame":
+          clients[obj.target].sock.send(JSON.stringify(
+            {code:"askfullgame", from:sid}));
+          break;
+        case "fullgame":
+          clients[obj.target].sock.send(JSON.stringify(
+            {code:"fullgame", game:obj.game}));
           break;
         case "identity":
           clients[obj.target].sock.send(JSON.stringify(