Implemented multi-move possibility in a moves list => better support for multi-moves...
[vchess.git] / client / src / playCompMove.js
index b93a8d1..05c2286 100644 (file)
@@ -1,22 +1,27 @@
 // Logic to play a computer move in a web worker
-onmessage = async function(e)
-{
-  switch (e.data[0])
-  {
-    case "scripts":
+onmessage = async function(e) {
+  switch (e.data[0]) {
+    case "scripts": {
       const vModule = await import("@/variants/" + e.data[1] + ".js");
       self.V = vModule.VariantRules;
       break;
-    case "init":
+    }
+    case "init": {
       const fen = e.data[1];
       self.vr = new self.V(fen);
       break;
+    }
     case "newmove":
-      self.vr.play(e.data[1]);
+      let move = e.data[1];
+      // Caution: could be a multi-move
+      if (!Array.isArray(move))
+        move = [move];
+      move.forEach(m => self.vr.play(m));
       break;
-    case "askmove":
+    case "askmove": {
       const compMove = self.vr.getComputerMove();
       postMessage(compMove);
       break;
+    }
   }
-}
+};