Draft for challenges + games handling in main hall
[vchess.git] / client / src / views / Hall.vue
index ba3ad41..08867a7 100644 (file)
@@ -108,11 +108,25 @@ export default {
   created: function() {
     // Always add myself to players' list
     this.players.push(this.st.user);
-    // TODO: ask server for current corr games (all but mines: names, ID, time control)
-    // also ask for corr challenges
-    // Ask server for for room composition:
+    // Ask server for current corr games (all but mines)
+    ajax(
+      "",
+      "GET",
+      response => {
+
+      }
+    );
+    // Also ask for corr challenges (all)
+    ajax(
+      "",
+      "GET",
+      response => {
+
+      }
+    );
+    // 0.1] Ask server for for room composition:
     const socketOpenListener = () => {
-      this.st.conn.send(JSON.stringify({code:"askplayers"}));
+      this.st.conn.send(JSON.stringify({code:"askclients"}));
     };
     this.st.conn.onopen = socketOpenListener;
     this.st.conn.onmessage = this.socketMessageListener;
@@ -128,9 +142,27 @@ export default {
       const data = JSON.parse(msg.data);
       switch (data.code)
       {
-        case "room":
-          // TODO: receive room composition (sids at least, id + names if registered)
-        // TODO: also receive "askchallenges", "askgames"
+        // 0.2] Receive clients list (just socket IDs)
+        case "clients":
+          data.sockIds.forEach(sid => {
+            this.players.push({sid:sid, id:0, name:""});
+            this.st.conn.send(JSON.stringify({code:"askidentity", target:sid}));
+          });
+          break;
+          // TODO: also receive "askchallenges", "askgames"
+        case "identify":
+          // Request for identification
+          this.st.conn.send(JSON.stringify(
+            {code:"identity", user:this.st.user, target:data.from}));
+          break;
+        case "identity":
+          if (data.user.id > 0) //otherwise "anonymous", nothing to retrieve
+          {
+            const pIdx = this.players.findIndex(p => p.sid == data.user.sid);
+            this.players[pIdx].id = data.user.id;
+            this.players[pIdx].name = data.user.name;
+          }
+          break;
 // *  - receive "new game": if live, store locally + redirect to game
 // *    If corr: notify "new game has started", give link, but do not redirect
         case "newgame":
@@ -153,6 +185,7 @@ export default {
 // *  - receive new challenge: if targeted, replace our name with sender name
         case "newchallenge":
           // receive live or corr challenge
+          this.challenges.push(data.chall);
           break;
 // *  - receive "accept/withdraw/cancel challenge": apply action to challenges list
         case "acceptchallenge":
@@ -174,13 +207,14 @@ export default {
 // *  - receive "player connect": send all our current challenges (to him or global)
 // *    Also send all our games (live - max 1 - and corr) [in web worker ?]
 // *    + all our sent challenges.
-          this.players.push({name:data.name, id:data.uid});
+          this.players.push({name:"", id:0, sid:data.sid});
+          this.st.conn.send(JSON.stringify({code:"askidentity", target:data.sid}));
           // TODO: si on est en train de jouer une partie, le notifier au nouveau connecté
           // envoyer aussi nos défis
           break;
 // *  - receive "player disconnect": remove from players list
         case "disconnect":
-          ArrayFun.remove(this.players, p => p.id == data.uid);
+          ArrayFun.remove(this.players, p => p.sid == data.sid);
           // TODO: also remove all challenges sent by this player,
           // and all live games where he plays and no other opponent is online
           break;
@@ -263,6 +297,7 @@ export default {
       const vModule = await import("@/variants/" + vname + ".js");
       window.V = vModule.VariantRules;
       // checkChallenge side-effect = set FEN, and mainTime + increment in seconds
+      // TODO: should not be a side-effect but set here ; for received server challenges we do not have mainTime+increment
       const error = checkChallenge(this.newchallenge);
       if (!!error)
         return alert(error);
@@ -305,36 +340,39 @@ export default {
       const finishAddChallenge = () => {
         this.challenges.push(chall);
         // Send challenge to peers
-        const challSock =
+        let challSock =
         {
           code: "newchallenge",
           chall: chall,
+          target: "",
+        };
+        const sendChallengeTo = (sid) => {
+          challSock.target = sid;
+          this.st.conn.send(JSON.stringify(challSock));
         };
         if (chall.to[0].id > 0)
         {
           // Challenge with targeted players
           chall.to.forEach(p => {
             if (p.id > 0)
-            {
-              this.st.conn.send(JSON.stringify(Object.assign(
-                {},
-                challSock,
-                {receiver: p.sid}
-              )));
-            }
+              sendChallengeTo(p.sid);
           });
         }
         else
         {
           // Open challenge: send to all connected players (except us)
-          const strChallSock = JSON.stringify(challSock);
           this.players.forEach(p => {
             if (p.sid != this.st.user.sid) //only sid is always set
-              this.st.conn.send(strChallSock);
+              sendChallengeTo(p.sid);
           });
         }
         document.getElementById("modalNewgame").checked = false;
       };
+      
+      
+      // TODO: challenges all have IDs: "c" + genRandString()
+      
+      
       if (liveGame)
       {
         // Live challenges have cid = 0
@@ -342,11 +380,19 @@ export default {
       }
       else
       {
+        const chall = {
+          uid: req.body["from"],
+          vid: req.body["vid"],
+          fen: req.body["fen"],
+          timeControl: req.body["timeControl"],
+          nbPlayers: req.body["nbPlayers"],
+          to: req.body["to"], //array of IDs
+        };
         // Correspondance game: send challenge to server
         ajax(
           "/challenges/" + this.newchallenge.vid,
           "POST",
-          chall,
+          ,
           response => {
             chall.id = response.cid;
             finishAddChallenge();