Commit | Line | Data |
---|---|---|
afd3240d BA |
1 | <template lang="pug"> |
2 | main | |
3 | .row | |
4 | .col-sm-12.col-md-10.col-md-offset-1.col-lg-8.col-lg-offset-2 | |
5 | .button-group | |
910d631b BA |
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"] }} | |
10 | GameList( | |
11 | v-show="display=='live'" | |
12 | :games="liveGames" | |
13 | @show-game="showGame" | |
3b0f26c1 | 14 | @abortgame="abortGame" |
910d631b BA |
15 | ) |
16 | GameList( | |
17 | v-show="display=='corr'" | |
18 | :games="corrGames" | |
19 | @show-game="showGame" | |
3b0f26c1 | 20 | @abortgame="abortGame" |
910d631b | 21 | ) |
afd3240d BA |
22 | </template> |
23 | ||
24 | <script> | |
afd3240d BA |
25 | import { store } from "@/store"; |
26 | import { GameStorage } from "@/utils/gameStorage"; | |
27 | import { ajax } from "@/utils/ajax"; | |
aae89b49 | 28 | import { getScoreMessage } from "@/utils/scoring"; |
23ecf008 BA |
29 | import params from "@/parameters"; |
30 | import { getRandString } from "@/utils/alea"; | |
afd3240d BA |
31 | import GameList from "@/components/GameList.vue"; |
32 | export default { | |
89021f18 | 33 | name: "my-my-games", |
afd3240d | 34 | components: { |
6808d7a1 | 35 | GameList |
afd3240d BA |
36 | }, |
37 | data: function() { | |
38 | return { | |
39 | st: store.state, | |
dac39588 | 40 | display: "live", |
2f258c37 | 41 | liveGames: [], |
db1f1f9a BA |
42 | corrGames: [], |
43 | conn: null, | |
44 | connexionString: "" | |
afd3240d BA |
45 | }; |
46 | }, | |
47 | created: function() { | |
db1f1f9a | 48 | GameStorage.getAll(true, localGames => { |
aae89b49 | 49 | localGames.forEach(g => g.type = "live"); |
2f258c37 | 50 | this.liveGames = localGames; |
afd3240d | 51 | }); |
6808d7a1 | 52 | if (this.st.user.id > 0) { |
aae89b49 BA |
53 | ajax( |
54 | "/games", | |
55 | "GET", | |
e57c4de4 BA |
56 | { |
57 | data: { uid: this.st.user.id }, | |
58 | success: (res) => { | |
59 | let serverGames = res.games.filter(g => { | |
60 | const mySide = | |
61 | g.players[0].uid == this.st.user.id | |
62 | ? "White" | |
63 | : "Black"; | |
64 | return !g["deletedBy" + mySide]; | |
65 | }); | |
66 | serverGames.forEach(g => g.type = "corr"); | |
67 | this.corrGames = serverGames; | |
68 | } | |
69 | } | |
70 | ); | |
afd3240d | 71 | } |
db1f1f9a BA |
72 | // Initialize connection |
73 | this.connexionString = | |
74 | params.socketUrl + | |
75 | "/?sid=" + | |
76 | this.st.user.sid + | |
77 | "&tmpId=" + | |
78 | getRandString() + | |
79 | "&page=" + | |
80 | encodeURIComponent(this.$route.path); | |
81 | this.conn = new WebSocket(this.connexionString); | |
82 | this.conn.onmessage = this.socketMessageListener; | |
83 | this.conn.onclose = this.socketCloseListener; | |
afd3240d | 84 | }, |
2f258c37 BA |
85 | mounted: function() { |
86 | const showType = localStorage.getItem("type-myGames") || "live"; | |
87 | this.setDisplay(showType); | |
88 | }, | |
23ecf008 BA |
89 | beforeDestroy: function() { |
90 | this.conn.send(JSON.stringify({code: "disconnect"})); | |
91 | }, | |
afd3240d | 92 | methods: { |
2f258c37 BA |
93 | setDisplay: function(type, e) { |
94 | this.display = type; | |
95 | localStorage.setItem("type-myGames", type); | |
6808d7a1 | 96 | let elt = e ? e.target : document.getElementById(type + "Games"); |
2f258c37 | 97 | elt.classList.add("active"); |
23ecf008 | 98 | elt.classList.remove("somethingnew"); //in case of |
6808d7a1 | 99 | if (elt.previousElementSibling) |
2f258c37 | 100 | elt.previousElementSibling.classList.remove("active"); |
6808d7a1 | 101 | else elt.nextElementSibling.classList.remove("active"); |
2f258c37 BA |
102 | }, |
103 | // TODO: classifyObject is redundant (see Hall.vue) | |
afd3240d | 104 | classifyObject: function(o) { |
6808d7a1 | 105 | return o.cadence.indexOf("d") === -1 ? "live" : "corr"; |
afd3240d | 106 | }, |
feaf1bf7 BA |
107 | showGame: function(game) { |
108 | // TODO: "isMyTurn" is duplicated (see GameList component). myColor also | |
109 | const isMyTurn = (g) => { | |
dc821737 | 110 | if (g.score != "*") return false; |
feaf1bf7 BA |
111 | const myColor = |
112 | g.players[0].uid == this.st.user.id || | |
113 | g.players[0].sid == this.st.user.sid | |
114 | ? "w" | |
115 | : "b"; | |
116 | const rem = g.movesCount % 2; | |
117 | return ( | |
118 | (rem == 0 && myColor == "w") || | |
119 | (rem == 1 && myColor == "b") | |
120 | ); | |
121 | }; | |
620a88ed | 122 | if (game.type == "live" || !isMyTurn(game)) { |
feaf1bf7 | 123 | this.$router.push("/game/" + game.id); |
620a88ed BA |
124 | return; |
125 | } | |
feaf1bf7 BA |
126 | // It's my turn in this game. Are there others? |
127 | let nextIds = ""; | |
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) + "]"; | |
135 | } | |
136 | this.$router.push("/game/" + game.id + nextIds); | |
db1f1f9a | 137 | }, |
aae89b49 BA |
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") { | |
142 | const oppsid = | |
143 | game.players[0].sid == this.st.user.sid | |
144 | ? game.players[1].sid | |
145 | : game.players[0].sid; | |
146 | this.conn.send( | |
147 | JSON.stringify( | |
148 | { | |
149 | code: "mabort", | |
150 | gid: game.id, | |
151 | // NOTE: target might not be online | |
152 | target: oppsid | |
153 | } | |
154 | ) | |
155 | ); | |
156 | } | |
157 | else if (!game.deletedByWhite || !game.deletedByBlack) { | |
158 | // Set score if game isn't deleted on server: | |
159 | ajax( | |
160 | "/games", | |
161 | "PUT", | |
162 | { | |
e57c4de4 BA |
163 | data: { |
164 | gid: game.id, | |
165 | newObj: { | |
166 | score: "?", | |
167 | scoreMsg: getScoreMessage("?") | |
168 | } | |
aae89b49 BA |
169 | } |
170 | } | |
171 | ); | |
172 | } | |
173 | }, | |
db1f1f9a BA |
174 | socketMessageListener: function(msg) { |
175 | const data = JSON.parse(msg.data); | |
f9c36b2d | 176 | if (data.code == "changeturn") { |
db1f1f9a BA |
177 | let games = !!parseInt(data.gid) |
178 | ? this.corrGames | |
179 | : this.liveGames; | |
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); | |
23ecf008 BA |
183 | if ( |
184 | (g.type == "live" && this.display == "corr") || | |
185 | (g.type == "corr" && this.display == "live") | |
186 | ) { | |
187 | document | |
188 | .getElementById(g.type + "Games") | |
189 | .classList.add("somethingnew"); | |
190 | } | |
db1f1f9a BA |
191 | } |
192 | }, | |
193 | socketCloseListener: function() { | |
194 | this.conn = new WebSocket(this.connexionString); | |
195 | this.conn.addEventListener("message", this.socketMessageListener); | |
196 | this.conn.addEventListener("close", this.socketCloseListener); | |
6808d7a1 BA |
197 | } |
198 | } | |
afd3240d BA |
199 | }; |
200 | </script> | |
2f258c37 | 201 | |
e2590fa8 | 202 | <style lang="sass"> |
2f258c37 BA |
203 | .active |
204 | color: #42a983 | |
5fe7e71c BA |
205 | |
206 | .tabbtn | |
207 | background-color: #f9faee | |
e2590fa8 BA |
208 | |
209 | table.game-list | |
210 | max-height: 100% | |
23ecf008 BA |
211 | |
212 | .somethingnew | |
213 | background-color: #c5fefe !important | |
2f258c37 | 214 | </style> |