4 .col-sm-12.col-md-10.col-md-offset-1.col-lg-8.col-lg-offset-2
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 button.tabbtn#importGames(@click="setDisplay('import',$event)")
11 | {{ st.tr["Imported games"] }}
14 v-show="display=='live'"
17 @abortgame="abortGame"
20 v-show="display=='corr'"
24 @abortgame="abortGame"
27 v-show="display=='import'"
28 @game-uploaded="addGameImport"
31 v-show="display=='import'"
37 v-show="hasMore[display]"
38 @click="loadMore(display)"
40 | {{ st.tr["Load more"] }}
44 import { store } from "@/store";
45 import { GameStorage } from "@/utils/gameStorage";
46 import { ImportgameStorage } from "@/utils/importgameStorage";
47 import { ajax } from "@/utils/ajax";
48 import { getScoreMessage } from "@/utils/scoring";
49 import params from "@/parameters";
50 import { getRandString } from "@/utils/alea";
51 import GameList from "@/components/GameList.vue";
52 import UploadGame from "@/components/UploadGame.vue";
66 // timestamp of last showed (oldest) game:
68 live: Number.MAX_SAFE_INTEGER,
69 "import": Number.MAX_SAFE_INTEGER,
70 corr: Number.MAX_SAFE_INTEGER
72 // hasMore == TRUE: a priori there could be more games to load
76 corr: (store.state.user.id > 0)
80 socketCloseListener: 0
84 $route: function(to, from) {
85 if (to.path != "/mygames") this.cleanBeforeDestroy();
89 window.addEventListener("beforeunload", this.cleanBeforeDestroy);
90 // Initialize connection
91 this.connexionString =
93 "/?sid=" + this.st.user.sid +
94 "&id=" + this.st.user.id +
95 "&tmpId=" + getRandString() +
97 encodeURIComponent(this.$route.path);
98 this.conn = new WebSocket(this.connexionString);
99 this.conn.onmessage = this.socketMessageListener;
100 this.socketCloseListener = setInterval(
102 if (this.conn.readyState == 3) {
103 // Connexion is closed: re-open
104 this.conn.removeEventListener("message", this.socketMessageListener);
105 this.conn = new WebSocket(this.connexionString);
106 this.conn.addEventListener("message", this.socketMessageListener);
112 mounted: function() {
113 const adjustAndSetDisplay = () => {
114 // showType is the last type viwed by the user (default)
115 let showType = localStorage.getItem("type-myGames") || "live";
116 // Live games, my turn: highest priority:
117 if (this.liveGames.some(g => !!g.myTurn)) showType = "live";
118 // Then corr games, my turn:
119 else if (this.corrGames.some(g => !!g.myTurn)) showType = "corr";
121 // If a listing is empty, try showing the other (if non-empty)
122 const types = ["corr", "live"];
123 for (let i of [0,1]) {
125 this[types[i] + "Games"].length > 0 &&
126 this[types[1-i] + "Games"].length == 0
132 this.setDisplay(showType);
134 GameStorage.getRunning(localGames => {
135 localGames.forEach(g => g.type = "live");
136 this.decorate(localGames);
137 this.liveGames = localGames;
138 if (this.st.user.id > 0) {
139 // Ask running corr games first
146 // These games are garanteed to not be deleted
147 this.corrGames = res.games;
148 this.corrGames.forEach(g => {
152 this.decorate(this.corrGames);
153 // Now ask completed games (partial list)
156 () => this.loadMore("corr", () => {
157 this.loadMore("import", adjustAndSetDisplay);
165 this.loadMore("live", () => {
166 this.loadMore("import", adjustAndSetDisplay);
171 beforeDestroy: function() {
172 this.cleanBeforeDestroy();
175 cleanBeforeDestroy: function() {
176 clearInterval(this.socketCloseListener);
177 window.removeEventListener("beforeunload", this.cleanBeforeDestroy);
178 this.conn.removeEventListener("message", this.socketMessageListener);
179 this.conn.send(JSON.stringify({code: "disconnect"}));
182 setDisplay: function(type, e) {
184 localStorage.setItem("type-myGames", type);
185 let elt = e ? e.target : document.getElementById(type + "Games");
186 elt.classList.add("active");
187 elt.classList.remove("somethingnew"); //in case of
188 for (let t of ["live","corr","import"]) {
190 document.getElementById(t + "Games").classList.remove("active");
193 addGameImport(game) {
194 game.type = "import";
195 ImportgameStorage.add(game, (err) => {
197 if (err.message.indexOf("Key already exists") < 0) {
198 alert(this.st.tr["An error occurred. Try again!"]);
201 else alert(this.st.tr["The game was already imported"]);
203 this.$router.push("/game/" + game.id);
206 tryShowNewsIndicator: function(type) {
208 (type == "live" && this.display != "live") ||
209 (type == "corr" && this.display != "corr")
212 .getElementById(type + "Games")
213 .classList.add("somethingnew");
216 // Called at loading to augment games with myColor + myTurn infos
217 decorate: function(games) {
220 (g.type == "corr" && g.players[0].id == this.st.user.id) ||
221 (g.type == "live" && g.players[0].sid == this.st.user.sid)
224 // If game is over, myTurn doesn't exist:
225 if (g.score == "*") {
226 const rem = g.movesCount % 2;
227 if ((rem == 0 && g.myColor == 'w') || (rem == 1 && g.myColor == 'b'))
232 socketMessageListener: function(msg) {
233 if (!this.conn) return;
234 const data = JSON.parse(msg.data);
235 // NOTE: no imported games here
237 "corr": this.corrGames,
238 "live": this.liveGames
242 case "notifyscore": {
243 const info = data.data;
244 const type = (!!parseInt(info.gid) ? "corr" : "live");
245 let game = gamesArrays[type].find(g => g.id == info.gid);
246 // "notifything" --> "thing":
247 const thing = data.code.substr(6);
248 game[thing] = info[thing];
249 if (thing == "turn") {
250 game.myTurn = !game.myTurn;
251 if (game.myTurn) this.tryShowNewsIndicator(type);
252 } else game.myTurn = false;
253 // TODO: forcing refresh like that is ugly and wrong.
254 // How to do it cleanly?
255 this.$refs[type + "games"].$forceUpdate();
258 case "notifynewgame": {
259 const gameInfo = data.data;
260 // st.variants might be uninitialized,
261 // if unlucky and newgame right after connect:
262 const v = this.st.variants.find(v => v.id == gameInfo.vid);
263 const vname = !!v ? v.name : "";
264 const type = (gameInfo.cadence.indexOf('d') >= 0 ? "corr": "live");
265 let game = Object.assign(
275 (type == "corr" && game.players[0].id == this.st.user.id) ||
276 (type == "live" && game.players[0].sid == this.st.user.sid);
277 gamesArrays[type].push(game);
278 if (game.myTurn) this.tryShowNewsIndicator(type);
279 // TODO: cleaner refresh
280 this.$refs[type + "games"].$forceUpdate();
285 showGame: function(game) {
286 if (game.type != "corr" || !game.myTurn) {
287 this.$router.push("/game/" + game.id);
290 // It's my turn in this game. Are there others?
292 let otherCorrGamesMyTurn = this.corrGames.filter(g =>
293 g.id != game.id && !!g.myTurn);
294 if (otherCorrGamesMyTurn.length > 0) {
295 nextIds += "/?next=[";
296 otherCorrGamesMyTurn.forEach(g => { nextIds += g.id + ","; });
297 // Remove last comma and close array:
298 nextIds = nextIds.slice(0, -1) + "]";
300 this.$router.push("/game/" + game.id + nextIds);
302 abortGame: function(game) {
303 // Special "trans-pages" case: from MyGames to Game
304 // TODO: also for corr games? (It's less important)
305 if (game.type == "live") {
307 game.players[0].sid == this.st.user.sid
308 ? game.players[1].sid
309 : game.players[0].sid;
316 // NOTE: target might not be online
323 // NOTE: no imported games here
324 else if (!game.deletedByWhite || !game.deletedByBlack) {
325 // Set score if game isn't deleted on server:
334 scoreMsg: getScoreMessage("?")
341 loadMore: function(type, cb) {
342 if (type == "corr" && this.st.user.id > 0) {
348 data: { cursor: this.cursor["corr"] },
350 const L = res.games.length;
352 this.cursor["corr"] = res.games[L - 1].created;
353 let moreGames = res.games;
354 moreGames.forEach(g => g.type = "corr");
355 this.decorate(moreGames);
356 this.corrGames = this.corrGames.concat(moreGames);
357 } else this.hasMore["corr"] = false;
363 else if (type == "live") {
364 GameStorage.getNext(this.cursor["live"], localGames => {
365 const L = localGames.length;
367 // Add "-1" because IDBKeyRange.upperBound includes boundary
368 this.cursor["live"] = localGames[L - 1].created - 1;
369 localGames.forEach(g => g.type = "live");
370 this.decorate(localGames);
371 this.liveGames = this.liveGames.concat(localGames);
372 } else this.hasMore["live"] = false;
376 else if (type == "import") {
377 ImportgameStorage.getNext(this.cursor["import"], importGames => {
378 const L = importGames.length;
380 // Add "-1" because IDBKeyRange.upperBound includes boundary
381 this.cursor["import"] = importGames[L - 1].created - 1;
382 importGames.forEach(g => g.type = "import");
383 this.importGames = this.importGames.concat(importGames);
384 } else this.hasMore["import"] = false;
398 background-color: #f9faee
408 background-color: #c5fefe !important