X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=client%2Fsrc%2FplayCompMove.js;h=05c2286c49e612feb744e147a5a904fac8a08d9b;hb=e71161fbfffe53b0f4b174e0467cdd98cc70b7b0;hp=45da5113a21c9a57e96741e0b9f390ef4a227642;hpb=8d61fc4ab7373b4a576f3f9108cdf7768ae27096;p=vchess.git diff --git a/client/src/playCompMove.js b/client/src/playCompMove.js index 45da5113..05c2286c 100644 --- a/client/src/playCompMove.js +++ b/client/src/playCompMove.js @@ -1,26 +1,27 @@ -// For asynchronous computer move search -onmessage = function(e) -{ - switch (e.data[0]) - { - case "scripts": - self.importScripts( - '//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js', - '/javascripts/base_rules.js', - '/javascripts/utils/array.js', - '/javascripts/variants/' + e.data[1] + '.js'); - self.V = eval(e.data[1] + "Rules"); - break; - case "init": - const fen = e.data[1]; - self.vr = new VariantRules(fen); - break; - case "newmove": - self.vr.play(e.data[1]); - break; - case "askmove": - const compMove = self.vr.getComputerMove(); - postMessage(compMove); - break; - } -} +// Logic to play a computer move in a web worker +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": { + const fen = e.data[1]; + self.vr = new self.V(fen); + break; + } + case "newmove": + 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": { + const compMove = self.vr.getComputerMove(); + postMessage(compMove); + break; + } + } +};