Fix Arena diag
[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 + (g.options.abridged || '') }}
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 { ImportgameStorage } from "@/utils/importgameStorage";
32 import { ajax } from "@/utils/ajax";
33 export default {
34 name: "my-game-list",
35 props: ["games", "showBoth"],
36 data: function() {
37 return {
38 st: store.state,
39 deleted: {}, //mark deleted games
40 showCadence: window.innerWidth >= 425 //TODO: arbitrary value
41 };
42 },
43 mounted: function() {
44 // timeout to avoid calling too many time the adjust method
45 let timeoutLaunched = false;
46 window.addEventListener("resize", () => {
47 if (!timeoutLaunched) {
48 timeoutLaunched = true;
49 setTimeout(() => {
50 this.showCadence = window.innerWidth >= 425;
51 timeoutLaunched = false;
52 }, 500);
53 }
54 });
55 },
56 methods: {
57 player_s: function(g) {
58 if (this.showBoth)
59 return (
60 (g.players[0].name || "@nonymous") +
61 " - " +
62 (g.players[1].name || "@nonymous")
63 );
64 if (
65 this.st.user.sid == g.players[0].sid ||
66 this.st.user.id == g.players[0].id
67 ) {
68 return g.players[1].name || "@nonymous";
69 }
70 return g.players[0].name || "@nonymous";
71 },
72 sortedGames: function() {
73 // Show in order: it's my turn, running games, completed games
74 let minCreated = Number.MAX_SAFE_INTEGER;
75 let maxCreated = 0;
76 let remGames = this.games.filter(g => !this.deleted[g.id]);
77 remGames.forEach(g => {
78 if (g.created < minCreated) minCreated = g.created;
79 if (g.created > maxCreated) maxCreated = g.created;
80 g.priority = 0;
81 if (g.score == "*") {
82 g.priority++;
83 if (!!g.myColor) g.priority++;
84 if (!!g.myTurn) g.priority++;
85 }
86 });
87 const deltaCreated = maxCreated - minCreated;
88 return remGames.sort((g1, g2) => {
89 return (
90 g2.priority - g1.priority +
91 // Modulate with creation time (value in ]0,1[)
92 (g2.created - g1.created) / (deltaCreated + 1)
93 );
94 });
95 },
96 scoreClass: function(g) {
97 if (g.score == "*" || !g.myColor) return {};
98 // Ok it's my finished game: determine if I won, drew or lost.
99 let res = {};
100 switch (g.score) {
101 case "1-0":
102 res[g.myColor == "w" ? "won" : "lost"] = true;
103 break;
104 case "0-1":
105 res[g.myColor == "b" ? "won" : "lost"] = true;
106 break;
107 case "1/2":
108 res["draw"] = true;
109 break;
110 // default case: "?" for unknown finished
111 default:
112 res["unknown"] = true;
113 }
114 return res;
115 },
116 deleteGame: function(game, e) {
117 if (
118 // Import ?
119 (typeof game.id == "string" && game.id.charAt(0) == 'i') ||
120 // My game ?
121 game.players.some(p =>
122 p.sid == this.st.user.sid || p.id == this.st.user.id
123 )
124 ) {
125 const message =
126 game.score != "*"
127 ? "Remove game?"
128 : "Abort and remove game?";
129 if (confirm(this.st.tr[message])) {
130 const afterDelete = () => {
131 if (game.score == "*" && game.type != "import")
132 this.$emit("abortgame", game);
133 this.$set(this.deleted, game.id, true);
134 };
135 if (game.type == "live")
136 // Effectively remove game:
137 GameStorage.remove(game.id, afterDelete);
138 else if (game.type == "import")
139 ImportgameStorage.remove(game.id, afterDelete);
140 else {
141 const mySide =
142 game.players[0].id == this.st.user.id
143 ? "White"
144 : "Black";
145 game["deletedBy" + mySide] = true;
146 // Mark the game for deletion on server
147 // If both people mark it, it is deleted
148 ajax(
149 "/games",
150 "PUT",
151 {
152 data: {
153 gid: game.id,
154 newObj: { removeFlag: true }
155 },
156 success: afterDelete
157 }
158 );
159 }
160 }
161 e.stopPropagation();
162 }
163 }
164 }
165 };
166 </script>
167
168 <style lang="sass" scoped>
169 p
170 text-align: center
171 font-weight: bold
172
173 // NOTE: the style applied to <tr> element doesn't work
174 tr.my-turn > td
175 background-color: #fcd785
176
177 tr
178 td.lost
179 background-color: #f5b7b1
180 td.won
181 background-color: lightgreen
182 td.draw
183 background-color: lightblue
184 td.unknown
185 background-color: lightgrey
186 </style>