Commit | Line | Data |
---|---|---|
afd3240d BA |
1 | <template lang="pug"> |
2 | main | |
3 | .row | |
4 | .col-sm-12.col-md-10.col-md-offset-1.col-lg-8.col-lg-offset-2 | |
5 | .button-group | |
910d631b BA |
6 | button.tabbtn#liveGames(@click="setDisplay('live',$event)") |
7 | | {{ st.tr["Live games"] }} | |
8 | button.tabbtn#corrGames(@click="setDisplay('corr',$event)") | |
9 | | {{ st.tr["Correspondance games"] }} | |
10 | GameList( | |
585d0955 | 11 | ref="livegames" |
910d631b BA |
12 | v-show="display=='live'" |
13 | :games="liveGames" | |
14 | @show-game="showGame" | |
3b0f26c1 | 15 | @abortgame="abortGame" |
910d631b | 16 | ) |
0234201f BA |
17 | div(v-show="display=='corr'") |
18 | GameList( | |
19 | ref="corrgames" | |
20 | :games="corrGames" | |
21 | @show-game="showGame" | |
22 | @abortgame="abortGame" | |
23 | ) | |
24 | button#loadMoreBtn( | |
25 | v-if="hasMore" | |
26 | @click="loadMore()" | |
27 | ) | |
28 | | {{ st.tr["Load more"] }} | |
afd3240d BA |
29 | </template> |
30 | ||
31 | <script> | |
afd3240d BA |
32 | import { store } from "@/store"; |
33 | import { GameStorage } from "@/utils/gameStorage"; | |
34 | import { ajax } from "@/utils/ajax"; | |
aae89b49 | 35 | import { getScoreMessage } from "@/utils/scoring"; |
23ecf008 BA |
36 | import params from "@/parameters"; |
37 | import { getRandString } from "@/utils/alea"; | |
afd3240d BA |
38 | import GameList from "@/components/GameList.vue"; |
39 | export default { | |
89021f18 | 40 | name: "my-my-games", |
afd3240d | 41 | components: { |
6808d7a1 | 42 | GameList |
afd3240d BA |
43 | }, |
44 | data: function() { | |
45 | return { | |
46 | st: store.state, | |
dac39588 | 47 | display: "live", |
2f258c37 | 48 | liveGames: [], |
db1f1f9a | 49 | corrGames: [], |
0234201f BA |
50 | // timestamp of last showed (oldest) corr game: |
51 | cursor: Number.MAX_SAFE_INTEGER, | |
52 | // hasMore == TRUE: a priori there could be more games to load | |
53 | hasMore: true, | |
db1f1f9a BA |
54 | conn: null, |
55 | connexionString: "" | |
afd3240d BA |
56 | }; |
57 | }, | |
58 | created: function() { | |
db1f1f9a BA |
59 | // Initialize connection |
60 | this.connexionString = | |
61 | params.socketUrl + | |
62 | "/?sid=" + | |
63 | this.st.user.sid + | |
cafe0166 BA |
64 | "&id=" + |
65 | this.st.user.id + | |
db1f1f9a BA |
66 | "&tmpId=" + |
67 | getRandString() + | |
68 | "&page=" + | |
69 | encodeURIComponent(this.$route.path); | |
70 | this.conn = new WebSocket(this.connexionString); | |
71 | this.conn.onmessage = this.socketMessageListener; | |
72 | this.conn.onclose = this.socketCloseListener; | |
afd3240d | 73 | }, |
2f258c37 | 74 | mounted: function() { |
585d0955 BA |
75 | const adjustAndSetDisplay = () => { |
76 | // showType is the last type viwed by the user (default) | |
77 | let showType = localStorage.getItem("type-myGames") || "live"; | |
78 | // Live games, my turn: highest priority: | |
79 | if (this.liveGames.some(g => !!g.myTurn)) showType = "live"; | |
80 | // Then corr games, my turn: | |
81 | else if (this.corrGames.some(g => !!g.myTurn)) showType = "corr"; | |
82 | else { | |
83 | // If a listing is empty, try showing the other (if non-empty) | |
84 | const types = ["corr", "live"]; | |
85 | for (let i of [0,1]) { | |
86 | if ( | |
87 | this[types[i] + "Games"].length > 0 && | |
88 | this[types[1-i] + "Games"].length == 0 | |
89 | ) { | |
90 | showType = types[i]; | |
91 | } | |
92 | } | |
93 | } | |
94 | this.setDisplay(showType); | |
95 | }; | |
96 | GameStorage.getAll(localGames => { | |
97 | localGames.forEach(g => g.type = "live"); | |
98 | this.decorate(localGames); | |
99 | this.liveGames = localGames; | |
100 | if (this.st.user.id > 0) { | |
0234201f | 101 | // Ask running corr games first |
585d0955 | 102 | ajax( |
0234201f | 103 | "/runninggames", |
585d0955 BA |
104 | "GET", |
105 | { | |
585d0955 | 106 | success: (res) => { |
0234201f BA |
107 | // These games are garanteed to not be deleted |
108 | this.corrGames = res.games; | |
109 | this.corrGames.forEach(g => g.type = "corr"); | |
110 | this.decorate(this.corrGames); | |
111 | // Now ask completed games (partial list) | |
112 | ajax( | |
113 | "/completedgames", | |
114 | "GET", | |
115 | { | |
116 | data: { cursor: this.cursor }, | |
117 | success: (res2) => { | |
118 | if (res2.games.length > 0) { | |
119 | const L = res2.games.length; | |
120 | this.cursor = res2.games[L - 1].created; | |
121 | let completedGames = res2.games; | |
122 | completedGames.forEach(g => g.type = "corr"); | |
123 | this.decorate(completedGames); | |
124 | this.corrGames = this.corrGames.concat(completedGames); | |
125 | adjustAndSetDisplay(); | |
126 | } | |
127 | } | |
128 | } | |
129 | ); | |
585d0955 BA |
130 | } |
131 | } | |
132 | ); | |
133 | } else adjustAndSetDisplay(); | |
134 | }); | |
2f258c37 | 135 | }, |
23ecf008 BA |
136 | beforeDestroy: function() { |
137 | this.conn.send(JSON.stringify({code: "disconnect"})); | |
138 | }, | |
afd3240d | 139 | methods: { |
2f258c37 BA |
140 | setDisplay: function(type, e) { |
141 | this.display = type; | |
142 | localStorage.setItem("type-myGames", type); | |
6808d7a1 | 143 | let elt = e ? e.target : document.getElementById(type + "Games"); |
2f258c37 | 144 | elt.classList.add("active"); |
23ecf008 | 145 | elt.classList.remove("somethingnew"); //in case of |
6808d7a1 | 146 | if (elt.previousElementSibling) |
2f258c37 | 147 | elt.previousElementSibling.classList.remove("active"); |
6808d7a1 | 148 | else elt.nextElementSibling.classList.remove("active"); |
2f258c37 | 149 | }, |
cafe0166 BA |
150 | tryShowNewsIndicator: function(type) { |
151 | if ( | |
152 | (type == "live" && this.display == "corr") || | |
153 | (type == "corr" && this.display == "live") | |
154 | ) { | |
155 | document | |
156 | .getElementById(type + "Games") | |
157 | .classList.add("somethingnew"); | |
158 | } | |
159 | }, | |
28b32b4f | 160 | // Called at loading to augment games with myColor + myTurn infos |
e727fe31 BA |
161 | decorate: function(games) { |
162 | games.forEach(g => { | |
6b7b2cf7 | 163 | g.myColor = |
0234201f | 164 | (g.type == "corr" && g.players[0].id == this.st.user.id) || |
6b7b2cf7 BA |
165 | (g.type == "live" && g.players[0].sid == this.st.user.sid) |
166 | ? 'w' | |
167 | : 'b'; | |
168 | // If game is over, myTurn doesn't exist: | |
e727fe31 | 169 | if (g.score == "*") { |
e727fe31 | 170 | const rem = g.movesCount % 2; |
6b7b2cf7 | 171 | if ((rem == 0 && g.myColor == 'w') || (rem == 1 && g.myColor == 'b')) |
e727fe31 | 172 | g.myTurn = true; |
e727fe31 BA |
173 | } |
174 | }); | |
175 | }, | |
cafe0166 BA |
176 | socketMessageListener: function(msg) { |
177 | const data = JSON.parse(msg.data); | |
e727fe31 BA |
178 | let gamesArrays = { |
179 | "corr": this.corrGames, | |
180 | "live": this.liveGames | |
181 | }; | |
cafe0166 | 182 | switch (data.code) { |
cafe0166 BA |
183 | case "notifyturn": |
184 | case "notifyscore": { | |
185 | const info = data.data; | |
e727fe31 BA |
186 | const type = (!!parseInt(info.gid) ? "corr" : "live"); |
187 | let game = gamesArrays[type].find(g => g.id == info.gid); | |
cafe0166 BA |
188 | // "notifything" --> "thing": |
189 | const thing = data.code.substr(6); | |
e727fe31 | 190 | game[thing] = info[thing]; |
585d0955 BA |
191 | if (thing == "turn") { |
192 | game.myTurn = !game.myTurn; | |
193 | if (game.myTurn) this.tryShowNewsIndicator(type); | |
194 | } | |
195 | // TODO: forcing refresh like that is ugly and wrong. | |
196 | // How to do it cleanly? | |
197 | this.$refs[type + "games"].$forceUpdate(); | |
cafe0166 BA |
198 | break; |
199 | } | |
200 | case "notifynewgame": { | |
201 | const gameInfo = data.data; | |
202 | // st.variants might be uninitialized, | |
203 | // if unlucky and newgame right after connect: | |
204 | const v = this.st.variants.find(v => v.id == gameInfo.vid); | |
205 | const vname = !!v ? v.name : ""; | |
e727fe31 BA |
206 | const type = (gameInfo.cadence.indexOf('d') >= 0 ? "corr": "live"); |
207 | let game = Object.assign( | |
cafe0166 BA |
208 | { |
209 | vname: vname, | |
210 | type: type, | |
2a8a94c9 | 211 | score: "*", |
e727fe31 | 212 | created: Date.now() |
cafe0166 BA |
213 | }, |
214 | gameInfo | |
215 | ); | |
28b32b4f | 216 | game.myTurn = |
0234201f | 217 | (type == "corr" && game.players[0].id == this.st.user.id) || |
28b32b4f | 218 | (type == "live" && game.players[0].sid == this.st.user.sid); |
e727fe31 | 219 | gamesArrays[type].push(game); |
585d0955 BA |
220 | if (game.myTurn) this.tryShowNewsIndicator(type); |
221 | // TODO: cleaner refresh | |
222 | this.$refs[type + "games"].$forceUpdate(); | |
cafe0166 BA |
223 | break; |
224 | } | |
225 | } | |
226 | }, | |
227 | socketCloseListener: function() { | |
228 | this.conn = new WebSocket(this.connexionString); | |
229 | this.conn.addEventListener("message", this.socketMessageListener); | |
230 | this.conn.addEventListener("close", this.socketCloseListener); | |
afd3240d | 231 | }, |
feaf1bf7 | 232 | showGame: function(game) { |
e727fe31 | 233 | if (game.type == "live" || !game.myTurn) { |
feaf1bf7 | 234 | this.$router.push("/game/" + game.id); |
620a88ed BA |
235 | return; |
236 | } | |
feaf1bf7 BA |
237 | // It's my turn in this game. Are there others? |
238 | let nextIds = ""; | |
e727fe31 BA |
239 | let otherCorrGamesMyTurn = this.corrGames.filter(g => |
240 | g.id != game.id && !!g.myTurn); | |
feaf1bf7 BA |
241 | if (otherCorrGamesMyTurn.length > 0) { |
242 | nextIds += "/?next=["; | |
243 | otherCorrGamesMyTurn.forEach(g => { nextIds += g.id + ","; }); | |
244 | // Remove last comma and close array: | |
245 | nextIds = nextIds.slice(0, -1) + "]"; | |
246 | } | |
247 | this.$router.push("/game/" + game.id + nextIds); | |
db1f1f9a | 248 | }, |
aae89b49 BA |
249 | abortGame: function(game) { |
250 | // Special "trans-pages" case: from MyGames to Game | |
251 | // TODO: also for corr games? (It's less important) | |
252 | if (game.type == "live") { | |
253 | const oppsid = | |
254 | game.players[0].sid == this.st.user.sid | |
255 | ? game.players[1].sid | |
256 | : game.players[0].sid; | |
257 | this.conn.send( | |
258 | JSON.stringify( | |
259 | { | |
260 | code: "mabort", | |
261 | gid: game.id, | |
262 | // NOTE: target might not be online | |
263 | target: oppsid | |
264 | } | |
265 | ) | |
266 | ); | |
267 | } | |
268 | else if (!game.deletedByWhite || !game.deletedByBlack) { | |
269 | // Set score if game isn't deleted on server: | |
270 | ajax( | |
271 | "/games", | |
272 | "PUT", | |
273 | { | |
e57c4de4 BA |
274 | data: { |
275 | gid: game.id, | |
276 | newObj: { | |
277 | score: "?", | |
278 | scoreMsg: getScoreMessage("?") | |
279 | } | |
aae89b49 BA |
280 | } |
281 | } | |
282 | ); | |
283 | } | |
0234201f BA |
284 | }, |
285 | loadMore: function() { | |
286 | ajax( | |
287 | "/completedgames", | |
288 | "GET", | |
289 | { | |
290 | data: { cursor: this.cursor }, | |
291 | success: (res) => { | |
292 | if (res.games.length > 0) { | |
293 | const L = res.games.length; | |
294 | this.cursor = res.games[L - 1].created; | |
295 | let moreGames = res.games; | |
296 | moreGames.forEach(g => g.type = "corr"); | |
297 | this.decorate(moreGames); | |
298 | this.corrGames = this.corrGames.concat(moreGames); | |
299 | } else this.hasMore = false; | |
300 | } | |
301 | } | |
302 | ); | |
6808d7a1 BA |
303 | } |
304 | } | |
afd3240d BA |
305 | }; |
306 | </script> | |
2f258c37 | 307 | |
e2590fa8 | 308 | <style lang="sass"> |
2f258c37 BA |
309 | .active |
310 | color: #42a983 | |
5fe7e71c BA |
311 | |
312 | .tabbtn | |
313 | background-color: #f9faee | |
e2590fa8 BA |
314 | |
315 | table.game-list | |
316 | max-height: 100% | |
23ecf008 | 317 | |
0234201f BA |
318 | button#loadMoreBtn |
319 | margin-top: 0 | |
320 | margin-bottom: 0 | |
321 | ||
23ecf008 BA |
322 | .somethingnew |
323 | background-color: #c5fefe !important | |
2f258c37 | 324 | </style> |