X-Git-Url: https://git.auder.net/?p=xogo.git;a=blobdiff_plain;f=app.js;h=6f17c6491e5244759e6d835a0e2ed7b0331434a9;hp=4c0b5e4e239b1a4129d118e82cd6aa79d568f98d;hb=5f08c59b29c2173cc8b2df1a3799ee971a14e691;hpb=fc12475fd434835816796ece83d93341af6c1550 diff --git a/app.js b/app.js index 4c0b5e4..6f17c64 100644 --- a/app.js +++ b/app.js @@ -456,9 +456,28 @@ function notifyMe(code) { let curMoves = [], lastFen; -const afterPlay = (move_s) => { - const callbackAfterSend = () => { - curMoves = []; +const afterPlay = (move_s, newTurn, ops) => { + if (ops.send) { + // Pack into one moves array, then send (if turn changed) + if (Array.isArray(move_s)) + // Array of simple moves (e.g. Chakart) + Array.prototype.push.apply(curMoves, move_s); + else + // Usual case + curMoves.push(move_s); + if (newTurn != playerColor) { + send("newmove", + {gid: gid, moves: curMoves, fen: vr.getFen()}, + { + retry: true, + success: () => curMoves = [], + error: () => alert("Move not sent: reload page") + } + ); + } + } + if (ops.res && newTurn != playerColor) { + toggleTurnIndicator(false); //now all moves are sent and animated const result = vr.getCurrentScore(move_s); if (result != "*") { setTimeout(() => { @@ -466,23 +485,6 @@ const afterPlay = (move_s) => { send("gameover", {gid: gid}); }, 2000); } - }; - // Pack into one moves array, then send - if (Array.isArray(move_s)) - // Array of simple moves (e.g. Chakart) - Array.prototype.push.apply(curMoves, move_s); - else - // Usual case - curMoves.push(move_s); - if (vr.turn != playerColor) { - toggleTurnIndicator(false); - send("newmove", - {gid: gid, moves: curMoves, fen: vr.getFen()}, - { - retry: true, - success: callbackAfterSend, - error: () => alert("Move not sent: reload page") - }); } }; @@ -532,8 +534,9 @@ function initializeGame(obj) { afterPlay: afterPlay, options: options }); - if (!obj.fen) { - // Game creation: both players set FEN, in case of one is offline + const gameCreation = !obj.fen; + if (gameCreation) { + // Both players set FEN, in case of one is offline send("setfen", {gid: obj.gid, fen: vr.getFen()}); localStorage.setItem("gid", obj.gid); } @@ -547,7 +550,7 @@ function initializeGame(obj) { } const playerIndex = (playerColor == "w" ? 0 : 1); fillGameInfos(obj, 1 - playerIndex); - if (obj.players[playerIndex].randvar) + if (obj.players[playerIndex].randvar && gameCreation) toggleVisible("gameInfos"); else toggleVisible("boardContainer");