X-Git-Url: https://git.auder.net/?p=xogo.git;a=blobdiff_plain;f=app.js;h=12c2b7ab65fea59a497ed1901a924889b0225491;hp=4c0b5e4e239b1a4129d118e82cd6aa79d568f98d;hb=HEAD;hpb=554e3ad3773a3123701bd894db1df4c1843283b8 diff --git a/app.js b/app.js index 4c0b5e4..c5d5c7f 100644 --- a/app.js +++ b/app.js @@ -373,7 +373,7 @@ const messageCenter = (msg) => { if (document.hidden) notifyMe("move"); vr.playReceivedMove(obj.moves, () => { - if (vr.getCurrentScore(obj.moves[obj.moves.length-1]) != "*") { + if (vr.getCurrentScore(obj.moves) != "*") { localStorage.removeItem("gid"); setTimeout( () => toggleVisible("gameStopped"), 2000 ); } @@ -390,6 +390,10 @@ const messageCenter = (msg) => { case "closerematch": toggleVisible("newGame"); break; + case "filechange": + // TODO?: could be more subtle + setTimeout(() => location.reload(), 100); + break; } }; @@ -456,51 +460,54 @@ function notifyMe(code) { let curMoves = [], lastFen; -const afterPlay = (move_s) => { - const callbackAfterSend = () => { +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, + 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(curMoves); curMoves = []; - const result = vr.getCurrentScore(move_s); if (result != "*") { setTimeout(() => { toggleVisible("gameStopped"); 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") - }); } }; -let vr, playerColor; +let vr = null, playerColor, lastVname = undefined; function initializeGame(obj) { const options = obj.options || {}; import(`/variants/${obj.vname}/class.js`).then(module => { window.V = module.default; for (const [k, v] of Object.entries(V.Aliases)) window[k] = v; - // Load CSS. Avoid loading twice the same stylesheet: - const allIds = [].slice.call($.styleSheets).map(s => s.id); - const newId = obj.vname + "_css"; - if (!allIds.includes(newId)) { + if (lastVname != obj.vname) { + // Load CSS + unload potential previous one. + if (lastVname) + document.getElementById(lastVname + "_css").remove(); $.getElementsByTagName("head")[0].insertAdjacentHTML( "beforeend", - ``); + lastVname = obj.vname; } playerColor = (sid == obj.players[0].sid ? "w" : "b"); // Init + remove potential extra DOM elements from a previous game: @@ -524,6 +531,9 @@ function initializeGame(obj) {
`; + if (vr) + // Avoid interferences: + vr.removeListeners(); vr = new V({ seed: obj.seed, //may be null if FEN already exists (running game) fen: obj.fen, @@ -532,8 +542,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 +558,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");