X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=client%2Fsrc%2Fviews%2FHall.vue;h=d0cfdd17c975d54cfbb16163dccdee91a251b1f9;hb=485fccd5c925e796b61d5ed0674f29d742ea1a13;hp=13d9b63398cb2ce07db28e89c6216d0851708275;hpb=1ba761c8fc1dafaaf8e47d08be6b38cdf0766ea3;p=vchess.git diff --git a/client/src/views/Hall.vue b/client/src/views/Hall.vue index 13d9b633..d0cfdd17 100644 --- a/client/src/views/Hall.vue +++ b/client/src/views/Hall.vue @@ -141,7 +141,7 @@ export default { response => { console.log(response.challenges); // TODO: post-treatment on challenges ? - this.challenges = this.challenges.concat(response.challenges); + Array.prototype.push.apply(this.challenges, response.challenges); } ); } @@ -178,6 +178,7 @@ export default { // ==> Moves sent by connected remote player(s) if live game // TODO: this doesn't work: choose a SID at random + // --> do we have players' names ? let url = "/" + g.id; if (g.type == "live") @@ -331,31 +332,17 @@ export default { this.games.push(newGame); 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": { - // Delete corresponding challenge: - ArrayFun.remove(this.challenges, c => c.id == data.cid); // New game just started: data contain all informations this.startNewGame(data.gameInfo); - break; - } -// * - receive "accept/cancel challenge": apply action to challenges list - // NOTE: challenge "socket" actions accept+withdraw only for live challenges - case "acceptchallenge": - { - // Someone accept an open (or targeted) challenge - const cIdx = this.challenges.findIndex(c => c.id == data.cid); - let c = this.challenges[cIdx]; - const pIdx = this.people.findIndex(p => p.sid == data.from); - c.seat = this.people[pIdx]; - this.launchGame(c); + // TODO: if live, redirect to game + // if corr, notify with link but do not redirect break; } case "refusechallenge": { - alert(this.getPname(data.from) + " refused your challenge"); + alert(this.getPname(data.from) + " declined your challenge"); ArrayFun.remove(this.challenges, c => c.id == data.cid); break; } @@ -454,73 +441,44 @@ export default { ); } }, -// * - accept challenge (corr or live) --> send info to challenge creator -// * - cancel challenge (click on sent challenge) --> send info to all concerned players -// * --> send info to challenge creator -// * - refuse challenge: send "refuse" to challenge sender, and "delete" to others -// * - prepare and start new game (if challenge is full after acceptation) -// * --> include challenge ID (so that opponents can delete the challenge too) clickChallenge: function(c) { - - console.log("click challenge"); - console.log(c); - - if (c.from.sid == this.st.user.sid - || (this.st.user.id > 0 && c.from.id == this.st.user.id)) - { - // It's my challenge: cancel it - this.sendSomethingTo(c.to, "deletechallenge", {cid:c.id}); - ArrayFun.remove(this.challenges, ch => ch.id == c.id); - if (c.type == "corr") - { - ajax( - "/challenges", - "DELETE", - {id: this.challenges[cIdx].id} - ); - } - } - else //accept (or refuse) a challenge + const myChallenge = (c.from.sid == this.st.user.sid //live + || (this.st.user.id > 0 && c.from.id == this.st.user.id)); //corr + if (!myChallenge) { c.accepted = true; - if (!!c.to) + if (!!c.to) //c.to == this.st.user.name (connected) { // TODO: if special FEN, show diagram after loading variant c.accepted = confirm("Accept challenge?"); } - this.st.conn.send(JSON.stringify({ - code: (c.accepted ? "accept" : "refuse") + "challenge", - cid: c.id, target: c.from.sid})); if (c.accepted) { - if (c.type == "corr") - { - ajax( - "/challenges", - "PUT", - {id: this.challenges[cIdx].id} - ); - } + c.seat = this.st.user; + this.launchGame(c); } else { - ArrayFun.remove(this.challenges, ch => ch.id == c.id); - if (!c.to) //TODO: send to everyone except me and opponent ? - this.sendSomethingTo("", "deletechallenge", {cid: this.challenges[cIdx].id}); - if (c.type == "corr") - { - ajax( - "/challenges", - "DELETE", - {id: this.challenges[cIdx].id} - ); - } + this.st.conn.send(JSON.stringify({ + code: "refusechallenge", + cid: c.id, target: c.from.sid})); } } + // In all cases, the challenge is consumed: + ArrayFun.remove(this.challenges, ch => ch.id == c.id); + // NOTE: deletechallenge event might be redundant (but it's easier this way) + this.sendSomethingTo(c.to, "deletechallenge", {cid:c.id}); + if (c.type == "corr") + { + ajax( + "/challenges", + "DELETE", + {id: this.challenges[cIdx].id} + ); + } }, - // NOTE: for live games only (corr games are launched on server) + // NOTE: when launching game, the challenge is already deleted launchGame: async function(c) { - // Just assign colors and pass the message const vname = this.getVname(c.vid); const vModule = await import("@/variants/" + vname + ".js"); window.V = vModule.VariantRules; @@ -531,17 +489,23 @@ export default { gameId: getRandString(), fen: c.fen || V.GenRandInitFen(), // Players' names may be required if game start when a player is offline - // Shuffle players order (white then black then other colors). + // Shuffle players order (white then black). players: shuffle(players.map(p => { return {name:p.name, sid:p.sid} })), vid: c.vid, timeControl: c.timeControl, }; - // NOTE: cid required to remove challenge this.st.conn.send(JSON.stringify({code:"newgame", - gameInfo:gameInfo, cid:c.id, target:c.seat.sid})); - // Delete corresponding challenge: - ArrayFun.remove(this.challenges, ch => ch.id == c.id); - this.startNewGame(gameInfo); //also! + gameInfo:gameInfo, target:c.seat.sid})); + if (c.type == "live") + this.startNewGame(gameInfo); + else //corr: game only on server + { + ajax( + "/games", + "POST", + {gameInfo: gameInfo} + ); + } }, // NOTE: for live games only (corr games are launched on server) startNewGame: function(gameInfo) {