7 var get_game = function() {
8 var game = new Chess();
9 for (var i = 0; i < move_override; ++i) {
15 var update = function() {
16 var game = get_game();
17 board.position(game.fen());
21 var fetch_analysis = function() {
22 var game = get_game();
24 url: "/opening-stats.pl?fen=" + encodeURIComponent(game.fen())
25 }).done(function(data, textstatus, xhr) {
26 show_lines(data, game);
30 var add_td = function(tr, value) {
31 var td = document.createElement("td");
33 $(td).addClass("num");
38 "Move", "Games", "%", "Win%", "WWin", "%WW", "Bwin", "%BW", "Draw", "Draw%",
39 "AvWElo", "AvBElo", "EloVar", "AWin%"
42 var show_lines = function(data, game) {
43 var moves = data['moves'];
44 $('#numviewers').text(data['opening']);
46 for (var i = 0; i < moves.length; ++i) {
48 total_num += parseInt(move['white']);
49 total_num += parseInt(move['draw']);
50 total_num += parseInt(move['black']);
53 var headings_tr = $("#headings");
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]);
61 var tbl = $("#lines");
64 for (var i = 0; i < moves.length; ++i) {
66 var tr = document.createElement("tr");
68 var white = parseInt(move['white']);
69 var draw = parseInt(move['draw']);
70 var black = parseInt(move['black']);
73 var move_td = document.createElement("td");
74 tr.appendChild(move_td);
75 $(move_td).addClass("move");
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']);
83 var num = white + draw + black;
87 add_td(tr, (100.0 * num / total_num).toFixed(1) + "%");
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) + "%"));
96 add_td(tr, (100.0 * white / num).toFixed(1) + "%");
100 add_td(tr, (100.0 * black / num).toFixed(1) + "%");
104 add_td(tr, ((100.0 * draw / num).toFixed(1) + "%"));
106 if (move['num_elo'] >= 10) {
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));
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) + "%"));
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);
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);
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);
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);
165 var make_move = function(move) {
166 moves.length = move_override;
168 move_override = moves.length;
171 window['make_move'] = make_move;
173 var prev_move = function() {
174 if (move_override > 0) {
179 window['prev_move'] = prev_move;
181 var next_move = function() {
182 if (move_override < moves.length) {
187 window['next_move'] = next_move;
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)) {
199 var onDrop = function(source, target) {
200 // see if the move is legal
201 var game = get_game();
202 var move = game.move({
205 promotion: 'q' // NOTE: always promote to a queen for example simplicity
209 if (move === null) return 'snapback';
211 moves = game.history({ verbose: true });
212 move_override = moves.length;
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());
223 var init = function() {
225 board = new window.ChessBoard('board', {
228 onDragStart: onDragStart,
234 $(window).keyup(function(event) {
235 if (event.which == 39) {
237 } else if (event.which == 37) {
244 $(document).ready(init);