aboutsummaryrefslogtreecommitdiff
path: root/web/http/worker.mjs
diff options
context:
space:
mode:
authorSebastiano Tronto <sebastiano@tronto.net>2025-05-30 14:06:34 +0200
committerSebastiano Tronto <sebastiano@tronto.net>2025-05-30 14:06:34 +0200
commit37a2ebd4bffa99d465396fde0acfc52284ee3b7c (patch)
tree617921a35782fd81bbf76dcbd09ccc539361155e /web/http/worker.mjs
parentdc47c318bd7272099ec8a66ddc48810f91fa52b9 (diff)
downloadnissy-core-37a2ebd4bffa99d465396fde0acfc52284ee3b7c.tar.gz
nissy-core-37a2ebd4bffa99d465396fde0acfc52284ee3b7c.zip
More improvements to web solver
Diffstat (limited to '')
-rw-r--r--web/http/worker.mjs28
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();
4const log = (msg) => postMessage({ command: "log", id: -1, object: msg }); 4const log = (msg) => postMessage({ command: "log", id: -1, object: msg });
5nissy.setLogger(nissy._addCallbackFunction(log)); 5nissy.setLogger(nissy._addCallbackFunction(log));
6 6
7var solveStatus = nissy.statusRUN; // For now this is a global variable
8const pollStatusCallback = nissy._addCallbackFunction(() => {
9console.log("Calling pollstatus, returning " + solveStatus);
10return solveStatus;
11});
12
7const commands = [ 13const 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
204function 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};

Generated with cgit - Back to sebastiano.tronto.net