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