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"
18 v-show="display=='corr'"
22 @abortgame="abortGame"
25 v-show="hasMore[display]"
26 @click="loadMore(display)"
28 | {{ st.tr["Load more"] }}
32 import { store } from "@/store";
33 import { GameStorage } from "@/utils/gameStorage";
34 import { ajax } from "@/utils/ajax";
35 import { getScoreMessage } from "@/utils/scoring";
36 import params from "@/parameters";
37 import { getRandString } from "@/utils/alea";
38 import GameList from "@/components/GameList.vue";
50 // timestamp of last showed (oldest) game:
52 live: Number.MAX_SAFE_INTEGER,
53 corr: Number.MAX_SAFE_INTEGER
55 // hasMore == TRUE: a priori there could be more games to load
56 hasMore: { live: true, corr: true },
62 // Initialize connection
63 this.connexionString =
72 encodeURIComponent(this.$route.path);
73 this.conn = new WebSocket(this.connexionString);
74 this.conn.onmessage = this.socketMessageListener;
75 this.conn.onclose = this.socketCloseListener;
78 const adjustAndSetDisplay = () => {
79 // showType is the last type viwed by the user (default)
80 let showType = localStorage.getItem("type-myGames") || "live";
81 // Live games, my turn: highest priority:
82 if (this.liveGames.some(g => !!g.myTurn)) showType = "live";
83 // Then corr games, my turn:
84 else if (this.corrGames.some(g => !!g.myTurn)) showType = "corr";
86 // If a listing is empty, try showing the other (if non-empty)
87 const types = ["corr", "live"];
88 for (let i of [0,1]) {
90 this[types[i] + "Games"].length > 0 &&
91 this[types[1-i] + "Games"].length == 0
97 this.setDisplay(showType);
99 GameStorage.getRunning(localGames => {
100 localGames.forEach(g => g.type = "live");
101 this.decorate(localGames);
102 this.liveGames = localGames;
103 if (this.st.user.id > 0) {
104 // Ask running corr games first
111 // These games are garanteed to not be deleted
112 this.corrGames = res.games;
113 this.corrGames.forEach(g => {
117 this.decorate(this.corrGames);
118 // Now ask completed games (partial list)
121 () => this.loadMore("corr", adjustAndSetDisplay)
129 () => this.loadMore("corr", adjustAndSetDisplay)
134 beforeDestroy: function() {
135 this.conn.send(JSON.stringify({code: "disconnect"}));
138 setDisplay: function(type, e) {
140 localStorage.setItem("type-myGames", type);
141 let elt = e ? e.target : document.getElementById(type + "Games");
142 elt.classList.add("active");
143 elt.classList.remove("somethingnew"); //in case of
144 if (elt.previousElementSibling)
145 elt.previousElementSibling.classList.remove("active");
146 else elt.nextElementSibling.classList.remove("active");
148 tryShowNewsIndicator: function(type) {
150 (type == "live" && this.display == "corr") ||
151 (type == "corr" && this.display == "live")
154 .getElementById(type + "Games")
155 .classList.add("somethingnew");
158 // Called at loading to augment games with myColor + myTurn infos
159 decorate: function(games) {
162 (g.type == "corr" && g.players[0].id == this.st.user.id) ||
163 (g.type == "live" && g.players[0].sid == this.st.user.sid)
166 // If game is over, myTurn doesn't exist:
167 if (g.score == "*") {
168 const rem = g.movesCount % 2;
169 if ((rem == 0 && g.myColor == 'w') || (rem == 1 && g.myColor == 'b'))
174 socketMessageListener: function(msg) {
175 const data = JSON.parse(msg.data);
177 "corr": this.corrGames,
178 "live": this.liveGames
182 case "notifyscore": {
183 const info = data.data;
184 const type = (!!parseInt(info.gid) ? "corr" : "live");
185 let game = gamesArrays[type].find(g => g.id == info.gid);
186 // "notifything" --> "thing":
187 const thing = data.code.substr(6);
188 game[thing] = info[thing];
189 if (thing == "turn") {
190 game.myTurn = !game.myTurn;
191 if (game.myTurn) this.tryShowNewsIndicator(type);
192 } else game.myTurn = false;
193 // TODO: forcing refresh like that is ugly and wrong.
194 // How to do it cleanly?
195 this.$refs[type + "games"].$forceUpdate();
198 case "notifynewgame": {
199 const gameInfo = data.data;
200 // st.variants might be uninitialized,
201 // if unlucky and newgame right after connect:
202 const v = this.st.variants.find(v => v.id == gameInfo.vid);
203 const vname = !!v ? v.name : "";
204 const type = (gameInfo.cadence.indexOf('d') >= 0 ? "corr": "live");
205 let game = Object.assign(
215 (type == "corr" && game.players[0].id == this.st.user.id) ||
216 (type == "live" && game.players[0].sid == this.st.user.sid);
217 gamesArrays[type].push(game);
218 if (game.myTurn) this.tryShowNewsIndicator(type);
219 // TODO: cleaner refresh
220 this.$refs[type + "games"].$forceUpdate();
225 socketCloseListener: function() {
226 this.conn = new WebSocket(this.connexionString);
227 this.conn.addEventListener("message", this.socketMessageListener);
228 this.conn.addEventListener("close", this.socketCloseListener);
230 showGame: function(game) {
231 if (game.type == "live" || !game.myTurn) {
232 this.$router.push("/game/" + game.id);
235 // It's my turn in this game. Are there others?
237 let otherCorrGamesMyTurn = this.corrGames.filter(g =>
238 g.id != game.id && !!g.myTurn);
239 if (otherCorrGamesMyTurn.length > 0) {
240 nextIds += "/?next=[";
241 otherCorrGamesMyTurn.forEach(g => { nextIds += g.id + ","; });
242 // Remove last comma and close array:
243 nextIds = nextIds.slice(0, -1) + "]";
245 this.$router.push("/game/" + game.id + nextIds);
247 abortGame: function(game) {
248 // Special "trans-pages" case: from MyGames to Game
249 // TODO: also for corr games? (It's less important)
250 if (game.type == "live") {
252 game.players[0].sid == this.st.user.sid
253 ? game.players[1].sid
254 : game.players[0].sid;
260 // NOTE: target might not be online
266 else if (!game.deletedByWhite || !game.deletedByBlack) {
267 // Set score if game isn't deleted on server:
276 scoreMsg: getScoreMessage("?")
283 loadMore: function(type, cb) {
284 if (type == "corr") {
290 data: { cursor: this.cursor["corr"] },
292 const L = res.games.length;
294 this.cursor["corr"] = res.games[L - 1].created;
295 let moreGames = res.games;
296 moreGames.forEach(g => g.type = "corr");
297 this.decorate(moreGames);
298 this.corrGames = this.corrGames.concat(moreGames);
299 } else this.hasMore["corr"] = false;
304 } else if (type == "live") {
305 GameStorage.getNext(this.cursor["live"], localGames => {
306 const L = localGames.length;
308 // Add "-1" because IDBKeyRange.upperBound seems to include boundary
309 this.cursor["live"] = localGames[L - 1].created - 1;
310 localGames.forEach(g => g.type = "live");
311 this.decorate(localGames);
312 this.liveGames = this.liveGames.concat(localGames);
313 } else this.hasMore["live"] = false;
327 background-color: #f9faee
337 background-color: #c5fefe !important