X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=public%2Fjavascripts%2Fcomponents%2Fgame.js;h=1b216efae937c63afda5cc41cdd17dbd0110b2c0;hb=3c09dc498791ac478679bf2f42f441342c4fa22c;hp=397ba521b2bd89f062e445b8f101df90735c621b;hpb=55eb331d0a9262baafcae5a42258a44d00f38da4;p=vchess.git diff --git a/public/javascripts/components/game.js b/public/javascripts/components/game.js index 397ba521..1b216efa 100644 --- a/public/javascripts/components/game.js +++ b/public/javascripts/components/game.js @@ -1,4 +1,3 @@ -// TODO: use indexedDB instead of localStorage? (more flexible: allow several games) Vue.component('my-game', { data: function() { return { @@ -136,7 +135,8 @@ Vue.component('my-game', { }, [h('img', { - attrs: { "src": '/images/pieces/' + VariantRules.getPpath(m.appear[0].c+m.appear[0].p) + '.svg' }, + attrs: { "src": '/images/pieces/' + + VariantRules.getPpath(m.appear[0].c+m.appear[0].p) + '.svg' }, 'class': { 'choice-piece': true, 'board': true }, on: { "click": e => { this.play(m); this.choices=[]; } }, }) @@ -170,10 +170,12 @@ Vue.component('my-game', { { 'class': { 'piece': true, - 'ghost': !!this.selectedPiece && this.selectedPiece.parentNode.id == "sq-"+ci+"-"+cj, + 'ghost': !!this.selectedPiece + && this.selectedPiece.parentNode.id == "sq-"+ci+"-"+cj, }, attrs: { - src: "/images/pieces/" + VariantRules.getPpath(this.vr.board[ci][cj]) + ".svg", + src: "/images/pieces/" + + VariantRules.getPpath(this.vr.board[ci][cj]) + ".svg", }, } ) @@ -391,6 +393,11 @@ Vue.component('my-game', { ? localStorage.getItem("myid") // random enough (TODO: function) : (Date.now().toString(36) + Math.random().toString(36).substr(2, 7)).toUpperCase(); + if (!continuation) + { + // HACK: play a small silent sound to allow "new game" sound later if tab not focused + new Audio("/sounds/silent.mp3").play().then(() => {}).catch(err => {}); + } this.conn = new WebSocket(url + "/?sid=" + this.myid + "&page=" + variant); const socketOpenListener = () => { if (continuation) @@ -574,6 +581,7 @@ Vue.component('my-game', { this.newGame("computer"); }, newGame: function(mode, fenInit, color, oppId, moves, continuation) { + //const fen = "1n2T1n0/p2pO2p/1s1k1s2/8/3S2p1/2U2cO1/P3PuPP/3K1BR1 0100"; const fen = fenInit || VariantRules.GenRandInitFen(); console.log(fen); //DEBUG this.score = "*"; @@ -640,9 +648,18 @@ Vue.component('my-game', { } }, playComputerMove: function() { + const timeStart = Date.now(); + const nbMoves = this.vr.moves.length; //using played moves to know if search finished + setTimeout( + () => { + const L = this.vr.moves.length; + if (nbMoves == L || !this.vr.moves[L-1].notation) //move search didn't finish + this.vr.shouldReturn = true; + }, 5000); const compMove = this.vr.getComputerMove(); - // HACK: avoid selecting elements before they appear on page: - setTimeout(() => this.play(compMove, "animate"), 500); + // (first move) HACK: avoid selecting elements before they appear on page: + const delay = Math.max(500-(Date.now()-timeStart), 0); + setTimeout(() => this.play(compMove, "animate"), delay); }, // Get the identifier of a HTML table cell from its numeric coordinates o.x,o.y. getSquareId: function(o) { @@ -729,8 +746,9 @@ Vue.component('my-game', { let rectStart = startSquare.getBoundingClientRect(); let rectEnd = endSquare.getBoundingClientRect(); let translation = {x:rectEnd.x-rectStart.x, y:rectEnd.y-rectStart.y}; - let movingPiece = document.querySelector("#" + this.getSquareId(move.start) + " > img.piece"); - // HACK for animation (otherwise with positive translate, image slides "under background"...) + let movingPiece = + document.querySelector("#" + this.getSquareId(move.start) + " > img.piece"); + // HACK for animation (with positive translate, image slides "under background"...) // Possible improvement: just alter squares on the piece's way... squares = document.getElementsByClassName("board"); for (let i=0; i