Experimental update: preview corr move + allow deletion of any game
[vchess.git] / client / src / components / GameList.vue
index f85c49f..0a8bd8c 100644 (file)
@@ -1,6 +1,6 @@
 <template lang="pug">
 div
-  table
+  table.game-list(v-if="games.length > 0")
     thead
       tr
         th {{ st.tr["Variant"] }}
@@ -21,11 +21,14 @@ div
           @click="deleteGame(g,$event)"
         )
           | {{ g.score }}
+  p(v-else)
+    | {{ st.tr["No games found :( Send a challenge!"] }}
 </template>
 
 <script>
 import { store } from "@/store";
 import { GameStorage } from "@/utils/gameStorage";
+import { ajax } from "@/utils/ajax";
 export default {
   name: "my-game-list",
   props: ["games", "showBoth"],
@@ -54,6 +57,13 @@ export default {
       // Show in order: games where it's my turn, my running games, my games, other games
       let minCreated = Number.MAX_SAFE_INTEGER;
       let maxCreated = 0;
+      const isMyTurn = (g, myColor) => {
+        const rem = g.movesCount % 2;
+        return (
+          (rem == 0 && myColor == "w") ||
+          (rem == 1 && myColor == "b")
+        );
+      };
       let augmentedGames = this.games
         .filter(g => !this.deleted[g.id])
         .map(g => {
@@ -72,11 +82,7 @@ export default {
                 : "b";
             if (g.score == "*") {
               priority++;
-              // I play in this game, so g.fen will be defined
-              // NOTE: this is a fragile way to detect turn,
-              // but since V isn't defined let's do that for now. (TODO:)
-              //if (V.ParseFen(g.fen).turn == myColor)
-              if (g.fen.match(" " + myColor + " ")) priority++;
+              if (isMyTurn(g, myColor)) priority++;
             }
           }
           if (g.created < minCreated) minCreated = g.created;
@@ -131,14 +137,43 @@ export default {
       return res;
     },
     deleteGame: function(game, e) {
-      if (game.score != "*") {
-        if (confirm(this.st.tr["Remove game?"])) {
-          GameStorage.remove(
-            game.id,
-            () => {
-              this.$set(this.deleted, game.id, true);
-            }
-          );
+      if (
+        // My game ?
+        game.players.some(p =>
+          p.sid == this.st.user.sid ||
+          p.uid == this.st.user.id
+        )
+      ) {
+        const message =
+          game.score != "*"
+            ? "Remove game?"
+            : "Abort and remove game?";
+        if (confirm(this.st.tr[message])) {
+          const afterDelete = () => {
+            if (game.score == "*") this.$emit("abort", game);
+            this.$set(this.deleted, game.id, true);
+          };
+          if (game.type == "live")
+            // Effectively remove game:
+            GameStorage.remove(game.id, afterDelete);
+          else {
+            const mySide =
+              game.players[0].uid == this.st.user.id
+                ? "White"
+                : "Black";
+            game["deletedBy" + mySide] = true;
+            // Mark the game for deletion on server
+            // If both people mark it, it is deleted
+            ajax(
+              "/games",
+              "PUT",
+              {
+                gid: game.id,
+                newObj: { removeFlag: true }
+              },
+              afterDelete
+            );
+          }
         }
         e.stopPropagation();
       }
@@ -148,6 +183,10 @@ export default {
 </script>
 
 <style lang="sass" scoped>
+p
+  text-align: center
+  font-weight: bold
+
 // NOTE: the style applied to <tr> element doesn't work
 tr.my-turn > td
   background-color: #fcd785