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");
43 [ "Move", TYPE_MOVE ],
44 [ "Games", TYPE_INTEGER ],
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 ],
59 var show_lines = function(data, game) {
60 var moves = data['moves'];
61 $('#numviewers').text(data['opening']);
63 for (var i = 0; i < moves.length; ++i) {
65 total_num += parseInt(move['white']);
66 total_num += parseInt(move['draw']);
67 total_num += parseInt(move['black']);
70 var headings_tr = $("#headings");
72 for (var i = 0; i < headings.length; ++i) {
73 var th = document.createElement("th");
74 headings_tr.append(th);
75 $(th).text(headings[i][0]);
79 for (var i = 0; i < moves.length; ++i) {
83 var white = parseInt(move['white']);
84 var draw = parseInt(move['draw']);
85 var black = parseInt(move['black']);
87 line.push(move['move']); // Move.
88 var num = white + draw + black;
90 line.push(num / total_num); // %.
93 var white_win_ratio = (white + 0.5 * draw) / num;
94 var win_ratio = (game.turn() == 'w') ? white_win_ratio : 1.0 - white_win_ratio;
97 line.push(white); // WWin.
98 line.push(white / num); // %WW.
99 line.push(black); // BWin.
100 line.push(black / num); // %BW.
101 line.push(draw); // Draw.
102 line.push(draw / num); // %Draw.
104 if (move['num_elo'] >= 10) {
106 line.push(move['white_avg_elo']);
107 line.push(move['black_avg_elo']);
108 line.push(move['white_avg_elo'] - move['black_avg_elo']);
110 // Win% corrected for Elo.
111 var win_elo = -400.0 * Math.log(1.0 / white_win_ratio - 1.0) / Math.LN10;
112 win_elo -= (move['white_avg_elo'] - move['black_avg_elo']);
113 white_win_ratio = 1.0 / (1.0 + Math.pow(10, win_elo / -400.0));
114 win_ratio = (game.turn() == 'w') ? white_win_ratio : 1.0 - white_win_ratio;
115 line.push(win_ratio);
125 var tbl = $("#lines");
128 for (var i = 0; i < moves.length; ++i) {
130 var tr = document.createElement("tr");
132 for (var j = 0; j < line.length; ++j) {
133 if (line[j] === null) {
135 } else if (headings[j][1] == TYPE_MOVE) {
136 var td = document.createElement("td");
138 $(td).addClass("move");
139 var move_a = document.createElement("a");
140 move_a.href = "javascript:make_move('" + line[j] + "')";
141 td.appendChild(move_a);
142 $(move_a).text(line[j]);
143 } else if (headings[j][1] == TYPE_INTEGER) {
145 } else if (headings[j][1] == TYPE_FLOAT) {
146 add_td(tr, line[j].toFixed(1));
148 add_td(tr, (100.0 * line[j]).toFixed(1) + "%");
156 var make_move = function(move) {
157 moves.length = move_override;
159 move_override = moves.length;
162 window['make_move'] = make_move;
164 var prev_move = function() {
165 if (move_override > 0) {
170 window['prev_move'] = prev_move;
172 var next_move = function() {
173 if (move_override < moves.length) {
178 window['next_move'] = next_move;
180 // almost all of this stuff comes from the chessboard.js example page
181 var onDragStart = function(source, piece, position, orientation) {
182 var game = get_game();
183 if (game.game_over() === true ||
184 (game.turn() === 'w' && piece.search(/^b/) !== -1) ||
185 (game.turn() === 'b' && piece.search(/^w/) !== -1)) {
190 var onDrop = function(source, target) {
191 // see if the move is legal
192 var game = get_game();
193 var move = game.move({
196 promotion: 'q' // NOTE: always promote to a queen for example simplicity
200 if (move === null) return 'snapback';
202 moves = game.history({ verbose: true });
203 move_override = moves.length;
206 // update the board position after the piece snap
207 // for castling, en passant, pawn promotion
208 var onSnapEnd = function() {
209 var game = get_game();
210 board.position(game.fen());
214 var init = function() {
216 board = new window.ChessBoard('board', {
219 onDragStart: onDragStart,
225 $(window).keyup(function(event) {
226 if (event.which == 39) {
228 } else if (event.which == 37) {
235 $(document).ready(init);