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