diff options
Diffstat (limited to 'web/http/worker.mjs')
| -rw-r--r-- | web/http/worker.mjs | 28 |
1 files changed, 25 insertions, 3 deletions
diff --git a/web/http/worker.mjs b/web/http/worker.mjs index 12de1dc..d912e8b 100644 --- a/web/http/worker.mjs +++ b/web/http/worker.mjs | |||
| @@ -4,12 +4,19 @@ const nissy = await Nissy(); | |||
| 4 | const log = (msg) => postMessage({ command: "log", id: -1, object: msg }); | 4 | const log = (msg) => postMessage({ command: "log", id: -1, object: msg }); |
| 5 | nissy.setLogger(nissy._addCallbackFunction(log)); | 5 | nissy.setLogger(nissy._addCallbackFunction(log)); |
| 6 | 6 | ||
| 7 | var solveStatus = nissy.statusRUN; // For now this is a global variable | ||
| 8 | const pollStatusCallback = nissy._addCallbackFunction(() => { | ||
| 9 | console.log("Calling pollstatus, returning " + solveStatus); | ||
| 10 | return solveStatus; | ||
| 11 | }); | ||
| 12 | |||
| 7 | const commands = [ | 13 | const commands = [ |
| 8 | { name: "load solver data", exec: loadSolverDataFromStorage }, | 14 | { name: "load solver data", exec: loadSolverDataFromStorage }, |
| 9 | { name: "download solver data", exec: downloadSolverData }, | 15 | { name: "download solver data", exec: downloadSolverData }, |
| 10 | { name: "generate solver data", exec: generateSolverData }, | 16 | { name: "generate solver data", exec: generateSolverData }, |
| 11 | { name: "validate scramble", exec: validateScramble }, | 17 | { name: "validate scramble", exec: validateScramble }, |
| 12 | { name: "solve", exec: solve }, | 18 | { name: "solve", exec: solve }, |
| 19 | { name: "update status", exec: updateStatus }, | ||
| 13 | ]; | 20 | ]; |
| 14 | 21 | ||
| 15 | // Message structure: | 22 | // Message structure: |
| @@ -161,10 +168,11 @@ async function solve(id, arg) { | |||
| 161 | return; | 168 | return; |
| 162 | } | 169 | } |
| 163 | 170 | ||
| 171 | solveStatus = nissy.statusRUN; | ||
| 172 | |||
| 164 | var cube = new nissy.Cube(); | 173 | var cube = new nissy.Cube(); |
| 165 | cube.move(arg.scramble); | 174 | cube.move(arg.scramble); |
| 166 | 175 | ||
| 167 | // TODO: error handling here? | ||
| 168 | const nissFlag = ((str) => { | 176 | const nissFlag = ((str) => { |
| 169 | switch (str) { | 177 | switch (str) { |
| 170 | case "inverse": return nissy.NissFlag.inverse; | 178 | case "inverse": return nissy.NissFlag.inverse; |
| @@ -176,15 +184,29 @@ async function solve(id, arg) { | |||
| 176 | })(arg.nissFlag); | 184 | })(arg.nissFlag); |
| 177 | 185 | ||
| 178 | const result = await nissy.solve(arg.solver, cube, nissFlag, arg.minmoves, | 186 | const result = await nissy.solve(arg.solver, cube, nissFlag, arg.minmoves, |
| 179 | arg.maxmoves, arg.maxsolutions, arg.optimal, arg.threads, id); | 187 | arg.maxmoves, arg.maxsolutions, arg.optimal, arg.threads, |
| 188 | pollStatusCallback); | ||
| 180 | 189 | ||
| 181 | if (result.err.ok()) { | 190 | if (result.err.ok()) { |
| 182 | //TODO: add solution lenght? | ||
| 183 | var solutions = result.solutions == "" ? [] : result.solutions.split("\n"); | 191 | var solutions = result.solutions == "" ? [] : result.solutions.split("\n"); |
| 184 | solutions.pop(); // Solver always returns string ending in newline | 192 | solutions.pop(); // Solver always returns string ending in newline |
| 193 | for (var i = 0; i < solutions.length; i++) { | ||
| 194 | const movecount = await nissy.countMoves(solutions[i]); | ||
| 195 | solutions[i] += " (" + movecount.value + ")"; | ||
| 196 | } | ||
| 185 | async_return(true, solutions); | 197 | async_return(true, solutions); |
| 186 | } else { | 198 | } else { |
| 187 | async_return(false, [], | 199 | async_return(false, [], |
| 188 | "Error while solving (error " + result.err.value + ")"); | 200 | "Error while solving (error " + result.err.value + ")"); |
| 189 | } | 201 | } |
| 190 | }; | 202 | }; |
| 203 | |||
| 204 | function updateStatus(id, arg) { | ||
| 205 | if (arg == "stop") { | ||
| 206 | solveStatus = nissy.statusSTOP; | ||
| 207 | } else if (arg == "pause") { | ||
| 208 | solveStatus = nissy.statusPAUSE; | ||
| 209 | } else { | ||
| 210 | solveStatus = nissy.statusRUN; | ||
| 211 | } | ||
| 212 | }; | ||
