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["Correspondence 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)
86 $route: function(to, from) {
87 if (to.path != "/mygames") this.cleanBeforeDestroy();
89 // st.variants changes only once, at loading from [] to [...]
90 "st.variants": function() {
91 // Set potential games variant names + display:
92 this.liveGames.concat(this.corrGames).concat(this.importGames)
94 if (!o.vname) this.setVname(o);
99 window.addEventListener("beforeunload", this.cleanBeforeDestroy);
100 this.connexionString =
102 "/?sid=" + this.st.user.sid +
103 "&id=" + this.st.user.id +
104 "&tmpId=" + getRandString() +
106 encodeURIComponent(this.$route.path);
107 this.openConnection();
109 mounted: function() {
110 const adjustAndSetDisplay = () => {
111 // showType is the last type viewed by the user (default)
112 let showType = localStorage.getItem("type-myGames") || "live";
113 // Live games, my turn: highest priority:
114 if (this.liveGames.some(g => !!g.myTurn)) showType = "live";
115 // Then corr games, my turn:
116 else if (this.corrGames.some(g => !!g.myTurn)) showType = "corr";
118 // If a listing is empty, try showing the other (if non-empty)
119 const types = ["corr", "live"];
120 for (let i of [0,1]) {
122 this[types[i] + "Games"].length > 0 &&
123 this[types[1-i] + "Games"].length == 0
129 this.setDisplay(showType);
131 GameStorage.getRunning(localGames => {
132 localGames.forEach(g => g.type = "live");
133 this.decorate(localGames);
134 this.liveGames = localGames;
135 if (this.st.user.id > 0) {
136 // Ask running corr games first
143 // These games are garanteed to not be deleted
144 this.corrGames = res.games;
145 this.corrGames.forEach(g => {
148 g.options = JSON.parse(g.options);
150 this.decorate(this.corrGames);
151 // Now ask completed games (partial list)
154 () => this.loadMore("corr", () => {
155 this.loadMore("import", adjustAndSetDisplay);
163 this.loadMore("live", () => {
164 this.loadMore("import", adjustAndSetDisplay);
169 beforeDestroy: function() {
170 this.cleanBeforeDestroy();
173 openConnection: function() {
174 // Initialize connection
175 this.conn = new WebSocket(this.connexionString);
176 this.conn.onopen = () => { this.reconnectTimeout = 250; };
177 this.conn.onmessage = this.socketMessageListener;
178 const closeConnection = () => {
179 this.reopenTimeout = setTimeout(
181 this.openConnection();
182 this.reconnectTimeout = Math.min(2*this.reconnectTimeout, 30000);
184 this.reconnectTimeout
187 this.conn.onerror = closeConnection;
188 this.conn.onclose = closeConnection;
190 cleanBeforeDestroy: function() {
191 window.removeEventListener("beforeunload", this.cleanBeforeDestroy);
192 clearTimeout(this.reopenTimeout);
193 this.conn.send(JSON.stringify({code: "disconnect"}));
196 setDisplay: function(type, e) {
198 localStorage.setItem("type-myGames", type);
199 let elt = (!!e ? e.target : document.getElementById(type + "Games"));
200 elt.classList.add("active");
201 elt.classList.remove("somethingnew"); //in case of
202 for (let t of ["live","corr","import"]) {
204 document.getElementById(t + "Games").classList.remove("active");
207 // TODO: duplicated from Hall.vue:
208 setVname: function(obj) {
209 const variant = this.st.variants.find(v => v.id == obj.vid);
211 obj.vname = variant.name;
212 obj.vdisp = variant.display;
215 addGameImport(game) {
216 game.type = "import";
217 ImportgameStorage.add(game, (err) => {
219 if (err.message.indexOf("Key already exists") < 0) {
220 alert(this.st.tr["An error occurred. Try again!"]);
223 // NOTE: since a random new ID is generated for imported games,
224 // this error will not occur.
225 else alert(this.st.tr["The game was already imported"]);
227 this.$router.push("/game/" + game.id);
230 tryShowNewsIndicator: function(type) {
232 (type == "live" && this.display != "live") ||
233 (type == "corr" && this.display != "corr")
236 .getElementById(type + "Games")
237 .classList.add("somethingnew");
240 // Called at loading to augment games with myColor + myTurn infos
241 decorate: function(games) {
244 (g.type == "corr" && g.players[0].id == this.st.user.id) ||
245 (g.type == "live" && g.players[0].sid == this.st.user.sid)
248 // If game is over, myTurn doesn't exist:
249 if (g.score == "*") {
250 const rem = g.movesCount % 2;
251 if ((rem == 0 && g.myColor == 'w') || (rem == 1 && g.myColor == 'b'))
257 socketMessageListener: function(msg) {
258 if (!this.conn) return;
259 const data = JSON.parse(msg.data);
260 // NOTE: no imported games here
262 "corr": this.corrGames,
263 "live": this.liveGames
267 case "notifyscore": {
268 const info = data.data;
269 const type = (!!parseInt(info.gid, 10) ? "corr" : "live");
270 let game = gamesArrays[type].find(g => g.id == info.gid);
271 // "notifything" --> "thing":
272 const thing = data.code.substr(6);
273 game[thing] = info[thing];
274 if (thing == "turn") {
275 game.myTurn = !game.myTurn;
276 if (game.myTurn) this.tryShowNewsIndicator(type);
278 else game.myTurn = false;
279 // TODO: forcing refresh like that is ugly and wrong.
280 // How to do it cleanly?
281 this.$refs[type + "games"].$forceUpdate();
284 case "notifynewgame": {
285 let gameInfo = data.data;
286 this.setVname(gameInfo);
287 // TODO: remove patch on next line (options || "{}")
288 gameInfo.options = JSON.parse(gameInfo.options || "{}");
289 const type = (gameInfo.cadence.indexOf('d') >= 0 ? "corr": "live");
290 let game = Object.assign(
299 (type == "corr" && game.players[0].id == this.st.user.id) ||
300 (type == "live" && game.players[0].sid == this.st.user.sid);
301 gamesArrays[type].push(game);
302 if (game.myTurn) this.tryShowNewsIndicator(type);
303 // TODO: cleaner refresh
304 this.$refs[type + "games"].$forceUpdate();
309 showGame: function(game) {
310 if (game.type != "corr" || !game.myTurn) {
311 this.$router.push("/game/" + game.id);
314 // It's my turn in this game. Are there others?
316 let otherCorrGamesMyTurn = this.corrGames.filter(g =>
317 g.id != game.id && !!g.myTurn);
318 if (otherCorrGamesMyTurn.length > 0) {
319 nextIds += "/?next=[";
320 otherCorrGamesMyTurn.forEach(g => { nextIds += g.id + ","; });
321 // Remove last comma and close array:
322 nextIds = nextIds.slice(0, -1) + "]";
324 this.$router.push("/game/" + game.id + nextIds);
326 abortGame: function(game) {
327 // Special "trans-pages" case: from MyGames to Game
328 // TODO: also for corr games? (It's less important)
329 if (game.type == "live") {
331 game.players[0].sid == this.st.user.sid
332 ? game.players[1].sid
333 : game.players[0].sid;
340 // NOTE: target might not be online
347 // NOTE: no imported games here
348 else if (!game.deletedByWhite || !game.deletedByBlack) {
349 // Set score if game isn't deleted on server:
358 scoreMsg: getScoreMessage("?")
365 loadMore: function(type, cb) {
366 if (type == "corr" && this.st.user.id > 0) {
372 data: { cursor: this.cursor["corr"] },
374 const L = res.games.length;
376 this.cursor["corr"] = res.games[L - 1].created;
377 let moreGames = res.games;
378 moreGames.forEach(g => {
380 g.options = JSON.parse(g.options);
382 this.decorate(moreGames);
383 this.corrGames = this.corrGames.concat(moreGames);
385 else this.hasMore["corr"] = false;
391 else if (type == "live") {
392 GameStorage.getNext(this.cursor["live"], localGames => {
393 const L = localGames.length;
395 // Add "-1" because IDBKeyRange.upperBound includes boundary
396 this.cursor["live"] = localGames[L - 1].created - 1;
397 localGames.forEach(g => {
399 // TODO: remove patch on next line (options || "{}")
400 g.options = JSON.parse(g.options || "{}");
402 this.decorate(localGames);
403 this.liveGames = this.liveGames.concat(localGames);
405 else this.hasMore["live"] = false;
409 else if (type == "import") {
410 ImportgameStorage.getNext(this.cursor["import"], importGames => {
411 const L = importGames.length;
413 // Add "-1" because IDBKeyRange.upperBound includes boundary
414 this.cursor["import"] = importGames[L - 1].created - 1;
415 importGames.forEach(g => {
417 // TODO: remove following patch (options || "{}")
418 g.options = JSON.parse(g.options || "{}");
421 this.importGames = this.importGames.concat(importGames);
423 else this.hasMore["import"] = false;
432 <style lang="sass" scoped>
437 background-color: #f9faee
444 background-color: #90C4EC !important
447 <!-- Not scoped because acting on GameList -->