-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
116 lines (90 loc) · 2.72 KB
/
index.ts
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
import bodyParser from "body-parser";
import { GameRecord } from "dstnd-io";
import express, { Application, Request, Response } from "express";
import { SearchOptions } from "node-uci";
import { Analyzer, AnalyzerOpts, GameToAnalyze } from "./src/Engine";
const app: Application = express();
const port = 5454;
// Body parsing Middleware
// app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(bodyParser.json());
const analyzersByGameId: Record<GameRecord["id"], Analyzer> = {};
const getExistentAnalyzerByGameId = (gameId: GameToAnalyze["id"]) => {
const { [gameId]: engine } = analyzersByGameId;
return engine;
};
const getExistentAnalyzerOrCreateNew = (
game: GameToAnalyze,
opts?: AnalyzerOpts
) => {
const { [game.id]: engine } = analyzersByGameId;
if (engine && !engine.hasQuit) {
return engine;
}
analyzersByGameId[game.id] = new Analyzer(game, {
...{ searchOpts: { depth: 25, nodes: 25 * 10000 } },
...opts,
});
return analyzersByGameId[game.id];
};
app.get("/", async (req, res) => {
const fen = "rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1";
const engine = getExistentAnalyzerOrCreateNew({ id: "test", fen });
const engineRes = await engine.updateAndSearchOnce(fen);
await engine.quit();
return res.status(200).send({
ok: true,
engineRes,
});
});
app.post("/analyze", async (req: Request, res: Response) => {
try {
const { gameId, fen, searchOpts } = req.body;
if (!(gameId && fen)) {
throw new Error("Body not good");
}
const engine = getExistentAnalyzerOrCreateNew(
{ id: gameId, fen },
typeof searchOpts === "object"
? { searchOpts: searchOpts as SearchOptions }
: {}
);
const engineRes = await engine.updateAndSearchOnce(fen);
return res.status(200).send(engineRes);
} catch (e) {
console.error("Engine errror", e);
return res.status(500).send({
error: e,
});
}
});
app.post("/quit", async (req: Request, res: Response) => {
try {
const { gameId } = req.body;
if (!gameId) {
throw new Error("Body not good");
}
const engine = getExistentAnalyzerByGameId(gameId);
if (!engine) {
return res.status(200).send({ message: "Engine Not Existent" });
}
if (engine.hasQuit) {
return res.status(200).send({ message: "Engine Already Quitted" });
}
await engine.quit();
return res.status(200).send({ message: "Engine Quitted" });
} catch (e) {
console.error("Engine errror", e);
return res.status(500).send({
error: e,
});
}
});
try {
app.listen(port, () => {
console.log(`Connected successfully on port ${port}`);
});
} catch (error) {
console.error(`Error occured`, error);
}