| 1 | // Logic to play a computer move in a web worker |
| 2 | onmessage = async function(e) { |
| 3 | switch (e.data[0]) { |
| 4 | case "scripts": { |
| 5 | await import("@/variants/" + e.data[1] + ".js") |
| 6 | .then((vModule) => { self.V = vModule[e.data[1] + "Rules"]; }); |
| 7 | break; |
| 8 | } |
| 9 | case "init": { |
| 10 | const fen = e.data[1]; |
| 11 | self.vr = new self.V(fen); |
| 12 | break; |
| 13 | } |
| 14 | case "newmove": |
| 15 | let move = e.data[1]; |
| 16 | // Caution: could be a multi-move |
| 17 | if (!Array.isArray(move)) |
| 18 | move = [move]; |
| 19 | move.forEach(m => self.vr.play(m)); |
| 20 | break; |
| 21 | case "askmove": { |
| 22 | const compMove = self.vr.getComputerMove(); |
| 23 | postMessage(compMove); |
| 24 | break; |
| 25 | } |
| 26 | } |
| 27 | }; |