Convert all remaining tabs by 2spaces
[vchess.git] / client / src / views / Hall.vue
index 2df03e6..2180509 100644 (file)
@@ -14,15 +14,21 @@ main
       fieldset
         label(for="selectVariant") {{ st.tr["Variant"] }}
         select#selectVariant(v-model="newchallenge.vid")
-          option(v-for="v in st.variants" :value="v.id") {{ v.name }}
+          option(v-for="v in st.variants" :value="v.id"
+              :selected="newchallenge.vid==v.id")
+            | {{ v.name }}
       fieldset
         label(for="timeControl") {{ st.tr["Time control"] }}
+        div#predefinedTimeControls
+          button 3+2
+          button 5+3
+          button 15+5
         input#timeControl(type="text" v-model="newchallenge.timeControl"
-          placeholder="3m+2s, 1h+30s, 7d+1d ...")
+          placeholder="5+0, 1h+30s, 7d+1d ...")
       fieldset(v-if="st.user.id > 0")
         label(for="selectPlayers") {{ st.tr["Play with? (optional)"] }}
         input#selectPlayers(type="text" v-model="newchallenge.to")
-      fieldset(v-if="st.user.id > 0")
+      fieldset(v-if="st.user.id > 0 && newchallenge.to.length > 0")
         label(for="inputFen") {{ st.tr["FEN (optional)"] }}
         input#inputFen(type="text" v-model="newchallenge.fen")
       button(@click="newChallenge") {{ st.tr["Send challenge"] }}
@@ -31,42 +37,40 @@ main
       button#newGame(onClick="doClick('modalNewgame')") New game
   .row
     .col-sm-12.col-md-10.col-md-offset-1.col-lg-8.col-lg-offset-2
-      .collapse
-        input#challengeSection(type="radio" checked aria-hidden="true" name="accordion")
-        label(for="challengeSection" aria-hidden="true") Challenges
-        div
-          .button-group
-            button(@click="cdisplay='live'") Live Challenges
-            button(@click="cdisplay='corr'") Correspondance challenges
-          ChallengeList(v-show="cdisplay=='live'"
-            :challenges="filterChallenges('live')" @click-challenge="clickChallenge")
-          ChallengeList(v-show="cdisplay=='corr'"
-            :challenges="filterChallenges('corr')" @click-challenge="clickChallenge")
-        input#peopleSection(type="radio" aria-hidden="true" name="accordion")
-        label(for="peopleSection" aria-hidden="true") People
-        div
-          .button-group
-            button(@click="pdisplay='players'") Players
-            button(@click="pdisplay='chat'") Chat
-          #players(v-show="pdisplay=='players'")
-            p(v-for="p in uniquePlayers")
-              span(:class="{anonymous: !!p.count}")
-                | {{ (p.name || '@nonymous') + (!!p.count ? " ("+p.count+")" : "") }}
-              button.player-action(v-if="!p.count && p.name != st.user.name"
-                  @click="challOrWatch(p,$event)")
-                | {{ whatPlayerDoes(p) }}
-          #chat(v-show="pdisplay=='chat'")
-            Chat(:players="[]")
-        input#gameSection(type="radio" aria-hidden="true" name="accordion")
-        label(for="gameSection" aria-hidden="true") Games
-        div
-          .button-group
-            button(@click="gdisplay='live'") Live games
-            button(@click="gdisplay='corr'") Correspondance games
-          GameList(v-show="gdisplay=='live'" :games="filterGames('live')"
-            @show-game="showGame")
-          GameList(v-show="gdisplay=='corr'" :games="filterGames('corr')"
-            @show-game="showGame")
+      div
+        .button-group
+          button(@click="(e) => setDisplay('c','live',e)" class="active")
+            | Live Challenges
+          button(@click="(e) => setDisplay('c','corr',e)")
+            | Correspondance challenges
+        ChallengeList(v-show="cdisplay=='live'"
+          :challenges="filterChallenges('live')" @click-challenge="clickChallenge")
+        ChallengeList(v-show="cdisplay=='corr'"
+          :challenges="filterChallenges('corr')" @click-challenge="clickChallenge")
+      #people
+        h3.text-center Who's there?
+        #players
+          p(v-for="p in Object.values(people)" v-if="!!p.name")
+            span {{ p.name }}
+            button.player-action(
+              v-if="p.name != st.user.name"
+              @click="challOrWatch(p,$event)"
+            )
+              | {{ whatPlayerDoes(p) }}
+          p.anonymous @nonymous ({{ anonymousCount }})
+        #chat
+          Chat(:players="[]")
+        .clearer
+      div
+        .button-group
+          button(@click="(e) => setDisplay('g','live',e)" class="active")
+            | Live games
+          button(@click="(e) => setDisplay('g','corr',e)")
+            | Correspondance games
+        GameList(v-show="gdisplay=='live'" :games="filterGames('live')"
+          @show-game="showGame")
+        GameList(v-show="gdisplay=='corr'" :games="filterGames('corr')"
+          @show-game="showGame")
 </template>
 
 <script>
