From: Benjamin Auder Date: Mon, 11 Feb 2019 17:15:11 +0000 (+0100) Subject: Implemented players discovery X-Git-Url: https://git.auder.net/assets/%7B%7B%20asset%28%27mixstore/css/doc/html/index.html?a=commitdiff_plain;h=5a3da96860d36e5131cc5c7c761ac46fb6787685;p=vchess.git Implemented players discovery --- diff --git a/client/src/views/Hall.vue b/client/src/views/Hall.vue index ba3ad41a..ba4e84ab 100644 --- a/client/src/views/Hall.vue +++ b/client/src/views/Hall.vue @@ -112,7 +112,7 @@ export default { // also ask for corr challenges // Ask server for for room composition: const socketOpenListener = () => { - this.st.conn.send(JSON.stringify({code:"askplayers"})); + this.st.conn.send(JSON.stringify({code:"askclients"})); }; this.st.conn.onopen = socketOpenListener; this.st.conn.onmessage = this.socketMessageListener; @@ -128,9 +128,25 @@ export default { const data = JSON.parse(msg.data); switch (data.code) { - case "room": - // TODO: receive room composition (sids at least, id + names if registered) - // TODO: also receive "askchallenges", "askgames" + 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": @@ -153,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": @@ -174,13 +191,14 @@ 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; @@ -305,32 +323,30 @@ export default { const finishAddChallenge = () => { this.challenges.push(chall); // Send challenge to peers - const challSock = + let challSock = { code: "newchallenge", 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 chall.to.forEach(p => { if (p.id > 0) - { - this.st.conn.send(JSON.stringify(Object.assign( - {}, - challSock, - {receiver: p.sid} - ))); - } + sendChallengeTo(p.sid); }); } else { // Open challenge: send to all connected players (except us) - const strChallSock = JSON.stringify(challSock); this.players.forEach(p => { if (p.sid != this.st.user.sid) //only sid is always set - this.st.conn.send(strChallSock); + sendChallengeTo(p.sid); }); } document.getElementById("modalNewgame").checked = false; diff --git a/server/sockets.js b/server/sockets.js index 7db8ed51..333fe77b 100644 --- a/server/sockets.js +++ b/server/sockets.js @@ -37,44 +37,56 @@ module.exports = function(wss) { if (!!clients[sid]) return socket.send(JSON.stringify({code:"duplicate"})); clients[sid] = socket; - socket.on("message", objtxt => { + // Notify room: + Object.keys(clients).forEach(k => { + if (k != sid) + clients[k].send(JSON.stringify({code:"connect",sid:sid})); + }); + socket.on("message", objtxt => { let obj = JSON.parse(objtxt); - if (!!obj.oppid && !clients[oppid]) + if (!!obj.target && !clients[obj.target]) return; //receiver not connected, nothing we can do + //console.log(obj.code); switch (obj.code) { - case "askplayers": - socket.send(JSON.stringify({code:"room", players:clients})); + case "askclients": + socket.send(JSON.stringify({code:"clients", sockIds:Object.keys(clients).filter(k => k != sid)})); + break; + case "askidentity": + clients[obj.target].send(JSON.stringify({code:"identify",from:sid})); + break; + case "identity": + clients[obj.target].send(JSON.stringify({code:"identity",user:obj.user})); break; case "askchallenges": // TODO: ask directly to people (webRTC) + // TODO... + clarify socket system break; + case "newchallenge": + clients[obj.target].send(JSON.stringify({code:"newchallenge",chall:obj.chall})); case "askgames": // TODO: ask directly to people (webRTC) break; case "newchat": - clients[obj.oppid].send(JSON.stringify({code:"newchat",msg:obj.msg})); + clients[obj.target].send(JSON.stringify({code:"newchat",msg:obj.msg})); break; // Transmit chats and moves to current room // TODO: WebRTC instead in this case (most demanding?) case "newmove": - clients[obj.oppid].send(JSON.stringify({code:"newmove",move:obj.move})); + clients[obj.target].send(JSON.stringify({code:"newmove",move:obj.move})); break; // TODO: generalize that for several opponents case "ping": socket.send(JSON.stringify({code:"pong",gameId:obj.gameId})); break; case "lastate": - const oppId = obj.oppid; - obj.oppid = sid; //I'm oppid for my opponent + const oppId = obj.target; + obj.oppid = sid; //I'm the opponent of my opponent(s) clients[oppId].send(JSON.stringify(obj)); break; // TODO: moreover, here, game info should be sent (through challenge; not stored here) case "newgame": - clients[oppId].send( - JSON.stringify( - {code:"newgame",fen:fen,oppid:sid,color:"w",gameid:"TODO"}), - noop); + clients[obj.target].send(JSON.stringify({code:"newgame", game:obj.game})); break; case "cancelnewgame": //if a user cancel his seek // TODO: just transmit event @@ -82,7 +94,7 @@ module.exports = function(wss) { break; // TODO: also other challenge events case "resign": - clients[obj.oppid].send(JSON.stringify({code:"resign"})); + clients[obj.target].send(JSON.stringify({code:"resign"})); break; // TODO: case "challenge" (get ID) --> send to all, "acceptchallenge" (with ID) --> send to all, "cancelchallenge" --> send to all // also, "sendgame" (give current game info, if any) --> to new connections, "sendchallenges" (same for challenges) --> to new connections