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