]> git.sesse.net Git - remoteglot/blob - www/js/hash-lookup.js
9ecca5389009227a14bef7d785af39f52a989ba3
[remoteglot] / www / js / hash-lookup.js
1 var grpc = require('grpc');
2 var Chess = require(__dirname + '/chess.min.js').Chess;
3
4 var PROTO_PATH = __dirname + '/hashprobe.proto';
5 var hashprobe_proto = grpc.load(PROTO_PATH).hashprobe;
6
7 // TODO: Make destination configurable.
8 var client = new hashprobe_proto.HashProbe('localhost:50051', grpc.credentials.createInsecure());
9
10 var board = new Chess();
11
12 var handle_request = function(fen, response) {
13         if (!board.validate_fen(fen).valid) {
14                 response.writeHead(400, {});
15                 response.end();
16                 return;
17         }
18         client.probe({fen: fen}, function(err, probe_response) {
19                 if (err) {
20                         response.writeHead(500, {});
21                         response.end();
22                 } else {
23                         handle_response(fen, response, probe_response);
24                 }
25         });
26 }
27 exports.handle_request = handle_request;
28
29 var handle_response = function(fen, response, probe_response) {
30         var lines = {};
31
32         var root = translate_line(board, fen, probe_response['root'], true);
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, false);
37         }
38
39         var text = JSON.stringify({
40                 root: root,
41                 lines: lines
42         });
43         var headers = {
44                 'Content-Type': 'text/json; charset=utf-8'
45                 //'Content-Length': text.length
46         };
47         response.writeHead(200, headers);
48         response.write(text);
49         response.end();
50 }
51
52 var translate_line = function(board, fen, line, pretty_score) {
53         var r = {};
54         board.load(fen);
55         var toplay = board.turn();
56
57         if (line['move'] && line['move']['from_sq']) {
58                 r['pretty_move'] = board.move({ from: line['move']['from_sq'], to: line['move']['to_sq'], promotion: line['move']['promotion'] }).san;
59         } else {
60                 r['pretty_move'] = '';
61         }
62         r['sort_key'] = r['pretty_move'];
63         if (!line['found']) {
64                 r['pv_pretty'] = [];
65                 r['score_sort_key'] = -100000000;
66                 return r;
67         }
68         r['depth'] = line['depth'];
69
70         // Convert the PV.
71         var pv = [];
72         if (r['pretty_move']) {
73                 pv.push(r['pretty_move']);
74         }
75         for (var j = 0; j < line['pv'].length; ++j) {
76                 var move = line['pv'][j];
77                 var decoded = board.move({ from: move['from_sq'], to: move['to_sq'], promotion: move['promotion'] });
78                 if (decoded === null) {
79                         break;
80                 }
81                 pv.push(decoded.san);
82         }
83         r['pv_pretty'] = pv;
84
85         // Write out the pretty score.
86         // TODO: mates!
87         var score = pretty_score ? 'Score: ' : '';
88
89         if (line['bound'] === 'BOUND_UPPER') {
90                 score += '≤\u00a0';
91         } else if (line['bound'] === 'BOUND_LOWER') {
92                 score += '≥\u00a0';
93         }
94
95         var value = line['value']['score_cp'];
96         if (value > 0) {
97                 score += '+' + (value / 100.0).toFixed(2);
98         } else if (value < 0) {
99                 score += (value / 100.0).toFixed(2);
100         } else if (value == 0) {
101                 score += '0.00';
102         } else {
103                 score += '';
104         }
105         r['pretty_score'] = score;
106         r['score_sort_key'] = score_sort_key(line['value'], toplay === 'b') * 200 + r['depth'];
107
108         return r;
109 }
110
111 var score_sort_key = function(score, invert) {
112         if (score['score_type'] === 'SCORE_MATE') {
113                 var mate = score['score_mate'];
114                 var score;
115                 if (mate > 0) {
116                         // Side to move mates
117                         score = 99999 - mate;
118                 } else {
119                         // Side to move is getting mated (note the double negative for mate)
120                         score = -99999 - mate;
121                 }
122                 if (invert) {
123                         score = -score;
124                 }
125                 return score;
126         } else if (score['score_type'] === 'SCORE_CP') {
127                 var score = score['score_cp'];
128                 if (invert) {
129                         score = -score;
130                 }
131                 return score;
132         }
133
134         return null;
135 }