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 =
80 encodeURIComponent(this.$route.path);
81 this.conn = new WebSocket(this.connexionString);
82 this.conn.onmessage = this.socketMessageListener;
83 this.conn.onclose = this.socketCloseListener;
86 const showType = localStorage.getItem("type-myGames") || "live";
87 this.setDisplay(showType);
89 beforeDestroy: function() {
90 this.conn.send(JSON.stringify({code: "disconnect"}));
93 setDisplay: function(type, e) {
95 localStorage.setItem("type-myGames", type);
96 let elt = e ? e.target : document.getElementById(type + "Games");
97 elt.classList.add("active");
98 elt.classList.remove("somethingnew"); //in case of
99 if (elt.previousElementSibling)
100 elt.previousElementSibling.classList.remove("active");
101 else elt.nextElementSibling.classList.remove("active");
103 // TODO: classifyObject is redundant (see Hall.vue)
104 classifyObject: function(o) {
105 return o.cadence.indexOf("d") === -1 ? "live" : "corr";
107 showGame: function(game) {
108 // TODO: "isMyTurn" is duplicated (see GameList component). myColor also
109 const isMyTurn = (g) => {
110 if (g.score != "*") return false;
112 g.players[0].uid == this.st.user.id ||
113 g.players[0].sid == this.st.user.sid
116 const rem = g.movesCount % 2;
118 (rem == 0 && myColor == "w") ||
119 (rem == 1 && myColor == "b")
122 if (game.type == "live" || !isMyTurn(game)) {
123 this.$router.push("/game/" + game.id);
126 // It's my turn in this game. Are there others?
128 let otherCorrGamesMyTurn = this.corrGames.filter(
129 g => g.id != game.id && isMyTurn(g));
130 if (otherCorrGamesMyTurn.length > 0) {
131 nextIds += "/?next=[";
132 otherCorrGamesMyTurn.forEach(g => { nextIds += g.id + ","; });
133 // Remove last comma and close array:
134 nextIds = nextIds.slice(0, -1) + "]";
136 this.$router.push("/game/" + game.id + nextIds);
138 abortGame: function(game) {
139 // Special "trans-pages" case: from MyGames to Game
140 // TODO: also for corr games? (It's less important)
141 if (game.type == "live") {
143 game.players[0].sid == this.st.user.sid
144 ? game.players[1].sid
145 : game.players[0].sid;
151 // NOTE: target might not be online
157 else if (!game.deletedByWhite || !game.deletedByBlack) {
158 // Set score if game isn't deleted on server:
167 scoreMsg: getScoreMessage("?")
174 socketMessageListener: function(msg) {
175 const data = JSON.parse(msg.data);
176 if (data.code == "changeturn") {
177 let games = !!parseInt(data.gid)
180 // NOTE: new move itself is not received, because it wouldn't be used.
181 let g = games.find(g => g.id == data.gid);
182 this.$set(g, "movesCount", g.movesCount + 1);
184 (g.type == "live" && this.display == "corr") ||
185 (g.type == "corr" && this.display == "live")
188 .getElementById(g.type + "Games")
189 .classList.add("somethingnew");
193 socketCloseListener: function() {
194 this.conn = new WebSocket(this.connexionString);
195 this.conn.addEventListener("message", this.socketMessageListener);
196 this.conn.addEventListener("close", this.socketCloseListener);
207 background-color: #f9faee
213 background-color: #c5fefe !important