aboutsummaryrefslogtreecommitdiff
path: root/web/worker.mjs
blob: 12de1dcfb9c114401f1aced7eaacbe8ae7f408bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import Nissy from "./nissy_web_module.mjs"

const nissy = await Nissy();
const log = (msg) => postMessage({ command: "log", id: -1, object: msg });
nissy.setLogger(nissy._addCallbackFunction(log));

const commands = [
  { name: "load solver data", exec: loadSolverDataFromStorage },
  { name: "download solver data", exec: downloadSolverData },
  { name: "generate solver data", exec: generateSolverData },
  { name: "validate scramble", exec: validateScramble },
  { name: "solve", exec: solve },
];

// Message structure:
// {
//   command: string // The function to run.
//   id: number      // An identification number for the command, used for
//                   // sending responses back to the caller and having them
//                   // caught by the correct handler.
//   arg: object     // The actual argument of the message, command-specific.
//                   // See below for details.
// }
onmessage = (event) => {
  for (var i = 0; i < commands.length; i++) {
    if (commands[i].name == event.data.command) {
      commands[i].exec(event.data.id, event.data.arg);
      console.log("[worker] Received '" + commands[i].name + "'");
      return;
    }
  }
 
  console.log("[nissy worker] unknown command " + event.data.command);
};

// Load solver data from storage.
// Argument: string (the name of the solver)
async function loadSolverDataFromStorage(id, solver) {
  const async_return = (success, message = "") => postMessage({
    command: "load solver data",
    id: id,
    arg: {
      success: success,
      message: message
    }
  });

  if (!(await nissy.isSolverAvailable(solver))) {
    async_return(false, "Error: solver " + solver +
      " is not available in this version of nissy.");
    return;
  }

  if (await nissy.isSolverLoaded(solver)) {
    async_return(true);
    return;
  }

  if (await nissy.initSolverFromStorage(solver)) {
    async_return(true);
  } else {
    async_return(false, "Could not read data for " + solver +
      " from local storage");
  }
}

// Download solver data.
// Argument: string (the name of the solver)
async function downloadSolverData(id, solver) {
  const async_return = (success, message = "") => postMessage({
    command: "download solver data",
    id: id,
    arg: {
      success: success,
      message: message
    }
  });

  if (!(await nissy.isSolverAvailable(solver))) {
    async_return(false, "Error: solver " + arg.solver +
      " is not available in this version of nissy.");
    return;
  }

  if (await nissy.initSolverDownload(solver, "/tables")) {
    async_return(true);
  } else {
    async_return(false, "Error retrieving solver data");
  }
}

// Generate solver data locally.
// Argument: string (the name of the solver)
async function generateSolverData(id, solver) {
  const async_return = (success, message = "") => postMessage({
    command: "generate solver data",
    id: id,
    arg: {
      success: success,
      message: message
    }
  });

  if (!(await nissy.isSolverAvailable(solver))) {
    async_return(false, "Error: solver " + arg.solver +
      " is not available in this version of nissy.");
    return;
  }

  if (await nissy.initSolverGenerate(solver)) {
    async_return(true);
  } else {
    async_return(false, "Error generating solver data");
  }
}

// Check if the scramble is valid.
// Argument: string (the scramble).
async function validateScramble(id, arg) {
  const async_return = (success) => postMessage({
    command: "validate scramble",
    id: id,
    arg: success
  });

  var cube = new nissy.Cube();
  async_return(cube.move(arg).ok());
}

// Solve the cube with the given options.
// Argument: {
//   solver: string
//   scramble: string
//   minmoves: number
//   maxmoves: number
//   maxsolutions: number
//   optimal: number
//   threads: number
// }
async function solve(id, arg) {
  const async_return = (success, solutions = [], message = "") => postMessage({
    command: "solve",
    id: id,
    arg: {
      success: success,
      solutions: solutions,
      message: message
    }
  });

  if (!(await nissy.isSolverAvailable(arg.solver))) {
    async_return(false, [], "Error: solver " + arg.solver +
      " is not available in this version of nissy.");
    return;
  }

  if (!(await nissy.isSolverLoaded(arg.solver)) &&
      !(await nissy.initSolverFromStorage(arg.solver))) {
    async_return(false, [], "Error: solver " + arg.solver + " has not been " +
      "loaded. Its data must be donwloaded or generated before using it.");
    return;
  }

  var cube = new nissy.Cube();
  cube.move(arg.scramble);

  // TODO: error handling here?
  const nissFlag = ((str) => {
    switch (str) {
      case "inverse": return nissy.NissFlag.inverse;
      case "mixed": return nissy.NissFlag.mixed;
      case "linear": return nissy.NissFlag.linear;
      case "all": return nissy.NissFlag.all;
      default: return nissy.NissFlag.normal;
    }
  })(arg.nissFlag);

  const result = await nissy.solve(arg.solver, cube, nissFlag, arg.minmoves,
    arg.maxmoves, arg.maxsolutions, arg.optimal, arg.threads, id);

  if (result.err.ok()) {
    //TODO: add solution lenght?
    var solutions = result.solutions == "" ? [] : result.solutions.split("\n");
    solutions.pop(); // Solver always returns string ending in newline
    async_return(true, solutions);
  } else {
    async_return(false, [],
      "Error while solving (error " + result.err.value + ")");
  }
};

Generated with cgit - Back to sebastiano.tronto.net