GameStorage.getCorrGame ok + add watchers for st.variants
authorBenjamin Auder <benjamin.auder@somewhere>
Sat, 30 Nov 2019 10:16:43 +0000 (11:16 +0100)
committerBenjamin Auder <benjamin.auder@somewhere>
Sat, 30 Nov 2019 10:16:43 +0000 (11:16 +0100)
client/src/utils/gameStorage.js
client/src/views/Game.vue
client/src/views/Hall.vue
client/src/views/MyGames.vue
server/models/Game.js

index 77acf94..031f62f 100644 (file)
@@ -16,6 +16,8 @@
 //   score: string (several options; '*' == running),
 // }
 
+import { ajax } from "@/utils/ajax";
+
 function dbOperation(callback)
 {
   let db = null;
@@ -85,34 +87,46 @@ export const GameStorage =
     });
   },
 
-  // Retrieve any live game from its identifiers (locally, running or not)
-  // NOTE: need callback because result is obtained asynchronously
-  get: function(gameId, callback)
+  // Retrieve all local games (running, completed, imported...)
+  getAll: function(callback)
   {
     dbOperation((db) => {
       let objectStore = db.transaction('games').objectStore('games');
-      if (!gameId) //retrieve all
-      {
-        let games = [];
-        objectStore.openCursor().onsuccess = function(event) {
-          let cursor = event.target.result;
-          // if there is still another cursor to go, keep running this code
-          if (cursor)
-          {
-            games.push(cursor.value);
-            cursor.continue();
-          }
-          else
-            callback(games);
+      let games = [];
+      objectStore.openCursor().onsuccess = function(event) {
+        let cursor = event.target.result;
+        // if there is still another cursor to go, keep running this code
+        if (cursor)
+        {
+          games.push(cursor.value);
+          cursor.continue();
         }
+        else
+          callback(games);
       }
-      else //just one game
-      {
+    });
+  },
+
+  // Retrieve any game from its identifiers (locally or on server)
+  // NOTE: need callback because result is obtained asynchronously
+  get: function(gameId, callback)
+  {
+    // corr games identifiers are integers
+    if (Number.isInteger(gameId) || !isNaN(parseInt(gameId)))
+    {
+      ajax("/games", "GET", {gid:gameId}, res => {
+        callback(res.game);
+      });
+    }
+    else //local game
+    {
+      dbOperation((db) => {
+        let objectStore = db.transaction('games').objectStore('games');
         objectStore.get(gameId).onsuccess = function(event) {
           callback(event.target.result);
         }
-      }
-    });
+      });
+    }
   },
 
   getCurrent: function(callback)
index 47605c4..6cb75f4 100644 (file)
@@ -106,6 +106,11 @@ export default {
         }
       }, 1000);
     },
