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) {
56 { uid: this.st.user.id },
58 let serverGames = res.games.filter(g => {
60 g.players[0].uid == this.st.user.id
63 return !g["deletedBy" + mySide];
65 serverGames.forEach(g => g.type = "corr");
66 this.corrGames = serverGames;
69 // Initialize connection
70 this.connexionString =
77 encodeURIComponent(this.$route.path);
78 this.conn = new WebSocket(this.connexionString);
79 this.conn.onmessage = this.socketMessageListener;
80 this.conn.onclose = this.socketCloseListener;
83 const showType = localStorage.getItem("type-myGames") || "live";
84 this.setDisplay(showType);
86 beforeDestroy: function() {
87 this.conn.send(JSON.stringify({code: "disconnect"}));
90 setDisplay: function(type, e) {
92 localStorage.setItem("type-myGames", type);
93 let elt = e ? e.target : document.getElementById(type + "Games");
94 elt.classList.add("active");
95 elt.classList.remove("somethingnew"); //in case of
96 if (elt.previousElementSibling)
97 elt.previousElementSibling.classList.remove("active");
98 else elt.nextElementSibling.classList.remove("active");
100 // TODO: classifyObject is redundant (see Hall.vue)
101 classifyObject: function(o) {
102 return o.cadence.indexOf("d") === -1 ? "live" : "corr";
104 showGame: function(game) {
105 // TODO: "isMyTurn" is duplicated (see GameList component). myColor also
106 const isMyTurn = (g) => {
107 if (g.score != "*") return false;
109 g.players[0].uid == this.st.user.id ||
110 g.players[0].sid == this.st.user.sid
113 const rem = g.movesCount % 2;
115 (rem == 0 && myColor == "w") ||
116 (rem == 1 && myColor == "b")
119 if (game.type == "live" || !isMyTurn(game)) {
120 this.$router.push("/game/" + game.id);
123 // It's my turn in this game. Are there others?
125 let otherCorrGamesMyTurn = this.corrGames.filter(
126 g => g.id != game.id && isMyTurn(g));
127 if (otherCorrGamesMyTurn.length > 0) {
128 nextIds += "/?next=[";
129 otherCorrGamesMyTurn.forEach(g => { nextIds += g.id + ","; });
130 // Remove last comma and close array:
131 nextIds = nextIds.slice(0, -1) + "]";
133 this.$router.push("/game/" + game.id + nextIds);
135 abortGame: function(game) {
136 // Special "trans-pages" case: from MyGames to Game
137 // TODO: also for corr games? (It's less important)
138 if (game.type == "live") {
140 game.players[0].sid == this.st.user.sid
141 ? game.players[1].sid
142 : game.players[0].sid;
148 // NOTE: target might not be online
154 else if (!game.deletedByWhite || !game.deletedByBlack) {
155 // Set score if game isn't deleted on server:
163 scoreMsg: getScoreMessage("?")
169 socketMessageListener: function(msg) {
170 const data = JSON.parse(msg.data);
171 if (data.code == "changeturn") {
172 let games = !!parseInt(data.gid)
175 // NOTE: new move itself is not received, because it wouldn't be used.
176 let g = games.find(g => g.id == data.gid);
177 this.$set(g, "movesCount", g.movesCount + 1);
179 (g.type == "live" && this.display == "corr") ||
180 (g.type == "corr" && this.display == "live")
183 .getElementById(g.type + "Games")
184 .classList.add("somethingnew");
188 socketCloseListener: function() {
189 this.conn = new WebSocket(this.connexionString);
190 this.conn.addEventListener("message", this.socketMessageListener);
191 this.conn.addEventListener("close", this.socketCloseListener);
202 background-color: #f9faee
208 background-color: #c5fefe !important