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"] }}
12 v-show="display=='live'"
15 @abortgame="abortGame"
18 v-show="display=='corr'"
22 @abortgame="abortGame"
25 v-show="hasMore[display]"
26 @click="loadMore(display)"
28 | {{ st.tr["Load more"] }}
32 import { store } from "@/store";
33 import { GameStorage } from "@/utils/gameStorage";
34 import { ajax } from "@/utils/ajax";
35 import { getScoreMessage } from "@/utils/scoring";
36 import params from "@/parameters";
37 import { getRandString } from "@/utils/alea";
38 import GameList from "@/components/GameList.vue";
50 // timestamp of last showed (oldest) game:
52 live: Number.MAX_SAFE_INTEGER,
53 corr: Number.MAX_SAFE_INTEGER
55 // hasMore == TRUE: a priori there could be more games to load
56 hasMore: { live: true, corr: store.state.user.id > 0 },
62 $route: function(to, from) {
63 if (to.path != "/mygames") this.cleanBeforeDestroy();
67 window.addEventListener("beforeunload", this.cleanBeforeDestroy);
68 // Initialize connection
69 this.connexionString =
78 encodeURIComponent(this.$route.path);
79 this.conn = new WebSocket(this.connexionString);
80 this.conn.onmessage = this.socketMessageListener;
81 this.conn.onclose = this.socketCloseListener;
84 const adjustAndSetDisplay = () => {
85 // showType is the last type viwed by the user (default)
86 let showType = localStorage.getItem("type-myGames") || "live";
87 // Live games, my turn: highest priority:
88 if (this.liveGames.some(g => !!g.myTurn)) showType = "live";
89 // Then corr games, my turn:
90 else if (this.corrGames.some(g => !!g.myTurn)) showType = "corr";
92 // If a listing is empty, try showing the other (if non-empty)
93 const types = ["corr", "live"];
94 for (let i of [0,1]) {
96 this[types[i] + "Games"].length > 0 &&
97 this[types[1-i] + "Games"].length == 0
103 this.setDisplay(showType);
105 GameStorage.getRunning(localGames => {
106 localGames.forEach(g => g.type = "live");
107 this.decorate(localGames);
108 this.liveGames = localGames;
109 if (this.st.user.id > 0) {
110 // Ask running corr games first
117 // These games are garanteed to not be deleted
118 this.corrGames = res.games;
119 this.corrGames.forEach(g => {
123 this.decorate(this.corrGames);
124 // Now ask completed games (partial list)
127 () => this.loadMore("corr", adjustAndSetDisplay)
132 } else this.loadMore("live", adjustAndSetDisplay);
135 beforeDestroy: function() {
136 this.cleanBeforeDestroy();
139 cleanBeforeDestroy: function() {
140 window.removeEventListener("beforeunload", this.cleanBeforeDestroy);
141 this.conn.removeEventListener("message", this.socketMessageListener);
142 this.conn.removeEventListener("close", this.socketCloseListener);
143 this.conn.send(JSON.stringify({code: "disconnect"}));
146 setDisplay: function(type, e) {
148 localStorage.setItem("type-myGames", type);
149 let elt = e ? e.target : document.getElementById(type + "Games");
150 elt.classList.add("active");
151 elt.classList.remove("somethingnew"); //in case of
152 if (elt.previousElementSibling)
153 elt.previousElementSibling.classList.remove("active");
154 else elt.nextElementSibling.classList.remove("active");
156 tryShowNewsIndicator: function(type) {
158 (type == "live" && this.display == "corr") ||
159 (type == "corr" && this.display == "live")
162 .getElementById(type + "Games")
163 .classList.add("somethingnew");
166 // Called at loading to augment games with myColor + myTurn infos
167 decorate: function(games) {
170 (g.type == "corr" && g.players[0].id == this.st.user.id) ||
171 (g.type == "live" && g.players[0].sid == this.st.user.sid)
174 // If game is over, myTurn doesn't exist:
175 if (g.score == "*") {
176 const rem = g.movesCount % 2;
177 if ((rem == 0 && g.myColor == 'w') || (rem == 1 && g.myColor == 'b'))
182 socketMessageListener: function(msg) {
183 if (!this.conn) return;
184 const data = JSON.parse(msg.data);
186 "corr": this.corrGames,
187 "live": this.liveGames
191 case "notifyscore": {
192 const info = data.data;
193 const type = (!!parseInt(info.gid) ? "corr" : "live");
194 let game = gamesArrays[type].find(g => g.id == info.gid);
195 // "notifything" --> "thing":
196 const thing = data.code.substr(6);
197 game[thing] = info[thing];
198 if (thing == "turn") {
199 game.myTurn = !game.myTurn;
200 if (game.myTurn) this.tryShowNewsIndicator(type);
201 } else game.myTurn = false;
202 // TODO: forcing refresh like that is ugly and wrong.
203 // How to do it cleanly?
204 this.$refs[type + "games"].$forceUpdate();
207 case "notifynewgame": {
208 const gameInfo = data.data;
209 // st.variants might be uninitialized,
210 // if unlucky and newgame right after connect:
211 const v = this.st.variants.find(v => v.id == gameInfo.vid);
212 const vname = !!v ? v.name : "";
213 const type = (gameInfo.cadence.indexOf('d') >= 0 ? "corr": "live");
214 let game = Object.assign(
224 (type == "corr" && game.players[0].id == this.st.user.id) ||
225 (type == "live" && game.players[0].sid == this.st.user.sid);
226 gamesArrays[type].push(game);
227 if (game.myTurn) this.tryShowNewsIndicator(type);
228 // TODO: cleaner refresh
229 this.$refs[type + "games"].$forceUpdate();
234 socketCloseListener: function() {
235 this.conn = new WebSocket(this.connexionString);
236 this.conn.addEventListener("message", this.socketMessageListener);
237 this.conn.addEventListener("close", this.socketCloseListener);
239 showGame: function(game) {
240 if (game.type == "live" || !game.myTurn) {
241 this.$router.push("/game/" + game.id);
244 // It's my turn in this game. Are there others?
246 let otherCorrGamesMyTurn = this.corrGames.filter(g =>
247 g.id != game.id && !!g.myTurn);
248 if (otherCorrGamesMyTurn.length > 0) {
249 nextIds += "/?next=[";
250 otherCorrGamesMyTurn.forEach(g => { nextIds += g.id + ","; });
251 // Remove last comma and close array:
252 nextIds = nextIds.slice(0, -1) + "]";
254 this.$router.push("/game/" + game.id + nextIds);
256 abortGame: function(game) {
257 // Special "trans-pages" case: from MyGames to Game
258 // TODO: also for corr games? (It's less important)
259 if (game.type == "live") {
261 game.players[0].sid == this.st.user.sid
262 ? game.players[1].sid
263 : game.players[0].sid;
270 // NOTE: target might not be online
277 else if (!game.deletedByWhite || !game.deletedByBlack) {
278 // Set score if game isn't deleted on server:
287 scoreMsg: getScoreMessage("?")
294 loadMore: function(type, cb) {
295 if (type == "corr" && this.st.user.id > 0) {
301 data: { cursor: this.cursor["corr"] },
303 const L = res.games.length;
305 this.cursor["corr"] = res.games[L - 1].created;
306 let moreGames = res.games;
307 moreGames.forEach(g => g.type = "corr");
308 this.decorate(moreGames);
309 this.corrGames = this.corrGames.concat(moreGames);
310 } else this.hasMore["corr"] = false;
315 } else if (type == "live") {
316 GameStorage.getNext(this.cursor["live"], localGames => {
317 const L = localGames.length;
319 // Add "-1" because IDBKeyRange.upperBound seems to include boundary
320 this.cursor["live"] = localGames[L - 1].created - 1;
321 localGames.forEach(g => g.type = "live");
322 this.decorate(localGames);
323 this.liveGames = this.liveGames.concat(localGames);
324 } else this.hasMore["live"] = false;
338 background-color: #f9faee
348 background-color: #c5fefe !important