354cfa0347087c352f9c7ea29e8a532f5f49414f
[vchess.git] / client / src / components / GameList.vue
1 <template lang="pug">
2 div
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>
29 import { store } from "@/store";
30 import { GameStorage } from "@/utils/gameStorage";
31 import { ajax } from "@/utils/ajax";
32 export 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 g.priority = 0;
65 if (g.score == "*") {
66 g.priority++;
67 if (!!g.myColor) g.priority++;
68 if (!!g.myTurn) g.priority++;
69 }
70 });
71 const deltaCreated = maxCreated - minCreated;
72 return remGames.sort((g1, g2) => {
73 return (
74 g2.priority - g1.priority + (g2.created - g1.created) / deltaCreated
75 );
76 });
77 },
78 },
79 methods: {
80 player_s: function(g) {
81 if (this.showBoth)
82 return (
83 (g.players[0].name || "@nonymous") +
84 " - " +
85 (g.players[1].name || "@nonymous")
86 );
87 if (
88 this.st.user.sid == g.players[0].sid ||
89 this.st.user.id == g.players[0].uid
90 )
91 return g.players[1].name || "@nonymous";
92 return g.players[0].name || "@nonymous";
93 },
94 scoreClass: function(g) {
95 if (g.score == "*" || !g.myColor) return {};
96 // Ok it's my finished game: determine if I won, drew or lost.
97 let res = {};
98 switch (g.score) {
99 case "1-0":
100 res[g.myColor == "w" ? "won" : "lost"] = true;
101 break;
102 case "0-1":
103 res[g.myColor == "b" ? "won" : "lost"] = true;
104 break;
105 case "1/2":
106 res["draw"] = true;
107 break;
108 // default case: "?" for unknown finished
109 default:
110 res["unknown"] = true;
111 }
112 return res;
113 },
114 deleteGame: function(game, e) {
115 if (
116 // My game ?
117 game.players.some(p =>
118 p.sid == this.st.user.sid ||
119 p.uid == this.st.user.id
120 )
121 ) {
122 const message =
123 game.score != "*"
124 ? "Remove game?"
125 : "Abort and remove game?";
126 if (confirm(this.st.tr[message])) {
127 const afterDelete = () => {
128 if (game.score == "*") this.$emit("abortgame", game);
129 this.$set(this.deleted, game.id, true);
130 };
131 if (game.type == "live")
132 // Effectively remove game:
133 GameStorage.remove(game.id, afterDelete);
134 else {
135 const mySide =
136 game.players[0].uid == this.st.user.id
137 ? "White"
138 : "Black";
139 game["deletedBy" + mySide] = true;
140 // Mark the game for deletion on server
141 // If both people mark it, it is deleted
142 ajax(
143 "/games",
144 "PUT",
145 {
146 data: {
147 gid: game.id,
148 newObj: { removeFlag: true }
149 },
150 success: afterDelete
151 }
152 );
153 }
154 }
155 e.stopPropagation();
156 }
157 }
158 }
159 };
160 </script>
161
162 <style lang="sass" scoped>
163 p
164 text-align: center
165 font-weight: bold
166
167 // NOTE: the style applied to <tr> element doesn't work
168 tr.my-turn > td
169 background-color: #fcd785
170
171 tr
172 td.lost
173 background-color: #f5b7b1
174 td.won
175 background-color: lightgreen
176 td.draw
177 background-color: lightblue
178 td.unknown
179 background-color: lightgrey
180 </style>