From 9335d45b03966f433df8dd84ec31e8a22585a97f Mon Sep 17 00:00:00 2001 From: Benjamin Auder Date: Thu, 6 Feb 2020 01:20:03 +0100 Subject: [PATCH] Still logging an issue when transitioning Hall <--> Game (button Available/Playing not updated well) --- client/src/components/BaseGame.vue | 1 + client/src/translations/en.js | 2 + client/src/views/Hall.vue | 105 +++++++++++++---------------- server/sockets.js | 9 +-- 4 files changed, 53 insertions(+), 64 deletions(-) diff --git a/client/src/components/BaseGame.vue b/client/src/components/BaseGame.vue index f64c68f4..523e0ef6 100644 --- a/client/src/components/BaseGame.vue +++ b/client/src/components/BaseGame.vue @@ -81,6 +81,7 @@ export default { this.re_setVariables(); }, // Received a new move to play: + // TODO: error "flush nextTick callbacks" when observer reloads page "game.moveToPlay": function(newMove) { if (!!newMove) //if stop + launch new game, get undefined move this.play(newMove, "receive"); diff --git a/client/src/translations/en.js b/client/src/translations/en.js index 4ee18556..3681a4d4 100644 --- a/client/src/translations/en.js +++ b/client/src/translations/en.js @@ -7,6 +7,7 @@ export const translations = "Analyze": "Analyze", "Analyze in Dark mode makes no sense!": "Analyze in Dark mode makes no sense!", "Apply": "Apply", + "Available": "Available", "Black": "Black", "Black to move": "Black to move", "Black win": "Black win", @@ -55,6 +56,7 @@ export const translations = "Opponent action": "Opponent action", "Play sounds?": "Play sounds?", "Play with?": "Play with?", + "Playing": "Playing", "Please log in to accept corr challenges": "Please log in to accept corr challenges", "Please log in to play corr games": "Please log in to play corr games", "Please select a variant": "Please select a variant", diff --git a/client/src/views/Hall.vue b/client/src/views/Hall.vue index 8cd330ff..e8f7785f 100644 --- a/client/src/views/Hall.vue +++ b/client/src/views/Hall.vue @@ -50,13 +50,13 @@ main #people h3.text-center {{ st.tr["Who's there?"] }} #players - p(v-for="p in Object.values(people)" v-if="!!p.name") - span {{ p.name }} + p(v-for="sid in Object.keys(people)" v-if="!!people[sid].name") + span {{ people[sid].name }} button.player-action( - v-if="p.name != st.user.name" - @click="challOrWatch(p,$event)" + v-if="people[sid].name != st.user.name" + @click="challOrWatch(sid, $event)" ) - | {{ whatPlayerDoes(p) }} + | {{ st.tr[!!people[sid].gamer ? 'Playing' : 'Available'] }} p.anonymous @nonymous ({{ anonymousCount }}) #chat Chat(:newChat="newChat" @mychat="processChat") @@ -253,22 +253,6 @@ export default { // this.st.variants might be uninitialized (variant == null) return (!!variant ? variant.name : ""); }, - -// TODO: now that we can distinguish people from Hall or Game, -// improve this --> the info is already known - - whatPlayerDoes: function(p) { - if (this.games.some(g => g.type == "live" - && g.players.some(pl => pl.sid == p.sid))) - { - return "Playing"; - } - return "Challenge"; //player is available - }, - -// Also debug: when from game going to Hall, player appears still connected. -// when reloading a finished (observed) game, some ghost moveToPlay errors - processChat: function(chat) { // When received on server, this will trigger a "notifyRoom" this.st.conn.send(JSON.stringify({code:"newchat", chat: chat})); @@ -281,21 +265,7 @@ export default { {target: sid} ))); }; - if (!!to) - { - // Challenge with targeted players - const targetSid = - Object.keys(this.people).find(sid => this.people[sid].name == to); - if (!targetSid) - { - if (!!warnDisconnected) - alert(this.st.tr["Warning: target is not connected"]); - return false; - } - else - doSend(code, obj, targetSid); - } - else + if (!to || (!to.sid && !to.name)) { // Open challenge: send to all connected players (me excepted) Object.keys(this.people).forEach(sid => { @@ -303,6 +273,27 @@ export default { doSend(code, obj, sid); }); } + else + { + let targetSid = ""; + if (!!to.sid) + targetSid = to.sid; + else + { + if (to.name == this.st.user.name) + return alert(this.st.tr["Cannot challenge self"]); + // Challenge with targeted players + targetSid = + Object.keys(this.people).find(sid => this.people[sid].name == to.name); + if (!targetSid) + { + if (!!warnDisconnected) + alert(this.st.tr["Warning: target is not connected"]); + return false; + } + } + doSend(code, obj, targetSid); + } return true; }, // Messaging center: @@ -326,7 +317,7 @@ export default { // NOTE: we could make a difference between people in hall // and gamers, but is it necessary? data.sockIds.forEach(sid => { - this.$set(this.people, sid, {id:0, name:""}); + this.$set(this.people, sid, {id:0, name:"", gamer:true}); this.st.conn.send(JSON.stringify({code:"askidentity", target:sid})); }); // Also ask current games to all playing peers (TODO: some design issue) @@ -351,7 +342,11 @@ export default { case "identity": { this.$set(this.people, data.user.sid, - {id: data.user.id, name: data.user.name}); + { + id: data.user.id, + name: data.user.name, + gamer: this.people[data.user.sid].gamer, + }); break; } case "askchallenge": @@ -452,7 +447,8 @@ export default { } case "connect": case "gconnect": - this.$set(this.people, data.from, {name:"", id:0}); +console.log(data.code + " " + data.from); + this.$set(this.people, data.from, {name:"", id:0, gamer:data.code[0]=='g'}); this.st.conn.send(JSON.stringify({code:"askidentity", target:data.from})); if (data.code == "connect") this.st.conn.send(JSON.stringify({code:"askchallenge", target:data.from})); @@ -460,6 +456,8 @@ export default { this.st.conn.send(JSON.stringify({code:"askgame", target:data.from})); break; case "disconnect": + case "pdisconnect": +console.log(data.code + " " + data.from); this.$delete(this.people, data.from); if (data.code == "disconnect") { @@ -477,22 +475,22 @@ export default { } }, // Challenge lifecycle: - tryChallenge: function(player) { - if (player.id == 0) + tryChallenge: function(sid) { + if (this.people[sid].id == 0) return; //anonymous players cannot be challenged - this.newchallenge.to = player.name; + // TODO: SID is available, so we could use it instead of searching from name + this.newchallenge.to = this.people[sid].name; doClick("modalNewgame"); }, - challOrWatch: function(p, e) { + challOrWatch: function(sid, e) { switch (e.target.innerHTML) { case "Challenge": - this.tryChallenge(p); + this.tryChallenge(sid); break; case "Playing": - // NOTE: this search for game was already done for rendering this.showGame(this.games.find( - g => g.type=="live" && g.players.some(pl => pl.sid == p.sid))); + g => g.type=="live" && g.players.some(pl => pl.sid == sid))); break; }; }, @@ -515,7 +513,7 @@ export default { const finishAddChallenge = (cid,warnDisconnected) => { chall.id = cid || "c" + getRandString(); // Send challenge to peers (if connected) - const isSent = this.sendSomethingTo(chall.to, "challenge", + const isSent = this.sendSomethingTo({name:chall.to}, "challenge", {chall:chall}, !!warnDisconnected); if (!isSent) return; @@ -525,7 +523,7 @@ export default { if (cIdx >= 0) { // Delete current challenge (will be replaced now) - this.sendSomethingTo(this.challenges[cIdx].to, + this.sendSomethingTo({name:this.challenges[cIdx].to}, "deletechallenge", {cid:this.challenges[cIdx].id}); if (ctype == "corr") { @@ -599,14 +597,7 @@ export default { code: "refusechallenge", cid: c.id, target: c.from.sid})); } - // TODO: refactor the "sendSomethingTo()" function - if (!c.to) - this.sendSomethingTo(null, "deletechallenge", {cid:c.id}); - else - { - this.st.conn.send(JSON.stringify({ - code:"deletechallenge", target: c.from.sid, cid: c.id})); - } + this.sendSomethingTo(!!c.to ? {sid:c.from.sid} : null, "deletechallenge", {cid:c.id}); } else //my challenge { @@ -620,7 +611,7 @@ export default { } else //live localStorage.removeItem("challenge"); - this.sendSomethingTo(c.to, "deletechallenge", {cid:c.id}); + this.sendSomethingTo({name:c.to}, "deletechallenge", {cid:c.id}); } // In all cases, the challenge is consumed: ArrayFun.remove(this.challenges, ch => ch.id == c.id); diff --git a/server/sockets.js b/server/sockets.js index d296efa1..795ed433 100644 --- a/server/sockets.js +++ b/server/sockets.js @@ -38,10 +38,6 @@ module.exports = function(wss) { let obj = JSON.parse(objtxt); if (!!obj.target && !clients[obj.target]) return; //receiver not connected, nothing we can do - -// TODO: debug -console.log(obj.code + " " + clients[sid].page); - switch (obj.code) { case "connect": @@ -59,15 +55,14 @@ console.log(obj.code + " " + clients[sid].page); break; } case "pollgamers": - { - const curPage = clients[sid].page; socket.send(JSON.stringify({code:"pollgamers", sockIds: Object.keys(clients).filter(k => k != sid && clients[k].page.indexOf("/game/") >= 0 )})); break; - } case "pagechange": + // page change clients[sid].page --> obj.page +console.log("page change: " + clients[sid].page + " " + obj.page + " " + sid); notifyRoom(clients[sid].page, "disconnect"); if (clients[sid].page.indexOf("/game/") >= 0) notifyRoom("/", "gdisconnect"); -- 2.44.0