X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=client%2Fsrc%2Fviews%2FHall.vue;h=ba4e84abfd706751287109e557f69b6e6d2c3af1;hb=5a3da96860d36e5131cc5c7c761ac46fb6787685;hp=b94d85b504852b283b25037386b09f8b81fd325f;hpb=6faa92f2f81bc0ac6a937222cb8ef3a64ac6acd8;p=vchess.git diff --git a/client/src/views/Hall.vue b/client/src/views/Hall.vue index b94d85b5..ba4e84ab 100644 --- a/client/src/views/Hall.vue +++ b/client/src/views/Hall.vue @@ -92,40 +92,61 @@ export default { computed: { uniquePlayers: function() { // Show e.g. "5 @nonymous", and do nothing on click on anonymous - let playerList = [{id:0, name:"@nonymous", count:0}]; + let anonymous = {id:0, name:"@nonymous", count:0}; + let playerList = []; this.players.forEach(p => { if (p.id > 0) playerList.push(p); else - playerList[0].count++; + anonymous.count++; }); + if (anonymous.count > 0) + playerList.push(anonymous); return playerList; }, }, - // TODO: this looks ugly... (use VueX ?!) - watch: { - "st.conn": function() { - this.st.conn.onmessage = this.socketMessageListener; - this.st.conn.onclose = this.socketCloseListener; - }, - }, created: function() { + // Always add myself to players' list + this.players.push(this.st.user); // TODO: ask server for current corr games (all but mines: names, ID, time control) // also ask for corr challenges - // TODO: add myself to players - // --> when sending something, send to all players but NOT me ! - if (!!this.st.conn) - { - this.st.conn.onmessage = this.socketMessageListener; - this.st.conn.onclose = this.socketCloseListener; - } - this.players.push(this.st.user); + // Ask server for for room composition: + const socketOpenListener = () => { + this.st.conn.send(JSON.stringify({code:"askclients"})); + }; + this.st.conn.onopen = socketOpenListener; + this.st.conn.onmessage = this.socketMessageListener; + const socketCloseListener = () => { + // connexion is reinitialized in store.js + this.st.conn.addEventListener('message', this.socketMessageListener); + this.st.conn.addEventListener('close', socketCloseListener); + }; + this.st.conn.onclose = socketCloseListener; }, methods: { socketMessageListener: function(msg) { const data = JSON.parse(msg.data); switch (data.code) { + case "clients": + data.sockIds.forEach(sid => { + this.players.push({sid:sid, id:0, name:""}); + this.st.conn.send(JSON.stringify({code:"askidentity", target:sid})); + }); + break; + // TODO: also receive "askchallenges", "askgames" + case "identify": + // Request for identification + this.st.conn.send(JSON.stringify({code:"identity", user:this.st.user, target:data.from})); + break; + case "identity": + if (data.user.id > 0) + { + const pIdx = this.players.findIndex(p => p.sid == data.user.sid); + this.players[pIdx].id = data.user.id; + this.players[pIdx].name = data.user.name; + } + break; // * - receive "new game": if live, store locally + redirect to game // * If corr: notify "new game has started", give link, but do not redirect case "newgame": @@ -148,6 +169,7 @@ export default { // * - receive new challenge: if targeted, replace our name with sender name case "newchallenge": // receive live or corr challenge + this.challenges.push(data.chall); break; // * - receive "accept/withdraw/cancel challenge": apply action to challenges list case "acceptchallenge": @@ -169,23 +191,19 @@ export default { // * - receive "player connect": send all our current challenges (to him or global) // * Also send all our games (live - max 1 - and corr) [in web worker ?] // * + all our sent challenges. - this.players.push({name:data.name, id:data.uid}); + this.players.push({name:"", id:0, sid:data.sid}); + this.st.conn.send(JSON.stringify({code:"askidentity", target:data.sid})); // TODO: si on est en train de jouer une partie, le notifier au nouveau connecté // envoyer aussi nos défis break; // * - receive "player disconnect": remove from players list case "disconnect": - ArrayFun.remove(this.players, p => p.id == data.uid); + ArrayFun.remove(this.players, p => p.sid == data.sid); // TODO: also remove all challenges sent by this player, // and all live games where he plays and no other opponent is online break; } }, - socketCloseListener: function() { - // connexion is reinitialized in store.js - this.st.conn.addEventListener('message', this.socketMessageListener); - this.st.conn.addEventListener('close', this.socketCloseListener); - }, showGame: function(game) { // NOTE: if we are an observer, the game will be found in main games list // (sent by connected remote players) @@ -252,6 +270,11 @@ export default { // }, // Send new challenge (corr or live, cf. time control), with button or click on player newChallenge: async function() { + if (this.challenges.some(c => c.from.sid == this.st.user.sid)) + { + document.getElementById("modalNewgame").checked = false; + return alert("You already have a pending challenge"); + } const idxInVariants = this.st.variants.findIndex(v => v.id == this.newchallenge.vid); const vname = this.st.variants[idxInVariants].name; @@ -265,58 +288,73 @@ export default { const liveGame = this.newchallenge.mainTime + 40 * this.newchallenge.increment < 3*24*60*60; // Check that the players (if any indicated) are online - for (let p of this.newchallenge.to) + let chall = + { + id: 0, //unknown yet (no ID for live challenges) + from: this.st.user, + added: Date.now(), + fen: this.newchallenge.fen, + variant: {id: this.newchallenge.vid, name: vname}, + nbPlayers: this.newchallenge.nbPlayers, + to: [ + {id: 0, name: this.newchallenge.to[0], sid: ""}, + {id: 0, name: this.newchallenge.to[1], sid: ""}, + {id: 0, name: this.newchallenge.to[2], sid: ""}, + ], + timeControl: this.newchallenge.timeControl, + mainTime: this.newchallenge.mainTime, + increment: this.newchallenge.increment, + }; + for (let p of chall.to) { if (p.name != "") { const pIdx = this.players.findIndex(pl => pl.name == p.name); - // TODO: for correspondance play we don't require players to be online - // (==> we don't have IDs, and no sid) // NOTE: id (server DB) and sid (socket ID). // Anonymous players just have a socket ID. - if (pIdx === -1) + // NOTE: for correspondance play we don't require players to be online + // (==> we don't have IDs, and no sid) + if (liveGame && pIdx === -1) return alert(p.name + " is not connected"); p.id = this.players[pIdx].id; p.sid = this.players[pIdx].sid; } } - // TODO: clarify challenge format (too many fields for now :/ ) - const finishAddChallenge = (cid) => { - const chall = Object.assign( - {}, - this.newchallenge, - { - id: cid, - from: this.st.user, - added: Date.now(), - vname: vname, - } - ); + const finishAddChallenge = () => { this.challenges.push(chall); // Send challenge to peers - const chall = JSON.stringify({ + let challSock = + { code: "newchallenge", - sender: {name:this.st.user.name, id:this.st.user.id, sid:this.st.user.sid}, - }); - if (this.newchallenge.to[0].id > 0) + chall: chall, + target: "", + }; + const sendChallengeTo = (sid) => { + challSock.target = sid; + this.st.conn.send(JSON.stringify(challSock)); + }; + if (chall.to[0].id > 0) { // Challenge with targeted players - this.newchallenge.to.forEach(p => { + chall.to.forEach(p => { if (p.id > 0) - this.st.conn.send(Object.assign({}, chall, {receiver: p.sid})); + sendChallengeTo(p.sid); }); } else { - // Open challenge: send to all connected players - this.players.forEach(p => { this.st.conn.send(chall); }); + // Open challenge: send to all connected players (except us) + this.players.forEach(p => { + if (p.sid != this.st.user.sid) //only sid is always set + sendChallengeTo(p.sid); + }); } document.getElementById("modalNewgame").checked = false; }; if (liveGame) { // Live challenges have cid = 0 - finishAddChallenge(0); + finishAddChallenge(); } else { @@ -324,8 +362,11 @@ export default { ajax( "/challenges/" + this.newchallenge.vid, "POST", - this.newchallenge, - response => { finishAddChallenge(cid); } + chall, + response => { + chall.id = response.cid; + finishAddChallenge(); + } ); } },