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 ],
61 var show_lines = function(data, game) {
62 var moves = data['moves'];
63 $('#numviewers').text(data['opening']);
64 $('#rootgame').text(data['root_pgn']);
66 for (var i = 0; i < moves.length; ++i) {
68 total_num += parseInt(move['white']);
69 total_num += parseInt(move['draw']);
70 total_num += parseInt(move['black']);
73 var headings_tr = $("#headings");
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;
84 sort_by = new_sort_by;
87 show_lines(data, game);
93 for (var i = 0; i < moves.length; ++i) {
97 var white = parseInt(move['white']);
98 var draw = parseInt(move['draw']);
99 var black = parseInt(move['black']);
101 line.push(move['move']); // Move.
102 var num = white + draw + black;
103 line.push(num); // N.
104 line.push(num / total_num); // %.
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);
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.
118 if (move['num_elo'] >= 10) {
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']);
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);
139 lines.sort(function(a, b) { return direction * ( b[sort_by] - a[sort_by]); });
141 var tbl = $("#lines");
144 for (var i = 0; i < moves.length; ++i) {
146 var tr = document.createElement("tr");
148 for (var j = 0; j < line.length; ++j) {
149 if (line[j] === null) {
151 } else if (headings[j][1] == TYPE_MOVE) {
152 var td = document.createElement("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) {
161 } else if (headings[j][1] == TYPE_FLOAT) {
162 add_td(tr, line[j].toFixed(1));
164 add_td(tr, (100.0 * line[j]).toFixed(1) + "%");
172 var make_move = function(move) {
173 moves.length = move_override;
175 move_override = moves.length;
178 window['make_move'] = make_move;
180 var prev_move = function() {
181 if (move_override > 0) {
186 window['prev_move'] = prev_move;
188 var next_move = function() {
189 if (move_override < moves.length) {
194 window['next_move'] = next_move;
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)) {
206 var onDrop = function(source, target) {
207 // see if the move is legal
208 var game = get_game();
209 var move = game.move({
212 promotion: 'q' // NOTE: always promote to a queen for example simplicity
216 if (move === null) return 'snapback';
218 moves = game.history({ verbose: true });
219 move_override = moves.length;
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());
230 var init = function() {
232 board = new window.ChessBoard('board', {
235 onDragStart: onDragStart,
241 $(window).keyup(function(event) {
242 if (event.which == 39) {
244 } else if (event.which == 37) {
251 $(document).ready(init);