]> git.sesse.net Git - remoteglot/blob - www/js/book.js
Click on book line headings to sort by that heading.
[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 var sort_by = 1;
59
60 var show_lines = function(data, game) {
61         var moves = data['moves'];
62         $('#numviewers').text(data['opening']);
63         var total_num = 0;
64         for (var i = 0; i < moves.length; ++i) {
65                 var move = moves[i];
66                 total_num += parseInt(move['white']);
67                 total_num += parseInt(move['draw']);
68                 total_num += parseInt(move['black']);
69         }
70
71         var headings_tr = $("#headings");
72         headings_tr.empty();
73         for (var i = 0; i < headings.length; ++i) {
74                 var th = document.createElement("th");
75                 headings_tr.append(th);
76                 $(th).text(headings[i][0]);
77                 (function(new_sort_by) {
78                         $(th).click(function() {
79                                 sort_by = new_sort_by;
80                                 show_lines(data, game);
81                         });
82                 })(i);
83         }
84
85         var lines = [];
86         for (var i = 0; i < moves.length; ++i) {
87                 var move = moves[i];
88                 var line = [];
89
90                 var white = parseInt(move['white']);
91                 var draw = parseInt(move['draw']);
92                 var black = parseInt(move['black']);
93
94                 line.push(move['move']);  // Move.
95                 var num = white + draw + black;
96                 line.push(num);  // N.
97                 line.push(num / total_num);  // %.
98
99                 // Win%.
100                 var white_win_ratio = (white + 0.5 * draw) / num;
101                 var win_ratio = (game.turn() == 'w') ? white_win_ratio : 1.0 - white_win_ratio;
102                 line.push(win_ratio);
103
104                 line.push(white);        // WWin.
105                 line.push(white / num);  // %WW.
106                 line.push(black);        // BWin.
107                 line.push(black / num);  // %BW.
108                 line.push(draw);         // Draw.
109                 line.push(draw / num);   // %Draw.
110
111                 if (move['num_elo'] >= 10) {
112                         // Elo.
113                         line.push(move['white_avg_elo']);
114                         line.push(move['black_avg_elo']);
115                         line.push(move['white_avg_elo'] - move['black_avg_elo']);
116
117                         // Win% corrected for Elo.
118                         var win_elo = -400.0 * Math.log(1.0 / white_win_ratio - 1.0) / Math.LN10;
119                         win_elo -= (move['white_avg_elo'] - move['black_avg_elo']);
120                         white_win_ratio = 1.0 / (1.0 + Math.pow(10, win_elo / -400.0));
121                         win_ratio = (game.turn() == 'w') ? white_win_ratio : 1.0 - white_win_ratio;
122                         line.push(win_ratio);
123                 } else {
124                         line.push(null);
125                         line.push(null);
126                         line.push(null);
127                         line.push(null);
128                 }
129                 lines.push(line);
130         }
131
132         lines.sort(function(a, b) { return b[sort_by] - a[sort_by]; });
133
134         var tbl = $("#lines");
135         tbl.empty();
136
137         for (var i = 0; i < moves.length; ++i) {
138                 var line = lines[i];
139                 var tr = document.createElement("tr");
140
141                 for (var j = 0; j < line.length; ++j) {
142                         if (line[j] === null) {
143                                 add_td(tr, "");
144                         } else if (headings[j][1] == TYPE_MOVE) {
145                                 var td = document.createElement("td");
146                                 tr.appendChild(td);
147                                 $(td).addClass("move");
148                                 var move_a = document.createElement("a");
149                                 move_a.href = "javascript:make_move('" + line[j] + "')";
150                                 td.appendChild(move_a);
151                                 $(move_a).text(line[j]);
152                         } else if (headings[j][1] == TYPE_INTEGER) {
153                                 add_td(tr, line[j]);
154                         } else if (headings[j][1] == TYPE_FLOAT) {
155                                 add_td(tr, line[j].toFixed(1));
156                         } else {
157                                 add_td(tr, (100.0 * line[j]).toFixed(1) + "%");
158                         }
159                 }
160
161                 tbl.append(tr);
162         }
163 }
164
165 var make_move = function(move) {
166         moves.length = move_override;
167         moves.push(move);
168         move_override = moves.length;
169         update();
170 }
171 window['make_move'] = make_move;
172
173 var prev_move = function() {
174         if (move_override > 0) {
175                 --move_override;
176                 update();
177         }
178 }
179 window['prev_move'] = prev_move;
180
181 var next_move = function() {
182         if (move_override < moves.length) {
183                 ++move_override;
184                 update();
185         }
186 }
187 window['next_move'] = next_move;
188
189 // almost all of this stuff comes from the chessboard.js example page
190 var onDragStart = function(source, piece, position, orientation) {
191         var game = get_game();
192         if (game.game_over() === true ||
193             (game.turn() === 'w' && piece.search(/^b/) !== -1) ||
194             (game.turn() === 'b' && piece.search(/^w/) !== -1)) {
195                 return false;
196         }
197 }
198
199 var onDrop = function(source, target) {
200         // see if the move is legal
201         var game = get_game();
202         var move = game.move({
203                 from: source,
204                 to: target,
205                 promotion: 'q' // NOTE: always promote to a queen for example simplicity
206         });
207
208         // illegal move
209         if (move === null) return 'snapback';
210
211         moves = game.history({ verbose: true });
212         move_override = moves.length;
213 };
214
215 // update the board position after the piece snap 
216 // for castling, en passant, pawn promotion
217 var onSnapEnd = function() {
218         var game = get_game();
219         board.position(game.fen());
220         fetch_analysis();
221 };
222
223 var init = function() {
224         // Create board.
225         board = new window.ChessBoard('board', {
226                 draggable: true,
227                 position: 'start',
228                 onDragStart: onDragStart,
229                 onDrop: onDrop,
230                 onSnapEnd: onSnapEnd
231         });
232         update();
233
234         $(window).keyup(function(event) {
235                 if (event.which == 39) {
236                         next_move();
237                 } else if (event.which == 37) {
238                         prev_move();
239                 }
240         });
241 }
242
243
244 $(document).ready(init);
245
246 })();