forked from nmrugg/stockfish.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
postscript.js
243 lines (214 loc) · 7.64 KB
/
postscript.js
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
return Stockfish;
/// End of STOCKFISH()
};
(function ()
{
var isNode;
var args;
var myEngine;
var queue = [];
var myConsole = {
log: log,
error: log,
warn: log,
}
function log(line)
{
/// Uncomment for debugging
//console.log("-->", line);
}
function completer(line)
{
var completions = [
"d",
"eval",
"exit",
"flip",
"go",
"isready",
"ponderhit",
"position fen ",
"position startpos",
"position startpos moves",
"quit",
"setoption name Clear Hash value ",
"setoption name Contempt value ",
"setoption name Hash value ",
"setoption name Minimum Thinking Time value ",
"setoption name Move Overhead value ",
"setoption name MultiPV value ",
"setoption name Ponder value ",
"setoption name Skill Level Maximum Error value ",
"setoption name Skill Level Probability value ",
"setoption name Skill Level value ",
"setoption name Slow Mover value ",
"setoption name Threads value ",
"setoption name UCI_Chess960 value false",
"setoption name UCI_Chess960 value true",
"setoption name UCI_Variant value chess",
"setoption name UCI_Variant value atomic",
"setoption name UCI_Variant value crazyhouse",
"setoption name UCI_Variant value giveaway",
"setoption name UCI_Variant value horde",
"setoption name UCI_Variant value kingofthehill",
"setoption name UCI_Variant value racingkings",
"setoption name UCI_Variant value relay",
"setoption name UCI_Variant value threecheck",
"setoption name Use NNUE value true",
"setoption name Use NNUE value false",
"setoption name nodestime value ",
"stop",
"uci",
"ucinewgame"
];
var completionsMid = [
"binc ",
"btime ",
"confidence ",
"depth ",
"infinite ",
"mate ",
"maxdepth ",
"maxtime ",
"mindepth ",
"mintime ",
"moves ", /// for position fen ... moves
"movestogo ",
"movetime ",
"ponder ",
"searchmoves ",
"shallow ",
"winc ",
"wtime "
];
function filter(c)
{
return c.indexOf(line) === 0;
}
/// This looks for completions starting at the very beginning of the line.
/// If the user has typed nothing, it will match everything.
var hits = completions.filter(filter);
if (!hits.length) {
/// Just get the last word.
line = line.replace(/^.*\s/, "");
if (line) {
/// Find completion mid line too.
hits = completionsMid.filter(filter);
} else {
/// If no word has been typed, show all options.
hits = completionsMid;
}
}
return [hits, line];
}
isNode = typeof global !== "undefined" && Object.prototype.toString.call(global.process) === "[object process]";
if (isNode) {
global.XMLHttpRequest = function () {
var xhr = {
open: function (_, url) {
xhr._path = url;
},
send: function ()
{
xhr.response = require("fs").readFileSync(require("path").join(__dirname, xhr._path));
xhr.status = 200;
setImmediate(xhr.onload);
}
};
return xhr;
};
/// Is it a pThread or was it called directly?
if (typeof module === "undefined" || require.main === module) {
Stockfish = STOCKFISH(myConsole, require("path").join(require.main.path, "stockfish.wasm"));
if (require("worker_threads").isMainThread) {
myConsole.log = console.log;
require("readline").createInterface({
input: process.stdin,
output: process.stdout,
completer: completer
}).on("line", function online(line)
{
if (line) {
if (line === "quit" || line === "exit") {
process.exit();
}
if (myEngine) {
myEngine.postMessage(line, true);
} else {
queue.push(line);
}
}
}).on("SIGINT", process.exit).on("close", process.exit).setPrompt("");
process.stdin.on("end", process.exit);
}
Stockfish().then(function (sf)
{
myEngine = sf;
sf.addMessageListener(function onlog(line)
{
console.log(line);
});
if (queue.length) {
queue.forEach(function (line)
{
sf.postMessage(line, true);
});
}
queue = null;
});
/// Is this a node module?
} else {
module.exports = STOCKFISH;
}
/// Is it a web worker?
} else if (typeof onmessage !== "undefined" && (typeof window === "undefined" || typeof window.document === "undefined")) {
if (self && self.location && self.location.hash) {
args = self.location.hash.substr(1).split(",");
Stockfish = STOCKFISH(myConsole, args[0], !Number(args[1]));
/// If this is a pthread, then we need to stop here.
if (args[1]) {
return;
}
} else {
Stockfish = STOCKFISH(myConsole, "", true);
}
/// Make sure that this is only added once.
if (!onmessage) {
onmessage = function (event)
{
if (myEngine) {
myEngine.postMessage(event.data, true);
} else {
queue.push(event.data);
}
};
}
Stockfish().then(function (sf)
{
///NOTE: To get the number of loaded threads loop through the sf.PThread.runningWorkers array and check for .loaded.
myEngine = sf;
sf.addMessageListener(function onlog(line)
{
postMessage(line);
});
if (queue.length) {
queue.forEach(function (line)
{
sf.postMessage(line, true);
});
}
queue = null;
});
}
///NOTE: If it's a normal browser, we don't need to do anything. The client can use the INIT_ENGINE() function directly.
}());
/// End of init();
}
if ((typeof self !== "undefined" && self.location.hash.split(",")[1] === "1") || (typeof global !== "undefined" && Object.prototype.toString.call(global.process) === "[object process]" && !require("worker_threads").isMainThread)) {
(function ()
{
/// Insert worker here
}());
} else {
INIT_ENGINE();
}