1 var grpc = require('grpc');
2 var Chess = require('../www/js/chess.min.js').Chess;
4 var PROTO_PATH = __dirname + '/hashprobe.proto';
5 var hashprobe_proto = grpc.load(PROTO_PATH).hashprobe;
7 // TODO: Make destination configurable.
8 var client = new hashprobe_proto.HashProbe('localhost:50051', grpc.credentials.createInsecure());
10 var board = new Chess();
12 var handle_request = function(fen, response) {
13 if (!board.validate_fen(fen).valid) {
14 response.writeHead(400, {});
18 client.probe({fen: fen}, function(err, probe_response) {
20 response.writeHead(500, {});
23 handle_response(fen, response, probe_response);
27 exports.handle_request = handle_request;
29 var handle_response = function(fen, response, probe_response) {
32 var root = translate_line(board, fen, probe_response['root']);
33 for (var i = 0; i < probe_response['line'].length; ++i) {
34 var line = probe_response['line'][i];
35 var uci_move = line['move']['from_sq'] + line['move']['to_sq'] + line['move']['promotion'];
36 lines[uci_move] = translate_line(board, fen, line);
39 var text = JSON.stringify({
44 'Content-Type': 'text/json; charset=utf-8'
45 //'Content-Length': text.length
47 response.writeHead(200, headers);
52 var translate_line = function(board, fen, line) {
55 var toplay = board.turn();
57 if (line['move'] && line['move']['from_sq']) {
58 var promo = line['move']['promotion'];
60 r['pretty_move'] = board.move({ from: line['move']['from_sq'], to: line['move']['to_sq'], promotion: promo.toLowerCase() }).san;
62 r['pretty_move'] = board.move({ from: line['move']['from_sq'], to: line['move']['to_sq'] }).san;
65 r['pretty_move'] = '';
67 r['sort_key'] = r['pretty_move'];
72 r['depth'] = line['depth'];
76 if (r['pretty_move']) {
77 pv.push(r['pretty_move']);
79 for (var j = 0; j < line['pv'].length; ++j) {
80 var move = line['pv'][j];
81 var decoded = board.move({ from: move['from_sq'], to: move['to_sq'], promotion: move['promotion'] });
82 if (decoded === null) {
89 // Convert the score. Use the static eval if no search.
90 var value = line['value'] || line['eval'];
92 if (value['score_type'] === 'SCORE_CP') {
93 score = ['cp', value['score_cp']];
94 } else if (value['score_type'] === 'SCORE_MATE') {
95 score = ['m', value['score_mate']];
98 if (line['bound'] === 'BOUND_UPPER') {
100 } else if (line['bound'] === 'BOUND_LOWER') {