-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
206 lines (179 loc) · 6.1 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
200
201
202
203
204
205
206
var express = require('express');
var app = express();
var connection = require('./database/database');
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);
require('./middleware/express-config')(app);
require('./router/index')(app);
connection.once('open', function() {
server.listen(3000, function() {
console.log('Listening on port %d', server.address().port);
});
});
var Engine = require('uci');
var engine = new Engine("stockfish");
engine.runProcess().then(function () {
console.log('Started');
return engine.uciCommand();
}).then(function (idAndOptions) {
console.log('Engine name - ' + idAndOptions.id.name);
return engine.isReadyCommand();
}).then(function () {
console.log('Ready');
return engine.uciNewGameCommand();
}).done();
io.sockets.on('connection', function (socket) {
socket.on('join room', function(data) {
var room = data.room;
if (socket.room) {
console.log("Leaving room");
socket.leave(room);
socket.room = undefined;
}
console.log("Joining room");
socket.join(room);
socket.room = room;
io.sockets.emit('roomConnected', {room: room});
var clients = findClientsSocket(room);
if (clients.length > 0) {
var socketId = clients[0].id;
var mySocketId = socket.id;
io.sockets.connected[socketId].emit('canIhaveYourGameData', { socketId: mySocketId });
}
engine.stopCommand();
});
socket.on('leave room', function(data) {
if (socket.room) {
var room = socket.room;
socket.leave(room);
console.log("Leaving room");
socket.room = undefined;
}
io.sockets.emit('roomDisconnected', { room: "hi" });
engine.stopCommand();
});
socket.on('update client', function(data) {
var socketId = data.socketId;
io.sockets.connected[socketId].emit('getUpdated', data);
});
socket.on('send points', function(data) {
console.log(data);
socket.broadcast.to(socket.room).emit('addPointsForOthers', data);
});
socket.on('remove points', function() {
socket.broadcast.to(socket.room).emit('removePointsForOthers');
});
socket.on('startingGameOver', function() {
socket.broadcast.to(socket.room).emit('startGameOver');
console.log('Stopping analysis');
engine.stopCommand();
});
socket.on('startPos', function() {
socket.broadcast.to(socket.room).emit('startPos');
console.log('Stopping analysis');
engine.stopCommand();
});
socket.on('start analyzing', function(data) {
var fen = data.fen;
io.sockets.in(socket.room).emit('stockfishOn');
stockfishAnalysis(fen, socket);
});
socket.on('stop analyzing', function() {
console.log('Stopping analysis. Turning Stockfish off.');
engine.stopCommand();
io.sockets.in(socket.room).emit('stockfishOff');
});
socket.on('sendPosition', function(data) {
socket.broadcast.to(socket.room).emit('changePosition', data);
var fen = data.gameFen;
if (data.stockfish) {
stockfishAnalysis(fen, socket);
}
});
socket.on('send note', function(data) {
socket.broadcast.to(socket.room).emit('addNewNote', data);
});
socket.on('undo move', function() {
socket.broadcast.to(socket.room).emit('undoMove');
});
socket.on('upload pgn', function(data) {
socket.broadcast.to(socket.room).emit('uploadTheirPgn', data);
});
socket.on('sandbox mode clicked', function() {
socket.broadcast.to(socket.room).emit('sandboxModeClicked');
});
socket.on('sandbox mode clicked with pos', function(data) {
socket.broadcast.to(socket.room).emit('sandboxModeClickedWithPosition', data);
});
socket.on('puzzle clicked', function(data) {
socket.broadcast.to(socket.room).emit('getThePuzzleClicked', data);
});
socket.on('sandbox position', function(data) {
socket.broadcast.to(socket.room).emit('sandboxPositionChanged', data);
});
socket.on('starting pos', function() {
socket.broadcast.to(socket.room).emit('sandboxStartingPos');
});
socket.on('clear board', function() {
socket.broadcast.to(socket.room).emit('sandboxClearBoard');
});
});
// Find clients connected to a room
function findClientsSocket(roomId, namespace) {
var res = []
, ns = io.of(namespace ||"/"); // the default namespace is "/"
if (ns) {
for (var id in ns.connected) {
if(roomId) {
var index = ns.connected[id].rooms.indexOf(roomId) ;
if(index !== -1) {
res.push(ns.connected[id]);
}
} else {
res.push(ns.connected[id]);
}
}
}
return res;
}
// Perform Stockfish analysis
function stockfishAnalysis(fen, socket) {
console.log('Stopping analysis');
engine.stopCommand();
engine.positionCommand(fen).then(function () {
console.log('Starting position set');
console.log('Starting analysis');
return engine.goInfiniteCommand(function infoHandler(info) {
var score = null;
var depth = null;
var nps = null;
var variation = null;
var bestmove = null;
var match = info.match(/^info .*\bdepth (\d+) .*\bnps (\d+)/);
if (match) {
depth = match[1];
nps = 'Nps: ' + match[2];
}
if (match = info.match(/^info .*\bscore (\w+) (-?\d+)/)) {
score = parseInt(match[2]);
if (match[1] == 'cp') {
score = (score / 100.0).toFixed(2);
} else if(match[1] == 'mate') {
score = '#' + score;
}
if(match = info.match(/\b(upper|lower)bound\b/)) {
// console.log("Match: ", match);
// console.log(((match[1] == 'upper') == (game.turn() == 'w') ? '<= ' : '>= ') + engineStatus.score);
}
}
if (match = info.match(/^info .*\bpv ([a-z0-9\s]+)/)) {
variation = match[1];
}
if (match = info.match(/^info .*\bcurrmove ([a-z0-9\s]+) .*\bcurrmovenumber (1$)/)) {
bestmove = match[1];
}
var engineData = {score: score, depth: depth, nps: nps, bestmove: bestmove, variation: variation};
io.sockets.in(socket.room).emit('engineData', engineData);
});
});
}