@@ -98,9 +102,9 @@ export default {
       infoMessage: "",
       newchallenge: {
         fen: "",
-        vid: 0,
+        vid: localStorage.getItem("vid") || "",
         to: "", //name of challenged player (if any)
-        timeControl: "", //"2m+2s" ...etc
+        timeControl: localStorage.getItem("timeControl") || "",
       },
     };
   },
@@ -119,23 +123,10 @@ export default {
     },
   },
   computed: {
-    uniquePlayers: function() {
-      // Show e.g. "@nonymous (5)", and do nothing on click on anonymous
-      let anonymous = {name:"", count:0};
-      let playerList = {};
-      Object.values(this.people).forEach(p => {
-        if (p.id > 0)
-        {
-          // We don't count registered users connections: either they are here or not.
-          if (!playerList[p.id])
-            playerList[p.id] = {name: p.name};
-        }
-        else
-          anonymous.count++;
-      });
-      if (anonymous.count > 0)
-        playerList[0] = anonymous;
-      return Object.values(playerList);
+    anonymousCount: function() {
+      let count = 0;
+      Object.values(this.people).forEach(p => { count += (!p.name ? 1 : 0); });
+      return count;
     },
   },
   created: function() {
@@ -146,8 +137,13 @@ export default {
     const chall = JSON.parse(localStorage.getItem("challenge") || "false");
     if (!!chall)
     {
-      if ((Date.now() - chall.added)/1000 <= 30*60)
+      // NOTE: a challenge survives 3 minutes, for potential connection issues
+      if ((Date.now() - chall.added)/1000 <= 3*60)
+      {
+        chall.added = Date.now(); //update added time, for next disconnect...
         this.challenges.push(chall);
+        localStorage.setItem("challenge", JSON.stringify(chall));
+      }
       else
         localStorage.removeItem("challenge");
     }
@@ -194,6 +190,9 @@ export default {
     );
     // 0.1] Ask server for room composition:
     const funcPollClients = () => {
+      // Same strategy as in Game.vue: send connection
+      // after we're sure WebSocket is initialized
+      this.st.conn.send(JSON.stringify({code:"connect"}));
       this.st.conn.send(JSON.stringify({code:"pollclients"}));
     };
     if (!!this.st.conn && this.st.conn.readyState == 1) //1 == OPEN state
@@ -208,6 +207,13 @@ export default {
     };
     this.st.conn.onclose = socketCloseListener;
   },
+  mounted: function() {
+    document.querySelectorAll("#predefinedTimeControls > button").forEach(
+      (b) => { b.addEventListener("click",
+        () => { this.newchallenge.timeControl = b.innerHTML; }
+      )}
+    );
+  },
   methods: {
     // Helpers:
     filterChallenges: function(type) {
@@ -228,6 +234,14 @@ export default {
         url += "?rid=" + g.rid;
       this.$router.push(url);
     },
+    setDisplay: function(letter, type, e) {
+      this[letter + "display"] = type;
+      e.target.classList.add("active");
+      if (!!e.target.previousElementSibling)
+        e.target.previousElementSibling.classList.remove("active");
+      else
+        e.target.nextElementSibling.classList.remove("active");
+    },
     getVname: function(vid) {
       const variant = this.st.variants.find(v => v.id == vid);
       // this.st.variants might be uninitialized (variant == null)
@@ -244,7 +258,6 @@ export default {
     sendSomethingTo: function(to, code, obj, warnDisconnected) {
       const doSend = (code, obj, sid) => {
         this.st.conn.send(JSON.stringify(Object.assign(
-          {},
           {code: code},
           obj,
           {target: sid}
@@ -258,19 +271,21 @@ export default {
         if (!targetSid)
         {
           if (!!warnDisconnected)
-            alert("Warning: " + pname + " is not connected");
+            alert("Warning: " + to + " is not connected");
+          return false;
         }
         else
           doSend(code, obj, targetSid);
       }
       else
       {
-        // Open challenge: send to all connected players (except us)
+        // Open challenge: send to all connected players (me excepted)
         Object.keys(this.people).forEach(sid => {
           if (sid != this.st.user.sid)
             doSend(code, obj, sid);
         });
       }
+      return true;
     },
     // Messaging center:
     socketMessageListener: function(msg) {
@@ -318,11 +333,19 @@ export default {
         case "askchallenge":
         {
           // Send my current live challenge (if any)
-          const cIdx = this.challenges
-            .findIndex(c => c.from.sid == this.st.user.sid && c.type == "live");
+          const cIdx = this.challenges.findIndex(c =>
+            c.from.sid == this.st.user.sid && c.type == "live");
           if (cIdx >= 0)
           {
             const c = this.challenges[cIdx];
+            if (!!c.to)
+            {
+              // Only share targeted challenges to the targets:
+              const toSid = Object.keys(this.people).find(k =>
+                this.people[k].name == c.to);
+              if (toSid != data.from)
+                return;
+            }
             const myChallenge =
             {
               // Minimal challenge informations: (from not required)
@@ -330,7 +353,7 @@ export default {
               to: c.to,
               fen: c.fen,
               vid: c.vid,
-              timeControl: c.timeControl
+              timeControl: c.timeControl,
             };
             this.st.conn.send(JSON.stringify({code:"challenge",
               chall:myChallenge, target:data.from}));
@@ -353,7 +376,11 @@ export default {
         {
           // Receive game from some player (+sid)
           // NOTE: it may be correspondance (if newgame while we are connected)
-          if (!this.games.some(g => g.id == data.game.id)) //ignore duplicates
+          // If duplicate found: select rid (remote ID) at random
+          let game = this.games.find(g => g.id == data.game.id);
+          if (!!game && Math.random() < 0.5)
+            game.rid = data.from;
+          else
           {
             let newGame = data.game;
             newGame.type = this.classifyObject(data.game);
@@ -384,8 +411,9 @@ export default {
         }
         case "refusechallenge":
         {
-          alert(this.people[data.from].name + " declined your challenge");
           ArrayFun.remove(this.challenges, c => c.id == data.cid);
+          localStorage.removeItem("challenge");
+          alert(this.people[data.from].name + " declined your challenge");
           break;
         }
         case "deletechallenge":
@@ -437,6 +465,8 @@ export default {
       };
     },
     newChallenge: async function() {
+      if (this.newchallenge.vid == "")
+        return alert("Please select a variant");
       const vname = this.getVname(this.newchallenge.vid);
       const vModule = await import("@/variants/" + vname + ".js");
       window.V = vModule.VariantRules;
@@ -453,7 +483,29 @@ export default {
       const finishAddChallenge = (cid,warnDisconnected) => {
         chall.id = cid || "c" + getRandString();
         // Send challenge to peers (if connected)
-        this.sendSomethingTo(chall.to, "challenge", {chall:chall}, !!warnDisconnected);
+        const isSent = this.sendSomethingTo(chall.to, "challenge",
+          {chall:chall}, !!warnDisconnected);
+        if (!isSent)
+          return;
+        // Remove old challenge if any (only one at a time):
+        const cIdx = this.challenges.findIndex(c =>
+          c.from.sid == this.st.user.sid && c.type == ctype);
+        if (cIdx >= 0)
+        {
+          // Delete current challenge (will be replaced now)
+          this.sendSomethingTo(this.challenges[cIdx].to,
+            "deletechallenge", {cid:this.challenges[cIdx].id});
+          if (ctype == "corr")
+          {
+            ajax(
+              "/challenges",
+              "DELETE",
+              {id: this.challenges[cIdx].id}
+            );
+          }
+          this.challenges.splice(cIdx, 1);
+        }
+        // Add new challenge:
         chall.added = Date.now();
         // NOTE: vname and type are redundant (can be deduced from timeControl + vid)
         chall.type = ctype;
@@ -466,25 +518,11 @@ export default {
         this.challenges.push(chall);
         if (ctype == "live")
           localStorage.setItem("challenge", JSON.stringify(chall));
+        // Also remember timeControl  + vid for quicker further challenges:
+        localStorage.setItem("timeControl", chall.timeControl);
+        localStorage.setItem("vid", chall.vid);
         document.getElementById("modalNewgame").checked = false;
       };
-      const cIdx = this.challenges.findIndex(
-        c => c.from.sid == this.st.user.sid && c.type == ctype);
-      if (cIdx >= 0)
-      {
-        // Delete current challenge (will be replaced now)
-        this.sendSomethingTo(this.challenges[cIdx].to,
-          "deletechallenge", {cid:this.challenges[cIdx].id});
-        if (ctype == "corr")
-        {
-          ajax(
-            "/challenges",
-            "DELETE",
-            {id: this.challenges[cIdx].id}
-          );
-        }
-        this.challenges.splice(cIdx, 1);
-      }
       if (ctype == "live")
       {
         // Live challenges have a random ID
@@ -529,6 +567,14 @@ 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}));
+        }
       }
       else //my challenge
       {
@@ -542,11 +588,10 @@ export default {
         }
         else //live
           localStorage.removeItem("challenge");
+        this.sendSomethingTo(c.to, "deletechallenge", {cid:c.id});
       }
-      // In (almost) all cases, the challenge is consumed:
+      // In all cases, the challenge is consumed:
       ArrayFun.remove(this.challenges, ch => ch.id == c.id);
-      // NOTE: deletechallenge event might be redundant (but it's easier this way)
-      this.sendSomethingTo((!!c.to ? c.from : null), "deletechallenge", {cid:c.id});
     },
     // NOTE: when launching game, the challenge is already deleted
     launchGame: async function(c) {
@@ -562,21 +607,22 @@ export default {
         vname: c.vname, //theoretically vid is enough, but much easier with vname
         timeControl: c.timeControl,
       };
-      let target = c.from.sid; //may not be defined if corr + offline opp
-      if (!target)
+      let oppsid = c.from.sid; //may not be defined if corr + offline opp
+      if (!oppsid)
       {
-        target = Object.keys(this.people).find(sid =>
+        oppsid = Object.keys(this.people).find(sid =>
           this.people[sid].id == c.from.id);
       }
       const tryNotifyOpponent = () => {
-        if (!!target) //opponent is online
+        if (!!oppsid) //opponent is online
         {
           this.st.conn.send(JSON.stringify({code:"newgame",
-            gameInfo:gameInfo, target:target, cid:c.id}));
+            gameInfo:gameInfo, target:oppsid, cid:c.id}));
         }
       };
       if (c.type == "live")
       {
+        // NOTE: in this case we are sure opponent is online
         tryNotifyOpponent();
         this.startNewGame(gameInfo);
       }
@@ -594,14 +640,19 @@ export default {
         );
       }
       // Send game info to everyone except opponent (and me)
-      this.st.conn.send(JSON.stringify({code:"game",
-        game: { //minimal game info:
-          id: gameInfo.id,
-          players: gameInfo.players.map(p => p.name),
-          vid: gameInfo.vid,
-          timeControl: gameInfo.timeControl,
-        },
-        oppsid: target}));
+      Object.keys(this.people).forEach(sid => {
+        if (![this.st.user.sid,oppsid].includes(sid))
+        {
+          this.st.conn.send(JSON.stringify({code:"game",
+            game: { //minimal game info:
+              id: gameInfo.id,
+              players: gameInfo.players,
+              vid: gameInfo.vid,
+              timeControl: gameInfo.timeControl,
+            },
+            target: sid}));
+        }
+      });
     },
     // NOTE: for live games only (corr games start on the server)
     startNewGame: function(gameInfo) {
@@ -625,18 +676,30 @@ export default {
 </script>
 
 <style lang="sass" scoped>
+.active
+  color: #42a983
 #newGame
   display: block
   margin: 10px auto 5px auto
+#people
+  width: 100%
+#players
+  width: 50%
+  position: relative
+  float: left
+#chat
+  width: 50%
+  float: left
+  position: relative
+@media screen and (max-width: 767px)
+  #players, #chats
+    width: 100%
 #chat > .card
   max-width: 100%
   margin: 0;
   border: none;
 #players > p
-  margin-left: 40%
-@media screen and (max-width: 767px)
-  #players > p
-    margin-left: 5px
+  margin-left: 5px
 .anonymous
   font-style: italic
 button.player-action