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");
37 var show_lines = function(data, game) {
38 var moves = data['moves'];
39 $('#numviewers').text(data['opening']);
41 for (var i = 0; i < moves.length; ++i) {
43 total_num += parseInt(move['white']);
44 total_num += parseInt(move['draw']);
45 total_num += parseInt(move['black']);
48 var tbl = $("#lines");
51 for (var i = 0; i < moves.length; ++i) {
53 var tr = document.createElement("tr");
55 var white = parseInt(move['white']);
56 var draw = parseInt(move['draw']);
57 var black = parseInt(move['black']);
60 var move_td = document.createElement("td");
61 tr.appendChild(move_td);
62 $(move_td).addClass("move");
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']);
70 var num = white + draw + black;
74 add_td(tr, (100.0 * num / total_num).toFixed(1) + "%");
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) + "%"));
82 add_td(tr, move['white_avg_elo'].toFixed(1));
83 add_td(tr, move['black_avg_elo'].toFixed(1));
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) + "%"));
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);
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);
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);
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);
132 var make_move = function(move) {
133 moves.length = move_override;
135 move_override = moves.length;
138 window['make_move'] = make_move;
140 var prev_move = function() {
141 if (move_override > 0) {
146 window['prev_move'] = prev_move;
148 var next_move = function() {
149 if (move_override < moves.length) {
154 window['next_move'] = next_move;
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)) {
166 var onDrop = function(source, target) {
167 // see if the move is legal
168 var game = get_game();
169 var move = game.move({
172 promotion: 'q' // NOTE: always promote to a queen for example simplicity
176 if (move === null) return 'snapback';
178 moves = game.history({ verbose: true });
179 move_override = moves.length;
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());
190 var init = function() {
192 board = new window.ChessBoard('board', {
195 onDragStart: onDragStart,
201 $(window).keyup(function(event) {
202 if (event.which == 39) {
204 } else if (event.which == 37) {
211 $(document).ready(init);