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"
19 v-show="display=='corr'"
22 @abortgame="abortGame"
27 import { store } from "@/store";
28 import { GameStorage } from "@/utils/gameStorage";
29 import { ajax } from "@/utils/ajax";
30 import { getScoreMessage } from "@/utils/scoring";
31 import params from "@/parameters";
32 import { getRandString } from "@/utils/alea";
33 import GameList from "@/components/GameList.vue";
50 // Initialize connection
51 this.connexionString =
60 encodeURIComponent(this.$route.path);
61 this.conn = new WebSocket(this.connexionString);
62 this.conn.onmessage = this.socketMessageListener;
63 this.conn.onclose = this.socketCloseListener;
66 const adjustAndSetDisplay = () => {
67 // showType is the last type viwed by the user (default)
68 let showType = localStorage.getItem("type-myGames") || "live";
69 // Live games, my turn: highest priority:
70 if (this.liveGames.some(g => !!g.myTurn)) showType = "live";
71 // Then corr games, my turn:
72 else if (this.corrGames.some(g => !!g.myTurn)) showType = "corr";
74 // If a listing is empty, try showing the other (if non-empty)
75 const types = ["corr", "live"];
76 for (let i of [0,1]) {
78 this[types[i] + "Games"].length > 0 &&
79 this[types[1-i] + "Games"].length == 0
85 this.setDisplay(showType);
87 GameStorage.getAll(localGames => {
88 localGames.forEach(g => g.type = "live");
89 this.decorate(localGames);
90 this.liveGames = localGames;
91 if (this.st.user.id > 0) {
96 data: { uid: this.st.user.id },
98 let serverGames = res.games.filter(g => {
100 g.players[0].uid == this.st.user.id
103 return !g["deletedBy" + mySide];
105 serverGames.forEach(g => g.type = "corr");
106 this.decorate(serverGames);
107 this.corrGames = serverGames;
108 adjustAndSetDisplay();
112 } else adjustAndSetDisplay();
115 beforeDestroy: function() {
116 this.conn.send(JSON.stringify({code: "disconnect"}));
119 setDisplay: function(type, e) {
121 localStorage.setItem("type-myGames", type);
122 let elt = e ? e.target : document.getElementById(type + "Games");
123 elt.classList.add("active");
124 elt.classList.remove("somethingnew"); //in case of
125 if (elt.previousElementSibling)
126 elt.previousElementSibling.classList.remove("active");
127 else elt.nextElementSibling.classList.remove("active");
129 tryShowNewsIndicator: function(type) {
131 (type == "live" && this.display == "corr") ||
132 (type == "corr" && this.display == "live")
135 .getElementById(type + "Games")
136 .classList.add("somethingnew");
139 // Called at loading to augment games with myColor + myTurn infos
140 decorate: function(games) {
143 (g.type == "corr" && g.players[0].uid == this.st.user.id) ||
144 (g.type == "live" && g.players[0].sid == this.st.user.sid)
147 // If game is over, myTurn doesn't exist:
148 if (g.score == "*") {
149 const rem = g.movesCount % 2;
150 if ((rem == 0 && g.myColor == 'w') || (rem == 1 && g.myColor == 'b'))
155 socketMessageListener: function(msg) {
156 const data = JSON.parse(msg.data);
158 "corr": this.corrGames,
159 "live": this.liveGames
163 case "notifyscore": {
164 const info = data.data;
165 const type = (!!parseInt(info.gid) ? "corr" : "live");
166 let game = gamesArrays[type].find(g => g.id == info.gid);
167 // "notifything" --> "thing":
168 const thing = data.code.substr(6);
169 game[thing] = info[thing];
170 if (thing == "turn") {
171 game.myTurn = !game.myTurn;
172 if (game.myTurn) this.tryShowNewsIndicator(type);
174 // TODO: forcing refresh like that is ugly and wrong.
175 // How to do it cleanly?
176 this.$refs[type + "games"].$forceUpdate();
179 case "notifynewgame": {
180 const gameInfo = data.data;
181 // st.variants might be uninitialized,
182 // if unlucky and newgame right after connect:
183 const v = this.st.variants.find(v => v.id == gameInfo.vid);
184 const vname = !!v ? v.name : "";
185 const type = (gameInfo.cadence.indexOf('d') >= 0 ? "corr": "live");
186 let game = Object.assign(
196 (type == "corr" && game.players[0].uid == this.st.user.id) ||
197 (type == "live" && game.players[0].sid == this.st.user.sid);
198 gamesArrays[type].push(game);
199 if (game.myTurn) this.tryShowNewsIndicator(type);
200 // TODO: cleaner refresh
201 this.$refs[type + "games"].$forceUpdate();
206 socketCloseListener: function() {
207 this.conn = new WebSocket(this.connexionString);
208 this.conn.addEventListener("message", this.socketMessageListener);
209 this.conn.addEventListener("close", this.socketCloseListener);
211 showGame: function(game) {
212 if (game.type == "live" || !game.myTurn) {
213 this.$router.push("/game/" + game.id);
216 // It's my turn in this game. Are there others?
218 let otherCorrGamesMyTurn = this.corrGames.filter(g =>
219 g.id != game.id && !!g.myTurn);
220 if (otherCorrGamesMyTurn.length > 0) {
221 nextIds += "/?next=[";
222 otherCorrGamesMyTurn.forEach(g => { nextIds += g.id + ","; });
223 // Remove last comma and close array:
224 nextIds = nextIds.slice(0, -1) + "]";
226 this.$router.push("/game/" + game.id + nextIds);
228 abortGame: function(game) {
229 // Special "trans-pages" case: from MyGames to Game
230 // TODO: also for corr games? (It's less important)
231 if (game.type == "live") {
233 game.players[0].sid == this.st.user.sid
234 ? game.players[1].sid
235 : game.players[0].sid;
241 // NOTE: target might not be online
247 else if (!game.deletedByWhite || !game.deletedByBlack) {
248 // Set score if game isn't deleted on server:
257 scoreMsg: getScoreMessage("?")
273 background-color: #f9faee
279 background-color: #c5fefe !important