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: true },
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)
135 () => this.loadMore("corr", adjustAndSetDisplay)
140 beforeDestroy: function() {
141 this.cleanBeforeDestroy();
144 cleanBeforeDestroy: function() {
145 window.removeEventListener("beforeunload", this.cleanBeforeDestroy);
146 this.conn.send(JSON.stringify({code: "disconnect"}));
148 setDisplay: function(type, e) {
150 localStorage.setItem("type-myGames", type);
151 let elt = e ? e.target : document.getElementById(type + "Games");
152 elt.classList.add("active");
153 elt.classList.remove("somethingnew"); //in case of
154 if (elt.previousElementSibling)
155 elt.previousElementSibling.classList.remove("active");
156 else elt.nextElementSibling.classList.remove("active");
158 tryShowNewsIndicator: function(type) {
160 (type == "live" && this.display == "corr") ||
161 (type == "corr" && this.display == "live")
164 .getElementById(type + "Games")
165 .classList.add("somethingnew");
168 // Called at loading to augment games with myColor + myTurn infos
169 decorate: function(games) {
172 (g.type == "corr" && g.players[0].id == this.st.user.id) ||
173 (g.type == "live" && g.players[0].sid == this.st.user.sid)
176 // If game is over, myTurn doesn't exist:
177 if (g.score == "*") {
178 const rem = g.movesCount % 2;
179 if ((rem == 0 && g.myColor == 'w') || (rem == 1 && g.myColor == 'b'))
184 socketMessageListener: function(msg) {
185 const data = JSON.parse(msg.data);
187 "corr": this.corrGames,
188 "live": this.liveGames
192 case "notifyscore": {
193 const info = data.data;
194 const type = (!!parseInt(info.gid) ? "corr" : "live");
195 let game = gamesArrays[type].find(g => g.id == info.gid);
196 // "notifything" --> "thing":
197 const thing = data.code.substr(6);
198 game[thing] = info[thing];
199 if (thing == "turn") {
200 game.myTurn = !game.myTurn;
201 if (game.myTurn) this.tryShowNewsIndicator(type);
202 } else game.myTurn = false;
203 // TODO: forcing refresh like that is ugly and wrong.
204 // How to do it cleanly?
205 this.$refs[type + "games"].$forceUpdate();
208 case "notifynewgame": {
209 const gameInfo = data.data;
210 // st.variants might be uninitialized,
211 // if unlucky and newgame right after connect:
212 const v = this.st.variants.find(v => v.id == gameInfo.vid);
213 const vname = !!v ? v.name : "";
214 const type = (gameInfo.cadence.indexOf('d') >= 0 ? "corr": "live");
215 let game = Object.assign(
225 (type == "corr" && game.players[0].id == this.st.user.id) ||
226 (type == "live" && game.players[0].sid == this.st.user.sid);
227 gamesArrays[type].push(game);
228 if (game.myTurn) this.tryShowNewsIndicator(type);
229 // TODO: cleaner refresh
230 this.$refs[type + "games"].$forceUpdate();
235 socketCloseListener: function() {
236 this.conn = new WebSocket(this.connexionString);
237 this.conn.addEventListener("message", this.socketMessageListener);
238 this.conn.addEventListener("close", this.socketCloseListener);
240 showGame: function(game) {
241 if (game.type == "live" || !game.myTurn) {
242 this.$router.push("/game/" + game.id);
245 // It's my turn in this game. Are there others?
247 let otherCorrGamesMyTurn = this.corrGames.filter(g =>
248 g.id != game.id && !!g.myTurn);
249 if (otherCorrGamesMyTurn.length > 0) {
250 nextIds += "/?next=[";
251 otherCorrGamesMyTurn.forEach(g => { nextIds += g.id + ","; });
252 // Remove last comma and close array:
253 nextIds = nextIds.slice(0, -1) + "]";
255 this.$router.push("/game/" + game.id + nextIds);
257 abortGame: function(game) {
258 // Special "trans-pages" case: from MyGames to Game
259 // TODO: also for corr games? (It's less important)
260 if (game.type == "live") {
262 game.players[0].sid == this.st.user.sid
263 ? game.players[1].sid
264 : game.players[0].sid;
270 // NOTE: target might not be online
276 else if (!game.deletedByWhite || !game.deletedByBlack) {
277 // Set score if game isn't deleted on server:
286 scoreMsg: getScoreMessage("?")
293 loadMore: function(type, cb) {
294 if (type == "corr") {
300 data: { cursor: this.cursor["corr"] },
302 const L = res.games.length;
304 this.cursor["corr"] = res.games[L - 1].created;
305 let moreGames = res.games;
306 moreGames.forEach(g => g.type = "corr");
307 this.decorate(moreGames);
308 this.corrGames = this.corrGames.concat(moreGames);
309 } else this.hasMore["corr"] = false;
314 } else if (type == "live") {
315 GameStorage.getNext(this.cursor["live"], localGames => {
316 const L = localGames.length;
318 // Add "-1" because IDBKeyRange.upperBound seems to include boundary
319 this.cursor["live"] = localGames[L - 1].created - 1;
320 localGames.forEach(g => g.type = "live");
321 this.decorate(localGames);
322 this.liveGames = this.liveGames.concat(localGames);
323 } else this.hasMore["live"] = false;
337 background-color: #f9faee
347 background-color: #c5fefe !important