Some advances in challenge handling (server+client)
[vchess.git] / client / src / views / Hall.vue
index 08867a7..0c99178 100644 (file)
@@ -63,6 +63,7 @@ import { NbPlayers } from "@/data/nbPlayers";
 import { checkChallenge } from "@/data/challengeCheck";
 import { ArrayFun } from "@/utils/array";
 import { ajax } from "@/utils/ajax";
+import { getRandString } from "@/utils/alea";
 import GameList from "@/components/GameList.vue";
 import ChallengeList from "@/components/ChallengeList.vue";
 export default {
@@ -109,29 +110,31 @@ export default {
     // Always add myself to players' list
     this.players.push(this.st.user);
     // Ask server for current corr games (all but mines)
-    ajax(
-      "",
-      "GET",
-      response => {
-
-      }
-    );
-    // Also ask for corr challenges (all)
-    ajax(
-      "",
-      "GET",
-      response => {
-
-      }
-    );
+//    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:"askclients"}));
     };
     this.st.conn.onopen = socketOpenListener;
+    this.oldOnmessage = this.st.conn.onmessage || Function.prototype; //TODO: required here?
     this.st.conn.onmessage = this.socketMessageListener;
+    const oldOnclose = this.st.conn.onclose;
     const socketCloseListener = () => {
-      // connexion is reinitialized in store.js
+      oldOnclose(); //reinitialize connexion (in store.js)
       this.st.conn.addEventListener('message', this.socketMessageListener);
       this.st.conn.addEventListener('close', socketCloseListener);
     };
@@ -139,6 +142,10 @@ export default {
   },
   methods: {
     socketMessageListener: function(msg) {
+      // Save and call current st.conn.onmessage if one was already defined
+      // --> also needed in future Game.vue (also in Chat.vue component)
+      // TODO: merge Game.vue and MoveList.vue (one logic entity, no ?)
+      this.oldOnmessage(msg);
       const data = JSON.parse(msg.data);
       switch (data.code)
       {
@@ -284,6 +291,14 @@ export default {
 //      if (this.settings.sound >= 1)
 //        new Audio("/sounds/newgame.mp3").play().catch(err => {});
 //    },
+    // Load a variant file (TODO: should probably be global)
+    loadVariant: async function(vid, variantArray) {
+      const idxInVariants = variantArray.findIndex(v => v.id == vid);
+      const vname = variantArray[idxInVariants].name;
+      const vModule = await import("@/variants/" + vname + ".js");
+      window.V = vModule.VariantRules;
+      return vname;
+    },
     // Send new challenge (corr or live, cf. time control), with button or click on player
     newChallenge: async function() {
       if (this.challenges.some(c => c.from.sid == this.st.user.sid))
@@ -291,16 +306,16 @@ export default {
         document.getElementById("modalNewgame").checked = false;
         return alert("You already have a pending challenge");
       }
-      const idxInVariants =
-        this.st.variants.findIndex(v => v.id == this.newchallenge.vid);
-      const vname = this.st.variants[idxInVariants].name;
-      const vModule = await import("@/variants/" + vname + ".js");
-      window.V = vModule.VariantRules;
+      // TODO: put this "load variant" block elsewhere
+      const vname = this.loadVariant(this.newchallenge.vid, this.st.variants);
       // 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);
+// TODO: set FEN, set mainTime and increment ?!
+//else //generate a FEN
+//    c.fen = V.GenRandInitFen();
       // Less than 3 days ==> live game (TODO: heuristic... 40 moves also)
       const liveGame =
         this.newchallenge.mainTime + 40 * this.newchallenge.increment < 3*24*60*60;
@@ -337,7 +352,8 @@ export default {
           p.sid = this.players[pIdx].sid;
         }
       }
-      const finishAddChallenge = () => {
+      const finishAddChallenge = (cid) => {
+        chall.id = cid || "c" + getRandString();
         this.challenges.push(chall);
         // Send challenge to peers
         let challSock =
@@ -368,11 +384,6 @@ export default {
         }
         document.getElementById("modalNewgame").checked = false;
       };
-      
-      
-      // TODO: challenges all have IDs: "c" + genRandString()
-      
-      
       if (liveGame)
       {
         // Live challenges have cid = 0
@@ -392,7 +403,7 @@ export default {
         ajax(
           "/challenges/" + this.newchallenge.vid,
           "POST",
-          ,
+          chall,
           response => {
             chall.id = response.cid;
             finishAddChallenge();