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"] }}
11 v-show="display=='live'"
14 @abortgame="abortGame"
17 v-show="display=='corr'"
20 @abortgame="abortGame"
25 import { store } from "@/store";
26 import { GameStorage } from "@/utils/gameStorage";
27 import { ajax } from "@/utils/ajax";
28 import { getScoreMessage } from "@/utils/scoring";
29 import params from "@/parameters";
30 import { getRandString } from "@/utils/alea";
31 import GameList from "@/components/GameList.vue";
48 GameStorage.getAll(true, localGames => {
49 localGames.forEach(g => g.type = "live");
50 this.decorate(localGames);
51 this.liveGames = localGames;
53 if (this.st.user.id > 0) {
58 data: { uid: this.st.user.id },
60 let serverGames = res.games.filter(g => {
62 g.players[0].uid == this.st.user.id
65 return !g["deletedBy" + mySide];
67 serverGames.forEach(g => g.type = "corr");
68 this.decorate(serverGames);
69 this.corrGames = serverGames;
74 // Initialize connection
75 this.connexionString =
84 encodeURIComponent(this.$route.path);
85 this.conn = new WebSocket(this.connexionString);
86 this.conn.onmessage = this.socketMessageListener;
87 this.conn.onclose = this.socketCloseListener;
90 const showType = localStorage.getItem("type-myGames") || "live";
91 this.setDisplay(showType);
93 beforeDestroy: function() {
94 this.conn.send(JSON.stringify({code: "disconnect"}));
97 setDisplay: function(type, e) {
99 localStorage.setItem("type-myGames", type);
100 let elt = e ? e.target : document.getElementById(type + "Games");
101 elt.classList.add("active");
102 elt.classList.remove("somethingnew"); //in case of
103 if (elt.previousElementSibling)
104 elt.previousElementSibling.classList.remove("active");
105 else elt.nextElementSibling.classList.remove("active");
107 tryShowNewsIndicator: function(type) {
109 (type == "live" && this.display == "corr") ||
110 (type == "corr" && this.display == "live")
113 .getElementById(type + "Games")
114 .classList.add("somethingnew");
117 // Called at loading to augment games with myColor + myTurn infos
118 decorate: function(games) {
120 // If game is over, myColor and myTurn are ignored:
121 if (g.score == "*") {
123 (g.type == "corr" && g.players[0].uid == this.st.user.id) ||
124 (g.type == "live" && g.players[0].sid == this.st.user.sid)
127 const rem = g.movesCount % 2;
128 if ((rem == 0 && g.myColor == 'w') || (rem == 1 && g.myColor == 'b')) {
134 socketMessageListener: function(msg) {
135 const data = JSON.parse(msg.data);
137 "corr": this.corrGames,
138 "live": this.liveGames
142 case "notifyscore": {
143 const info = data.data;
144 const type = (!!parseInt(info.gid) ? "corr" : "live");
145 let game = gamesArrays[type].find(g => g.id == info.gid);
146 // "notifything" --> "thing":
147 const thing = data.code.substr(6);
148 game[thing] = info[thing];
149 if (thing == "turn") game.myTurn = !game.myTurn;
151 this.tryShowNewsIndicator(type);
154 case "notifynewgame": {
155 const gameInfo = data.data;
156 // st.variants might be uninitialized,
157 // if unlucky and newgame right after connect:
158 const v = this.st.variants.find(v => v.id == gameInfo.vid);
159 const vname = !!v ? v.name : "";
160 const type = (gameInfo.cadence.indexOf('d') >= 0 ? "corr": "live");
161 let game = Object.assign(
171 (type == "corr" && game.players[0].uid == this.st.user.id) ||
172 (type == "live" && game.players[0].sid == this.st.user.sid);
173 gamesArrays[type].push(game);
175 this.tryShowNewsIndicator(type);
180 socketCloseListener: function() {
181 this.conn = new WebSocket(this.connexionString);
182 this.conn.addEventListener("message", this.socketMessageListener);
183 this.conn.addEventListener("close", this.socketCloseListener);
185 showGame: function(game) {
186 if (game.type == "live" || !game.myTurn) {
187 this.$router.push("/game/" + game.id);
190 // It's my turn in this game. Are there others?
192 let otherCorrGamesMyTurn = this.corrGames.filter(g =>
193 g.id != game.id && !!g.myTurn);
194 if (otherCorrGamesMyTurn.length > 0) {
195 nextIds += "/?next=[";
196 otherCorrGamesMyTurn.forEach(g => { nextIds += g.id + ","; });
197 // Remove last comma and close array:
198 nextIds = nextIds.slice(0, -1) + "]";
200 this.$router.push("/game/" + game.id + nextIds);
202 abortGame: function(game) {
203 // Special "trans-pages" case: from MyGames to Game
204 // TODO: also for corr games? (It's less important)
205 if (game.type == "live") {
207 game.players[0].sid == this.st.user.sid
208 ? game.players[1].sid
209 : game.players[0].sid;
215 // NOTE: target might not be online
221 else if (!game.deletedByWhite || !game.deletedByBlack) {
222 // Set score if game isn't deleted on server:
231 scoreMsg: getScoreMessage("?")
247 background-color: #f9faee
253 background-color: #c5fefe !important