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)
81 socketCloseListener: 0
85 $route: function(to, from) {
86 if (to.path != "/mygames") this.cleanBeforeDestroy();
88 // st.variants changes only once, at loading from [] to [...]
89 "st.variants": function() {
90 // Set potential games variant names + display:
91 this.liveGames.concat(this.corrGames).concat(this.importGames)
93 if (!o.vname) this.setVname(o);
98 window.addEventListener("beforeunload", this.cleanBeforeDestroy);
99 // Initialize connection
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.conn = new WebSocket(this.connexionString);
108 this.conn.onmessage = this.socketMessageListener;
109 this.socketCloseListener = setInterval(
111 if (this.conn.readyState == 3) {
112 // Connexion is closed: re-open
113 this.conn.removeEventListener("message", this.socketMessageListener);
114 this.conn = new WebSocket(this.connexionString);
115 this.conn.addEventListener("message", this.socketMessageListener);
121 mounted: function() {
122 const adjustAndSetDisplay = () => {
123 // showType is the last type viewed by the user (default)
124 let showType = localStorage.getItem("type-myGames") || "live";
125 // Live games, my turn: highest priority:
126 if (this.liveGames.some(g => !!g.myTurn)) showType = "live";
127 // Then corr games, my turn:
128 else if (this.corrGames.some(g => !!g.myTurn)) showType = "corr";
130 // If a listing is empty, try showing the other (if non-empty)
131 const types = ["corr", "live"];
132 for (let i of [0,1]) {
134 this[types[i] + "Games"].length > 0 &&
135 this[types[1-i] + "Games"].length == 0
141 this.setDisplay(showType);
143 GameStorage.getRunning(localGames => {
144 localGames.forEach(g => g.type = "live");
145 this.decorate(localGames);
146 this.liveGames = localGames;
147 if (this.st.user.id > 0) {
148 // Ask running corr games first
155 // These games are garanteed to not be deleted
156 this.corrGames = res.games;
157 this.corrGames.forEach(g => {
160 g.options = JSON.parse(g.options);
162 this.decorate(this.corrGames);
163 // Now ask completed games (partial list)
166 () => this.loadMore("corr", () => {
167 this.loadMore("import", adjustAndSetDisplay);
175 this.loadMore("live", () => {
176 this.loadMore("import", adjustAndSetDisplay);
181 beforeDestroy: function() {
182 this.cleanBeforeDestroy();
185 cleanBeforeDestroy: function() {
186 clearInterval(this.socketCloseListener);
187 window.removeEventListener("beforeunload", this.cleanBeforeDestroy);
188 this.conn.removeEventListener("message", this.socketMessageListener);
189 this.conn.send(JSON.stringify({code: "disconnect"}));
192 setDisplay: function(type, e) {
194 localStorage.setItem("type-myGames", type);
195 let elt = (!!e ? e.target : document.getElementById(type + "Games"));
196 elt.classList.add("active");
197 elt.classList.remove("somethingnew"); //in case of
198 for (let t of ["live","corr","import"]) {
200 document.getElementById(t + "Games").classList.remove("active");
203 // TODO: duplicated from Hall.vue:
204 setVname: function(obj) {
205 const variant = this.st.variants.find(v => v.id == obj.vid);
207 obj.vname = variant.name;
208 obj.vdisp = variant.display;
211 addGameImport(game) {
212 game.type = "import";
213 ImportgameStorage.add(game, (err) => {
215 if (err.message.indexOf("Key already exists") < 0) {
216 alert(this.st.tr["An error occurred. Try again!"]);
219 // NOTE: since a random new ID is generated for imported games,
220 // this error will not occur.
221 else alert(this.st.tr["The game was already imported"]);
223 this.$router.push("/game/" + game.id);
226 tryShowNewsIndicator: function(type) {
228 (type == "live" && this.display != "live") ||
229 (type == "corr" && this.display != "corr")
232 .getElementById(type + "Games")
233 .classList.add("somethingnew");
236 // Called at loading to augment games with myColor + myTurn infos
237 decorate: function(games) {
240 (g.type == "corr" && g.players[0].id == this.st.user.id) ||
241 (g.type == "live" && g.players[0].sid == this.st.user.sid)
244 // If game is over, myTurn doesn't exist:
245 if (g.score == "*") {
246 const rem = g.movesCount % 2;
247 if ((rem == 0 && g.myColor == 'w') || (rem == 1 && g.myColor == 'b'))
253 socketMessageListener: function(msg) {
254 if (!this.conn) return;
255 const data = JSON.parse(msg.data);
256 // NOTE: no imported games here
258 "corr": this.corrGames,
259 "live": this.liveGames
263 case "notifyscore": {
264 const info = data.data;
265 const type = (!!parseInt(info.gid, 10) ? "corr" : "live");
266 let game = gamesArrays[type].find(g => g.id == info.gid);
267 // "notifything" --> "thing":
268 const thing = data.code.substr(6);
269 game[thing] = info[thing];
270 if (thing == "turn") {
271 game.myTurn = !game.myTurn;
272 if (game.myTurn) this.tryShowNewsIndicator(type);
274 else game.myTurn = false;
275 // TODO: forcing refresh like that is ugly and wrong.
276 // How to do it cleanly?
277 this.$refs[type + "games"].$forceUpdate();
280 case "notifynewgame": {
281 let gameInfo = data.data;
282 this.setVname(gameInfo);
283 // TODO: remove patch on next line (options || "{}")
284 gameInfo.options = JSON.parse(gameInfo.options || "{}");
285 const type = (gameInfo.cadence.indexOf('d') >= 0 ? "corr": "live");
286 let game = Object.assign(
295 (type == "corr" && game.players[0].id == this.st.user.id) ||
296 (type == "live" && game.players[0].sid == this.st.user.sid);
297 gamesArrays[type].push(game);
298 if (game.myTurn) this.tryShowNewsIndicator(type);
299 // TODO: cleaner refresh
300 this.$refs[type + "games"].$forceUpdate();
305 showGame: function(game) {
306 if (game.type != "corr" || !game.myTurn) {
307 this.$router.push("/game/" + game.id);
310 // It's my turn in this game. Are there others?
312 let otherCorrGamesMyTurn = this.corrGames.filter(g =>
313 g.id != game.id && !!g.myTurn);
314 if (otherCorrGamesMyTurn.length > 0) {
315 nextIds += "/?next=[";
316 otherCorrGamesMyTurn.forEach(g => { nextIds += g.id + ","; });
317 // Remove last comma and close array:
318 nextIds = nextIds.slice(0, -1) + "]";
320 this.$router.push("/game/" + game.id + nextIds);
322 abortGame: function(game) {
323 // Special "trans-pages" case: from MyGames to Game
324 // TODO: also for corr games? (It's less important)
325 if (game.type == "live") {
327 game.players[0].sid == this.st.user.sid
328 ? game.players[1].sid
329 : game.players[0].sid;
336 // NOTE: target might not be online
343 // NOTE: no imported games here
344 else if (!game.deletedByWhite || !game.deletedByBlack) {
345 // Set score if game isn't deleted on server:
354 scoreMsg: getScoreMessage("?")
361 loadMore: function(type, cb) {
362 if (type == "corr" && this.st.user.id > 0) {
368 data: { cursor: this.cursor["corr"] },
370 const L = res.games.length;
372 this.cursor["corr"] = res.games[L - 1].created;
373 let moreGames = res.games;
374 moreGames.forEach(g => {
376 g.options = JSON.parse(g.options);
378 this.decorate(moreGames);
379 this.corrGames = this.corrGames.concat(moreGames);
381 else this.hasMore["corr"] = false;
387 else if (type == "live") {
388 GameStorage.getNext(this.cursor["live"], localGames => {
389 const L = localGames.length;
391 // Add "-1" because IDBKeyRange.upperBound includes boundary
392 this.cursor["live"] = localGames[L - 1].created - 1;
393 localGames.forEach(g => {
395 // TODO: remove patch on next line (options || "{}")
396 g.options = JSON.parse(g.options || "{}");
398 this.decorate(localGames);
399 this.liveGames = this.liveGames.concat(localGames);
401 else this.hasMore["live"] = false;
405 else if (type == "import") {
406 ImportgameStorage.getNext(this.cursor["import"], importGames => {
407 const L = importGames.length;
409 // Add "-1" because IDBKeyRange.upperBound includes boundary
410 this.cursor["import"] = importGames[L - 1].created - 1;
411 importGames.forEach(g => {
413 // TODO: remove following patch (options || "{}")
414 g.options = JSON.parse(g.options || "{}");
417 this.importGames = this.importGames.concat(importGames);
419 else this.hasMore["import"] = false;
428 <style lang="sass" scoped>
433 background-color: #f9faee
440 background-color: #90C4EC !important
443 <!-- Not scoped because acting on GameList -->