New variant idea
[xogo.git] / app.js
diff --git a/app.js b/app.js
index 6f17c64..c5d5c7f 100644 (file)
--- 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;
   }
 };
 
@@ -470,7 +474,6 @@ const afterPlay = (move_s, newTurn, ops) => {
            {gid: gid, moves: curMoves, fen: vr.getFen()},
            {
              retry: true,
-             success: () => curMoves = [],
              error: () => alert("Move not sent: reload page")
            }
       );
@@ -478,7 +481,8 @@ const afterPlay = (move_s, newTurn, ops) => {
   }
   if (ops.res && newTurn != playerColor) {
     toggleTurnIndicator(false); //now all moves are sent and animated
-    const result = vr.getCurrentScore(move_s);
+    const result = vr.getCurrentScore(curMoves);
+    curMoves = [];
     if (result != "*") {
       setTimeout(() => {
         toggleVisible("gameStopped");
@@ -488,21 +492,22 @@ const afterPlay = (move_s, newTurn, ops) => {
   }
 };
 
-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",
-        `<link id="${newId}" rel="stylesheet"
+        `<link id="${obj.vname + '_css'}" rel="stylesheet"
                href="/variants/${obj.vname}/style.css"/>`);
+      lastVname = obj.vname;
     }
     playerColor = (sid == obj.players[0].sid ? "w" : "b");
     // Init + remove potential extra DOM elements from a previous game:
@@ -526,6 +531,9 @@ function initializeGame(obj) {
         </svg>
       </div>
       <div class="chessboard"></div>`;
+    if (vr)
+      // Avoid interferences:
+      vr.removeListeners();
     vr = new V({
       seed: obj.seed, //may be null if FEN already exists (running game)
       fen: obj.fen,