aboutsummaryrefslogtreecommitdiff
path: root/web/worker.mjs
diff options
context:
space:
mode:
Diffstat (limited to 'web/worker.mjs')
-rw-r--r--web/worker.mjs114
1 files changed, 114 insertions, 0 deletions
diff --git a/web/worker.mjs b/web/worker.mjs
new file mode 100644
index 0000000..dca980e
--- /dev/null
+++ b/web/worker.mjs
@@ -0,0 +1,114 @@
1import Nissy from "./nissy_web_module.mjs"
2
3const nissy = await Nissy();
4function log(message) {
5 postMessage({ command: "log", id: -1, object: message });
6 console.log(message);
7}
8nissy.setLogger(nissy._addCallbackFunction(log));
9
10const commands = [
11 { name: "solve", exec: solve },
12 { name: "download solver data", exec: downloadSolverData },
13];
14
15// Message structure:
16// {
17// command: string // The function to run.
18// id: number // An identification number for the command, used for
19// // sending responses back to the caller and having them
20// // caught by the correct handler.
21// arg: object // The actual argument of the message, command-specific.
22// // See below for details.
23// }
24onmessage = (event) => {
25 for (var i = 0; i < commands.length; i++) {
26 if (commands[i].name == event.data.command) {
27 commands[i].exec(event.data.id, event.data.arg);
28 return;
29 }
30 }
31
32 log("[nissy worker] unknown command " + event.data.command);
33};
34
35// Download solver data.
36// Argument: string
37async function downloadSolverData(id, solver) {
38 const downloadSolverDataReturn = (success, message = "") => postMessage({
39 command: "download solver data",
40 id: id,
41 arg: {
42 success: success,
43 message: message
44 }
45 });
46
47 if (await nissy.initSolverDownload(arg.solver, "/tables")) {
48 downloadSolverDataReturn(true);
49 } else {
50 downloadSolverDataReturn(false, "Error retrieving the solver data");
51 }
52}
53
54// Solve the cube with the given options.
55// Argument: {
56// solver: string
57// scramble: string
58// minmoves: number
59// maxmoves: number
60// maxsolutions: number
61// optimal: number
62// threads: number
63// }
64async function solve(id, arg) {
65 const solveReturn = (success, solutions = [], message = "") => postMessage({
66 command: "solve",
67 id: id,
68 arg: {
69 success: success,
70 solutions: solutions,
71 message: message
72 }
73 });
74
75 if (!(await nissy.isSolverAvailable(arg.solver))) {
76 solveReturn(false, [], "Error: solver " + arg.solver +
77 " is not available in this version of nissy.");
78 return;
79 }
80
81 if (!(await nissy.isSolverLoaded(arg.solver)) &&
82 !(await nissy.initSolverFromStorage(arg.solver))) {
83 solveReturn(false, [], "Error: solver " + arg.solver + " has not been " +
84 "loaded. Its data must be donwloaded or generated before using it.");
85 return;
86 }
87
88 var cube = new nissy.Cube();
89 cube.move(arg.scramble);
90
91 // TODO: error handling here?
92 const nissFlag = ((str) => {
93 switch (str) {
94 case "inverse": return nissy.NissFlag.inverse;
95 case "mixed": return nissy.NissFlag.mixed;
96 case "linear": return nissy.NissFlag.linear;
97 case "all": return nissy.NissFlag.all;
98 default: return nissy.NissFlag.normal;
99 }
100 })(arg.nissFlag);
101
102 const result = await nissy.solve(arg.solver, cube, nissFlag, arg.minmoves,
103 arg.maxmoves, arg.maxsolutions, arg.optimal, arg.threads, id);
104
105 if (result.err.ok()) {
106 //TODO: add solution lenght?
107 var solutions = result.solutions == "" ? [] : result.solutions.split("\n");
108 solutions.pop(); // Solver always returns string ending in newline
109 solveReturn(true, solutions);
110 } else {
111 solveReturn(false, [],
112 "Error while solving (error " + result.err.value + ")");
113 }
114};

Generated with cgit - Back to sebastiano.tronto.net