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'"
16 v-show="display=='corr'"
24 import { store } from "@/store";
25 import { GameStorage } from "@/utils/gameStorage";
26 import { ajax } from "@/utils/ajax";
27 import { getScoreMessage } from "@/utils/scoring";
28 import params from "@/parameters";
29 import { getRandString } from "@/utils/alea";
30 import GameList from "@/components/GameList.vue";
47 GameStorage.getAll(true, localGames => {
48 localGames.forEach(g => g.type = "live");
49 this.liveGames = localGames;
51 if (this.st.user.id > 0) {
55 { uid: this.st.user.id },
57 let serverGames = res.games.filter(g => {
59 g.players[0].uid == this.st.user.id
62 return !g["deletedBy" + mySide];
64 serverGames.forEach(g => g.type = "corr");
65 this.corrGames = serverGames;
68 // Initialize connection
69 this.connexionString =
76 encodeURIComponent(this.$route.path);
77 this.conn = new WebSocket(this.connexionString);
78 this.conn.onmessage = this.socketMessageListener;
79 this.conn.onclose = this.socketCloseListener;
82 const showType = localStorage.getItem("type-myGames") || "live";
83 this.setDisplay(showType);
85 beforeDestroy: function() {
86 this.conn.send(JSON.stringify({code: "disconnect"}));
89 setDisplay: function(type, e) {
91 localStorage.setItem("type-myGames", type);
92 let elt = e ? e.target : document.getElementById(type + "Games");
93 elt.classList.add("active");
94 elt.classList.remove("somethingnew"); //in case of
95 if (elt.previousElementSibling)
96 elt.previousElementSibling.classList.remove("active");
97 else elt.nextElementSibling.classList.remove("active");
99 // TODO: classifyObject is redundant (see Hall.vue)
100 classifyObject: function(o) {
101 return o.cadence.indexOf("d") === -1 ? "live" : "corr";
103 showGame: function(game) {
104 // TODO: "isMyTurn" is duplicated (see GameList component). myColor also
105 const isMyTurn = (g) => {
106 if (g.score != "*") return false;
108 g.players[0].uid == this.st.user.id ||
109 g.players[0].sid == this.st.user.sid
112 const rem = g.movesCount % 2;
114 (rem == 0 && myColor == "w") ||
115 (rem == 1 && myColor == "b")
118 if (game.type == "live" || !isMyTurn(game)) {
119 this.$router.push("/game/" + game.id);
122 // It's my turn in this game. Are there others?
124 let otherCorrGamesMyTurn = this.corrGames.filter(
125 g => g.id != game.id && isMyTurn(g));
126 if (otherCorrGamesMyTurn.length > 0) {
127 nextIds += "/?next=[";
128 otherCorrGamesMyTurn.forEach(g => { nextIds += g.id + ","; });
129 // Remove last comma and close array:
130 nextIds = nextIds.slice(0, -1) + "]";
132 this.$router.push("/game/" + game.id + nextIds);
134 abortGame: function(game) {
135 // Special "trans-pages" case: from MyGames to Game
136 // TODO: also for corr games? (It's less important)
137 if (game.type == "live") {
139 game.players[0].sid == this.st.user.sid
140 ? game.players[1].sid
141 : game.players[0].sid;
147 // NOTE: target might not be online
153 else if (!game.deletedByWhite || !game.deletedByBlack) {
154 // Set score if game isn't deleted on server:
162 scoreMsg: getScoreMessage("?")
168 socketMessageListener: function(msg) {
169 const data = JSON.parse(msg.data);
170 if (data.code == "changeturn") {
171 let games = !!parseInt(data.gid)
174 // NOTE: new move itself is not received, because it wouldn't be used.
175 let g = games.find(g => g.id == data.gid);
176 this.$set(g, "movesCount", g.movesCount + 1);
178 (g.type == "live" && this.display == "corr") ||
179 (g.type == "corr" && this.display == "live")
182 .getElementById(g.type + "Games")
183 .classList.add("somethingnew");
187 socketCloseListener: function() {
188 this.conn = new WebSocket(this.connexionString);
189 this.conn.addEventListener("message", this.socketMessageListener);
190 this.conn.addEventListener("close", this.socketCloseListener);
201 background-color: #f9faee
207 background-color: #c5fefe !important