Revise server code + a few fixes in trnalsations and ComputerGame
[vchess.git] / client / src / views / Hall.vue
index 2236ce3..82bd769 100644 (file)
@@ -28,9 +28,9 @@ main
         fieldset
           label(for="cadence") {{ st.tr["Cadence"] }} *
           div#predefinedCadences
-            button 3+2
-            button 5+3
-            button 15+5
+            button(type="button") 3+2
+            button(type="button") 5+3
+            button(type="button") 15+5
           input#cadence(
             type="text"
             v-model="newchallenge.cadence"
@@ -83,7 +83,7 @@ main
     .col-sm-12.col-md-10.col-md-offset-1.col-lg-8.col-lg-offset-2
       .button-group
         button#peopleBtn(onClick="window.doClick('modalPeople')")
-          | {{ st.tr["Social"] }}
+          | {{ st.tr["Who's there?"] }}
         button(onClick="window.doClick('modalNewgame')")
           | {{ st.tr["New game"] }}
   .row
@@ -275,8 +275,11 @@ export default {
         this.newchallenge.cadence = b.innerHTML;
       });
     });
-    const showCtype = localStorage.getItem("type-challenges") || "live";
-    const showGtype = localStorage.getItem("type-games") || "live";
+    const dispCorr = this.$route.query["disp"];
+    const showCtype =
+      dispCorr || localStorage.getItem("type-challenges") || "live";
+    const showGtype =
+      dispCorr || localStorage.getItem("type-games") || "live";
     this.setDisplay("c", showCtype);
     this.setDisplay("g", showGtype);
   },
@@ -560,28 +563,32 @@ export default {
         case "newgame": {
           // NOTE: it may be live or correspondance
           const game = data.data;
-          let locGame = this.games.find(g => g.id == game.id);
-          if (!locGame) {
-            let newGame = game;
-            newGame.type = this.classifyObject(game);
-            newGame.vname = this.getVname(game.vid);
-            if (!game.score)
-              //if new game from Hall
-              newGame.score = "*";
-            newGame.rids = [game.rid];
-            delete newGame["rid"];
-            this.games.push(newGame);
-            if (
-              (newGame.type == "live" && this.gdisplay == "corr") ||
-              (newGame.type == "corr" && this.gdisplay == "live")
-            ) {
-              document
-                .getElementById("btnG" + newGame.type)
-                .classList.add("somethingnew");
+          // Ignore games where I play (corr games)
+          if (game.players.every(p => p.id != this.st.user.id))
+          {
+            let locGame = this.games.find(g => g.id == game.id);
+            if (!locGame) {
+              let newGame = game;
+              newGame.type = this.classifyObject(game);
+              newGame.vname = this.getVname(game.vid);
+              if (!game.score)
+                //if new game from Hall
+                newGame.score = "*";
+              newGame.rids = [game.rid];
+              delete newGame["rid"];
+              this.games.push(newGame);
+              if (
+                (newGame.type == "live" && this.gdisplay == "corr") ||
+                (newGame.type == "corr" && this.gdisplay == "live")
+              ) {
+                document
+                  .getElementById("btnG" + newGame.type)
+                  .classList.add("somethingnew");
+              }
+            } else {
+              // Append rid (if not already in list)
+              if (!locGame.rids.includes(game.rid)) locGame.rids.push(game.rid);
             }
-          } else {
-            // Append rid (if not already in list)
-            if (!locGame.rids.includes(game.rid)) locGame.rids.push(game.rid);
           }
           break;
         }
@@ -624,11 +631,24 @@ export default {
     },
     // Challenge lifecycle:
     newChallenge: async function() {
+      if (this.newchallenge.cadence.match(/^[0-9]+$/))
+        this.newchallenge.cadence += "+0"; //assume minutes, no increment
+      const ctype = this.classifyObject(this.newchallenge);
+      // TODO: cadence still unchecked so ctype could be wrong...
       let error = "";
-      if (this.newchallenge.vid == "")
+      if (!this.newchallenge.vid)
         error = this.st.tr["Please select a variant"];
-      else if (!!this.newchallenge.to && this.newchallenge.to == this.st.user.name)
-        error = this.st.tr["Self-challenge is forbidden"];
+      else if (ctype == "corr" && this.st.user.id <= 0)
+        error = this.st.tr["Please log in to play correspondance games"];
+      else if (this.newchallenge.to) {
+        if (this.newchallenge.to == this.st.user.name)
+          error = this.st.tr["Self-challenge is forbidden"];
+        else if (
+          ctype == "live" &&
+          Object.values(this.people).every(p => p.name != this.newchallenge.to)
+        )
+          error = this.newchallenge.to + " " + this.st.tr["is not online"];
+      }
       if (error) {
         alert(error);
         return;
@@ -636,12 +656,7 @@ export default {
       const vname = this.getVname(this.newchallenge.vid);
       const vModule = await import("@/variants/" + vname + ".js");
       window.V = vModule.VariantRules;
-      if (this.newchallenge.cadence.match(/^[0-9]+$/))
-        this.newchallenge.cadence += "+0"; //assume minutes, no increment
-      const ctype = this.classifyObject(this.newchallenge);
       error = checkChallenge(this.newchallenge);
-      if (!error && ctype == "corr" && this.st.user.id <= 0)
-        error = this.st.tr["Please log in to play correspondance games"];
       if (error) {
         alert(error);
         return;
@@ -786,10 +801,14 @@ export default {
         initime: [0, 0], //initialized later
         score: "*"
       });
-      GameStorage.add(game);
-      if (this.st.settings.sound >= 1)
-        new Audio("/sounds/newgame.mp3").play().catch(() => {});
-      this.$router.push("/game/" + gameInfo.id);
+      GameStorage.add(game, (err) => {
+        // If an error occurred, game is not added: abort
+        if (!err) {
+          if (this.st.settings.sound >= 1)
+            new Audio("/sounds/newgame.mp3").play().catch(() => {});
+          this.$router.push("/game/" + gameInfo.id);
+        }
+      });
     }
   }
 };