Remove unused move.color, format moves corr --> live after retrieval
authorBenjamin Auder <benjamin.auder@somewhere>
Fri, 6 Dec 2019 22:43:12 +0000 (23:43 +0100)
committerBenjamin Auder <benjamin.auder@somewhere>
Fri, 6 Dec 2019 22:43:12 +0000 (23:43 +0100)
client/src/components/BaseGame.vue
client/src/utils/gameStorage.js
client/src/views/Game.vue
server/db/create.sql
server/models/Game.js
server/routes/games.js

index 19aaf15..e0bd937 100644 (file)
@@ -16,6 +16,7 @@
       button(@click="flip") Flip
       button(@click="gotoBegin") GotoBegin
       button(@click="gotoEnd") GotoEnd
+    #messageDiv.section-content {{ curMoveMessage() }}
     #fenDiv.section-content(v-if="showFen && !!vr")
       p#fenString.text-center {{ vr.getFen() }}
     #pgnDiv.section-content
@@ -98,6 +99,12 @@ export default {
       this.cursor = L-1;
       this.lastMove = (L > 0 ? this.moves[L-1]  : null);
     },
+    // For corr games, potential message with each move sent
+    curMoveMessage: function() {
+      if (this.cursor < 0)
+        return "";
+      return this.game.moves[this.cursor].message || "";
+    },
     download: function() {
       const content = this.getPgn();
       // Prepare and trigger download link
index 0b3a7da..dc65379 100644 (file)
@@ -133,7 +133,11 @@ export const GameStorage =
     if (Number.isInteger(gameId) || !isNaN(parseInt(gameId)))
     {
       ajax("/games", "GET", {gid:gameId}, res => {
-        callback(res.game);
+        let game = res.game;
+        game.moves.forEach(m => {
+          m.squares = JSON.parse(m.squares);
+        });
+        callback(game);
       });
     }
     else //local game
index 114b86b..cb00856 100644 (file)
@@ -349,6 +349,7 @@ export default {
           game.clocks = [tc.mainTime, tc.mainTime];
           game.initime = [0, 0];
           const L = game.moves.length;
+          game.moves.sort((m1,m2) => m1.idx - m2.idx); //in case of
           if (L >= 3)
           {
             let addTime = [0, 0];
@@ -362,6 +363,18 @@ export default {
           }
           if (L >= 1)
             game.initime[L%2] = game.moves[L-1].played;
+          // Now that we used idx and played, re-format moves as for live games
+          game.moves = game.moves.map(m => {
+            const s = m.squares;
+            return
+            {
+              appear: s.appear,
+              vanish: s.vanish,
+              start: s.start,
+              end: s.end,
+              message: m.message,
+            };
+          });
         }
         const myIdx = game.players.findIndex(p => {
           return p.sid == this.st.user.sid || p.uid == this.st.user.id;
@@ -453,7 +466,6 @@ export default {
           message: this.corrMsg, //TODO
           played: Date.now(), //TODO: on server?
           idx: this.game.moves.length,
-          color: move.color,
         },
         clocks: this.game.clocks.map((t,i) => i==colorIdx
           ? this.game.clocks[i] + addTime
index 0251c17..11c3574 100644 (file)
@@ -69,7 +69,6 @@ create table Moves (
   message varchar,
   played datetime, --when was this move played?
   idx integer, --index of the move in the game
-  color character, --required for e.g. Marseillais Chess
   foreign key (gid) references Games(id)
 );
 
index 0e42292..786f1e7 100644 (file)
@@ -20,7 +20,6 @@ var db = require("../utils/database");
  *   message: text
  *   played: datetime
  *   idx: integer
- *   color: character
  */
 
 const GameModel =
@@ -71,7 +70,7 @@ const GameModel =
                                        if (!!err2)
                                                return cb(err2);
                                        query =
-                                               "SELECT squares, message, played, idx, color " +
+                                               "SELECT squares, message, played, idx " +
                                                "FROM Moves " +
                                                "WHERE gid = " + id;
                                        db.all(query, (err3,moves) => {
@@ -147,9 +146,9 @@ const GameModel =
       {
         const m = obj.move;
         query =
-          "INSERT INTO Moves (gid,squares,message,played,idx,color) VALUES " +
+          "INSERT INTO Moves (gid, squares, message, played, idx) VALUES " +
           "(" + id + ",'" + JSON.stringify(m.squares) + "','" + m.message +
-            "'," + m.played + "," + m.idx + ",'" + m.color + "')";
+            "'," + m.played + "," + m.idx + ")";
         db.run(query);
       }
     });
index 128d9ee..9bd9a30 100644 (file)
@@ -34,7 +34,7 @@ router.get("/games", access.ajax, (req,res) => {
   {
     GameModel.getOne(gameId, (err,game) => {
                  access.checkRequest(res, err, game, "Game not found", () => {
-                         res.json({game: game});
+        res.json({game: game});
                  });
          });
   }