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