| 1 | // main playing hall: chat + online players + current challenges + button "new game" |
| 2 | // TODO: my-challenge-list, gérant clicks sur challenges, affichage, réception/émission des infos sur challenges ; de même, my-player-list |
| 3 | // TODO: si on est en train de jouer une partie, le notifier aux nouveaux connectés |
| 4 | /* |
| 5 | TODO: surligner si nouveau défi perso et pas affichage courant |
| 6 | (cadences base + incrément, corr == incr >= 1jour ou base >= 7j) |
| 7 | --> correspondance: stocker sur serveur lastMove + uid + color + movesCount + gameId + variant + timeleft |
| 8 | fin de partie corr: supprimer partie du serveur au bout de 7 jours (arbitraire) |
| 9 | main time should be positive (no 0+2 & cie...) |
| 10 | */ |
| 11 | // TODO: au moins l'échange des coups en P2P ? |
| 12 | // TODO: objet game, objet challenge ? et player ? |
| 13 | Vue.component('my-room', { |
| 14 | props: ["conn","settings"], |
| 15 | data: { |
| 16 | gdisplay: "live", |
| 17 | liveGames: [], |
| 18 | corrGames: [], |
| 19 | players: [], //online players |
| 20 | challenges: [], //live challenges |
| 21 | }, |
| 22 | // Modal new game, and then sub-components |
| 23 | template: ` |
| 24 | <div> |
| 25 | <input id="modalNewgame" type="checkbox" class"="modal"/> |
| 26 | <div role="dialog" aria-labelledby="titleFenedit"> |
| 27 | <div class="card smallpad"> |
| 28 | <label id="closeNewgame" for="modalNewgame" class="modal-close"> |
| 29 | </label> |
| 30 | <h3 id="titleFenedit" class="section"> |
| 31 | {{ translate("Game state (FEN):") }} |
| 32 | </h3> |
| 33 | <input id="input-fen" type="text"/> |
| 34 | <p>TODO: cadence, adversaire (pre-filled if click on name)</p> |
| 35 | <p>cadence 2m+12s ou 7d+1d (m,s ou d,d) --> main, increment</p> |
| 36 | <p>Note: leave FEN blank for random; FEN only for targeted challenge</p> |
| 37 | <button @click="newGame">Launch game</button> |
| 38 | </div> |
| 39 | </div> |
| 40 | <div> |
| 41 | <my-chat :conn="conn" :myname="myname" :people="people"></my-chat> |
| 42 | <my-challenge-list :challenges="challenges" @click-challenge="clickChallenge"> |
| 43 | </my-challenge-list> |
| 44 | </div> |
| 45 | <button onClick="doClick('modalNewgame')">New game</button> |
| 46 | <div> |
| 47 | <div> |
| 48 | <div v-for="p in players" @click="challenge(p)"> |
| 49 | {{ p.name }} |
| 50 | </div> |
| 51 | </div> |
| 52 | <div class="button-group"> |
| 53 | <button @click="gdisplay='live'>Live games</button> |
| 54 | <button @click="gdisplay='corr'>Correspondance games</button> |
| 55 | </div> |
| 56 | <my-game-list v-show="display=='live'" :games="liveGames" @show-game="showGame"> |
| 57 | </my-game-list> |
| 58 | <my-game-list v-show="display=='corr'" :games="corrGames" @show-game="showGame"> |
| 59 | </my-game-list> |
| 60 | </div> |
| 61 | </div> |
| 62 | `, |
| 63 | created: function() { |
| 64 | // TODO: ask server for current corr games (all but mines: names, ID, time control) |
| 65 | const socketMessageListener = msg => { |
| 66 | const data = JSON.parse(msg.data); |
| 67 | switch (data.code) |
| 68 | { |
| 69 | case "newgame": |
| 70 | // TODO: new game just started: data contain all informations |
| 71 | // (id, players, time control, fenStart ...) |
| 72 | break; |
| 73 | // TODO: also receive live games summaries (update) |
| 74 | // (just players names, time control, and ID + player ID) |
| 75 | case "acceptchallenge": |
| 76 | // oppid: opponent socket ID (or DB id if registered) |
| 77 | if (true) //TODO: if challenge is full |
| 78 | this.newGame(data.challenge, data.user); //user.id et user.name |
| 79 | break; |
| 80 | case "withdrawchallenge": |
| 81 | // TODO |
| 82 | break; |
| 83 | case "cancelchallenge": |
| 84 | // TODO |
| 85 | break; |
| 86 | // TODO: distinguish these (dis)connect events from their analogs in game.js |
| 87 | case "connect": |
| 88 | this.players.push({name:data.name, id:data.uid}); |
| 89 | break; |
| 90 | case "disconnect": |
| 91 | const pIdx = this.players.findIndex(p => p.id == data.uid); |
| 92 | this.players.splice(pIdx); |
| 93 | break; |
| 94 | } |
| 95 | }; |
| 96 | const socketCloseListener = () => { |
| 97 | this.conn.addEventListener('message', socketMessageListener); |
| 98 | this.conn.addEventListener('close', socketCloseListener); |
| 99 | }; |
| 100 | this.conn.onmessage = socketMessageListener; |
| 101 | this.conn.onclose = socketCloseListener; |
| 102 | }, |
| 103 | methods: { |
| 104 | showGame: function(game) { |
| 105 | let hash = "#game?id=" + game.id; |
| 106 | if (!!game.uid) |
| 107 | hash += "&uid=" + game.uid; |
| 108 | location.hash = hash; |
| 109 | }, |
| 110 | challenge: function(player) { |
| 111 | this.conn.send(JSON.stringify({code:"sendchallenge", oppid:p.id, |
| 112 | user:{name:user.name,id:user.id})); |
| 113 | }, |
| 114 | clickChallenge: function(challenge) { |
| 115 | const index = this.challenges.findIndex(c => c.id == challenge.id); |
| 116 | const toIdx = challenge.to.findIndex(p => p.id == user.id); |
| 117 | const me = {name:user.name,id:user.id}; |
| 118 | if (toIdx >= 0) |
| 119 | { |
| 120 | // It's a multiplayer challenge I accepted: withdraw |
| 121 | this.conn.send(JSON.stringify({code:"withdrawchallenge", |
| 122 | cid:challenge.id, user:me})); |
| 123 | this.challenges.to.splice(toIdx, 1); |
| 124 | } |
| 125 | else if (challenge.from.id == user.id) //it's my challenge: cancel it |
| 126 | { |
| 127 | this.conn.send(JSON.stringify({code:"cancelchallenge", cid:challenge.id})); |
| 128 | this.challenges.splice(index, 1); |
| 129 | } |
| 130 | else //accept a challenge |
| 131 | { |
| 132 | this.conn.send(JSON.stringify({code:"acceptchallenge", |
| 133 | cid:challenge.id, user:me})); |
| 134 | this.challenges[index].to.push(me); |
| 135 | } |
| 136 | }, |
| 137 | // user: last person to accept the challenge |
| 138 | newGame: function(chall, user) { |
| 139 | const fen = chall.fen || VariantRules.GenRandInitFen(); |
| 140 | const game = {}; //TODO: fen, players, time ... |
| 141 | //setStorage(game); //TODO |
| 142 | game.players.forEach(p => { |
| 143 | this.conn.send(JSON.stringify({code:"newgame", oppid:p.id, game:game}); |
| 144 | }); |
| 145 | if (this.settings.sound >= 1) |
| 146 | new Audio("/sounds/newgame.mp3").play().catch(err => {}); |
| 147 | }, |
| 148 | }, |
| 149 | }); |