Do not show deleted games in GameList
[vchess.git] / client / src / components / GameList.vue
... / ...
CommitLineData
1<template lang="pug">
2div
3 table.game-list(v-if="games.length > 0")
4 thead
5 tr
6 th {{ st.tr["Variant"] }}
7 th {{ st.tr[showBoth ? "Players" : "Versus"] }}
8 th(v-if="showCadence") {{ st.tr["Cadence"] }}
9 th {{ st.tr["Result"] }}
10 tbody
11 tr(
12 v-for="g in sortedGames"
13 @click="$emit('show-game',g)"
14 :class="{'my-turn': !!g.myTurn}"
15 )
16 td {{ g.vname }}
17 td {{ player_s(g) }}
18 td(v-if="showCadence") {{ g.cadence }}
19 td(
20 :class="scoreClass(g)"
21 @click="deleteGame(g,$event)"
22 )
23 | {{ g.score }}
24 p(v-else)
25 | {{ st.tr["No games found :( Send a challenge!"] }}
26</template>
27
28<script>
29import { store } from "@/store";
30import { GameStorage } from "@/utils/gameStorage";
31import { ajax } from "@/utils/ajax";
32export default {
33 name: "my-game-list",
34 props: ["games", "showBoth"],
35 data: function() {
36 return {
37 st: store.state,
38 deleted: {}, //mark deleted games
39 showCadence: window.innerWidth >= 425 //TODO: arbitrary value
40 };
41 },
42 mounted: function() {
43 // timeout to avoid calling too many time the adjust method
44 let timeoutLaunched = false;
45 window.addEventListener("resize", () => {
46 if (!timeoutLaunched) {
47 timeoutLaunched = true;
48 setTimeout(() => {
49 this.showCadence = window.innerWidth >= 425;
50 timeoutLaunched = false;
51 }, 500);
52 }
53 });
54 },
55 computed: {
56 sortedGames: function() {
57 // Show in order: it's my turn, running games, completed games
58 let minCreated = Number.MAX_SAFE_INTEGER;
59 let maxCreated = 0;
60 let remGames = this.games.filter(g => !this.deleted[g.id]);
61 remGames.forEach(g => {
62 if (g.created < minCreated) minCreated = g.created;
63 if (g.created > maxCreated) maxCreated = g.created;
64 });
65 const deltaCreated = maxCreated - minCreated;
66 return remGames.sort((g1, g2) => {
67 return (
68 g2.priority - g1.priority + (g2.created - g1.created) / deltaCreated
69 );
70 });
71 },
72 },
73 methods: {
74 player_s: function(g) {
75 if (this.showBoth)
76 return (
77 (g.players[0].name || "@nonymous") +
78 " - " +
79 (g.players[1].name || "@nonymous")
80 );
81 if (
82 this.st.user.sid == g.players[0].sid ||
83 this.st.user.id == g.players[0].uid
84 )
85 return g.players[1].name || "@nonymous";
86 return g.players[0].name || "@nonymous";
87 },
88 scoreClass: function(g) {
89 if (g.score == "*" || !g.myColor) return {};
90 // Ok it's my finished game: determine if I won, drew or lost.
91 let res = {};
92 switch (g.score) {
93 case "1-0":
94 res[g.myColor == "w" ? "won" : "lost"] = true;
95 break;
96 case "0-1":
97 res[g.myColor == "b" ? "won" : "lost"] = true;
98 break;
99 case "1/2":
100 res["draw"] = true;
101 break;
102 // default case: "?" for unknown finished
103 default:
104 res["unknown"] = true;
105 }
106 return res;
107 },
108 deleteGame: function(game, e) {
109 if (
110 // My game ?
111 game.players.some(p =>
112 p.sid == this.st.user.sid ||
113 p.uid == this.st.user.id
114 )
115 ) {
116 const message =
117 game.score != "*"
118 ? "Remove game?"
119 : "Abort and remove game?";
120 if (confirm(this.st.tr[message])) {
121 const afterDelete = () => {
122 if (game.score == "*") this.$emit("abortgame", game);
123 this.$set(this.deleted, game.id, true);
124 };
125 if (game.type == "live")
126 // Effectively remove game:
127 GameStorage.remove(game.id, afterDelete);
128 else {
129 const mySide =
130 game.players[0].uid == this.st.user.id
131 ? "White"
132 : "Black";
133 game["deletedBy" + mySide] = true;
134 // Mark the game for deletion on server
135 // If both people mark it, it is deleted
136 ajax(
137 "/games",
138 "PUT",
139 {
140 data: {
141 gid: game.id,
142 newObj: { removeFlag: true }
143 },
144 success: afterDelete
145 }
146 );
147 }
148 }
149 e.stopPropagation();
150 }
151 }
152 }
153};
154</script>
155
156<style lang="sass" scoped>
157p
158 text-align: center
159 font-weight: bold
160
161// NOTE: the style applied to <tr> element doesn't work
162tr.my-turn > td
163 background-color: #fcd785
164
165tr
166 td.lost
167 background-color: #f5b7b1
168 td.won
169 background-color: lightgreen
170 td.draw
171 background-color: lightblue
172 td.unknown
173 background-color: lightgrey
174</style>