-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathengine.worker.js
More file actions
49 lines (41 loc) · 1.29 KB
/
engine.worker.js
File metadata and controls
49 lines (41 loc) · 1.29 KB
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
importScripts("chess_engine.js");
let moduleInitialized = false;
let bookLoaded = false;
Module.onRuntimeInitialized = () => {
moduleInitialized = true;
};
loadBook();
self.onmessage = async (e) => {
// Wait for the module to be initialized
while (!moduleInitialized) {
await new Promise((resolve) => setTimeout(resolve, 100));
}
// Wait for the book to be loaded
while (!bookLoaded) {
await new Promise((resolve) => setTimeout(resolve, 100));
}
const { fen, bookPath, timeLeft, inc, moveTime } = e.data;
const bookMove = Module.ccall(
"getBookMove",
"string",
["string", "string"],
[bookPath, fen]
);
if (bookMove) {
self.postMessage({ action: "getBookMove", result: bookMove });
} else {
const bestMove = Module.ccall(
"getBestMove",
"string",
["string", "number", "number", "number", "number"],
[fen, timeLeft, inc, 0, moveTime]
);
self.postMessage({ action: "getBestMove", result: bestMove });
}
};
async function loadBook() {
const response = await fetch('komodo.bin');
const bookData = await response.arrayBuffer();
FS.createDataFile("/", "komodo.bin", new Uint8Array(bookData), true, false);
bookLoaded = true;
}