From: Benjamin Auder Date: Mon, 11 Feb 2019 11:21:35 +0000 (+0100) Subject: Save state (nothing really achieved) X-Git-Url: https://git.auder.net/doc/html/scripts/getGraph_%22%20%20%20this.image%20%20%20%22.php?a=commitdiff_plain;h=4d64881e3b2dc55fe260a53195f9f3bc2e959fdf;p=vchess.git Save state (nothing really achieved) --- diff --git a/client/src/components/Board.vue b/client/src/components/Board.vue index c36ea8ae..8392cfe4 100644 --- a/client/src/components/Board.vue +++ b/client/src/components/Board.vue @@ -2,6 +2,8 @@ // This can work for squared boards (2 or 4 players), with some adaptations (TODO) // TODO: for 3 players, write a "board3.js" +// TODO: current clicked square + moving square as parameters, + highlight + import { getSquareId, getSquareFromId } from "@/utils/squareId"; import { ArrayFun } from "@/utils/array"; diff --git a/client/src/views/Hall.vue b/client/src/views/Hall.vue index 2e17ceb0..ba3ad41a 100644 --- a/client/src/views/Hall.vue +++ b/client/src/views/Hall.vue @@ -92,35 +92,36 @@ 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; - // Ask server for for room composition: - this.st.conn.send(JSON.stringify({code:"askplayers"})); - }, - }, 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 - if (!!this.st.conn) - { - this.st.conn.onmessage = this.socketMessageListener; - this.st.conn.onclose = this.socketCloseListener; - // Ask server for for room composition: + // Ask server for for room composition: + const socketOpenListener = () => { this.st.conn.send(JSON.stringify({code:"askplayers"})); - } + }; + 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) { @@ -129,8 +130,6 @@ export default { { case "room": // TODO: receive room composition (sids at least, id + names if registered) - // Add myself to players - this.players.push(this.st.user); // TODO: also receive "askchallenges", "askgames" // * - receive "new game": if live, store locally + redirect to game // * If corr: notify "new game has started", give link, but do not redirect @@ -187,11 +186,6 @@ export default { 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) diff --git a/server/sockets.js b/server/sockets.js index 1d2f9400..7db8ed51 100644 --- a/server/sockets.js +++ b/server/sockets.js @@ -29,9 +29,7 @@ function remInArray(arr, item) //TODO: programmatic re-navigation on current game if we receive a move and are not there module.exports = function(wss) { - let clients = {}; //associative array client sid --> socket - // No-op function as a callback when sending messages - const noop = () => { }; + let clients = {}; //associative array sid --> socket wss.on("connection", (socket, req) => { const query = getJsonFromUrl(req.url); const sid = query["sid"]; @@ -45,13 +43,22 @@ module.exports = function(wss) { return; //receiver not connected, nothing we can do switch (obj.code) { - // Transmit chats and moves to current room - // TODO: WebRTC instead in this case (most demanding?) + case "askplayers": + socket.send(JSON.stringify({code:"room", players:clients})); + break; + case "askchallenges": + // TODO: ask directly to people (webRTC) + break; + case "askgames": + // TODO: ask directly to people (webRTC) + break; case "newchat": - clients[obj.oppid].send(JSON.stringify({code:"newchat",msg:obj.msg}), noop); + clients[obj.oppid].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}), noop); + clients[obj.oppid].send(JSON.stringify({code:"newmove",move:obj.move})); break; // TODO: generalize that for several opponents case "ping": @@ -60,7 +67,7 @@ module.exports = function(wss) { case "lastate": const oppId = obj.oppid; obj.oppid = sid; //I'm oppid for my opponent - clients[oppId].send(JSON.stringify(obj), noop); + clients[oppId].send(JSON.stringify(obj)); break; // TODO: moreover, here, game info should be sent (through challenge; not stored here) case "newgame": @@ -75,7 +82,7 @@ module.exports = function(wss) { break; // TODO: also other challenge events case "resign": - clients[obj.oppid].send(JSON.stringify({code:"resign"}), noop); + clients[obj.oppid].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 @@ -90,7 +97,7 @@ module.exports = function(wss) { delete clients[sid]; // Notify every other connected client Object.keys(clients).forEach( k => { - clients[k].send(JSON.stringify({code:"disconnect",sid:sid}), noop); + clients[k].send(JSON.stringify({code:"disconnect",sid:sid})); }); }); });