]> git.sesse.net Git - remoteglot/blob - www/js/book.js
bed05c1de71c8011c197842597c81fa73a27f125
[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 show_lines = function(data, game) {
38         var moves = data['moves'];
39         $('#numviewers').text(data['opening']);
40         var total_num = 0;
41         for (var i = 0; i < moves.length; ++i) {
42                 var move = moves[i];
43                 total_num += parseInt(move['white']);
44                 total_num += parseInt(move['draw']);
45                 total_num += parseInt(move['black']);
46         }       
47
48         var tbl = $("#lines");
49         tbl.empty();
50
51         for (var i = 0; i < moves.length; ++i) {
52                 var move = moves[i];
53                 var tr = document.createElement("tr");
54
55                 var white = parseInt(move['white']);
56                 var draw = parseInt(move['draw']);
57                 var black = parseInt(move['black']);
58
59                 // Move.
60                 var move_td = document.createElement("td");
61                 tr.appendChild(move_td);
62                 $(move_td).addClass("move");
63
64                 var move_a = document.createElement("a");
65                 move_a.href = "javascript:make_move('" + move['move'] + "')";
66                 move_td.appendChild(move_a);
67                 $(move_a).text(move['move']);
68
69                 // #.
70                 var num = white + draw + black;
71                 add_td(tr, num);
72
73                 // %.
74                 add_td(tr, (100.0 * num / total_num).toFixed(1) + "%");
75
76                 // Win%.
77                 var white_win_ratio = (white + 0.5 * draw) / num;
78                 var win_ratio = (game.turn() == 'w') ? white_win_ratio : 1.0 - white_win_ratio;
79                 add_td(tr, ((100.0 * win_ratio).toFixed(1) + "%"));
80
81                 // Elo.
82                 add_td(tr, move['white_avg_elo'].toFixed(1));
83                 add_td(tr, move['black_avg_elo'].toFixed(1));
84
85                 // Win% corrected for Elo.
86                 var win_elo = -400.0 * Math.log(1.0 / white_win_ratio - 1.0) / Math.LN10;
87                 win_elo -= (move['white_avg_elo'] - move['black_avg_elo']);
88                 white_win_ratio = 1.0 / (1.0 + Math.pow(10, win_elo / -400.0));
89                 win_ratio = (game.turn() == 'w') ? white_win_ratio : 1.0 - white_win_ratio;
90                 add_td(tr, ((100.0 * win_ratio).toFixed(1) + "%"));
91
92                 if (false) {
93                         // Win bars (W/D/B).
94                         var winbar_td = document.createElement("td");
95                         $(winbar_td).addClass("winbars");
96                         tr.appendChild(winbar_td);
97                         var winbar_table = document.createElement("table");
98                         winbar_td.appendChild(winbar_table);
99                         var winbar_tr = document.createElement("tr");
100                         winbar_table.appendChild(winbar_tr);
101
102                         if (white > 0) {
103                                 var white_percent = (100.0 * white / num).toFixed(0) + "%";
104                                 var white_td = document.createElement("td");
105                                 winbar_tr.appendChild(white_td);
106                                 $(white_td).addClass("white");
107                                 white_td.style.width = white_percent;
108                                 $(white_td).text(white_percent);
109                         }
110                         if (draw > 0) {
111                                 var draw_percent = (100.0 * draw / num).toFixed(0) + "%";
112                                 var draw_td = document.createElement("td");
113                                 winbar_tr.appendChild(draw_td);
114                                 $(draw_td).addClass("draw");
115                                 draw_td.style.width = draw_percent;
116                                 $(draw_td).text(draw_percent);
117                         }
118                         if (black > 0) {
119                                 var black_percent = (100.0 * black / num).toFixed(0) + "%";
120                                 var black_td = document.createElement("td");
121                                 winbar_tr.appendChild(black_td);
122                                 $(black_td).addClass("black");
123                                 black_td.style.width = black_percent;
124                                 $(black_td).text(black_percent);
125                         }
126                 }
127
128                 tbl.append(tr);
129         }
130 }
131
132 var make_move = function(move) {
133         moves.length = move_override;
134         moves.push(move);
135         move_override = moves.length;
136         update();
137 }
138 window['make_move'] = make_move;
139
140 var prev_move = function() {
141         if (move_override > 0) {
142                 --move_override;
143                 update();
144         }
145 }
146 window['prev_move'] = prev_move;
147
148 var next_move = function() {
149         if (move_override < moves.length) {
150                 ++move_override;
151                 update();
152         }
153 }
154 window['next_move'] = next_move;
155
156 // almost all of this stuff comes from the chessboard.js example page
157 var onDragStart = function(source, piece, position, orientation) {
158         var game = get_game();
159         if (game.game_over() === true ||
160             (game.turn() === 'w' && piece.search(/^b/) !== -1) ||
161             (game.turn() === 'b' && piece.search(/^w/) !== -1)) {
162                 return false;
163         }
164 }
165
166 var onDrop = function(source, target) {
167         // see if the move is legal
168         var game = get_game();
169         var move = game.move({
170                 from: source,
171                 to: target,
172                 promotion: 'q' // NOTE: always promote to a queen for example simplicity
173         });
174
175         // illegal move
176         if (move === null) return 'snapback';
177
178         moves = game.history({ verbose: true });
179         move_override = moves.length;
180 };
181
182 // update the board position after the piece snap 
183 // for castling, en passant, pawn promotion
184 var onSnapEnd = function() {
185         var game = get_game();
186         board.position(game.fen());
187         fetch_analysis();
188 };
189
190 var init = function() {
191         // Create board.
192         board = new window.ChessBoard('board', {
193                 draggable: true,
194                 position: 'start',
195                 onDragStart: onDragStart,
196                 onDrop: onDrop,
197                 onSnapEnd: onSnapEnd
198         });
199         update();
200
201         $(window).keyup(function(event) {
202                 if (event.which == 39) {
203                         next_move();
204                 } else if (event.which == 37) {
205                         prev_move();
206                 }
207         });
208 }
209
210
211 $(document).ready(init);
212
213 })();