Commit | Line | Data |
---|---|---|
ccd4a2b7 | 1 | <template lang="pug"> |
9d58ef95 | 2 | main |
dce792f6 | 3 | input#modalInfo.modal(type="checkbox") |
602d6bef | 4 | div#infoDiv(role="dialog" data-checkbox="modalInfo" aria-labelledby="infoMessage") |
dce792f6 BA |
5 | .card.smallpad.small-modal.text-center |
6 | label.modal-close(for="modalInfo") | |
7 | h3#infoMessage.section | |
3d55deea | 8 | p(v-html="infoMessage") |
5b020e73 | 9 | input#modalNewgame.modal(type="checkbox") |
602d6bef | 10 | div#newgameDiv(role="dialog" data-checkbox="modalNewgame" |
dcd68c41 | 11 | aria-labelledby="titleFenedit") |
c97830ea | 12 | .card.smallpad(@keyup.enter="newChallenge") |
5b020e73 BA |
13 | label#closeNewgame.modal-close(for="modalNewgame") |
14 | fieldset | |
602d6bef | 15 | label(for="selectVariant") {{ st.tr["Variant"] }} * |
9d58ef95 | 16 | select#selectVariant(v-model="newchallenge.vid") |
25d18342 BA |
17 | option(v-for="v in st.variants" :value="v.id" |
18 | :selected="newchallenge.vid==v.id") | |
19 | | {{ v.name }} | |
5b020e73 | 20 | fieldset |
f44fd3bf | 21 | label(for="timeControl") {{ st.tr["Cadence"] }} * |
25d18342 BA |
22 | div#predefinedTimeControls |
23 | button 3+2 | |
24 | button 5+3 | |
25 | button 15+5 | |
9d58ef95 | 26 | input#timeControl(type="text" v-model="newchallenge.timeControl" |
25d18342 | 27 | placeholder="5+0, 1h+30s, 7d+1d ...") |
b4d619d1 | 28 | fieldset(v-if="st.user.id > 0") |
602d6bef | 29 | label(for="selectPlayers") {{ st.tr["Play with?"] }} |
6fba6e0c | 30 | input#selectPlayers(type="text" v-model="newchallenge.to") |
25d18342 | 31 | fieldset(v-if="st.user.id > 0 && newchallenge.to.length > 0") |
ac8f441c | 32 | label(for="inputFen") FEN |
9d58ef95 | 33 | input#inputFen(type="text" v-model="newchallenge.fen") |
b4d619d1 | 34 | button(@click="newChallenge") {{ st.tr["Send challenge"] }} |
9d58ef95 | 35 | .row |
9ca1e26b | 36 | .col-sm-12 |
602d6bef | 37 | button#newGame(onClick="doClick('modalNewgame')") {{ st.tr["New game"] }} |
9d58ef95 | 38 | .row |
9ca1e26b | 39 | .col-sm-12.col-md-10.col-md-offset-1.col-lg-8.col-lg-offset-2 |
ed06d9e9 BA |
40 | div |
41 | .button-group | |
42 | button(@click="(e) => setDisplay('c','live',e)" class="active") | |
602d6bef | 43 | | {{ st.tr["Live challenges"] }} |
ed06d9e9 | 44 | button(@click="(e) => setDisplay('c','corr',e)") |
602d6bef | 45 | | {{ st.tr["Correspondance challenges"] }} |
ed06d9e9 BA |
46 | ChallengeList(v-show="cdisplay=='live'" |
47 | :challenges="filterChallenges('live')" @click-challenge="clickChallenge") | |
48 | ChallengeList(v-show="cdisplay=='corr'" | |
49 | :challenges="filterChallenges('corr')" @click-challenge="clickChallenge") | |
50 | #people | |
602d6bef | 51 | h3.text-center {{ st.tr["Who's there?"] }} |
ed06d9e9 | 52 | #players |
9335d45b BA |
53 | p(v-for="sid in Object.keys(people)" v-if="!!people[sid].name") |
54 | span {{ people[sid].name }} | |
ed06d9e9 | 55 | button.player-action( |
9335d45b BA |
56 | v-if="people[sid].name != st.user.name" |
57 | @click="challOrWatch(sid, $event)" | |
ed06d9e9 | 58 | ) |
9335d45b | 59 | | {{ st.tr[!!people[sid].gamer ? 'Playing' : 'Available'] }} |
ed06d9e9 BA |
60 | p.anonymous @nonymous ({{ anonymousCount }}) |
61 | #chat | |
ac8f441c | 62 | Chat(:newChat="newChat" @mychat="processChat") |
ed06d9e9 BA |
63 | .clearer |
64 | div | |
65 | .button-group | |
66 | button(@click="(e) => setDisplay('g','live',e)" class="active") | |
602d6bef | 67 | | {{ st.tr["Live games"] }} |
ed06d9e9 | 68 | button(@click="(e) => setDisplay('g','corr',e)") |
602d6bef | 69 | | {{ st.tr["Correspondance games"] }} |
ed06d9e9 BA |
70 | GameList(v-show="gdisplay=='live'" :games="filterGames('live')" |
71 | @show-game="showGame") | |
72 | GameList(v-show="gdisplay=='corr'" :games="filterGames('corr')" | |
73 | @show-game="showGame") | |
625022fd BA |
74 | </template> |
75 | ||
76 | <script> | |
5b020e73 | 77 | import { store } from "@/store"; |
9d58ef95 BA |
78 | import { checkChallenge } from "@/data/challengeCheck"; |
79 | import { ArrayFun } from "@/utils/array"; | |
03608482 | 80 | import { ajax } from "@/utils/ajax"; |
4b0384fa | 81 | import { getRandString, shuffle } from "@/utils/alea"; |
603b8a8b | 82 | import Chat from "@/components/Chat.vue"; |
5b020e73 BA |
83 | import GameList from "@/components/GameList.vue"; |
84 | import ChallengeList from "@/components/ChallengeList.vue"; | |
967a2686 | 85 | import { GameStorage } from "@/utils/gameStorage"; |
602d6bef BA |
86 | import { processModalClick } from "@/utils/modalClick"; |
87 | ||
625022fd | 88 | export default { |
cf2343ce | 89 | name: "my-hall", |
5b020e73 | 90 | components: { |
603b8a8b | 91 | Chat, |
5b020e73 BA |
92 | GameList, |
93 | ChallengeList, | |
94 | }, | |
fb54f098 BA |
95 | data: function () { |
96 | return { | |
5b020e73 | 97 | st: store.state, |
6855163c BA |
98 | cdisplay: "live", //or corr |
99 | pdisplay: "players", //or chat | |
fb54f098 | 100 | gdisplay: "live", |
6855163c | 101 | games: [], |
b4d619d1 | 102 | challenges: [], |
dcd68c41 | 103 | people: {}, //people in main hall |
3d55deea | 104 | infoMessage: "", |
9d58ef95 | 105 | newchallenge: { |
fb54f098 | 106 | fen: "", |
25d18342 | 107 | vid: localStorage.getItem("vid") || "", |
6fba6e0c | 108 | to: "", //name of challenged player (if any) |
25d18342 | 109 | timeControl: localStorage.getItem("timeControl") || "", |
fb54f098 | 110 | }, |
ac8f441c | 111 | newChat: "", |
fb54f098 BA |
112 | }; |
113 | }, | |
fd7aea36 BA |
114 | watch: { |
115 | // st.variants changes only once, at loading from [] to [...] | |
116 | "st.variants": function(variantArray) { | |
117 | // Set potential challenges and games variant names: | |
118 | this.challenges.forEach(c => { | |
119 | if (c.vname == "") | |
120 | c.vname = this.getVname(c.vid); | |
121 | }); | |
122 | this.games.forEach(g => { | |
123 | if (g.vname == "") | |
f41ce580 | 124 | g.vname = this.getVname(g.vid); |
fd7aea36 BA |
125 | }); |
126 | }, | |
127 | }, | |
b4d619d1 | 128 | computed: { |
ed06d9e9 BA |
129 | anonymousCount: function() { |
130 | let count = 0; | |
131 | Object.values(this.people).forEach(p => { count += (!p.name ? 1 : 0); }); | |
132 | return count; | |
b4d619d1 BA |
133 | }, |
134 | }, | |
9d58ef95 | 135 | created: function() { |
4d64881e | 136 | // Always add myself to players' list |
66d03f23 | 137 | const my = this.st.user; |
dcd68c41 | 138 | this.$set(this.people, my.sid, {id:my.id, name:my.name}); |
3d55deea BA |
139 | // Ask server for current corr games (all but mines) |
140 | ajax( | |
141 | "/games", | |
142 | "GET", | |
143 | {uid: this.st.user.id, excluded: true}, | |
144 | response => { | |
145 | this.games = this.games.concat(response.games.map(g => { | |
146 | const type = this.classifyObject(g); | |
147 | const vname = this.getVname(g.vid); | |
148 | return Object.assign({}, g, {type: type, vname: vname}); | |
149 | })); | |
150 | } | |
151 | ); | |
fe4c7e67 | 152 | // Also ask for corr challenges (open + sent by/to me) |
3d55deea BA |
153 | ajax( |
154 | "/challenges", | |
155 | "GET", | |
156 | {uid: this.st.user.id}, | |
157 | response => { | |
158 | // Gather all senders names, and then retrieve full identity: | |
159 | // (TODO [perf]: some might be online...) | |
fe4c7e67 BA |
160 | let names = {}; |
161 | response.challenges.forEach(c => { | |
162 | if (c.uid != this.st.user.id) | |
163 | names[c.uid] = ""; //unknwon for now | |
164 | else if (!!c.target && c.target != this.st.user.id) | |
165 | names[c.target] = ""; | |
166 | }); | |
167 | const addChallenges = (newChalls) => { | |
168 | names[this.st.user.id] = this.st.user.name; //in case of | |
169 | this.challenges = this.challenges.concat( | |
170 | response.challenges.map(c => { | |
171 | const from = {name: names[c.uid], id: c.uid}; //or just name | |
172 | const type = this.classifyObject(c); | |
173 | const vname = this.getVname(c.vid); | |
174 | return Object.assign({}, | |
175 | { | |
176 | type: type, | |
177 | vname: vname, | |
178 | from: from, | |
179 | to: (!!c.target ? names[c.target] : ""), | |
180 | }, | |
181 | c); | |
182 | }) | |
183 | ); | |
184 | }; | |
185 | if (names !== {}) | |
186 | { | |
187 | ajax("/users", | |
188 | "GET", | |
189 | { ids: Object.keys(names).join(",") }, | |
190 | response2 => { | |
191 | response2.users.forEach(u => {names[u.id] = u.name}); | |
192 | addChallenges(); | |
193 | } | |
194 | ); | |
195 | } | |
196 | else | |
197 | addChallenges(); | |
3d55deea BA |
198 | } |
199 | ); | |
2ada153c | 200 | // 0.1] Ask server for room composition: |
7b01e447 | 201 | const funcPollClients = () => { |
ed06d9e9 BA |
202 | // Same strategy as in Game.vue: send connection |
203 | // after we're sure WebSocket is initialized | |
204 | this.st.conn.send(JSON.stringify({code:"connect"})); | |
81d9ce72 | 205 | this.st.conn.send(JSON.stringify({code:"pollclients"})); |
ac8f441c | 206 | this.st.conn.send(JSON.stringify({code:"pollgamers"})); |
4d64881e | 207 | }; |
7b01e447 BA |
208 | if (!!this.st.conn && this.st.conn.readyState == 1) //1 == OPEN state |
209 | funcPollClients(); | |
210 | else //socket not ready yet (initial loading) | |
3cb412e9 | 211 | this.st.conn.onopen = funcPollClients; |
4d64881e BA |
212 | this.st.conn.onmessage = this.socketMessageListener; |
213 | const socketCloseListener = () => { | |
cdb34c93 | 214 | store.socketCloseListener(); //reinitialize connexion (in store.js) |
4d64881e BA |
215 | this.st.conn.addEventListener('message', this.socketMessageListener); |
216 | this.st.conn.addEventListener('close', socketCloseListener); | |
217 | }; | |
218 | this.st.conn.onclose = socketCloseListener; | |
9d58ef95 | 219 | }, |
25d18342 | 220 | mounted: function() { |
602d6bef BA |
221 | [document.getElementById("infoDiv"),document.getElementById("newgameDiv")] |
222 | .forEach(elt => elt.addEventListener("click", processModalClick)); | |
25d18342 BA |
223 | document.querySelectorAll("#predefinedTimeControls > button").forEach( |
224 | (b) => { b.addEventListener("click", | |
225 | () => { this.newchallenge.timeControl = b.innerHTML; } | |
226 | )} | |
227 | ); | |
228 | }, | |
fb54f098 | 229 | methods: { |
a6bddfc6 | 230 | // Helpers: |
6855163c BA |
231 | filterChallenges: function(type) { |
232 | return this.challenges.filter(c => c.type == type); | |
233 | }, | |
234 | filterGames: function(type) { | |
a9b131f1 | 235 | return this.games.filter(g => g.type == type); |
6855163c | 236 | }, |
2ada153c | 237 | classifyObject: function(o) { //challenge or game |
2ada153c | 238 | return (o.timeControl.indexOf('d') === -1 ? "live" : "corr"); |
6855163c | 239 | }, |
2ada153c | 240 | showGame: function(g) { |
5bd05dba | 241 | // NOTE: we are an observer, since only games I don't play are shown here |
2ada153c | 242 | // ==> Moves sent by connected remote player(s) if live game |
d9b86b16 | 243 | let url = "/game/" + g.id; |
2ada153c | 244 | if (g.type == "live") |
dc284d90 | 245 | url += "?rid=" + g.rid; |
2ada153c | 246 | this.$router.push(url); |
a6bddfc6 | 247 | }, |
5bcc9b31 BA |
248 | setDisplay: function(letter, type, e) { |
249 | this[letter + "display"] = type; | |
250 | e.target.classList.add("active"); | |
251 | if (!!e.target.previousElementSibling) | |
252 | e.target.previousElementSibling.classList.remove("active"); | |
253 | else | |
254 | e.target.nextElementSibling.classList.remove("active"); | |
255 | }, | |
a6bddfc6 | 256 | getVname: function(vid) { |
f41ce580 BA |
257 | const variant = this.st.variants.find(v => v.id == vid); |
258 | // this.st.variants might be uninitialized (variant == null) | |
259 | return (!!variant ? variant.name : ""); | |
a6bddfc6 | 260 | }, |
ac8f441c BA |
261 | processChat: function(chat) { |
262 | // When received on server, this will trigger a "notifyRoom" | |
263 | this.st.conn.send(JSON.stringify({code:"newchat", chat: chat})); | |
264 | }, | |
a6bddfc6 BA |
265 | sendSomethingTo: function(to, code, obj, warnDisconnected) { |
266 | const doSend = (code, obj, sid) => { | |
267 | this.st.conn.send(JSON.stringify(Object.assign( | |
a6bddfc6 BA |
268 | {code: code}, |
269 | obj, | |
270 | {target: sid} | |
271 | ))); | |
272 | }; | |
9335d45b | 273 | if (!to || (!to.sid && !to.name)) |
a6bddfc6 | 274 | { |
5ea8d113 | 275 | // Open challenge: send to all connected players (me excepted) |
dcd68c41 BA |
276 | Object.keys(this.people).forEach(sid => { |
277 | if (sid != this.st.user.sid) | |
278 | doSend(code, obj, sid); | |
a6bddfc6 BA |
279 | }); |
280 | } | |
9335d45b BA |
281 | else |
282 | { | |
283 | let targetSid = ""; | |
284 | if (!!to.sid) | |
285 | targetSid = to.sid; | |
286 | else | |
287 | { | |
288 | if (to.name == this.st.user.name) | |
289 | return alert(this.st.tr["Cannot challenge self"]); | |
290 | // Challenge with targeted players | |
291 | targetSid = | |
292 | Object.keys(this.people).find(sid => this.people[sid].name == to.name); | |
293 | if (!targetSid) | |
294 | { | |
295 | if (!!warnDisconnected) | |
296 | alert(this.st.tr["Warning: target is not connected"]); | |
297 | return false; | |
298 | } | |
299 | } | |
300 | doSend(code, obj, targetSid); | |
301 | } | |
5ea8d113 | 302 | return true; |
a6bddfc6 BA |
303 | }, |
304 | // Messaging center: | |
9d58ef95 BA |
305 | socketMessageListener: function(msg) { |
306 | const data = JSON.parse(msg.data); | |
307 | switch (data.code) | |
308 | { | |
6d9f4315 | 309 | case "duplicate": |
a3ac374b BA |
310 | this.st.conn.send(JSON.stringify({code:"duplicate"})); |
311 | this.st.conn.send = () => {}; | |
602d6bef | 312 | alert(this.st.tr["Warning: multi-tabs not supported"]); |
6d9f4315 | 313 | break; |
f4f4c03c | 314 | // 0.2] Receive clients list (just socket IDs) |
81d9ce72 | 315 | case "pollclients": |
5a3da968 | 316 | data.sockIds.forEach(sid => { |
dcd68c41 | 317 | this.$set(this.people, sid, {id:0, name:""}); |
ac8f441c | 318 | // Ask identity and challenges |
5a3da968 | 319 | this.st.conn.send(JSON.stringify({code:"askidentity", target:sid})); |
2ada153c | 320 | this.st.conn.send(JSON.stringify({code:"askchallenge", target:sid})); |
5a3da968 | 321 | }); |
ac8f441c BA |
322 | break; |
323 | case "pollgamers": | |
324 | // NOTE: we could make a difference between people in hall | |
325 | // and gamers, but is it necessary? | |
326 | data.sockIds.forEach(sid => { | |
9335d45b | 327 | this.$set(this.people, sid, {id:0, name:"", gamer:true}); |
ac8f441c BA |
328 | this.st.conn.send(JSON.stringify({code:"askidentity", target:sid})); |
329 | }); | |
cd0d7743 BA |
330 | // Also ask current games to all playing peers (TODO: some design issue) |
331 | this.st.conn.send(JSON.stringify({code:"askgames"})); | |
5a3da968 | 332 | break; |
81d9ce72 | 333 | case "askidentity": |
6855163c BA |
334 | // Request for identification: reply if I'm not anonymous |
335 | if (this.st.user.id > 0) | |
336 | { | |
dcd68c41 BA |
337 | this.st.conn.send(JSON.stringify({code:"identity", |
338 | user: { | |
339 | // NOTE: decompose to avoid revealing email | |
340 | name: this.st.user.name, | |
341 | sid: this.st.user.sid, | |
342 | id: this.st.user.id, | |
343 | }, | |
344 | target:data.from})); | |
6855163c | 345 | } |
5a3da968 | 346 | break; |
dcd68c41 | 347 | case "identity": |
dcd68c41 | 348 | this.$set(this.people, data.user.sid, |
9335d45b BA |
349 | { |
350 | id: data.user.id, | |
351 | name: data.user.name, | |
352 | gamer: this.people[data.user.sid].gamer, | |
353 | }); | |
dcd68c41 | 354 | break; |
dd75774d | 355 | case "askchallenge": |
1efe1d79 | 356 | { |
6855163c | 357 | // Send my current live challenge (if any) |
5ea8d113 BA |
358 | const cIdx = this.challenges.findIndex(c => |
359 | c.from.sid == this.st.user.sid && c.type == "live"); | |
dd75774d BA |
360 | if (cIdx >= 0) |
361 | { | |
362 | const c = this.challenges[cIdx]; | |
a64d9122 BA |
363 | // TODO: code below requires "c.to" to have given his identity, |
364 | // but it can happen that the identity arrives later, which | |
365 | // prevent him from receiving the challenge. | |
366 | // ==> Filter later (when receiving challenge) | |
367 | // if (!!c.to) | |
368 | // { | |
369 | // // Only share targeted challenges to the targets: | |
370 | // const toSid = Object.keys(this.people).find(k => | |
371 | // this.people[k].name == c.to); | |
372 | // if (toSid != data.from) | |
373 | // return; | |
374 | // } | |
dd75774d BA |
375 | const myChallenge = |
376 | { | |
81d9ce72 | 377 | // Minimal challenge informations: (from not required) |
2ada153c | 378 | id: c.id, |
81d9ce72 BA |
379 | to: c.to, |
380 | fen: c.fen, | |
381 | vid: c.vid, | |
5ea8d113 | 382 | timeControl: c.timeControl, |
a64d9122 | 383 | added: c.added, |
dd75774d BA |
384 | }; |
385 | this.st.conn.send(JSON.stringify({code:"challenge", | |
42c15a75 | 386 | chall:myChallenge, target:data.from})); |
81d9ce72 BA |
387 | } |
388 | break; | |
1efe1d79 | 389 | } |
dd75774d BA |
390 | case "challenge": |
391 | // Receive challenge from some player (+sid) | |
a64d9122 BA |
392 | // NOTE about next condition: see "askchallenge" case. |
393 | if (!data.chall.to || data.chall.to == this.st.user.name) | |
394 | { | |
395 | let newChall = data.chall; | |
396 | newChall.type = this.classifyObject(data.chall); | |
397 | newChall.from = | |
398 | Object.assign({sid:data.from}, this.people[data.from]); | |
399 | newChall.vname = this.getVname(newChall.vid); | |
400 | this.challenges.push(newChall); | |
401 | } | |
81d9ce72 | 402 | break; |
dd75774d | 403 | case "game": |
1efe1d79 | 404 | { |
6855163c | 405 | // Receive game from some player (+sid) |
6855163c | 406 | // NOTE: it may be correspondance (if newgame while we are connected) |
b1ea2149 BA |
407 | // If duplicate found: select rid (remote ID) at random |
408 | let game = this.games.find(g => g.id == data.game.id); | |
a64d9122 BA |
409 | if (!!game) |
410 | { | |
411 | if (Math.random() < 0.5) | |
412 | game.rid = data.from; | |
413 | } | |
b1ea2149 | 414 | else |
d9b86b16 BA |
415 | { |
416 | let newGame = data.game; | |
417 | newGame.type = this.classifyObject(data.game); | |
a9b131f1 | 418 | newGame.vname = this.getVname(data.game.vid); |
d9b86b16 | 419 | newGame.rid = data.from; |
a64d9122 BA |
420 | if (!data.game.score) |
421 | newGame.score = "*"; | |
d9b86b16 BA |
422 | this.games.push(newGame); |
423 | } | |
81d9ce72 | 424 | break; |
1efe1d79 | 425 | } |
9d58ef95 | 426 | case "newgame": |
5d04793e | 427 | // New game just started: data contain all information |
66d03f23 | 428 | if (this.classifyObject(data.gameInfo) == "live") |
5d04793e | 429 | this.startNewGame(data.gameInfo); |
5d04793e BA |
430 | else |
431 | { | |
3d55deea | 432 | this.infoMessage = "New game started: " + |
11667c79 BA |
433 | "<a href='#/game/" + data.gameInfo.id + "'>" + |
434 | "#/game/" + data.gameInfo.id + "</a>"; | |
dce792f6 BA |
435 | let modalBox = document.getElementById("modalInfo"); |
436 | modalBox.checked = true; | |
3d55deea | 437 | setTimeout(() => { modalBox.checked = false; }, 3000); |
5d04793e | 438 | } |
9d58ef95 | 439 | break; |
ac8f441c BA |
440 | case "newchat": |
441 | this.newChat = data.chat; | |
442 | break; | |
bb7dd7db | 443 | case "refusechallenge": |
5bd05dba | 444 | ArrayFun.remove(this.challenges, c => c.id == data.cid); |
602d6bef | 445 | alert(this.st.tr["Challenge declined"]); |
bb7dd7db | 446 | break; |
1efe1d79 | 447 | case "deletechallenge": |
1ba761c8 | 448 | // NOTE: the challenge may be already removed |
9d58ef95 BA |
449 | ArrayFun.remove(this.challenges, c => c.id == data.cid); |
450 | break; | |
b4d619d1 | 451 | case "connect": |
ac8f441c | 452 | case "gconnect": |
9335d45b | 453 | this.$set(this.people, data.from, {name:"", id:0, gamer:data.code[0]=='g'}); |
c6788ecf | 454 | this.st.conn.send(JSON.stringify({code:"askidentity", target:data.from})); |
ac8f441c BA |
455 | if (data.code == "connect") |
456 | this.st.conn.send(JSON.stringify({code:"askchallenge", target:data.from})); | |
457 | else | |
458 | this.st.conn.send(JSON.stringify({code:"askgame", target:data.from})); | |
9d58ef95 | 459 | break; |
b4d619d1 | 460 | case "disconnect": |
26f3a887 | 461 | case "gdisconnect": |
dcd68c41 | 462 | this.$delete(this.people, data.from); |
ac8f441c BA |
463 | if (data.code == "disconnect") |
464 | { | |
465 | // Also remove all challenges sent by this player: | |
466 | ArrayFun.remove(this.challenges, c => c.from.sid == data.from); | |
467 | } | |
468 | else | |
469 | { | |
470 | // And all live games where he plays and no other opponent is online | |
471 | ArrayFun.remove(this.games, g => | |
472 | g.type == "live" && (g.players.every(p => p.sid == data.from | |
473 | || !this.people[p.sid])), "all"); | |
474 | } | |
9d58ef95 BA |
475 | break; |
476 | } | |
477 | }, | |
a6bddfc6 | 478 | // Challenge lifecycle: |
9335d45b BA |
479 | tryChallenge: function(sid) { |
480 | if (this.people[sid].id == 0) | |
b4d619d1 | 481 | return; //anonymous players cannot be challenged |
9335d45b BA |
482 | // TODO: SID is available, so we could use it instead of searching from name |
483 | this.newchallenge.to = this.people[sid].name; | |
b4d619d1 | 484 | doClick("modalNewgame"); |
fb54f098 | 485 | }, |
29ced362 BA |
486 | challOrWatch: function(sid) { |
487 | if (!this.people[sid].gamer) | |
dcd68c41 | 488 | { |
29ced362 BA |
489 | // Available, in Hall |
490 | this.tryChallenge(sid); | |
491 | } | |
492 | else | |
493 | { | |
494 | // Playing, in Game | |
495 | this.showGame(this.games.find( | |
496 | g => g.players.some(pl => pl.sid == sid || pl.uid == this.people[sid].id))); | |
497 | } | |
dcd68c41 | 498 | }, |
9d58ef95 | 499 | newChallenge: async function() { |
25d18342 | 500 | if (this.newchallenge.vid == "") |
602d6bef | 501 | return alert(this.st.tr["Please select a variant"]); |
a64d9122 BA |
502 | if (!!this.newchallenge.to && this.newchallenge.to == this.st.user.name) |
503 | return alert(this.st.tr["Self-challenge is forbidden"]); | |
bb7dd7db | 504 | const vname = this.getVname(this.newchallenge.vid); |
1efe1d79 BA |
505 | const vModule = await import("@/variants/" + vname + ".js"); |
506 | window.V = vModule.VariantRules; | |
dcd68c41 BA |
507 | if (!!this.newchallenge.timeControl.match(/^[0-9]+$/)) |
508 | this.newchallenge.timeControl += "+0"; //assume minutes, no increment | |
9d58ef95 BA |
509 | const error = checkChallenge(this.newchallenge); |
510 | if (!!error) | |
511 | return alert(error); | |
2ada153c | 512 | const ctype = this.classifyObject(this.newchallenge); |
098cd7f1 | 513 | if (ctype == "corr" && this.st.user.id <= 0) |
602d6bef | 514 | return alert(this.st.tr["Please log in to play correspondance games"]); |
bb7dd7db | 515 | // NOTE: "from" information is not required here |
a7808884 | 516 | let chall = Object.assign({}, this.newchallenge); |
2ada153c | 517 | const finishAddChallenge = (cid,warnDisconnected) => { |
1efe1d79 | 518 | chall.id = cid || "c" + getRandString(); |
2ada153c | 519 | // Send challenge to peers (if connected) |
9335d45b | 520 | const isSent = this.sendSomethingTo({name:chall.to}, "challenge", |
5ea8d113 BA |
521 | {chall:chall}, !!warnDisconnected); |
522 | if (!isSent) | |
523 | return; | |
fe4c7e67 | 524 | // Remove old challenge if any (only one at a time of a given type): |
5ea8d113 | 525 | const cIdx = this.challenges.findIndex(c => |
fe4c7e67 | 526 | (c.from.sid == this.st.user.sid || c.from.id == this.st.user.id) && c.type == ctype); |
5ea8d113 BA |
527 | if (cIdx >= 0) |
528 | { | |
529 | // Delete current challenge (will be replaced now) | |
9335d45b | 530 | this.sendSomethingTo({name:this.challenges[cIdx].to}, |
5ea8d113 BA |
531 | "deletechallenge", {cid:this.challenges[cIdx].id}); |
532 | if (ctype == "corr") | |
533 | { | |
534 | ajax( | |
535 | "/challenges", | |
536 | "DELETE", | |
537 | {id: this.challenges[cIdx].id} | |
538 | ); | |
539 | } | |
540 | this.challenges.splice(cIdx, 1); | |
541 | } | |
542 | // Add new challenge: | |
1efe1d79 | 543 | chall.added = Date.now(); |
a9b131f1 | 544 | // NOTE: vname and type are redundant (can be deduced from timeControl + vid) |
bb7dd7db BA |
545 | chall.type = ctype; |
546 | chall.vname = vname; | |
dcd68c41 BA |
547 | chall.from = { //decompose to avoid revealing email |
548 | sid: this.st.user.sid, | |
549 | id: this.st.user.id, | |
550 | name: this.st.user.name, | |
551 | }; | |
1efe1d79 | 552 | this.challenges.push(chall); |
a3ac374b | 553 | // Remember timeControl + vid for quicker further challenges: |
25d18342 BA |
554 | localStorage.setItem("timeControl", chall.timeControl); |
555 | localStorage.setItem("vid", chall.vid); | |
b4d619d1 BA |
556 | document.getElementById("modalNewgame").checked = false; |
557 | }; | |
1efe1d79 BA |
558 | if (ctype == "live") |
559 | { | |
560 | // Live challenges have a random ID | |
2ada153c | 561 | finishAddChallenge(null, "warnDisconnected"); |
03608482 | 562 | } |
b4d619d1 | 563 | else |
03608482 | 564 | { |
b4d619d1 | 565 | // Correspondance game: send challenge to server |
03608482 | 566 | ajax( |
1efe1d79 | 567 | "/challenges", |
03608482 | 568 | "POST", |
bebcc8d4 | 569 | { chall: chall }, |
1efe1d79 | 570 | response => { finishAddChallenge(response.cid); } |
03608482 | 571 | ); |
9d58ef95 | 572 | } |
fb54f098 | 573 | }, |
a6bddfc6 | 574 | clickChallenge: function(c) { |
485fccd5 BA |
575 | const myChallenge = (c.from.sid == this.st.user.sid //live |
576 | || (this.st.user.id > 0 && c.from.id == this.st.user.id)); //corr | |
577 | if (!myChallenge) | |
a6bddfc6 | 578 | { |
3d55deea | 579 | if (c.type == "corr" && this.st.user.id <= 0) |
602d6bef | 580 | return alert(this.st.tr["Please log in to accept corr challenges"]); |
a6bddfc6 | 581 | c.accepted = true; |
485fccd5 | 582 | if (!!c.to) //c.to == this.st.user.name (connected) |
a6bddfc6 BA |
583 | { |
584 | // TODO: if special FEN, show diagram after loading variant | |
585 | c.accepted = confirm("Accept challenge?"); | |
586 | } | |
485fccd5 | 587 | if (c.accepted) |
36093eba | 588 | { |
dcd68c41 BA |
589 | c.seat = { //again, avoid c.seat = st.user to not reveal email |
590 | sid: this.st.user.sid, | |
591 | id: this.st.user.id, | |
592 | name: this.st.user.name, | |
593 | }; | |
485fccd5 BA |
594 | this.launchGame(c); |
595 | } | |
596 | else | |
597 | { | |
598 | this.st.conn.send(JSON.stringify({ | |
599 | code: "refusechallenge", | |
600 | cid: c.id, target: c.from.sid})); | |
36093eba | 601 | } |
9335d45b | 602 | this.sendSomethingTo(!!c.to ? {sid:c.from.sid} : null, "deletechallenge", {cid:c.id}); |
a6bddfc6 | 603 | } |
2be5d614 | 604 | else //my challenge |
485fccd5 | 605 | { |
2be5d614 BA |
606 | if (c.type == "corr") |
607 | { | |
608 | ajax( | |
609 | "/challenges", | |
610 | "DELETE", | |
611 | {id: c.id} | |
612 | ); | |
613 | } | |
9335d45b | 614 | this.sendSomethingTo({name:c.to}, "deletechallenge", {cid:c.id}); |
485fccd5 | 615 | } |
5ea8d113 | 616 | // In all cases, the challenge is consumed: |
3d55deea | 617 | ArrayFun.remove(this.challenges, ch => ch.id == c.id); |
a6bddfc6 | 618 | }, |
a64d9122 | 619 | // NOTE: when launching game, the challenge is already being deleted |
36093eba | 620 | launchGame: async function(c) { |
a9b131f1 | 621 | const vModule = await import("@/variants/" + c.vname + ".js"); |
a6bddfc6 | 622 | window.V = vModule.VariantRules; |
4b0384fa BA |
623 | // These game informations will be sent to other players |
624 | const gameInfo = | |
a6bddfc6 | 625 | { |
11667c79 | 626 | id: getRandString(), |
a6bddfc6 | 627 | fen: c.fen || V.GenRandInitFen(), |
5d04793e | 628 | players: shuffle([c.from, c.seat]), //white then black |
a6bddfc6 | 629 | vid: c.vid, |
f41ce580 | 630 | vname: c.vname, //theoretically vid is enough, but much easier with vname |
a9b131f1 | 631 | timeControl: c.timeControl, |
a6bddfc6 | 632 | }; |
5ea8d113 BA |
633 | let oppsid = c.from.sid; //may not be defined if corr + offline opp |
634 | if (!oppsid) | |
8c564f46 | 635 | { |
5ea8d113 | 636 | oppsid = Object.keys(this.people).find(sid => |
dcd68c41 | 637 | this.people[sid].id == c.from.id); |
8c564f46 | 638 | } |
411d23cd | 639 | const tryNotifyOpponent = () => { |
5ea8d113 | 640 | if (!!oppsid) //opponent is online |
411d23cd BA |
641 | { |
642 | this.st.conn.send(JSON.stringify({code:"newgame", | |
5ea8d113 | 643 | gameInfo:gameInfo, target:oppsid, cid:c.id})); |
411d23cd BA |
644 | } |
645 | }; | |
485fccd5 | 646 | if (c.type == "live") |
411d23cd | 647 | { |
b1ea2149 | 648 | // NOTE: in this case we are sure opponent is online |
411d23cd | 649 | tryNotifyOpponent(); |
485fccd5 | 650 | this.startNewGame(gameInfo); |
411d23cd | 651 | } |
485fccd5 BA |
652 | else //corr: game only on server |
653 | { | |
654 | ajax( | |
655 | "/games", | |
656 | "POST", | |
2be5d614 | 657 | {gameInfo: gameInfo, cid: c.id}, //cid useful to delete challenge |
411d23cd | 658 | response => { |
11667c79 | 659 | gameInfo.id = response.gameId; |
411d23cd BA |
660 | tryNotifyOpponent(); |
661 | this.$router.push("/game/" + response.gameId); | |
662 | } | |
485fccd5 BA |
663 | ); |
664 | } | |
c6788ecf | 665 | // Send game info to everyone except opponent (and me) |
5ea8d113 | 666 | Object.keys(this.people).forEach(sid => { |
b1ea2149 | 667 | if (![this.st.user.sid,oppsid].includes(sid)) |
5ea8d113 BA |
668 | { |
669 | this.st.conn.send(JSON.stringify({code:"game", | |
670 | game: { //minimal game info: | |
671 | id: gameInfo.id, | |
b1ea2149 | 672 | players: gameInfo.players, |
5ea8d113 BA |
673 | vid: gameInfo.vid, |
674 | timeControl: gameInfo.timeControl, | |
675 | }, | |
676 | target: sid})); | |
677 | } | |
678 | }); | |
fb54f098 | 679 | }, |
a9b131f1 | 680 | // NOTE: for live games only (corr games start on the server) |
42c15a75 | 681 | startNewGame: function(gameInfo) { |
25996aed BA |
682 | const game = Object.assign({}, gameInfo, { |
683 | // (other) Game infos: constant | |
6d01bb17 | 684 | fenStart: gameInfo.fen, |
92a523d1 | 685 | added: Date.now(), |
25996aed | 686 | // Game state (including FEN): will be updated |
967a2686 | 687 | moves: [], |
a9b131f1 | 688 | clocks: [-1, -1], //-1 = unstarted |
66d03f23 | 689 | initime: [0, 0], //initialized later |
967a2686 | 690 | score: "*", |
a7808884 | 691 | }); |
967a2686 | 692 | GameStorage.add(game); |
7b626bdd BA |
693 | if (this.st.settings.sound >= 1) |
694 | new Audio("/sounds/newgame.mp3").play().catch(err => {}); | |
11667c79 | 695 | this.$router.push("/game/" + gameInfo.id); |
1efe1d79 | 696 | }, |
fb54f098 | 697 | }, |
85e5b5c1 | 698 | }; |
ccd4a2b7 | 699 | </script> |
85e5b5c1 | 700 | |
41c80bb6 | 701 | <style lang="sass" scoped> |
5bcc9b31 BA |
702 | .active |
703 | color: #42a983 | |
9ca1e26b BA |
704 | #newGame |
705 | display: block | |
72ccbd67 | 706 | margin: 10px auto 5px auto |
ed06d9e9 BA |
707 | #people |
708 | width: 100% | |
709 | #players | |
710 | width: 50% | |
711 | position: relative | |
712 | float: left | |
713 | #chat | |
714 | width: 50% | |
715 | float: left | |
716 | position: relative | |
717 | @media screen and (max-width: 767px) | |
718 | #players, #chats | |
719 | width: 100% | |
72ccbd67 BA |
720 | #chat > .card |
721 | max-width: 100% | |
722 | margin: 0; | |
723 | border: none; | |
41c80bb6 | 724 | #players > p |
ed06d9e9 | 725 | margin-left: 5px |
dcd68c41 BA |
726 | .anonymous |
727 | font-style: italic | |
728 | button.player-action | |
41c80bb6 | 729 | margin-left: 32px |
85e5b5c1 | 730 | </style> |