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.liveGames = localGames;
52 if (this.st.user.id > 0) {
57 data: { uid: this.st.user.id },
59 let serverGames = res.games.filter(g => {
61 g.players[0].uid == this.st.user.id
64 return !g["deletedBy" + mySide];
66 serverGames.forEach(g => g.type = "corr");
67 this.corrGames = serverGames;
72 // Initialize connection
73 this.connexionString =
82 encodeURIComponent(this.$route.path);
83 this.conn = new WebSocket(this.connexionString);
84 this.conn.onmessage = this.socketMessageListener;
85 this.conn.onclose = this.socketCloseListener;
88 const showType = localStorage.getItem("type-myGames") || "live";
89 this.setDisplay(showType);
91 beforeDestroy: function() {
92 this.conn.send(JSON.stringify({code: "disconnect"}));
95 setDisplay: function(type, e) {
97 localStorage.setItem("type-myGames", type);
98 let elt = e ? e.target : document.getElementById(type + "Games");
99 elt.classList.add("active");
100 elt.classList.remove("somethingnew"); //in case of
101 if (elt.previousElementSibling)
102 elt.previousElementSibling.classList.remove("active");
103 else elt.nextElementSibling.classList.remove("active");
105 tryShowNewsIndicator: function(type) {
107 (type == "live" && this.display == "corr") ||
108 (type == "corr" && this.display == "live")
111 .getElementById(type + "Games")
112 .classList.add("somethingnew");
115 socketMessageListener: function(msg) {
116 const data = JSON.parse(msg.data);
118 // NOTE: no need to increment movesCount: unused if turn is provided
120 case "notifyscore": {
121 const info = data.data;
126 let g = games.find(g => g.id == info.gid);
127 // "notifything" --> "thing":
128 const thing = data.code.substr(6);
129 this.$set(g, thing, info[thing]);
130 this.tryShowNewsIndicator(g.type);
133 case "notifynewgame": {
134 const gameInfo = data.data;
135 // st.variants might be uninitialized,
136 // if unlucky and newgame right after connect:
137 const v = this.st.variants.find(v => v.id == gameInfo.vid);
138 const vname = !!v ? v.name : "";
139 const type = gameInfo.cadence.indexOf('d') >= 0 ? "corr": "live";
140 const game = Object.assign(
149 // TODO: the new game isn't sorted. Maybe apply a different strategy:
150 // 1) Sort all at loading,
151 // 2) Insert in place when new games arrive,
152 // 3) Change position when score or turn change.
153 // And GameList just show list unsorted.
154 this[type + "Games"].unshift(game);
155 this.tryShowNewsIndicator(type);
160 socketCloseListener: function() {
161 this.conn = new WebSocket(this.connexionString);
162 this.conn.addEventListener("message", this.socketMessageListener);
163 this.conn.addEventListener("close", this.socketCloseListener);
165 showGame: function(game) {
166 // TODO: "isMyTurn" is duplicated (see GameList component). myColor also
167 const isMyTurn = (g) => {
168 if (g.score != "*") return false;
170 g.players[0].uid == this.st.user.id ||
171 g.players[0].sid == this.st.user.sid
174 if (!!g.turn) return g.turn == myColor;
175 const rem = g.movesCount % 2;
177 (rem == 0 && myColor == "w") ||
178 (rem == 1 && myColor == "b")
181 if (game.type == "live" || !isMyTurn(game)) {
182 this.$router.push("/game/" + game.id);
185 // It's my turn in this game. Are there others?
187 let otherCorrGamesMyTurn = this.corrGames.filter(
188 g => g.id != game.id && isMyTurn(g));
189 if (otherCorrGamesMyTurn.length > 0) {
190 nextIds += "/?next=[";
191 otherCorrGamesMyTurn.forEach(g => { nextIds += g.id + ","; });
192 // Remove last comma and close array:
193 nextIds = nextIds.slice(0, -1) + "]";
195 this.$router.push("/game/" + game.id + nextIds);
197 abortGame: function(game) {
198 // Special "trans-pages" case: from MyGames to Game
199 // TODO: also for corr games? (It's less important)
200 if (game.type == "live") {
202 game.players[0].sid == this.st.user.sid
203 ? game.players[1].sid
204 : game.players[0].sid;
210 // NOTE: target might not be online
216 else if (!game.deletedByWhite || !game.deletedByBlack) {
217 // Set score if game isn't deleted on server:
226 scoreMsg: getScoreMessage("?")
242 background-color: #f9faee
248 background-color: #c5fefe !important