+    // In case variants array was't loaded when game was retrieved
+    "st.variants": function(variantArray) {
+      if (!!this.game.vname && this.game.vname == "")
+        this.game.vname = variantArray.filter(v => v.id == this.game.vid)[0].name;
+    },
   },
   created: function() {
     if (!!this.$route.params["id"])
@@ -287,7 +292,16 @@ export default {
     //  - from remote peer (one live game I don't play, finished or not)
     loadGame: function(game) {
       const afterRetrieval = async (game) => {
-        const vname = this.st.variants.filter(v => v.id == game.vid)[0].name;
+        // NOTE: variants array might not be available yet, thus the two next lines
+        const variantCell = this.st.variants.filter(v => v.id == game.vid);
+        const vname = (variantCell.length > 0 ? variantCell[0].name : "");
+        if (!game.fen)
+          game.fen = game.fenStart; //game wasn't started
+
+
+        // TODO: process rtime, clocks............ game.clocks doesn't exist anymore
+console.log(game);
+
         const tc = extractTime(game.timeControl);
         if (game.clocks[0] < 0) //game unstarted
         {
index 2fbb6e6..b7526cc 100644 (file)
@@ -93,6 +93,20 @@ export default {
       },
     };
   },
+  watch: {
+    // st.variants changes only once, at loading from [] to [...]
+    "st.variants": function(variantArray) {
+      // Set potential challenges and games variant names:
+      this.challenges.forEach(c => {
+        if (c.vname == "")
+          c.vname = this.getVname(c.vid);
+      });
+      this.games.forEach(g => {
+        if (g.vname == "")
+          g.vname = this.getVname(g.vid)
+      });
+    },
+  },
   computed: {
     uniquePlayers: function() {
       // Show e.g. "@nonymous (5)", and do nothing on click on anonymous
@@ -165,23 +179,6 @@ export default {
           );
         }
       );
-      // TODO: I don't like this code below; improvement?
-      let retryForVnames = setInterval(() => {
-        if (this.st.variants.length > 0) //variants array is loaded
-        {
-          if (this.games.length > 0 && this.games[0].vname == "")
-          {
-            // Fix games' vnames:
-            this.games.forEach(g => { g.vname = this.getVname(g.vid); });
-          }
-          if (this.challenges.length > 0 && this.challenges[0].vname == "")
-          {
-            // Fix challenges' vnames:
-            this.challenges.forEach(c => { c.vname = this.getVname(c.vid); });
-          }
-          clearInterval(retryForVnames);
-        }
-      }, 50);
     }
     // 0.1] Ask server for room composition:
     const funcPollClients = () => {
index ddb689d..c630668 100644 (file)
@@ -2,4 +2,4 @@
                // si dernier lastMove sur serveur n'est pas le mien et nextColor == moi, alors background orange
                // ==> background orange si à moi de jouer par corr (sur main index)
                // (helper: static fonction "GetNextCol()" dans base_rules.js)
-
+//use GameStorage.getAll()
index 78827e2..f99dbde 100644 (file)
@@ -30,8 +30,8 @@ const GameModel =
        {
                db.serialize(function() {
                        let query =
-                               "INSERT INTO Games (vid, fen, timeControl) " +
-                               "VALUES (" + vid + ",'" + fen + "','" + timeControl + "')";
+                               "INSERT INTO Games (vid, fenStart, score, timeControl) " +
+                               "VALUES (" + vid + ",'" + fen + "','*','" + timeControl + "')";
       db.run(query, function(err) {
                                if (!!err)
                                        return cb(err);
@@ -53,39 +53,33 @@ const GameModel =
        {
                db.serialize(function() {
                        let query =
-                               "SELECT v.name AS vname, g.fen, g.fenStart, g.score " +
-                               "FROM Games g " +
-                               "JOIN Variants v " +
-                               "  ON g.vid = v.id "
+                               "SELECT * " +
+                               "FROM Games " +
                                "WHERE id = " + id;
                        db.get(query, (err,gameInfo) => {
                                if (!!err)
                                        return cb(err);
                                query =
-                                       "SELECT p.uid AS id, p.color, p.rtime, u.name " +
-                                       "FROM Players p " +
-                                       "JOIN Users u " +
-                                       "  ON p.uid = u.id " +
-                                       "WHERE p.gid = " + id;
-                               db.run(query, (err2,players) => {
+                                       "SELECT uid, color, rtime " +
+                                       "FROM Players " +
+                                       "WHERE gid = " + id;
+                               db.all(query, (err2,players) => {
                                        if (!!err2)
                                                return cb(err2);
                                        query =
-                                               "SELECT move AS desc, message, played, idx, color " +
+                                               "SELECT move, message, played, idx, color " +
                                                "FROM Moves " +
                                                "WHERE gid = " + id;
-                                       db.run(query, (err3,moves) => {
+                                       db.all(query, (err3,moves) => {
                                                if (!!err3)
                                                        return cb(err3);
-                                               const game = {
-                                                       id: id,
-                                                       vname: gameInfo.vname,
-                                                       fenStart: gameInfo.fenStart,
-                                                       fen: gameInfo.fen,
-                                                       score: gameInfo.score,
-                                                       players: players,
-                                                       moves: moves,
-                                               };
+                                               const game = Object.assign({},
+              gameInfo,
+                                                 {
+                                                         players: players,
+                                                         moves: moves
+              }
+            );
                                                return cb(null, game);
                                        });
                                });