forked from bonezone2001/auto-chess-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
52 lines (42 loc) · 1.17 KB
/
index.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
// Dependancies
const express = require('express');
const bodyParser = require('body-parser')
// Engine path
const enginePath = "stockfish.exe";
// Setup server
const app = express();
// Parse data into body
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// Setup chess engine
const Engine = require('node-uci').Engine;
var engine = new Engine(enginePath);
async function setup() {
engine = new Engine(enginePath);
await engine.init();
await engine.setoption('Hash', 2048);
await engine.setoption('Threads', 6);
await engine.isready();
}
(async () => {
setup();
// Routes
app.get('/api/solve', async (req, res) => {
console.log(`Ran for ${req.query.fen}`);
try {
await engine.position(req.query.fen);
console.log(`Successfully applied fen`);
// Set the board to fen string provided and solve it
const result = await engine.go({ depth: 16 });
// Send back the results
console.log(`Got result!`);
res.send(result.bestmove);
} catch (error) {
setup();
console.log(error);
res.send("Failed to get AI result!");
}
});
// Start listening
app.listen(3000);
})()