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"] }} | |
5f918a27 BA |
10 | button.tabbtn#importGames(@click="setDisplay('import',$event)") |
11 | | {{ st.tr["Imported games"] }} | |
910d631b | 12 | GameList( |
585d0955 | 13 | ref="livegames" |
910d631b BA |
14 | v-show="display=='live'" |
15 | :games="liveGames" | |
16 | @show-game="showGame" | |
3b0f26c1 | 17 | @abortgame="abortGame" |
910d631b | 18 | ) |
934f7f70 BA |
19 | GameList( |
20 | v-show="display=='corr'" | |
21 | ref="corrgames" | |
22 | :games="corrGames" | |
23 | @show-game="showGame" | |
24 | @abortgame="abortGame" | |
25 | ) | |
73f8753f BA |
26 | UploadGame( |
27 | v-show="display=='import'" | |
28 | @game-uploaded="addGameImport" | |
29 | ) | |
5f918a27 BA |
30 | GameList( |
31 | v-show="display=='import'" | |
32 | ref="importgames" | |
33 | :games="importGames" | |
5b18515f | 34 | :show-both="true" |
5f918a27 BA |
35 | @show-game="showGame" |
36 | ) | |
934f7f70 BA |
37 | button#loadMoreBtn( |
38 | v-show="hasMore[display]" | |
39 | @click="loadMore(display)" | |
40 | ) | |
41 | | {{ st.tr["Load more"] }} | |
afd3240d BA |
42 | </template> |
43 | ||
44 | <script> | |
afd3240d BA |
45 | import { store } from "@/store"; |
46 | import { GameStorage } from "@/utils/gameStorage"; | |
5f918a27 | 47 | import { ImportgameStorage } from "@/utils/importgameStorage"; |
afd3240d | 48 | import { ajax } from "@/utils/ajax"; |
aae89b49 | 49 | import { getScoreMessage } from "@/utils/scoring"; |
23ecf008 BA |
50 | import params from "@/parameters"; |
51 | import { getRandString } from "@/utils/alea"; | |
afd3240d | 52 | import GameList from "@/components/GameList.vue"; |
91777c2c | 53 | import UploadGame from "@/components/UploadGame.vue"; |
afd3240d | 54 | export default { |
89021f18 | 55 | name: "my-my-games", |
afd3240d | 56 | components: { |
5f918a27 BA |
57 | GameList, |
58 | UploadGame | |
afd3240d BA |
59 | }, |
60 | data: function() { | |
61 | return { | |
62 | st: store.state, | |
dac39588 | 63 | display: "live", |
2f258c37 | 64 | liveGames: [], |
db1f1f9a | 65 | corrGames: [], |
5f918a27 | 66 | importGames: [], |
934f7f70 BA |
67 | // timestamp of last showed (oldest) game: |
68 | cursor: { | |
69 | live: Number.MAX_SAFE_INTEGER, | |
5f918a27 | 70 | "import": Number.MAX_SAFE_INTEGER, |
934f7f70 BA |
71 | corr: Number.MAX_SAFE_INTEGER |
72 | }, | |
0234201f | 73 | // hasMore == TRUE: a priori there could be more games to load |
5f918a27 BA |
74 | hasMore: { |
75 | live: true, | |
76 | "import": true, | |
77 | corr: (store.state.user.id > 0) | |
78 | }, | |
db1f1f9a | 79 | conn: null, |
882ec6fe BA |
80 | connexionString: "", |
81 | socketCloseListener: 0 | |
afd3240d BA |
82 | }; |
83 | }, | |
1112f1fd BA |
84 | watch: { |
85 | $route: function(to, from) { | |
86 | if (to.path != "/mygames") this.cleanBeforeDestroy(); | |
87 | } | |
88 | }, | |
afd3240d | 89 | created: function() { |
1112f1fd | 90 | window.addEventListener("beforeunload", this.cleanBeforeDestroy); |
db1f1f9a BA |
91 | // Initialize connection |
92 | this.connexionString = | |
93 | params.socketUrl + | |
7e476ce4 BA |
94 | "/?sid=" + this.st.user.sid + |
95 | "&id=" + this.st.user.id + | |
96 | "&tmpId=" + getRandString() + | |
db1f1f9a BA |
97 | "&page=" + |
98 | encodeURIComponent(this.$route.path); | |
99 | this.conn = new WebSocket(this.connexionString); | |
100 | this.conn.onmessage = this.socketMessageListener; | |
882ec6fe BA |
101 | this.socketCloseListener = setInterval( |
102 | () => { | |
103 | if (this.conn.readyState == 3) { | |
104 | // Connexion is closed: re-open | |
105 | this.conn.removeEventListener("message", this.socketMessageListener); | |
106 | this.conn = new WebSocket(this.connexionString); | |
107 | this.conn.addEventListener("message", this.socketMessageListener); | |
108 | } | |
109 | }, | |
110 | 1000 | |
111 | ); | |
afd3240d | 112 | }, |
2f258c37 | 113 | mounted: function() { |
585d0955 BA |
114 | const adjustAndSetDisplay = () => { |
115 | // showType is the last type viwed by the user (default) | |
116 | let showType = localStorage.getItem("type-myGames") || "live"; | |
117 | // Live games, my turn: highest priority: | |
118 | if (this.liveGames.some(g => !!g.myTurn)) showType = "live"; | |
119 | // Then corr games, my turn: | |
120 | else if (this.corrGames.some(g => !!g.myTurn)) showType = "corr"; | |
121 | else { | |
122 | // If a listing is empty, try showing the other (if non-empty) | |
123 | const types = ["corr", "live"]; | |
124 | for (let i of [0,1]) { | |
125 | if ( | |
126 | this[types[i] + "Games"].length > 0 && | |
127 | this[types[1-i] + "Games"].length == 0 | |
128 | ) { | |
129 | showType = types[i]; | |
130 | } | |
131 | } | |
132 | } | |
133 | this.setDisplay(showType); | |
134 | }; | |
934f7f70 | 135 | GameStorage.getRunning(localGames => { |
585d0955 BA |
136 | localGames.forEach(g => g.type = "live"); |
137 | this.decorate(localGames); | |
138 | this.liveGames = localGames; | |
139 | if (this.st.user.id > 0) { | |
0234201f | 140 | // Ask running corr games first |
585d0955 | 141 | ajax( |
0234201f | 142 | "/runninggames", |
585d0955 BA |
143 | "GET", |
144 | { | |
f14572c4 | 145 | credentials: true, |
585d0955 | 146 | success: (res) => { |
0234201f BA |
147 | // These games are garanteed to not be deleted |
148 | this.corrGames = res.games; | |
f14572c4 BA |
149 | this.corrGames.forEach(g => { |
150 | g.type = "corr"; | |
151 | g.score = "*"; | |
152 | }); | |
0234201f BA |
153 | this.decorate(this.corrGames); |
154 | // Now ask completed games (partial list) | |
934f7f70 BA |
155 | this.loadMore( |
156 | "live", | |
1ef65040 BA |
157 | () => this.loadMore("corr", () => { |
158 | this.loadMore("import", adjustAndSetDisplay); | |
159 | }) | |
0234201f | 160 | ); |
585d0955 BA |
161 | } |
162 | } | |
163 | ); | |
1ef65040 BA |
164 | } |
165 | else { | |
166 | this.loadMore("live", () => { | |
167 | this.loadMore("import", adjustAndSetDisplay); | |
168 | }); | |
169 | } | |
585d0955 | 170 | }); |
2f258c37 | 171 | }, |
23ecf008 | 172 | beforeDestroy: function() { |
1112f1fd | 173 | this.cleanBeforeDestroy(); |
23ecf008 | 174 | }, |
afd3240d | 175 | methods: { |
1112f1fd | 176 | cleanBeforeDestroy: function() { |
882ec6fe | 177 | clearInterval(this.socketCloseListener); |
1112f1fd | 178 | window.removeEventListener("beforeunload", this.cleanBeforeDestroy); |
68e3aa8c | 179 | this.conn.removeEventListener("message", this.socketMessageListener); |
1112f1fd | 180 | this.conn.send(JSON.stringify({code: "disconnect"})); |
68e3aa8c | 181 | this.conn = null; |
1112f1fd | 182 | }, |
2f258c37 BA |
183 | setDisplay: function(type, e) { |
184 | this.display = type; | |
185 | localStorage.setItem("type-myGames", type); | |
6808d7a1 | 186 | let elt = e ? e.target : document.getElementById(type + "Games"); |
2f258c37 | 187 | elt.classList.add("active"); |
23ecf008 | 188 | elt.classList.remove("somethingnew"); //in case of |
73f8753f BA |
189 | for (let t of ["live","corr","import"]) { |
190 | if (t != type) | |
191 | document.getElementById(t + "Games").classList.remove("active"); | |
192 | } | |
2f258c37 | 193 | }, |
5f918a27 | 194 | addGameImport(game) { |
1ef65040 BA |
195 | game.type = "import"; |
196 | ImportgameStorage.add(game, (err) => { | |
197 | if (!!err) { | |
198 | if (err.message.indexOf("Key already exists") < 0) { | |
199 | alert(this.st.tr["An error occurred. Try again!"]); | |
200 | return; | |
201 | } | |
fef153df BA |
202 | // NOTE: since a random new ID is generated for imported games, |
203 | // this error will not occur. | |
1ef65040 BA |
204 | else alert(this.st.tr["The game was already imported"]); |
205 | } | |
206 | this.$router.push("/game/" + game.id); | |
207 | }); | |
5f918a27 | 208 | }, |
cafe0166 BA |
209 | tryShowNewsIndicator: function(type) { |
210 | if ( | |
73f8753f BA |
211 | (type == "live" && this.display != "live") || |
212 | (type == "corr" && this.display != "corr") | |
cafe0166 BA |
213 | ) { |
214 | document | |
215 | .getElementById(type + "Games") | |
216 | .classList.add("somethingnew"); | |
217 | } | |
218 | }, | |
28b32b4f | 219 | // Called at loading to augment games with myColor + myTurn infos |
e727fe31 BA |
220 | decorate: function(games) { |
221 | games.forEach(g => { | |
6b7b2cf7 | 222 | g.myColor = |
0234201f | 223 | (g.type == "corr" && g.players[0].id == this.st.user.id) || |
6b7b2cf7 BA |
224 | (g.type == "live" && g.players[0].sid == this.st.user.sid) |
225 | ? 'w' | |
226 | : 'b'; | |
227 | // If game is over, myTurn doesn't exist: | |
e727fe31 | 228 | if (g.score == "*") { |
e727fe31 | 229 | const rem = g.movesCount % 2; |
6b7b2cf7 | 230 | if ((rem == 0 && g.myColor == 'w') || (rem == 1 && g.myColor == 'b')) |
e727fe31 | 231 | g.myTurn = true; |
e727fe31 BA |
232 | } |
233 | }); | |
234 | }, | |
cafe0166 | 235 | socketMessageListener: function(msg) { |
68e3aa8c | 236 | if (!this.conn) return; |
cafe0166 | 237 | const data = JSON.parse(msg.data); |
5f918a27 | 238 | // NOTE: no imported games here |
e727fe31 BA |
239 | let gamesArrays = { |
240 | "corr": this.corrGames, | |
241 | "live": this.liveGames | |
242 | }; | |
cafe0166 | 243 | switch (data.code) { |
cafe0166 BA |
244 | case "notifyturn": |
245 | case "notifyscore": { | |
246 | const info = data.data; | |
e727fe31 BA |
247 | const type = (!!parseInt(info.gid) ? "corr" : "live"); |
248 | let game = gamesArrays[type].find(g => g.id == info.gid); | |
cafe0166 BA |
249 | // "notifything" --> "thing": |
250 | const thing = data.code.substr(6); | |
e727fe31 | 251 | game[thing] = info[thing]; |
585d0955 BA |
252 | if (thing == "turn") { |
253 | game.myTurn = !game.myTurn; | |
254 | if (game.myTurn) this.tryShowNewsIndicator(type); | |
f14572c4 | 255 | } else game.myTurn = false; |
585d0955 BA |
256 | // TODO: forcing refresh like that is ugly and wrong. |
257 | // How to do it cleanly? | |
258 | this.$refs[type + "games"].$forceUpdate(); | |
cafe0166 BA |
259 | break; |
260 | } | |
261 | case "notifynewgame": { | |
262 | const gameInfo = data.data; | |
263 | // st.variants might be uninitialized, | |
264 | // if unlucky and newgame right after connect: | |
265 | const v = this.st.variants.find(v => v.id == gameInfo.vid); | |
266 | const vname = !!v ? v.name : ""; | |
e727fe31 BA |
267 | const type = (gameInfo.cadence.indexOf('d') >= 0 ? "corr": "live"); |
268 | let game = Object.assign( | |
cafe0166 BA |
269 | { |
270 | vname: vname, | |
271 | type: type, | |
2a8a94c9 | 272 | score: "*", |
e727fe31 | 273 | created: Date.now() |
cafe0166 BA |
274 | }, |
275 | gameInfo | |
276 | ); | |
28b32b4f | 277 | game.myTurn = |
0234201f | 278 | (type == "corr" && game.players[0].id == this.st.user.id) || |
28b32b4f | 279 | (type == "live" && game.players[0].sid == this.st.user.sid); |
e727fe31 | 280 | gamesArrays[type].push(game); |
585d0955 BA |
281 | if (game.myTurn) this.tryShowNewsIndicator(type); |
282 | // TODO: cleaner refresh | |
283 | this.$refs[type + "games"].$forceUpdate(); | |
cafe0166 BA |
284 | break; |
285 | } | |
286 | } | |
287 | }, | |
feaf1bf7 | 288 | showGame: function(game) { |
5f918a27 | 289 | if (game.type != "corr" || !game.myTurn) { |
feaf1bf7 | 290 | this.$router.push("/game/" + game.id); |
620a88ed BA |
291 | return; |
292 | } | |
feaf1bf7 BA |
293 | // It's my turn in this game. Are there others? |
294 | let nextIds = ""; | |
e727fe31 BA |
295 | let otherCorrGamesMyTurn = this.corrGames.filter(g => |
296 | g.id != game.id && !!g.myTurn); | |
feaf1bf7 BA |
297 | if (otherCorrGamesMyTurn.length > 0) { |
298 | nextIds += "/?next=["; | |
299 | otherCorrGamesMyTurn.forEach(g => { nextIds += g.id + ","; }); | |
300 | // Remove last comma and close array: | |
301 | nextIds = nextIds.slice(0, -1) + "]"; | |
302 | } | |
303 | this.$router.push("/game/" + game.id + nextIds); | |
db1f1f9a | 304 | }, |
aae89b49 BA |
305 | abortGame: function(game) { |
306 | // Special "trans-pages" case: from MyGames to Game | |
307 | // TODO: also for corr games? (It's less important) | |
308 | if (game.type == "live") { | |
309 | const oppsid = | |
310 | game.players[0].sid == this.st.user.sid | |
311 | ? game.players[1].sid | |
312 | : game.players[0].sid; | |
68e3aa8c BA |
313 | if (!!this.conn) { |
314 | this.conn.send( | |
315 | JSON.stringify( | |
316 | { | |
317 | code: "mabort", | |
318 | gid: game.id, | |
319 | // NOTE: target might not be online | |
320 | target: oppsid | |
321 | } | |
322 | ) | |
323 | ); | |
324 | } | |
aae89b49 | 325 | } |
5f918a27 | 326 | // NOTE: no imported games here |
aae89b49 BA |
327 | else if (!game.deletedByWhite || !game.deletedByBlack) { |
328 | // Set score if game isn't deleted on server: | |
329 | ajax( | |
330 | "/games", | |
331 | "PUT", | |
332 | { | |
e57c4de4 BA |
333 | data: { |
334 | gid: game.id, | |
335 | newObj: { | |
336 | score: "?", | |
337 | scoreMsg: getScoreMessage("?") | |
338 | } | |
aae89b49 BA |
339 | } |
340 | } | |
341 | ); | |
342 | } | |
0234201f | 343 | }, |
934f7f70 | 344 | loadMore: function(type, cb) { |
7ebc0408 | 345 | if (type == "corr" && this.st.user.id > 0) { |
934f7f70 BA |
346 | ajax( |
347 | "/completedgames", | |
348 | "GET", | |
349 | { | |
350 | credentials: true, | |
351 | data: { cursor: this.cursor["corr"] }, | |
352 | success: (res) => { | |
353 | const L = res.games.length; | |
354 | if (L > 0) { | |
355 | this.cursor["corr"] = res.games[L - 1].created; | |
356 | let moreGames = res.games; | |
357 | moreGames.forEach(g => g.type = "corr"); | |
358 | this.decorate(moreGames); | |
359 | this.corrGames = this.corrGames.concat(moreGames); | |
360 | } else this.hasMore["corr"] = false; | |
361 | if (!!cb) cb(); | |
362 | } | |
0234201f | 363 | } |
934f7f70 | 364 | ); |
5f918a27 BA |
365 | } |
366 | else if (type == "live") { | |
934f7f70 BA |
367 | GameStorage.getNext(this.cursor["live"], localGames => { |
368 | const L = localGames.length; | |
369 | if (L > 0) { | |
2c5d7b20 | 370 | // Add "-1" because IDBKeyRange.upperBound includes boundary |
934f7f70 BA |
371 | this.cursor["live"] = localGames[L - 1].created - 1; |
372 | localGames.forEach(g => g.type = "live"); | |
373 | this.decorate(localGames); | |
374 | this.liveGames = this.liveGames.concat(localGames); | |
375 | } else this.hasMore["live"] = false; | |
376 | if (!!cb) cb(); | |
377 | }); | |
378 | } | |
5f918a27 BA |
379 | else if (type == "import") { |
380 | ImportgameStorage.getNext(this.cursor["import"], importGames => { | |
381 | const L = importGames.length; | |
382 | if (L > 0) { | |
383 | // Add "-1" because IDBKeyRange.upperBound includes boundary | |
384 | this.cursor["import"] = importGames[L - 1].created - 1; | |
385 | importGames.forEach(g => g.type = "import"); | |
386 | this.importGames = this.importGames.concat(importGames); | |
387 | } else this.hasMore["import"] = false; | |
388 | if (!!cb) cb(); | |
389 | }); | |
390 | } | |
6808d7a1 BA |
391 | } |
392 | } | |
afd3240d BA |
393 | }; |
394 | </script> | |
2f258c37 | 395 | |
26d8a01a | 396 | <style lang="sass" scoped> |
2f258c37 BA |
397 | .active |
398 | color: #42a983 | |
5fe7e71c BA |
399 | |
400 | .tabbtn | |
401 | background-color: #f9faee | |
e2590fa8 | 402 | |
0234201f | 403 | button#loadMoreBtn |
f14572c4 BA |
404 | display: block |
405 | margin: 0 auto | |
0234201f | 406 | |
23ecf008 BA |
407 | .somethingnew |
408 | background-color: #c5fefe !important | |
2f258c37 | 409 | </style> |
4b8cf8ef BA |
410 | |
411 | <!-- Not scoped because acting on GameList --> | |
412 | <style lang="sass"> | |
413 | table.game-list | |
414 | max-height: 100% | |
415 | </style> |