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 =
71 "/?sid=" + this.st.user.sid +
72 "&id=" + this.st.user.id +
73 "&tmpId=" + getRandString() +
75 encodeURIComponent(this.$route.path);
76 this.conn = new WebSocket(this.connexionString);
77 this.conn.onmessage = this.socketMessageListener;
78 this.conn.onclose = this.socketCloseListener;
81 const adjustAndSetDisplay = () => {
82 // showType is the last type viwed by the user (default)
83 let showType = localStorage.getItem("type-myGames") || "live";
84 // Live games, my turn: highest priority:
85 if (this.liveGames.some(g => !!g.myTurn)) showType = "live";
86 // Then corr games, my turn:
87 else if (this.corrGames.some(g => !!g.myTurn)) showType = "corr";
89 // If a listing is empty, try showing the other (if non-empty)
90 const types = ["corr", "live"];
91 for (let i of [0,1]) {
93 this[types[i] + "Games"].length > 0 &&
94 this[types[1-i] + "Games"].length == 0
100 this.setDisplay(showType);
102 GameStorage.getRunning(localGames => {
103 localGames.forEach(g => g.type = "live");
104 this.decorate(localGames);
105 this.liveGames = localGames;
106 if (this.st.user.id > 0) {
107 // Ask running corr games first
114 // These games are garanteed to not be deleted
115 this.corrGames = res.games;
116 this.corrGames.forEach(g => {
120 this.decorate(this.corrGames);
121 // Now ask completed games (partial list)
124 () => this.loadMore("corr", adjustAndSetDisplay)
129 } else this.loadMore("live", adjustAndSetDisplay);
132 beforeDestroy: function() {
133 this.cleanBeforeDestroy();
136 cleanBeforeDestroy: function() {
137 window.removeEventListener("beforeunload", this.cleanBeforeDestroy);
138 this.conn.removeEventListener("message", this.socketMessageListener);
139 this.conn.removeEventListener("close", this.socketCloseListener);
140 this.conn.send(JSON.stringify({code: "disconnect"}));
143 setDisplay: function(type, e) {
145 localStorage.setItem("type-myGames", type);
146 let elt = e ? e.target : document.getElementById(type + "Games");
147 elt.classList.add("active");
148 elt.classList.remove("somethingnew"); //in case of
149 if (elt.previousElementSibling)
150 elt.previousElementSibling.classList.remove("active");
151 else elt.nextElementSibling.classList.remove("active");
153 tryShowNewsIndicator: function(type) {
155 (type == "live" && this.display == "corr") ||
156 (type == "corr" && this.display == "live")
159 .getElementById(type + "Games")
160 .classList.add("somethingnew");
163 // Called at loading to augment games with myColor + myTurn infos
164 decorate: function(games) {
167 (g.type == "corr" && g.players[0].id == this.st.user.id) ||
168 (g.type == "live" && g.players[0].sid == this.st.user.sid)
171 // If game is over, myTurn doesn't exist:
172 if (g.score == "*") {
173 const rem = g.movesCount % 2;
174 if ((rem == 0 && g.myColor == 'w') || (rem == 1 && g.myColor == 'b'))
179 socketMessageListener: function(msg) {
180 if (!this.conn) return;
181 const data = JSON.parse(msg.data);
183 "corr": this.corrGames,
184 "live": this.liveGames
188 case "notifyscore": {
189 const info = data.data;
190 const type = (!!parseInt(info.gid) ? "corr" : "live");
191 let game = gamesArrays[type].find(g => g.id == info.gid);
192 // "notifything" --> "thing":
193 const thing = data.code.substr(6);
194 game[thing] = info[thing];
195 if (thing == "turn") {
196 game.myTurn = !game.myTurn;
197 if (game.myTurn) this.tryShowNewsIndicator(type);
198 } else game.myTurn = false;
199 // TODO: forcing refresh like that is ugly and wrong.
200 // How to do it cleanly?
201 this.$refs[type + "games"].$forceUpdate();
204 case "notifynewgame": {
205 const gameInfo = data.data;
206 // st.variants might be uninitialized,
207 // if unlucky and newgame right after connect:
208 const v = this.st.variants.find(v => v.id == gameInfo.vid);
209 const vname = !!v ? v.name : "";
210 const type = (gameInfo.cadence.indexOf('d') >= 0 ? "corr": "live");
211 let game = Object.assign(
221 (type == "corr" && game.players[0].id == this.st.user.id) ||
222 (type == "live" && game.players[0].sid == this.st.user.sid);
223 gamesArrays[type].push(game);
224 if (game.myTurn) this.tryShowNewsIndicator(type);
225 // TODO: cleaner refresh
226 this.$refs[type + "games"].$forceUpdate();
231 socketCloseListener: function() {
232 this.conn = new WebSocket(this.connexionString);
233 this.conn.addEventListener("message", this.socketMessageListener);
234 this.conn.addEventListener("close", this.socketCloseListener);
236 showGame: function(game) {
237 if (game.type == "live" || !game.myTurn) {
238 this.$router.push("/game/" + game.id);
241 // It's my turn in this game. Are there others?
243 let otherCorrGamesMyTurn = this.corrGames.filter(g =>
244 g.id != game.id && !!g.myTurn);
245 if (otherCorrGamesMyTurn.length > 0) {
246 nextIds += "/?next=[";
247 otherCorrGamesMyTurn.forEach(g => { nextIds += g.id + ","; });
248 // Remove last comma and close array:
249 nextIds = nextIds.slice(0, -1) + "]";
251 this.$router.push("/game/" + game.id + nextIds);
253 abortGame: function(game) {
254 // Special "trans-pages" case: from MyGames to Game
255 // TODO: also for corr games? (It's less important)
256 if (game.type == "live") {
258 game.players[0].sid == this.st.user.sid
259 ? game.players[1].sid
260 : game.players[0].sid;
267 // NOTE: target might not be online
274 else if (!game.deletedByWhite || !game.deletedByBlack) {
275 // Set score if game isn't deleted on server:
284 scoreMsg: getScoreMessage("?")
291 loadMore: function(type, cb) {
292 if (type == "corr" && this.st.user.id > 0) {
298 data: { cursor: this.cursor["corr"] },
300 const L = res.games.length;
302 this.cursor["corr"] = res.games[L - 1].created;
303 let moreGames = res.games;
304 moreGames.forEach(g => g.type = "corr");
305 this.decorate(moreGames);
306 this.corrGames = this.corrGames.concat(moreGames);
307 } else this.hasMore["corr"] = false;
312 } else if (type == "live") {
313 GameStorage.getNext(this.cursor["live"], localGames => {
314 const L = localGames.length;
316 // Add "-1" because IDBKeyRange.upperBound seems to include boundary
317 this.cursor["live"] = localGames[L - 1].created - 1;
318 localGames.forEach(g => g.type = "live");
319 this.decorate(localGames);
320 this.liveGames = this.liveGames.concat(localGames);
321 } else this.hasMore["live"] = false;
335 background-color: #f9faee
345 background-color: #c5fefe !important