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.removeEventListener("message", this.socketMessageListener);
147 this.conn.removeEventListener("close", this.socketCloseListener);
148 this.conn.send(JSON.stringify({code: "disconnect"}));
151 setDisplay: function(type, e) {
153 localStorage.setItem("type-myGames", type);
154 let elt = e ? e.target : document.getElementById(type + "Games");
155 elt.classList.add("active");
156 elt.classList.remove("somethingnew"); //in case of
157 if (elt.previousElementSibling)
158 elt.previousElementSibling.classList.remove("active");
159 else elt.nextElementSibling.classList.remove("active");
161 tryShowNewsIndicator: function(type) {
163 (type == "live" && this.display == "corr") ||
164 (type == "corr" && this.display == "live")
167 .getElementById(type + "Games")
168 .classList.add("somethingnew");
171 // Called at loading to augment games with myColor + myTurn infos
172 decorate: function(games) {
175 (g.type == "corr" && g.players[0].id == this.st.user.id) ||
176 (g.type == "live" && g.players[0].sid == this.st.user.sid)
179 // If game is over, myTurn doesn't exist:
180 if (g.score == "*") {
181 const rem = g.movesCount % 2;
182 if ((rem == 0 && g.myColor == 'w') || (rem == 1 && g.myColor == 'b'))
187 socketMessageListener: function(msg) {
188 if (!this.conn) return;
189 const data = JSON.parse(msg.data);
191 "corr": this.corrGames,
192 "live": this.liveGames
196 case "notifyscore": {
197 const info = data.data;
198 const type = (!!parseInt(info.gid) ? "corr" : "live");
199 let game = gamesArrays[type].find(g => g.id == info.gid);
200 // "notifything" --> "thing":
201 const thing = data.code.substr(6);
202 game[thing] = info[thing];
203 if (thing == "turn") {
204 game.myTurn = !game.myTurn;
205 if (game.myTurn) this.tryShowNewsIndicator(type);
206 } else game.myTurn = false;
207 // TODO: forcing refresh like that is ugly and wrong.
208 // How to do it cleanly?
209 this.$refs[type + "games"].$forceUpdate();
212 case "notifynewgame": {
213 const gameInfo = data.data;
214 // st.variants might be uninitialized,
215 // if unlucky and newgame right after connect:
216 const v = this.st.variants.find(v => v.id == gameInfo.vid);
217 const vname = !!v ? v.name : "";
218 const type = (gameInfo.cadence.indexOf('d') >= 0 ? "corr": "live");
219 let game = Object.assign(
229 (type == "corr" && game.players[0].id == this.st.user.id) ||
230 (type == "live" && game.players[0].sid == this.st.user.sid);
231 gamesArrays[type].push(game);
232 if (game.myTurn) this.tryShowNewsIndicator(type);
233 // TODO: cleaner refresh
234 this.$refs[type + "games"].$forceUpdate();
239 socketCloseListener: function() {
240 this.conn = new WebSocket(this.connexionString);
241 this.conn.addEventListener("message", this.socketMessageListener);
242 this.conn.addEventListener("close", this.socketCloseListener);
244 showGame: function(game) {
245 if (game.type == "live" || !game.myTurn) {
246 this.$router.push("/game/" + game.id);
249 // It's my turn in this game. Are there others?
251 let otherCorrGamesMyTurn = this.corrGames.filter(g =>
252 g.id != game.id && !!g.myTurn);
253 if (otherCorrGamesMyTurn.length > 0) {
254 nextIds += "/?next=[";
255 otherCorrGamesMyTurn.forEach(g => { nextIds += g.id + ","; });
256 // Remove last comma and close array:
257 nextIds = nextIds.slice(0, -1) + "]";
259 this.$router.push("/game/" + game.id + nextIds);
261 abortGame: function(game) {
262 // Special "trans-pages" case: from MyGames to Game
263 // TODO: also for corr games? (It's less important)
264 if (game.type == "live") {
266 game.players[0].sid == this.st.user.sid
267 ? game.players[1].sid
268 : game.players[0].sid;
275 // NOTE: target might not be online
282 else if (!game.deletedByWhite || !game.deletedByBlack) {
283 // Set score if game isn't deleted on server:
292 scoreMsg: getScoreMessage("?")
299 loadMore: function(type, cb) {
300 if (type == "corr") {
306 data: { cursor: this.cursor["corr"] },
308 const L = res.games.length;
310 this.cursor["corr"] = res.games[L - 1].created;
311 let moreGames = res.games;
312 moreGames.forEach(g => g.type = "corr");
313 this.decorate(moreGames);
314 this.corrGames = this.corrGames.concat(moreGames);
315 } else this.hasMore["corr"] = false;
320 } else if (type == "live") {
321 GameStorage.getNext(this.cursor["live"], localGames => {
322 const L = localGames.length;
324 // Add "-1" because IDBKeyRange.upperBound seems to include boundary
325 this.cursor["live"] = localGames[L - 1].created - 1;
326 localGames.forEach(g => g.type = "live");
327 this.decorate(localGames);
328 this.liveGames = this.liveGames.concat(localGames);
329 } else this.hasMore["live"] = false;
343 background-color: #f9faee
353 background-color: #c5fefe !important