| 1 | <template lang="pug"> |
| 2 | main |
| 3 | input#modalNewgame.modal(type="checkbox") |
| 4 | div(role="dialog" aria-labelledby="titleFenedit") |
| 5 | .card.smallpad |
| 6 | label#closeNewgame.modal-close(for="modalNewgame") |
| 7 | fieldset |
| 8 | label(for="selectVariant") {{ st.tr["Variant"] }} |
| 9 | select#selectVariant(v-model="newchallenge.vid") |
| 10 | option(v-for="v in st.variants" :value="v.id") {{ v.name }} |
| 11 | fieldset |
| 12 | label(for="selectNbPlayers") {{ st.tr["Number of players"] }} |
| 13 | select#selectNbPlayers(v-model="newchallenge.nbPlayers") |
| 14 | option(v-show="possibleNbplayers(2)" value="2" selected) 2 |
| 15 | option(v-show="possibleNbplayers(3)" value="3") 3 |
| 16 | option(v-show="possibleNbplayers(4)" value="4") 4 |
| 17 | fieldset |
| 18 | label(for="timeControl") {{ st.tr["Time control"] }} |
| 19 | input#timeControl(type="text" v-model="newchallenge.timeControl" |
| 20 | placeholder="3m+2s, 1h+30s, 7d+1d ...") |
| 21 | fieldset(v-if="st.user.id > 0") |
| 22 | label(for="selectPlayers") {{ st.tr["Play with? (optional)"] }} |
| 23 | #selectPlayers |
| 24 | input(type="text" v-model="newchallenge.to[0]") |
| 25 | input(v-show="newchallenge.nbPlayers>=3" type="text" |
| 26 | v-model="newchallenge.to[1]") |
| 27 | input(v-show="newchallenge.nbPlayers==4" type="text" |
| 28 | v-model="newchallenge.to[2]") |
| 29 | fieldset(v-if="st.user.id > 0") |
| 30 | label(for="inputFen") {{ st.tr["FEN (optional)"] }} |
| 31 | input#inputFen(type="text" v-model="newchallenge.fen") |
| 32 | button(@click="newChallenge") {{ st.tr["Send challenge"] }} |
| 33 | .row |
| 34 | .col-sm-12.col-md-10.col-md-offset-1.col-lg-8.col-lg-offset-2 |
| 35 | button(onClick="doClick('modalNewgame')") New game |
| 36 | .row |
| 37 | .col-sm-12.col-md-10.col-md-offset-1.col-lg-8.col-lg-offset-2 |
| 38 | .collapse |
| 39 | input#challengeSection(type="radio" checked aria-hidden="true" name="accordion") |
| 40 | label(for="challengeSection" aria-hidden="true") Challenges |
| 41 | div |
| 42 | .button-group |
| 43 | button(@click="cdisplay='live'") Live Challenges |
| 44 | button(@click="cdisplay='corr'") Correspondance challenges |
| 45 | ChallengeList(v-show="cdisplay=='live'" |
| 46 | :challenges="filterChallenges('live')" @click-challenge="clickChallenge") |
| 47 | ChallengeList(v-show="cdisplay=='corr'" |
| 48 | :challenges="filterChallenges('corr')" @click-challenge="clickChallenge") |
| 49 | input#peopleSection(type="radio" aria-hidden="true" name="accordion") |
| 50 | label(for="peopleSection" aria-hidden="true") People |
| 51 | div |
| 52 | .button-group |
| 53 | button(@click="pdisplay='players'") Players |
| 54 | button(@click="pdisplay='chat'") Chat |
| 55 | #players(v-show="pdisplay=='players'") |
| 56 | h3 Online players |
| 57 | .player(v-for="p in uniquePlayers" @click="tryChallenge(p)" |
| 58 | :class="{anonymous: !!p.count}" |
| 59 | ) |
| 60 | | {{ p.name + (!!p.count ? " ("+p.count+")" : "") }} |
| 61 | #chat(v-show="pdisplay=='chat'") |
| 62 | h3 Chat (TODO) |
| 63 | input#gameSection(type="radio" aria-hidden="true" name="accordion") |
| 64 | label(for="gameSection" aria-hidden="true") Games |
| 65 | div |
| 66 | .button-group |
| 67 | button(@click="gdisplay='live'") Live games |
| 68 | button(@click="gdisplay='corr'") Correspondance games |
| 69 | GameList(v-show="gdisplay=='live'" :games="filterGames('live')" |
| 70 | @show-game="showGame") |
| 71 | GameList(v-show="gdisplay=='corr'" :games="filterGames('corr')" |
| 72 | @show-game="showGame") |
| 73 | </template> |
| 74 | |
| 75 | <script> |
| 76 | import { store } from "@/store"; |
| 77 | import { NbPlayers } from "@/data/nbPlayers"; |
| 78 | import { checkChallenge } from "@/data/challengeCheck"; |
| 79 | import { ArrayFun } from "@/utils/array"; |
| 80 | import { ajax } from "@/utils/ajax"; |
| 81 | import { getRandString } from "@/utils/alea"; |
| 82 | import GameList from "@/components/GameList.vue"; |
| 83 | import ChallengeList from "@/components/ChallengeList.vue"; |
| 84 | export default { |
| 85 | name: "my-hall", |
| 86 | components: { |
| 87 | GameList, |
| 88 | ChallengeList, |
| 89 | }, |
| 90 | data: function () { |
| 91 | return { |
| 92 | st: store.state, |
| 93 | cdisplay: "live", //or corr |
| 94 | pdisplay: "players", //or chat |
| 95 | gdisplay: "live", |
| 96 | games: [], |
| 97 | challenges: [], |
| 98 | players: [], //online players (rename into "people" ?) |
| 99 | newchallenge: { |
| 100 | fen: "", |
| 101 | vid: 0, |
| 102 | nbPlayers: 0, |
| 103 | to: ["", "", ""], //name of challenged players |
| 104 | timeControl: "", //"2m+2s" ...etc |
| 105 | }, |
| 106 | }; |
| 107 | }, |
| 108 | computed: { |
| 109 | uniquePlayers: function() { |
| 110 | // Show e.g. "@nonymous (5)", and do nothing on click on anonymous |
| 111 | let anonymous = {id:0, name:"@nonymous", count:0}; |
| 112 | let playerList = []; |
| 113 | this.players.forEach(p => { |
| 114 | if (p.id > 0) |
| 115 | playerList.push(p); |
| 116 | else |
| 117 | anonymous.count++; |
| 118 | }); |
| 119 | if (anonymous.count > 0) |
| 120 | playerList.push(anonymous); |
| 121 | return playerList; |
| 122 | }, |
| 123 | }, |
| 124 | created: function() { |
| 125 | // Always add myself to players' list |
| 126 | this.players.push(this.st.user); |
| 127 | // Ask server for current corr games (all but mines) |
| 128 | // ajax( |
| 129 | // "", |
| 130 | // "GET", |
| 131 | // response => { |
| 132 | // |
| 133 | // } |
| 134 | // ); |
| 135 | // // Also ask for corr challenges (all) |
| 136 | // ajax( |
| 137 | // "", |
| 138 | // "GET", |
| 139 | // response => { |
| 140 | // |
| 141 | // } |
| 142 | // ); |
| 143 | // 0.1] Ask server for for room composition: |
| 144 | const socketOpenListener = () => { |
| 145 | this.st.conn.send(JSON.stringify({code:"pollclients"})); |
| 146 | }; |
| 147 | this.st.conn.onopen = socketOpenListener; |
| 148 | // TODO: is this required here? |
| 149 | this.oldOnmessage = this.st.conn.onmessage || Function.prototype; |
| 150 | this.st.conn.onmessage = this.socketMessageListener; |
| 151 | const oldOnclose = this.st.conn.onclose; |
| 152 | const socketCloseListener = () => { |
| 153 | oldOnclose(); //reinitialize connexion (in store.js) |
| 154 | this.st.conn.addEventListener('message', this.socketMessageListener); |
| 155 | this.st.conn.addEventListener('close', socketCloseListener); |
| 156 | }; |
| 157 | this.st.conn.onclose = socketCloseListener; |
| 158 | }, |
| 159 | methods: { |
| 160 | filterChallenges: function(type) { |
| 161 | return this.challenges.filter(c => c.type == type); |
| 162 | }, |
| 163 | filterGames: function(type) { |
| 164 | return this.games.filter(c => c.type == type); |
| 165 | }, |
| 166 | classifyChallenge: function(c) { |
| 167 | // Heuristic: should work for most cases... (TODO) |
| 168 | return (c.timeControl.indexOf('d') === -1 ? "live" : "corr"); |
| 169 | }, |
| 170 | socketMessageListener: function(msg) { |
| 171 | // Save and call current st.conn.onmessage if one was already defined |
| 172 | // --> also needed in future Game.vue (also in Chat.vue component) |
| 173 | this.oldOnmessage(msg); |
| 174 | const data = JSON.parse(msg.data); |
| 175 | switch (data.code) |
| 176 | { |
| 177 | // 0.2] Receive clients list (just socket IDs) |
| 178 | case "pollclients": |
| 179 | { |
| 180 | data.sockIds.forEach(sid => { |
| 181 | this.players.push({sid:sid, id:0, name:""}); |
| 182 | // Ask identity, challenges and game(s) |
| 183 | this.st.conn.send(JSON.stringify({code:"askidentity", target:sid})); |
| 184 | this.st.conn.send(JSON.stringify({code:"askchallenges", target:sid})); |
| 185 | this.st.conn.send(JSON.stringify({code:"askgame", target:sid})); |
| 186 | }); |
| 187 | break; |
| 188 | } |
| 189 | case "askidentity": |
| 190 | { |
| 191 | // Request for identification: reply if I'm not anonymous |
| 192 | if (this.st.user.id > 0) |
| 193 | { |
| 194 | this.st.conn.send(JSON.stringify( |
| 195 | {code:"identity", user:this.st.user, target:data.from})); |
| 196 | } |
| 197 | break; |
| 198 | } |
| 199 | case "askchallenge": |
| 200 | { |
| 201 | // Send my current live challenge (if any) |
| 202 | const cIdx = this.challenges |
| 203 | .findIndex(c => c.from.sid == this.st.user.sid && c.type == "live"); |
| 204 | if (cIdx >= 0) |
| 205 | { |
| 206 | const c = this.challenges[cIdx]; |
| 207 | const myChallenge = |
| 208 | { |
| 209 | // Minimal challenge informations: (from not required) |
| 210 | to: c.to, |
| 211 | fen: c.fen, |
| 212 | vid: c.vid, |
| 213 | timeControl: c.timeControl |
| 214 | }; |
| 215 | this.st.conn.send(JSON.stringify({code:"challenge", |
| 216 | challenge:myChallenge, target:data.from})); |
| 217 | } |
| 218 | break; |
| 219 | } |
| 220 | case "askgame": |
| 221 | { |
| 222 | // TODO: Send my current live game (if any): variant, players, movesCount |
| 223 | break; |
| 224 | } |
| 225 | case "identity": |
| 226 | { |
| 227 | const pIdx = this.players.findIndex(p => p.sid == data.user.sid); |
| 228 | this.players[pIdx].id = data.user.id; |
| 229 | this.players[pIdx].name = data.user.name; |
| 230 | break; |
| 231 | } |
| 232 | case "challenge": |
| 233 | { |
| 234 | // Receive challenge from some player (+sid) |
| 235 | let newChall = data.chall; |
| 236 | newChall.type = this.classifyChallenge(data.chall); |
| 237 | const pIdx = this.players.findIndex(p => p.sid == data.from); |
| 238 | newChall.from = this.players[pIdx]; //may be anonymous |
| 239 | newChall.added = Date.now(); |
| 240 | newChall.vname = this.getVname(newChall.vid); |
| 241 | this.challenges.push(newChall); |
| 242 | break; |
| 243 | } |
| 244 | case "game": |
| 245 | { |
| 246 | // Receive game from some player (+sid) |
| 247 | // TODO: receive game summary (update, count moves) |
| 248 | // (just players names, time control, and ID + player ID) |
| 249 | // NOTE: it may be correspondance (if newgame while we are connected) |
| 250 | break; |
| 251 | } |
| 252 | // * - receive "new game": if live, store locally + redirect to game |
| 253 | // * If corr: notify "new game has started", give link, but do not redirect |
| 254 | case "newgame": |
| 255 | { |
| 256 | // TODO: new game just started: data contain all informations |
| 257 | // (id, players, time control, fenStart ...) |
| 258 | // + cid to remove challenge from list |
| 259 | break; |
| 260 | } |
| 261 | // * - receive "accept/withdraw/cancel challenge": apply action to challenges list |
| 262 | case "acceptchallenge": |
| 263 | { |
| 264 | // Someone accept an open (or targeted) challenge |
| 265 | // TODO: keep SIDs, since we need them to notify newgame after chall is complete |
| 266 | const cIdx = this.challenges.findIndex(c => c.id == data.cid); |
| 267 | let players = this.challenges[cIdx].to; |
| 268 | const pIdx = this.players.findIndex(p => p.sid == data.from); |
| 269 | for (let i=0; i<players.length; i++) |
| 270 | { |
| 271 | if (!players[i]) |
| 272 | { |
| 273 | players[i] = this.players[pIdx].name; |
| 274 | break; |
| 275 | } |
| 276 | } |
| 277 | // TODO: if challenge is complete, launch game |
| 278 | //this.newGame(data.challenge, data.user); //user.id et user.name |
| 279 | break; |
| 280 | } |
| 281 | case "withdrawchallenge": |
| 282 | { |
| 283 | const cIdx = this.challenges.findIndex(c => c.id == data.cid); |
| 284 | let chall = this.challenges[cIdx] |
| 285 | ArrayFun.remove(chall.players, p => p.id == data.uid); |
| 286 | chall.players.push({id:0, name:""}); |
| 287 | break; |
| 288 | } |
| 289 | case "refusechallenge": |
| 290 | { |
| 291 | // TODO: show "player XXX refused challenge", and |
| 292 | // remove challenge from list. |
| 293 | break; |
| 294 | } |
| 295 | case "deletechallenge": |
| 296 | { |
| 297 | ArrayFun.remove(this.challenges, c => c.id == data.cid); |
| 298 | break; |
| 299 | } |
| 300 | // TODO: distinguish hallConnect and gameConnect ? |
| 301 | // Or global variable players |
| 302 | // + game variable: "observers" |
| 303 | case "connect": |
| 304 | // * - receive "player connect": send our current challenge (to him or global) |
| 305 | // * Also send all our games (live - max 1 - and corr) [in web worker ?] |
| 306 | { |
| 307 | this.players.push({name:"", id:0, sid:data.sid}); |
| 308 | this.st.conn.send(JSON.stringify({code:"askidentity", target:data.sid})); |
| 309 | break; |
| 310 | } |
| 311 | // * - receive "player disconnect": remove from players list |
| 312 | case "disconnect": |
| 313 | { |
| 314 | ArrayFun.remove(this.players, p => p.sid == data.sid); |
| 315 | // TODO: also remove all challenges sent by this player, |
| 316 | // and all live games where he plays and no other opponent is online |
| 317 | break; |
| 318 | } |
| 319 | } |
| 320 | }, |
| 321 | showGame: function(game) { |
| 322 | // NOTE: if we are an observer, the game will be found in main games list |
| 323 | // (sent by connected remote players) |
| 324 | // TODO: game path ? /vname/gameId seems better |
| 325 | this.$router.push("/" + game.id); |
| 326 | }, |
| 327 | tryChallenge: function(player) { |
| 328 | if (player.id == 0) |
| 329 | return; //anonymous players cannot be challenged |
| 330 | this.newchallenge.to[0] = player.name; |
| 331 | doClick("modalNewgame"); |
| 332 | }, |
| 333 | // * - accept challenge (corr or live) --> send info to challenge creator |
| 334 | // * - cancel challenge (click on sent challenge) --> send info to all concerned players |
| 335 | // * - withdraw from challenge (if >= 3 players and previously accepted) |
| 336 | // * --> send info to challenge creator |
| 337 | // * - refuse challenge: send "refuse" to challenge sender, and "delete" to others |
| 338 | // * - prepare and start new game (if challenge is full after acceptation) |
| 339 | // * --> include challenge ID (so that opponents can delete the challenge too) |
| 340 | clickChallenge: function(c) { |
| 341 | // TODO: also correspondance case (send to server) |
| 342 | if (!!c.accepted) |
| 343 | { |
| 344 | // It's a multiplayer challenge I accepted: withdraw |
| 345 | this.sendSomethingTo(c.from.sid, "withdrawchallenge", |
| 346 | {cid:c.id, user:this.st.user.sid}); |
| 347 | c.accepted = false; |
| 348 | } |
| 349 | else if (c.from.sid == this.st.user.sid) //it's my challenge: cancel it |
| 350 | { |
| 351 | this.sendSomethingTo(c.to, "deletechallenge", {cid:c.id}); |
| 352 | ArrayFun.remove(this.challenges, ch => ch.id == c.id); |
| 353 | } |
| 354 | else //accept (or refuse) a challenge |
| 355 | { |
| 356 | c.accepted = true; |
| 357 | if (!!c.to[0]) |
| 358 | { |
| 359 | // TODO: if special FEN, show diagram after loading variant |
| 360 | c.accepted = confirm("Accept challenge?"); |
| 361 | } |
| 362 | this.sendSomethingTo(c.from.sid, |
| 363 | (c.accepted ? "accept" : "refuse") + "challenge", {cid: c.id}); |
| 364 | if (!c.accepted) |
| 365 | ArrayFun.remove(this.challenges, ch => ch.id == c.id); |
| 366 | } |
| 367 | }, |
| 368 | // newGame: function(chall, user) { |
| 369 | // const fen = chall.fen || V.GenRandInitFen(); |
| 370 | // const game = {}; //TODO: fen, players, time ... |
| 371 | // //setStorage(game); //TODO |
| 372 | // game.players.forEach(p => { //...even if game is by corr (could be played live, why not...) |
| 373 | // this.conn.send( |
| 374 | // JSON.stringify({code:"newgame", oppid:p.id, game:game})); |
| 375 | // }); |
| 376 | // if (this.settings.sound >= 1) |
| 377 | // new Audio("/sounds/newgame.mp3").play().catch(err => {}); |
| 378 | // }, |
| 379 | getVname: function(vid) { |
| 380 | const vIdx = this.st.variants.findIndex(v => v.id == vid); |
| 381 | return this.st.variants[vIdx].name; |
| 382 | }, |
| 383 | sendSomethingTo: function(to, code, obj, warnDisconnected) { |
| 384 | const doSend = (code, obj, sid) => { |
| 385 | this.st.conn.send(JSON.stringify(Object.assign( |
| 386 | {}, |
| 387 | {code: code}, |
| 388 | obj, |
| 389 | {target: sid} |
| 390 | ))); |
| 391 | }; |
| 392 | const getSid = (pname) => { |
| 393 | const pIdx = this.players.findIndex(pl => pl.name == pname); |
| 394 | if (!!warnDisconnected && ctype == "live" && pIdx === -1) |
| 395 | alert("Warning: " + p.name + " is not connected"); |
| 396 | return this.players[pIdx].sid; |
| 397 | }; |
| 398 | if (!Array.isArray(to)) //pass sid directly |
| 399 | doSend(code, obj, to); |
| 400 | else if (!!to[0]) |
| 401 | { |
| 402 | // Challenge with targeted players |
| 403 | to.forEach(pname => { doSend(code, obj, getSid(pname)); }); |
| 404 | } |
| 405 | else |
| 406 | { |
| 407 | // Open challenge: send to all connected players (except us) |
| 408 | this.players.forEach(p => { |
| 409 | if (p.sid != this.st.user.sid) //only sid is always set |
| 410 | doSend(code, obj, p.sid); |
| 411 | }); |
| 412 | } |
| 413 | }, |
| 414 | // Send new challenge (corr or live, cf. time control), with button or click on player |
| 415 | newChallenge: async function() { |
| 416 | // TODO: put this "load variant" block elsewhere |
| 417 | const vname = this.getVname(this.newchallenge.vid); |
| 418 | const vModule = await import("@/variants/" + vname + ".js"); |
| 419 | window.V = vModule.VariantRules; |
| 420 | const error = checkChallenge(this.newchallenge); |
| 421 | if (!!error) |
| 422 | return alert(error); |
| 423 | const ctype = this.classifyChallenge(this.newchallenge); |
| 424 | const cto = this.newchallenge.to.slice(0, this.newchallenge.nbPlayers); |
| 425 | // NOTE: "from" information is not required here |
| 426 | let chall = |
| 427 | { |
| 428 | fen: this.newchallenge.fen || V.GenRandInitFen(), |
| 429 | to: cto, |
| 430 | timeControl: this.newchallenge.timeControl, |
| 431 | vid: this.newchallenge.vid, |
| 432 | }; |
| 433 | const finishAddChallenge = (cid) => { |
| 434 | chall.id = cid || "c" + getRandString(); |
| 435 | // Send challenge to peers |
| 436 | this.sendSomethingTo(cto, "challenge", {chall:chall}, "warnDisconnected"); |
| 437 | chall.added = Date.now(); |
| 438 | chall.type = ctype; |
| 439 | chall.vname = vname; |
| 440 | chall.from = this.st.user; |
| 441 | this.challenges.push(chall); |
| 442 | document.getElementById("modalNewgame").checked = false; |
| 443 | }; |
| 444 | const cIdx = this.challenges.findIndex( |
| 445 | c => c.from.sid == this.st.user.sid && c.type == ctype); |
| 446 | if (cIdx >= 0) |
| 447 | { |
| 448 | // Delete current challenge (will be replaced now) |
| 449 | this.sendSomethingTo(this.challenges[cIdx].to, |
| 450 | "deletechallenge", {cid:this.challenges[cIdx].id}); |
| 451 | if (ctype == "corr") |
| 452 | { |
| 453 | ajax( |
| 454 | "/challenges", |
| 455 | "DELETE", |
| 456 | {id: this.challenges[cIdx].id} |
| 457 | ); |
| 458 | } |
| 459 | this.challenges.splice(cIdx, 1); |
| 460 | } |
| 461 | if (ctype == "live") |
| 462 | { |
| 463 | // Live challenges have a random ID |
| 464 | finishAddChallenge(); |
| 465 | } |
| 466 | else |
| 467 | { |
| 468 | // Correspondance game: send challenge to server |
| 469 | ajax( |
| 470 | "/challenges", |
| 471 | "POST", |
| 472 | chall, |
| 473 | response => { finishAddChallenge(response.cid); } |
| 474 | ); |
| 475 | } |
| 476 | }, |
| 477 | possibleNbplayers: function(nbp) { |
| 478 | if (this.newchallenge.vid == 0) |
| 479 | return false; |
| 480 | const variants = this.st.variants; |
| 481 | const idxInVariants = |
| 482 | variants.findIndex(v => v.id == this.newchallenge.vid); |
| 483 | return NbPlayers[variants[idxInVariants].name].includes(nbp); |
| 484 | }, |
| 485 | newGame: function(cid) { |
| 486 | // TODO: don't forget to send "deletechallenge" message to all concerned players |
| 487 | // + setup colors and send game infos to players (message "newgame") |
| 488 | }, |
| 489 | }, |
| 490 | }; |
| 491 | </script> |
| 492 | |
| 493 | <style lang="sass"> |
| 494 | // TODO |
| 495 | </style> |