X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=client%2Fsrc%2Fviews%2FHall.vue;h=afa2173d5af2fe3d49e707bbe46e72911a8e2a8b;hb=40477190d905a715d59a800279a20da4ef8135ed;hp=636eabc3cca71d147411ed4ef0647b821c0b8888;hpb=2cc10cdbc230f82202bdddb7a244c903c05ab351;p=vchess.git diff --git a/client/src/views/Hall.vue b/client/src/views/Hall.vue index 636eabc3..afa2173d 100644 --- a/client/src/views/Hall.vue +++ b/client/src/views/Hall.vue @@ -113,6 +113,15 @@ export default { created: function() { // Always add myself to players' list this.people.push(this.st.user); + // Retrieve live challenge (not older than 30 minute) if any: + const chall = JSON.parse(localStorage.getItem("challenge") || "false"); + if (!!chall) + { + if ((Date.now() - chall.added)/1000 <= 30*60) + this.challenges.push(chall); + else + localStorage.removeItem("challenge"); + } if (this.st.user.id > 0) { // Ask server for current corr games (all but mines) @@ -132,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); } ); } @@ -169,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") @@ -199,19 +209,17 @@ export default { {target: sid} ))); }; - if (!!to[0]) + if (!!to) { - to.forEach(pname => { - // Challenge with targeted players - const targetSid = this.getSid(pname); - if (!targetSid) - { - if (!!warnDisconnected) - alert("Warning: " + pname + " is not connected"); - } - else - doSend(code, obj, targetSid); - }); + // Challenge with targeted players + const targetSid = this.getSid(to); + if (!targetSid) + { + if (!!warnDisconnected) + alert("Warning: " + pname + " is not connected"); + } + else + doSend(code, obj, targetSid); } else { @@ -270,27 +278,28 @@ export default { timeControl: c.timeControl }; this.st.conn.send(JSON.stringify({code:"challenge", - challenge:myChallenge, target:data.from})); + chall:myChallenge, target:data.from})); } break; } case "askgame": { - // Send my current live games (if any) - // TODO: from indexedDB, through GameStorage. -// if (!!localStorage["gid"]) -// { -// const myGame = -// { -// // Minimal game informations: (fen+clock not required) -// id: localStorage["gid"], -// players: JSON.parse(localStorage["players"]), //array sid+id+name -// vname: localStorage["vname"], -// timeControl: localStorage["timeControl"], -// }; -// this.st.conn.send(JSON.stringify({code:"game", -// game:myGame, target:data.from})); -// } + // Send my current live game (if any) + GameStorage.getCurrent((game) => { + if (!!game) + { + const myGame = + { + // Minimal game informations: + id: game.id, + players: game.players.map(p => p.name), + vname: game.vname, + timeControl: game.timeControl, + }; + this.st.conn.send(JSON.stringify({code:"game", + game:myGame, target:data.from})); + } + }); break; } case "identity": @@ -307,8 +316,8 @@ export default { newChall.type = this.classifyObject(data.chall); const pIdx = this.people.findIndex(p => p.sid == data.from); newChall.from = this.people[pIdx]; //may be anonymous - newChall.added = Date.now(); - newChall.vname = this.getVname(newChall.vid); + newChall.added = Date.now(); //TODO: this is reception timestamp, not creation + newChall.vname = this.getVname(newChall.vid); //TODO: just send vname? this.challenges.push(newChall); break; } @@ -316,10 +325,10 @@ export default { { // Receive game from some player (+sid) // NOTE: it may be correspondance (if newgame while we are connected) - // TODO: ambiguous naming "newGame" ==> rename function ? let newGame = data.game; newGame.type = this.classifyObject(data.game); - newGame.vname = newGame.vname; + newGame.rid = data.from; + newGame.score = "*"; this.games.push(newGame); break; } @@ -330,7 +339,7 @@ export default { // Delete corresponding challenge: ArrayFun.remove(this.challenges, c => c.id == data.cid); // New game just started: data contain all informations - this.newGame(data.gameInfo); + this.startNewGame(data.gameInfo); break; } // * - receive "accept/cancel challenge": apply action to challenges list @@ -353,6 +362,7 @@ export default { } case "deletechallenge": { + // NOTE: the challenge may be already removed ArrayFun.remove(this.challenges, c => c.id == data.cid); break; } @@ -403,12 +413,13 @@ export default { const finishAddChallenge = (cid,warnDisconnected) => { chall.id = cid || "c" + getRandString(); // Send challenge to peers (if connected) - this.sendSomethingTo(cto, "challenge", {chall:chall}, !!warnDisconnected); + this.sendSomethingTo(chall.to, "challenge", {chall:chall}, !!warnDisconnected); chall.added = Date.now(); chall.type = ctype; chall.vname = vname; chall.from = this.st.user; this.challenges.push(chall); + localStorage.setItem("challenge", JSON.stringify(chall)); document.getElementById("modalNewgame").checked = false; }; const cIdx = this.challenges.findIndex( @@ -455,12 +466,14 @@ export default { 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)) + // In all cases, the challenge is consumed: + ArrayFun.remove(this.challenges, ch => ch.id == c.id); + + if (c.from.sid == this.st.user.sid //live + || (this.st.user.id > 0 && c.from.id == this.st.user.id)) //corr { // 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( @@ -473,7 +486,7 @@ export default { else //accept (or refuse) a challenge { c.accepted = true; - if (!!c.to[0]) + if (!!c.to) { // TODO: if special FEN, show diagram after loading variant c.accepted = confirm("Accept challenge?"); @@ -481,26 +494,14 @@ export default { this.st.conn.send(JSON.stringify({ code: (c.accepted ? "accept" : "refuse") + "challenge", cid: c.id, target: c.from.sid})); - if (c.type == "corr" && c.accepted) + if (c.type == "corr") { ajax( "/challenges", - "PUT", + accepted ? "PUT" : "DELETE", {id: this.challenges[cIdx].id} ); } - if (!c.accepted) - { - ArrayFun.remove(this.challenges, ch => ch.id == 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) @@ -526,10 +527,10 @@ export default { gameInfo:gameInfo, cid:c.id, target:c.seat.sid})); // Delete corresponding challenge: ArrayFun.remove(this.challenges, ch => ch.id == c.id); - this.newGame(gameInfo); //also! + this.startNewGame(gameInfo); //also! }, // NOTE: for live games only (corr games are launched on server) - newGame: function(gameInfo) { + startNewGame: function(gameInfo) { // Extract times (in [milli]seconds), set clocks const tc = extractTime(gameInfo.timeControl); let initime = [...Array(gameInfo.players.length)];