From 42c15a75118bfeb3251cea1f65acb01fcd023f01 Mon Sep 17 00:00:00 2001
From: Benjamin Auder <benjamin.auder@somewhere>
Date: Thu, 4 Jul 2019 18:20:50 +0200
Subject: [PATCH] Modifs to send current live game

---
 client/src/components/GameList.vue | 13 ++++-----
 client/src/utils/gameStorage.js    | 13 ++++++++-
 client/src/views/Hall.vue          | 47 +++++++++++++++---------------
 server/sockets.js                  | 11 +++----
 4 files changed, 48 insertions(+), 36 deletions(-)

diff --git a/client/src/components/GameList.vue b/client/src/components/GameList.vue
index bb97a742..8481048d 100644
--- a/client/src/components/GameList.vue
+++ b/client/src/components/GameList.vue
@@ -2,17 +2,16 @@
 table
   tr
     th Variant
-    th Players names
-    th Cadence
+    th White
+    th Black
+    th Time control
     th(v-if="showResult") Result
-    th(v-else) Moves count
   tr(v-for="g in games" @click="$emit('show-game',g)")
     td {{ g.vname }}
-    td
-      span(v-for="p in g.players") {{ p.name }}
-    td {{ g.mainTime }} + {{ g.increment }}
+    td {{ g.players[0] }}
+    td {{ g.players[1] }}
+    td {{ g.timeControl }}
     td(v-if="showResult") {{ g.score }}
-    td(v-else) {{ g.movesCount }}
 </template>
 
 <script>
diff --git a/client/src/utils/gameStorage.js b/client/src/utils/gameStorage.js
index 75696a09..3bd99347 100644
--- a/client/src/utils/gameStorage.js
+++ b/client/src/utils/gameStorage.js
@@ -37,7 +37,8 @@ function dbOperation(callback)
       alert("Error while loading database: " + event.target.errorCode);
     };
     // Create objectStore for vchess->games
-    db.createObjectStore("games", { keyPath: "gameId" });
+    let objectStore = db.createObjectStore("games", { keyPath: "gameId" });
+    objectStore.createIndex("score", "score"); //to search by game result
   }
 }
 
@@ -115,6 +116,16 @@ export const GameStorage =
     });
   },
 
+  getCurrent: function(callback)
+  {
+    dbOperation((db) => {
+      let objectStore = db.transaction('games').objectStore('games');
+      objectStore.get("*").onsuccess = function(event) {
+        callback(event.target.result);
+      };
+    });
+  },
+
   // Delete a game in indexedDB
   remove: function(gameId, callback)
   {
diff --git a/client/src/views/Hall.vue b/client/src/views/Hall.vue
index 6a576220..95a9e954 100644
--- a/client/src/views/Hall.vue
+++ b/client/src/views/Hall.vue
@@ -277,27 +277,28 @@ export default {
               timeControl: c.timeControl
             };
             this.st.conn.send(JSON.stringify({code:"challenge",
-              challenge:myChallenge, target:data.from}));
+              chall:myChallenge, target:data.from}));
           }
           break;
         }
         case "askgame":
         {
-          // Send my current live games (if any)
-          // TODO: from indexedDB, through GameStorage.
-//          if (!!localStorage["gid"])
-//          {
-//            const myGame =
-//            {
-//              // Minimal game informations: (fen+clock not required)
-//              id: localStorage["gid"],
-//              players: JSON.parse(localStorage["players"]), //array sid+id+name
-//              vname: localStorage["vname"],
-//              timeControl: localStorage["timeControl"],
-//            };
-//            this.st.conn.send(JSON.stringify({code:"game",
-//              game:myGame, target:data.from}));
-//          }
+          // Send my current live game (if any)
+          GameStorage.getCurrent((game) => {
+            if (!!game)
+            {
+              const myGame =
+              {
+                // Minimal game informations:
+                id: game.id,
+                players: game.players.map(p => p.name),
+                vname: game.vname,
+                timeControl: game.timeControl,
+              };
+              this.st.conn.send(JSON.stringify({code:"game",
+                game:myGame, target:data.from}));
+            }
+          });
           break;
         }
         case "identity":
@@ -314,8 +315,8 @@ export default {
           newChall.type = this.classifyObject(data.chall);
           const pIdx = this.people.findIndex(p => p.sid == data.from);
           newChall.from = this.people[pIdx]; //may be anonymous
-          newChall.added = Date.now();
-          newChall.vname = this.getVname(newChall.vid);
+          newChall.added = Date.now(); //TODO: this is reception timestamp, not creation
+          newChall.vname = this.getVname(newChall.vid); //TODO: just send vname?
           this.challenges.push(newChall);
           break;
         }
@@ -323,10 +324,10 @@ export default {
         {
           // Receive game from some player (+sid)
           // NOTE: it may be correspondance (if newgame while we are connected)
-          // TODO: ambiguous naming "newGame" ==> rename function ?
           let newGame = data.game;
           newGame.type = this.classifyObject(data.game);
-          newGame.vname = newGame.vname;
+          newGame.rid = data.from;
+          newGame.score = "*";
           this.games.push(newGame);
           break;
         }
@@ -337,7 +338,7 @@ export default {
           // Delete corresponding challenge:
           ArrayFun.remove(this.challenges, c => c.id == data.cid);
           // New game just started: data contain all informations
-          this.newGame(data.gameInfo);
+          this.startNewGame(data.gameInfo);
           break;
         }
 // *  - receive "accept/cancel challenge": apply action to challenges list
@@ -534,10 +535,10 @@ export default {
         gameInfo:gameInfo, cid:c.id, target:c.seat.sid}));
       // Delete corresponding challenge:
       ArrayFun.remove(this.challenges, ch => ch.id == c.id);
-      this.newGame(gameInfo); //also!
+      this.startNewGame(gameInfo); //also!
     },
     // NOTE: for live games only (corr games are launched on server)
-    newGame: function(gameInfo) {
+    startNewGame: function(gameInfo) {
       // Extract times (in [milli]seconds), set clocks
       const tc = extractTime(gameInfo.timeControl);
       let initime = [...Array(gameInfo.players.length)];
diff --git a/server/sockets.js b/server/sockets.js
index fcda9816..0885d7e0 100644
--- a/server/sockets.js
+++ b/server/sockets.js
@@ -53,10 +53,6 @@ module.exports = function(wss) {
           clients[obj.target].send(
             JSON.stringify({code:"identity",user:obj.user}));
           break;
-        case "challenge":
-          clients[obj.target].send(
-            JSON.stringify({code:"challenge", chall:obj.chall, from:sid}));
-          break;
         case "acceptchallenge":
           clients[obj.target].send(
             JSON.stringify({code:"acceptchallenge", cid:obj.cid, from:sid}));
@@ -77,8 +73,13 @@ module.exports = function(wss) {
           clients[obj.target].send(JSON.stringify(
             {code:"newgame", gameInfo:obj.gameInfo, cid:obj.cid}));
           break;
+        case "challenge":
+          clients[obj.target].send(JSON.stringify(
+            {code:"challenge", chall:obj.chall, from:sid}));
+          break;
         case "game":
-          // TODO: relay (live) game to other player
+          clients[obj.target].send(JSON.stringify(
+            {code:"game", game:obj.game, from:sid}));
           break;
         case "newchat":
           clients[obj.target].send(JSON.stringify({code:"newchat",msg:obj.msg}));
-- 
2.48.1