-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
executable file
·199 lines (185 loc) · 9.68 KB
/
server.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
#!/usr/bin/env node
// ora = require('ora'),
var l = console.log,
clear = require('cli-clear'),
robot = require("robotjs"),
md5 = require('md5'),
Engine = require('node-uci').Engine,
fs = require('fs'),
express = require('express'),
_ = require('underscore'),
c = require('chalk'),
config = require('./config'),
https = require('https'),
cors = require('cors'),
bodyParser = require('body-parser'),
prompt = require('cli-prompt'),
completedMoves = [],
games = [],
cachedBoardLocationsFile = __dirname + '/.boardLocations.json';
try {
var cachedBoardLocations = JSON.parse(fs.readFileSync(cachedBoardLocationsFile).toString());
} catch (e) {
var cachedBoardLocations = {};
};
clear();
robot.setMouseDelay(config.mouseMoveDelay);
prompt(c.green('Which player do you want to control? Enter player name, white, or black.\n\tDefault is ' + c.white(config.playerName) + ': '), function(val) {
if (val == 'white')
var controlledPlayer = 'white';
else if (val == 'black')
var controlledPlayer = 'black';
else {
if (val == '')
val = config.playerName;
var controlledPlayer = val;
}
console.log(c.green('Controlling player ') + c.white(controlledPlayer));
prompt(c.green('Hit enter to use previous location (' + c.white(JSON.stringify(cachedBoardLocations.topLeft)) + ') -or- Move mouse to the top left of the board and hit ' + c.white('y')), function(topLeft) {
if (topLeft == 'y')
var topLeft = robot.getMousePos();
else
var topLeft = cachedBoardLocations.topLeft;
prompt(c.green('Hit enter to use previous newGameButton (' + c.white(JSON.stringify(cachedBoardLocations.newGameButton)) + ') -or- Move mouse to the new game button location and hit ' + c.white('y')), function(newGameButton) {
if (newGameButton == 'y')
var newGameButton = robot.getMousePos();
else
var newGameButton = cachedBoardLocations.newGameButton;
prompt(c.green('Hit enter to use previous location (' + c.white(JSON.stringify(cachedBoardLocations.bottomRight)) + ') -or- Move mouse to the bottom right of the board and hit ' + c.white('y')), function(bottomRight) {
if (bottomRight == 'y')
var bottomRight = robot.getMousePos();
else
var bottomRight = cachedBoardLocations.bottomRight;
var boardLocations = {
topLeft: topLeft,
bottomRight: bottomRight,
newGameButton: newGameButton,
};
fs.writeFileSync(cachedBoardLocationsFile, JSON.stringify(boardLocations));
var boardConstraints = {
topLeft: topLeft,
bottomRight: bottomRight,
boxWidth: parseInt((bottomRight.x - topLeft.x) / 8),
bottomRight: bottomRight,
topLeft: topLeft
};
var app = express();
app.use(bodyParser.json());
app.use(cors());
/*
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});*/
app.get('/', function(req, res) {
res.send('Hello World!');
});
app.post('/newGame', function(req, res) {
//console.log(req.body);
l('Clicking new game @ ' + JSON.stringify(newGameButton));
// robot.moveMouseSmooth(newGameButton.X, newGameButton.Y);
// robot.mouseClick();
});
app.post('/api/', function(req, res) {
//console.log(req.body);
var gameState = req.body;
var gameID = gameState.id,
moves = gameState.moves;
var initialMovesMode = (parseInt(gameState.moves.length / 2) <= config.initialManualMoves);
///l('# Moves: ' + gameState.moves, gameState.moves.length);
//l('Initial Manual Moves: ' + config.initialManualMoves);
if (initialMovesMode) {
//l('In initial moves mode...');
}
var moveHash = md5(md5(JSON.stringify(gameState.moves)) + md5(JSON.stringify(gameState.id)));
if (!_.contains(games, gameState.id))
games.push(gameState.id);
if (_.contains(completedMoves, moveHash)) {
return res.json({});
}
completedMoves.push(moveHash);
var index = 0;
var Moves = [];
if (gameState.seq % 2 == 0) {
var turn = 'white';
var pid = 0;
} else {
var turn = 'black';
var pid = 1;
}
var playerTurn = gameState.players[pid].uid;
while (index < gameState.moves.length - 1) {
var s = gameState.moves[index];
index++;
s += gameState.moves[index];
index++;
Moves.push(config.decodeMove(s));
}
var startedTs = Date.now(),
gameStateFile = __dirname + '/.lastGameState_' + startedTs + '.json';
fs.writeFileSync(gameStateFile, JSON.stringify(gameState));
var engine = new Engine(config.engine);
engine.chain()
.init()
.setoption('MultiPV', config.principalVariation)
.setoption('Threads', config.Threads)
.setoption('Hash', config.Hash)
.setoption('Skill', config.Skill)
.setoption('Hash', config.Hash)
.setoption('Ponder', config.Ponder)
.position('startpos', Moves)
.go({
depth: config.depth,
})
.then(function(result) {
var duration = Date.now() - startedTs;
if (result.bestmove.length == 5) {
result.bestmove = result.bestmove[0] + result.bestmove[1] + result.bestmove[2] + result.bestmove[3];
}
res.json({
duration: duration,
bestmove: result.bestmove
});
if (turn == controlledPlayer || playerTurn == controlledPlayer) {
var movePosition = {
from: config.moveToPosition(boardConstraints, result.bestmove[0] + result.bestmove[1]),
to: config.moveToPosition(boardConstraints, result.bestmove[2] + result.bestmove[3]),
};
var msg = c.green(' Best move calculated in ' + c.white(duration) + 'ms.\tMoving mouse to perform move ' + c.white(result.bestmove));
//var moveSpinner = spinner = ora(msg).start();
robot.moveMouseSmooth(movePosition.from.X, movePosition.from.Y);
var moveDelay = (Math.floor(Math.random() * 2) + 0) * 1;
//setTimeout(function() {
setTimeout(function() {
robot.mouseClick();
setTimeout(function() {
robot.mouseClick();
setTimeout(function() {
robot.mouseToggle('down');
setTimeout(function() {
robot.moveMouseSmooth(movePosition.to.X, movePosition.to.Y);
setTimeout(function() {
robot.mouseToggle('up');
// moveSpinner.succeed();
// console.log('\t\t' + c.yellow('[Move Complete!]'));
}, config.mouseDelays.beforeMouseRelease);
}, config.mouseEventDelay);
}, config.mouseEventDelay);
}, config.mouseEventDelay);
}, config.mouseEventDelay);
//}, moveDelay + config.moveDelay(result));
}
});
delete engine;
});
var secureServer = https.createServer({
key: fs.readFileSync(__dirname + '/server.key'),
cert: fs.readFileSync(__dirname + '/server.crt'),
}, app).listen(config.port, function() {
console.log('API Server Started On Port %d', config.port);
});
});
});
});
});