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 priority + myTurn infos
118 decorate: function(games) {
121 if (g.score == "*") {
124 (g.type == "corr" && g.players[0].uid == this.st.user.id) ||
125 (g.type == "live" && g.players[0].sid == this.st.user.sid)
128 const rem = g.movesCount % 2;
129 if ((rem == 0 && myColor == 'w') || (rem == 1 && myColor == 'b')) {
136 socketMessageListener: function(msg) {
137 const data = JSON.parse(msg.data);
139 "corr": this.corrGames,
140 "live": this.liveGames
144 case "notifyscore": {
145 const info = data.data;
146 const type = (!!parseInt(info.gid) ? "corr" : "live");
147 let game = gamesArrays[type].find(g => g.id == info.gid);
148 // "notifything" --> "thing":
149 const thing = data.code.substr(6);
150 game[thing] = info[thing];
151 if (thing == "score") game.priority = 0;
153 game.priority = 3 - game.priority; //toggle turn
154 game.myTurn = !game.myTurn;
157 this.tryShowNewsIndicator(type);
160 case "notifynewgame": {
161 const gameInfo = data.data;
162 // st.variants might be uninitialized,
163 // if unlucky and newgame right after connect:
164 const v = this.st.variants.find(v => v.id == gameInfo.vid);
165 const vname = !!v ? v.name : "";
166 const type = (gameInfo.cadence.indexOf('d') >= 0 ? "corr": "live");
167 let game = Object.assign(
177 game.priority = 1; //at least: my running game
179 (type == "corr" && game.players[0].uid == this.st.user.id) ||
180 (type == "live" && game.players[0].sid == this.st.user.sid)
185 gamesArrays[type].push(game);
187 this.tryShowNewsIndicator(type);
192 socketCloseListener: function() {
193 this.conn = new WebSocket(this.connexionString);
194 this.conn.addEventListener("message", this.socketMessageListener);
195 this.conn.addEventListener("close", this.socketCloseListener);
197 showGame: function(game) {
198 if (game.type == "live" || !game.myTurn) {
199 this.$router.push("/game/" + game.id);
202 // It's my turn in this game. Are there others?
204 let otherCorrGamesMyTurn = this.corrGames.filter(g =>
205 g.id != game.id && !!g.myTurn);
206 if (otherCorrGamesMyTurn.length > 0) {
207 nextIds += "/?next=[";
208 otherCorrGamesMyTurn.forEach(g => { nextIds += g.id + ","; });
209 // Remove last comma and close array:
210 nextIds = nextIds.slice(0, -1) + "]";
212 this.$router.push("/game/" + game.id + nextIds);
214 abortGame: function(game) {
215 // Special "trans-pages" case: from MyGames to Game
216 // TODO: also for corr games? (It's less important)
217 if (game.type == "live") {
219 game.players[0].sid == this.st.user.sid
220 ? game.players[1].sid
221 : game.players[0].sid;
227 // NOTE: target might not be online
233 else if (!game.deletedByWhite || !game.deletedByBlack) {
234 // Set score if game isn't deleted on server:
243 scoreMsg: getScoreMessage("?")
259 background-color: #f9faee
265 background-color: #c5fefe !important