]> git.sesse.net Git - remoteglot/blob - www/js/book.js
Refactor/unify book line display.
[remoteglot] / www / js / book.js
1 (function() {
2
3 var board = null;
4 var moves = [];
5 var move_override = 0;
6
7 var get_game = function() {
8         var game = new Chess();
9         for (var i = 0; i < move_override; ++i) {
10                 game.move(moves[i]);
11         }
12         return game;
13 }
14
15 var update = function() {
16         var game = get_game();
17         board.position(game.fen());
18         fetch_analysis();
19 }
20
21 var fetch_analysis = function() {
22         var game = get_game();
23         $.ajax({
24                 url: "/opening-stats.pl?fen=" + encodeURIComponent(game.fen())
25         }).done(function(data, textstatus, xhr) {
26                 show_lines(data, game);
27         });
28 }
29
30 var add_td = function(tr, value) {
31         var td = document.createElement("td");
32         tr.appendChild(td);
33         $(td).addClass("num");
34         $(td).text(value);
35 }
36
37 var TYPE_MOVE = 0;
38 var TYPE_INTEGER = 1;
39 var TYPE_FLOAT = 2;
40 var TYPE_RATIO = 3;
41
42 var headings = [
43         [ "Move", TYPE_MOVE ],
44         [ "Games", TYPE_INTEGER ],
45         [ "%", TYPE_RATIO ],
46         [ "Win%", TYPE_RATIO ],
47         [ "WWin", TYPE_INTEGER ],
48         [ "%WW", TYPE_RATIO ],
49         [ "Bwin", TYPE_INTEGER ],
50         [ "%BW", TYPE_RATIO ],
51         [ "Draw", TYPE_INTEGER ],
52         [ "Draw%", TYPE_RATIO ],
53         [ "AvWElo", TYPE_FLOAT ],
54         [ "AvBElo", TYPE_FLOAT ],
55         [ "EloVar", TYPE_FLOAT ],
56         [ "AWin%", TYPE_RATIO ],
57 ];
58
59 var show_lines = function(data, game) {
60         var moves = data['moves'];
61         $('#numviewers').text(data['opening']);
62         var total_num = 0;
63         for (var i = 0; i < moves.length; ++i) {
64                 var move = moves[i];
65                 total_num += parseInt(move['white']);
66                 total_num += parseInt(move['draw']);
67                 total_num += parseInt(move['black']);
68         }
69
70         var headings_tr = $("#headings");
71         headings_tr.empty();
72         for (var i = 0; i < headings.length; ++i) {
73                 var th = document.createElement("th");
74                 headings_tr.append(th);
75                 $(th).text(headings[i][0]);
76         }
77
78         var lines = [];
79         for (var i = 0; i < moves.length; ++i) {
80                 var move = moves[i];
81                 var line = [];
82
83                 var white = parseInt(move['white']);
84                 var draw = parseInt(move['draw']);
85                 var black = parseInt(move['black']);
86
87                 line.push(move['move']);  // Move.
88                 var num = white + draw + black;
89                 line.push(num);  // N.
90                 line.push(num / total_num);  // %.
91
92                 // Win%.
93                 var white_win_ratio = (white + 0.5 * draw) / num;
94                 var win_ratio = (game.turn() == 'w') ? white_win_ratio : 1.0 - white_win_ratio;
95                 line.push(win_ratio);
96
97                 line.push(white);        // WWin.
98                 line.push(white / num);  // %WW.
99                 line.push(black);        // BWin.
100                 line.push(black / num);  // %BW.
101                 line.push(draw);         // Draw.
102                 line.push(draw / num);   // %Draw.
103
104                 if (move['num_elo'] >= 10) {
105                         // Elo.
106                         line.push(move['white_avg_elo']);
107                         line.push(move['black_avg_elo']);
108                         line.push(move['white_avg_elo'] - move['black_avg_elo']);
109
110                         // Win% corrected for Elo.
111                         var win_elo = -400.0 * Math.log(1.0 / white_win_ratio - 1.0) / Math.LN10;
112                         win_elo -= (move['white_avg_elo'] - move['black_avg_elo']);
113                         white_win_ratio = 1.0 / (1.0 + Math.pow(10, win_elo / -400.0));
114                         win_ratio = (game.turn() == 'w') ? white_win_ratio : 1.0 - white_win_ratio;
115                         line.push(win_ratio);
116                 } else {
117                         line.push(null);
118                         line.push(null);
119                         line.push(null);
120                         line.push(null);
121                 }
122                 lines.push(line);
123         }
124
125         var tbl = $("#lines");
126         tbl.empty();
127
128         for (var i = 0; i < moves.length; ++i) {
129                 var line = lines[i];
130                 var tr = document.createElement("tr");
131
132                 for (var j = 0; j < line.length; ++j) {
133                         if (line[j] === null) {
134                                 add_td(tr, "");
135                         } else if (headings[j][1] == TYPE_MOVE) {
136                                 var td = document.createElement("td");
137                                 tr.appendChild(td);
138                                 $(td).addClass("move");
139                                 var move_a = document.createElement("a");
140                                 move_a.href = "javascript:make_move('" + line[j] + "')";
141                                 td.appendChild(move_a);
142                                 $(move_a).text(line[j]);
143                         } else if (headings[j][1] == TYPE_INTEGER) {
144                                 add_td(tr, line[j]);
145                         } else if (headings[j][1] == TYPE_FLOAT) {
146                                 add_td(tr, line[j].toFixed(1));
147                         } else {
148                                 add_td(tr, (100.0 * line[j]).toFixed(1) + "%");
149                         }
150                 }
151
152                 tbl.append(tr);
153         }
154 }
155
156 var make_move = function(move) {
157         moves.length = move_override;
158         moves.push(move);
159         move_override = moves.length;
160         update();
161 }
162 window['make_move'] = make_move;
163
164 var prev_move = function() {
165         if (move_override > 0) {
166                 --move_override;
167                 update();
168         }
169 }
170 window['prev_move'] = prev_move;
171
172 var next_move = function() {
173         if (move_override < moves.length) {
174                 ++move_override;
175                 update();
176         }
177 }
178 window['next_move'] = next_move;
179
180 // almost all of this stuff comes from the chessboard.js example page
181 var onDragStart = function(source, piece, position, orientation) {
182         var game = get_game();
183         if (game.game_over() === true ||
184             (game.turn() === 'w' && piece.search(/^b/) !== -1) ||
185             (game.turn() === 'b' && piece.search(/^w/) !== -1)) {
186                 return false;
187         }
188 }
189
190 var onDrop = function(source, target) {
191         // see if the move is legal
192         var game = get_game();
193         var move = game.move({
194                 from: source,
195                 to: target,
196                 promotion: 'q' // NOTE: always promote to a queen for example simplicity
197         });
198
199         // illegal move
200         if (move === null) return 'snapback';
201
202         moves = game.history({ verbose: true });
203         move_override = moves.length;
204 };
205
206 // update the board position after the piece snap 
207 // for castling, en passant, pawn promotion
208 var onSnapEnd = function() {
209         var game = get_game();
210         board.position(game.fen());
211         fetch_analysis();
212 };
213
214 var init = function() {
215         // Create board.
216         board = new window.ChessBoard('board', {
217                 draggable: true,
218                 position: 'start',
219                 onDragStart: onDragStart,
220                 onDrop: onDrop,
221                 onSnapEnd: onSnapEnd
222         });
223         update();
224
225         $(window).keyup(function(event) {
226                 if (event.which == 39) {
227                         next_move();
228                 } else if (event.which == 37) {
229                         prev_move();
230                 }
231         });
232 }
233
234
235 $(document).ready(init);
236
237 })();