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'"
38 v-show="hasMore[display]"
39 @click="loadMore(display)"
41 | {{ st.tr["Load more"] }}
45 import { store } from "@/store";
46 import { GameStorage } from "@/utils/gameStorage";
47 import { ImportgameStorage } from "@/utils/importgameStorage";
48 import { ajax } from "@/utils/ajax";
49 import { getScoreMessage } from "@/utils/scoring";
50 import params from "@/parameters";
51 import { getRandString } from "@/utils/alea";
52 import GameList from "@/components/GameList.vue";
53 import UploadGame from "@/components/UploadGame.vue";
67 // timestamp of last showed (oldest) game:
69 live: Number.MAX_SAFE_INTEGER,
70 "import": Number.MAX_SAFE_INTEGER,
71 corr: Number.MAX_SAFE_INTEGER
73 // hasMore == TRUE: a priori there could be more games to load
77 corr: (store.state.user.id > 0)
81 socketCloseListener: 0
85 $route: function(to, from) {
86 if (to.path != "/mygames") this.cleanBeforeDestroy();
90 window.addEventListener("beforeunload", this.cleanBeforeDestroy);
91 // Initialize connection
92 this.connexionString =
94 "/?sid=" + this.st.user.sid +
95 "&id=" + this.st.user.id +
96 "&tmpId=" + getRandString() +
98 encodeURIComponent(this.$route.path);
99 this.conn = new WebSocket(this.connexionString);
100 this.conn.onmessage = this.socketMessageListener;
101 this.socketCloseListener = setInterval(
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);
113 mounted: function() {
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";
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]) {
126 this[types[i] + "Games"].length > 0 &&
127 this[types[1-i] + "Games"].length == 0
133 this.setDisplay(showType);
135 GameStorage.getRunning(localGames => {
136 localGames.forEach(g => g.type = "live");
137 this.decorate(localGames);
138 this.liveGames = localGames;
139 if (this.st.user.id > 0) {
140 // Ask running corr games first
147 // These games are garanteed to not be deleted
148 this.corrGames = res.games;
149 this.corrGames.forEach(g => {
153 this.decorate(this.corrGames);
154 // Now ask completed games (partial list)
157 () => this.loadMore("corr", () => {
158 this.loadMore("import", adjustAndSetDisplay);
166 this.loadMore("live", () => {
167 this.loadMore("import", adjustAndSetDisplay);
172 beforeDestroy: function() {
173 this.cleanBeforeDestroy();
176 cleanBeforeDestroy: function() {
177 clearInterval(this.socketCloseListener);
178 window.removeEventListener("beforeunload", this.cleanBeforeDestroy);
179 this.conn.removeEventListener("message", this.socketMessageListener);
180 this.conn.send(JSON.stringify({code: "disconnect"}));
183 setDisplay: function(type, e) {
185 localStorage.setItem("type-myGames", type);
186 let elt = e ? e.target : document.getElementById(type + "Games");
187 elt.classList.add("active");
188 elt.classList.remove("somethingnew"); //in case of
189 for (let t of ["live","corr","import"]) {
191 document.getElementById(t + "Games").classList.remove("active");
194 addGameImport(game) {
195 game.type = "import";
196 ImportgameStorage.add(game, (err) => {
198 if (err.message.indexOf("Key already exists") < 0) {
199 alert(this.st.tr["An error occurred. Try again!"]);
202 // NOTE: since a random new ID is generated for imported games,
203 // this error will not occur.
204 else alert(this.st.tr["The game was already imported"]);
206 this.$router.push("/game/" + game.id);
209 tryShowNewsIndicator: function(type) {
211 (type == "live" && this.display != "live") ||
212 (type == "corr" && this.display != "corr")
215 .getElementById(type + "Games")
216 .classList.add("somethingnew");
219 // Called at loading to augment games with myColor + myTurn infos
220 decorate: function(games) {
223 (g.type == "corr" && g.players[0].id == this.st.user.id) ||
224 (g.type == "live" && g.players[0].sid == this.st.user.sid)
227 // If game is over, myTurn doesn't exist:
228 if (g.score == "*") {
229 const rem = g.movesCount % 2;
230 if ((rem == 0 && g.myColor == 'w') || (rem == 1 && g.myColor == 'b'))
235 socketMessageListener: function(msg) {
236 if (!this.conn) return;
237 const data = JSON.parse(msg.data);
238 // NOTE: no imported games here
240 "corr": this.corrGames,
241 "live": this.liveGames
245 case "notifyscore": {
246 const info = data.data;
247 const type = (!!parseInt(info.gid) ? "corr" : "live");
248 let game = gamesArrays[type].find(g => g.id == info.gid);
249 // "notifything" --> "thing":
250 const thing = data.code.substr(6);
251 game[thing] = info[thing];
252 if (thing == "turn") {
253 game.myTurn = !game.myTurn;
254 if (game.myTurn) this.tryShowNewsIndicator(type);
255 } else game.myTurn = false;
256 // TODO: forcing refresh like that is ugly and wrong.
257 // How to do it cleanly?
258 this.$refs[type + "games"].$forceUpdate();
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 : "";
267 const type = (gameInfo.cadence.indexOf('d') >= 0 ? "corr": "live");
268 let game = Object.assign(
278 (type == "corr" && game.players[0].id == this.st.user.id) ||
279 (type == "live" && game.players[0].sid == this.st.user.sid);
280 gamesArrays[type].push(game);
281 if (game.myTurn) this.tryShowNewsIndicator(type);
282 // TODO: cleaner refresh
283 this.$refs[type + "games"].$forceUpdate();
288 showGame: function(game) {
289 if (game.type != "corr" || !game.myTurn) {
290 this.$router.push("/game/" + game.id);
293 // It's my turn in this game. Are there others?
295 let otherCorrGamesMyTurn = this.corrGames.filter(g =>
296 g.id != game.id && !!g.myTurn);
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) + "]";
303 this.$router.push("/game/" + game.id + nextIds);
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") {
310 game.players[0].sid == this.st.user.sid
311 ? game.players[1].sid
312 : game.players[0].sid;
319 // NOTE: target might not be online
326 // NOTE: no imported games here
327 else if (!game.deletedByWhite || !game.deletedByBlack) {
328 // Set score if game isn't deleted on server:
337 scoreMsg: getScoreMessage("?")
344 loadMore: function(type, cb) {
345 if (type == "corr" && this.st.user.id > 0) {
351 data: { cursor: this.cursor["corr"] },
353 const L = res.games.length;
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;
366 else if (type == "live") {
367 GameStorage.getNext(this.cursor["live"], localGames => {
368 const L = localGames.length;
370 // Add "-1" because IDBKeyRange.upperBound includes boundary
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;
379 else if (type == "import") {
380 ImportgameStorage.getNext(this.cursor["import"], importGames => {
381 const L = importGames.length;
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;
396 <style lang="sass" scoped>
401 background-color: #f9faee
408 background-color: #c5fefe !important
411 <!-- Not scoped because acting on GameList -->