X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=client%2Fsrc%2Fviews%2FGame.vue;h=e909f70e601ccb3c05ba5507ed00547937dcf8ea;hb=cafe016679ee9c14bf7bf0a37104ade7f74aff89;hp=a7cb6a90f676b8ee54e31f6b386b15e5bdcff5a3;hpb=c292ebb2a014646005b01e27253c162f1d639387;p=vchess.git diff --git a/client/src/views/Game.vue b/client/src/views/Game.vue index a7cb6a90..e909f70e 100644 --- a/client/src/views/Game.vue +++ b/client/src/views/Game.vue @@ -161,9 +161,10 @@ export default { virtualClocks: [], vr: null, //"variant rules" object initialized from FEN drawOffer: "", + infoMessage: "", rematchOffer: "", + lastateAsked: false, people: {}, //players + observers - onMygames: [], //opponents (or me) on "MyGames" page lastate: undefined, //used if opponent send lastate before game is ready repeat: {}, //detect position repetition curDiag: "", //for corr moves confirmation @@ -267,8 +268,8 @@ export default { this.virtualClocks = [[0,0], [0,0]]; this.vr = null; this.drawOffer = ""; + this.lastateAsked = false; this.rematchOffer = ""; - this.onMygames = []; this.lastate = undefined; this.newChat = ""; this.roomInitialized = false; @@ -287,6 +288,8 @@ export default { params.socketUrl + "/?sid=" + this.st.user.sid + + "&id=" + + this.st.user.id + "&tmpId=" + getRandString() + "&page=" + @@ -395,17 +398,20 @@ export default { this.$set(this.game, "chats", []); } }, - // Notify turn after a new move (to opponent and me on MyGames page) - notifyTurn: function(sid) { - const player = this.people[sid]; - const colorIdx = this.game.players.findIndex( - p => p.sid == sid || p.uid == player.id); - const color = ["w","b"][colorIdx]; - const movesCount = this.game.moves.length; - const yourTurn = - (color == "w" && movesCount % 2 == 0) || - (color == "b" && movesCount % 2 == 1); - this.send("turnchange", { target: sid, yourTurn: yourTurn }); + getGameType: function(game) { + return game.cadence.indexOf("d") >= 0 ? "corr" : "live"; + }, + // Notify something after a new move (to opponent and me on MyGames page) + notifyMyGames: function(thing, data) { + this.send( + "notify" + thing, + { + data: data, + targets: this.game.players.map(p => { + return { sid: p.sid, uid: p.uid }; + }) + } + ); }, showNextGame: function() { // Did I play in current game? If not, add it to nextIds list @@ -456,22 +462,6 @@ export default { case "disconnect": this.$delete(this.people, data.from); break; - case "mconnect": { - // TODO: from MyGames page : send mconnect message with the list of gid (live and corr) - // Either me (another tab) or opponent - const sid = data.from; - if (!this.onMygames.some(s => s == sid)) - { - this.onMygames.push(sid); - this.notifyTurn(sid); //TODO: this may require server ID (so, notify after receiving identity) - } - break; - if (!this.people[sid]) - this.send("askidentity", { target: sid }); - } - case "mdisconnect": - ArrayFun.remove(this.onMygames, sid => sid == data.from); - break; case "getfocus": { let player = this.people[data.from]; if (!!player) { @@ -595,31 +585,9 @@ export default { break; case "asklastate": // Sending informative last state if I played a move or score != "*" - if ( - (this.game.moves.length > 0 && this.vr.turn != this.game.mycolor) || - this.game.score != "*" || - this.drawOffer == "sent" || - this.rematchOffer == "sent" - ) { - // Send our "last state" informations to opponent - const L = this.game.moves.length; - const myIdx = ["w", "b"].indexOf(this.game.mycolor); - const myLastate = { - lastMove: L > 0 ? this.game.moves[L - 1] : undefined, - clock: this.game.clocks[myIdx], - // Since we played a move (or abort or resign), - // only drawOffer=="sent" is possible - drawSent: this.drawOffer == "sent", - rematchSent: this.rematchOffer == "sent", - score: this.game.score, - score: this.game.scoreMsg, - movesCount: L, - initime: this.game.initime[1 - myIdx] //relevant only if I played - }; - this.send("lastate", { data: myLastate, target: data.from }); - } else { - this.send("lastate", { data: {nothing: true}, target: data.from }); - } + // If the game or moves aren't loaded yet, delay the sending: + if (!this.game || !this.game.moves) this.lastateAsked = true; + else this.sendLastate(data.from); break; case "lastate": { // Got opponent infos about last move @@ -710,9 +678,15 @@ export default { case "newgame": { // A game started, redirect if I'm playing in const gameInfo = data.data; + const gameType = this.getGameType(gameInfo); if ( - gameInfo.players.some(p => - p.sid == this.st.user.sid || p.uid == this.st.user.id) + gameType == "live" && + gameInfo.players.some(p => p.sid == this.st.user.sid) + ) { + this.addAndGotoLiveGame(gameInfo); + } else if ( + gameType == "corr" && + gameInfo.players.some(p => p.uid == this.st.user.id) ) { this.$router.push("/game/" + gameInfo.id); } else { @@ -765,6 +739,33 @@ export default { } ); }, + sendLastate: function(target) { + if ( + (this.game.moves.length > 0 && this.vr.turn != this.game.mycolor) || + this.game.score != "*" || + this.drawOffer == "sent" || + this.rematchOffer == "sent" + ) { + // Send our "last state" informations to opponent + const L = this.game.moves.length; + const myIdx = ["w", "b"].indexOf(this.game.mycolor); + const myLastate = { + lastMove: L > 0 ? this.game.moves[L - 1] : undefined, + clock: this.game.clocks[myIdx], + // Since we played a move (or abort or resign), + // only drawOffer=="sent" is possible + drawSent: this.drawOffer == "sent", + rematchSent: this.rematchOffer == "sent", + score: this.game.score, + score: this.game.scoreMsg, + movesCount: L, + initime: this.game.initime[1 - myIdx] //relevant only if I played + }; + this.send("lastate", { data: myLastate, target: target }); + } else { + this.send("lastate", { data: {nothing: true}, target: target }); + } + }, // lastate was received, but maybe game wasn't ready yet: processLastate: function() { const data = this.lastate; @@ -810,6 +811,32 @@ export default { } else this.updateCorrGame({ drawOffer: this.game.mycolor }); } }, + addAndGotoLiveGame: function(gameInfo, callback) { + const game = Object.assign( + {}, + gameInfo, + { + // (other) Game infos: constant + fenStart: gameInfo.fen, + vname: this.game.vname, + created: Date.now(), + // Game state (including FEN): will be updated + moves: [], + clocks: [-1, -1], //-1 = unstarted + initime: [0, 0], //initialized later + score: "*" + } + ); + GameStorage.add(game, (err) => { + // No error expected. + if (!err) { + if (this.st.settings.sound) + new Audio("/sounds/newgame.flac").play().catch(() => {}); + callback(); + this.$router.push("/game/" + gameInfo.id); + } + }); + }, clickRematch: function() { if (!this.game.mycolor) return; //I'm just spectator if (this.rematchOffer == "received") { @@ -821,33 +848,14 @@ export default { vid: this.game.vid, cadence: this.game.cadence }; - let oppsid = this.getOppsid(); //may be null - this.send("rnewgame", { data: gameInfo, oppsid: oppsid }); - if (this.game.type == "live") { - const game = Object.assign( - {}, - gameInfo, - { - // (other) Game infos: constant - fenStart: gameInfo.fen, - vname: this.game.vname, - created: Date.now(), - // Game state (including FEN): will be updated - moves: [], - clocks: [-1, -1], //-1 = unstarted - initime: [0, 0], //initialized later - score: "*" - } - ); - GameStorage.add(game, (err) => { - // No error expected. - if (!err) { - if (this.st.settings.sound) - new Audio("/sounds/newgame.flac").play().catch(() => {}); - this.$router.push("/game/" + gameInfo.id); - } - }); - } + const notifyNewGame = () => { + let oppsid = this.getOppsid(); //may be null + this.send("rnewgame", { data: gameInfo, oppsid: oppsid }); + // Also to MyGames page: + this.notifyMyGames("newgame", gameInfo); + }; + if (this.game.type == "live") + this.addAndGotoLiveGame(gameInfo, notifyNewGame); else { // corr game ajax( @@ -858,6 +866,7 @@ export default { data: { gameInfo: gameInfo }, success: (response) => { gameInfo.id = response.gameId; + notifyNewGame(); this.$router.push("/game/" + response.gameId); } } @@ -902,11 +911,11 @@ export default { // - from server (one correspondance game I play[ed] or not) // - from remote peer (one live game I don't play, finished or not) loadGame: function(game, callback) { - const afterRetrieval = async game => { + const afterRetrieval = async (game) => { const vModule = await import("@/variants/" + game.vname + ".js"); window.V = vModule.VariantRules; this.vr = new V(game.fen); - const gtype = game.cadence.indexOf("d") >= 0 ? "corr" : "live"; + const gtype = this.getGameType(game); const tc = extractTime(game.cadence); const myIdx = game.players.findIndex(p => { return p.sid == this.st.user.sid || p.uid == this.st.user.id; @@ -1049,6 +1058,10 @@ export default { // Did lastate arrive before game was rendered? if (this.lastate) this.processLastate(); }); + if (this.lastateAsked) { + this.lastateAsked = false; + this.sendLastate(game.oppsid); + } if (this.gameIsLoading) { this.gameIsLoading = false; if (this.gotMoveIdx >= game.moves.length) @@ -1158,7 +1171,6 @@ export default { const score = this.vr.getCurrentScore(); if (score != "*") this.gameOver(score); } -// TODO: notifyTurn: "changeturn" message this.game.moves.push(move); this.game.fen = this.vr.getFen(); if (this.game.type == "live") { @@ -1189,6 +1201,16 @@ export default { // NOTE: 'var' to see that variable outside this block var filtered_move = getFilteredMove(move); } + if (moveCol == this.game.mycolor && !data.receiveMyMove) { + // Notify turn on MyGames page: + this.notifyMyGames( + "turn", + { + gid: this.gameRef.id, + turn: this.vr.turn + } + ); + } // Since corr games are stored at only one location, update should be // done only by one player for each move: if ( @@ -1367,6 +1389,14 @@ export default { else this.updateCorrGame(scoreObj, callback); // Notify the score to main Hall. TODO: only one player (currently double send) this.send("result", { gid: this.game.id, score: score }); + // Also to MyGames page (TODO: doubled as well...) + this.notifyMyGames( + "score", + { + gid: this.gameRef.id, + score: score + } + ); } else if (!!callback) callback(); }