]> git.sesse.net Git - remoteglot/blob - www/js/book.js
dfbc71fab1cb46064fafd994148d9e826f9c3c1b
[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 headings = [
38         "Move", "Games", "%", "Win%", "WWin", "%WW", "Bwin", "%BW", "Draw", "Draw%",
39         "AvWElo", "AvBElo", "EloVar", "AWin%"
40 ];
41
42 var show_lines = function(data, game) {
43         var moves = data['moves'];
44         $('#numviewers').text(data['opening']);
45         var total_num = 0;
46         for (var i = 0; i < moves.length; ++i) {
47                 var move = moves[i];
48                 total_num += parseInt(move['white']);
49                 total_num += parseInt(move['draw']);
50                 total_num += parseInt(move['black']);
51         }
52
53         var headings_tr = $("#headings");
54         headings_tr.empty();
55         for (var i = 0; i < headings.length; ++i) {
56                 var th = document.createElement("th");
57                 headings_tr.append(th);
58                 $(th).text(headings[i]);
59         }
60
61         var tbl = $("#lines");
62         tbl.empty();
63
64         for (var i = 0; i < moves.length; ++i) {
65                 var move = moves[i];
66                 var tr = document.createElement("tr");
67
68                 var white = parseInt(move['white']);
69                 var draw = parseInt(move['draw']);
70                 var black = parseInt(move['black']);
71
72                 // Move.
73                 var move_td = document.createElement("td");
74                 tr.appendChild(move_td);
75                 $(move_td).addClass("move");
76
77                 var move_a = document.createElement("a");
78                 move_a.href = "javascript:make_move('" + move['move'] + "')";
79                 move_td.appendChild(move_a);
80                 $(move_a).text(move['move']);
81
82                 // N.
83                 var num = white + draw + black;
84                 add_td(tr, num);
85
86                 // %.
87                 add_td(tr, (100.0 * num / total_num).toFixed(1) + "%");
88
89                 // Win%.
90                 var white_win_ratio = (white + 0.5 * draw) / num;
91                 var win_ratio = (game.turn() == 'w') ? white_win_ratio : 1.0 - white_win_ratio;
92                 add_td(tr, ((100.0 * win_ratio).toFixed(1) + "%"));
93
94                 // WWin and %WW.
95                 add_td(tr, white);
96                 add_td(tr, (100.0 * white / num).toFixed(1) + "%");
97
98                 // BWin and %BW.
99                 add_td(tr, black);
100                 add_td(tr, (100.0 * black / num).toFixed(1) + "%");
101
102                 // Draw and %Draw.
103                 add_td(tr, draw);
104                 add_td(tr, ((100.0 * draw / num).toFixed(1) + "%"));
105
106                 if (move['num_elo'] >= 10) {
107                         // Elo.
108                         add_td(tr, move['white_avg_elo'].toFixed(1));
109                         add_td(tr, move['black_avg_elo'].toFixed(1));
110                         add_td(tr, (move['white_avg_elo'] - move['black_avg_elo']).toFixed(1));
111
112                         // Win% corrected for Elo.
113                         var win_elo = -400.0 * Math.log(1.0 / white_win_ratio - 1.0) / Math.LN10;
114                         win_elo -= (move['white_avg_elo'] - move['black_avg_elo']);
115                         white_win_ratio = 1.0 / (1.0 + Math.pow(10, win_elo / -400.0));
116                         win_ratio = (game.turn() == 'w') ? white_win_ratio : 1.0 - white_win_ratio;
117                         add_td(tr, ((100.0 * win_ratio).toFixed(1) + "%"));
118                 } else {
119                         add_td(tr, "");
120                         add_td(tr, "");
121                         add_td(tr, "");
122                         add_td(tr, "");
123                 }
124
125                 if (false) {
126                         // Win bars (W/D/B).
127                         var winbar_td = document.createElement("td");
128                         $(winbar_td).addClass("winbars");
129                         tr.appendChild(winbar_td);
130                         var winbar_table = document.createElement("table");
131                         winbar_td.appendChild(winbar_table);
132                         var winbar_tr = document.createElement("tr");
133                         winbar_table.appendChild(winbar_tr);
134
135                         if (white > 0) {
136                                 var white_percent = (100.0 * white / num).toFixed(0) + "%";
137                                 var white_td = document.createElement("td");
138                                 winbar_tr.appendChild(white_td);
139                                 $(white_td).addClass("white");
140                                 white_td.style.width = white_percent;
141                                 $(white_td).text(white_percent);
142                         }
143                         if (draw > 0) {
144                                 var draw_percent = (100.0 * draw / num).toFixed(0) + "%";
145                                 var draw_td = document.createElement("td");
146                                 winbar_tr.appendChild(draw_td);
147                                 $(draw_td).addClass("draw");
148                                 draw_td.style.width = draw_percent;
149                                 $(draw_td).text(draw_percent);
150                         }
151                         if (black > 0) {
152                                 var black_percent = (100.0 * black / num).toFixed(0) + "%";
153                                 var black_td = document.createElement("td");
154                                 winbar_tr.appendChild(black_td);
155                                 $(black_td).addClass("black");
156                                 black_td.style.width = black_percent;
157                                 $(black_td).text(black_percent);
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 })();