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 ],
60 var show_lines = function(data, game) {
61 var moves = data['moves'];
62 $('#numviewers').text(data['opening']);
64 for (var i = 0; i < moves.length; ++i) {
66 total_num += parseInt(move['white']);
67 total_num += parseInt(move['draw']);
68 total_num += parseInt(move['black']);
71 var headings_tr = $("#headings");
73 for (var i = 0; i < headings.length; ++i) {
74 var th = document.createElement("th");
75 headings_tr.append(th);
76 $(th).text(headings[i][0]);
77 (function(new_sort_by) {
78 $(th).click(function() {
79 sort_by = new_sort_by;
80 show_lines(data, game);
86 for (var i = 0; i < moves.length; ++i) {
90 var white = parseInt(move['white']);
91 var draw = parseInt(move['draw']);
92 var black = parseInt(move['black']);
94 line.push(move['move']); // Move.
95 var num = white + draw + black;
97 line.push(num / total_num); // %.
100 var white_win_ratio = (white + 0.5 * draw) / num;
101 var win_ratio = (game.turn() == 'w') ? white_win_ratio : 1.0 - white_win_ratio;
102 line.push(win_ratio);
104 line.push(white); // WWin.
105 line.push(white / num); // %WW.
106 line.push(black); // BWin.
107 line.push(black / num); // %BW.
108 line.push(draw); // Draw.
109 line.push(draw / num); // %Draw.
111 if (move['num_elo'] >= 10) {
113 line.push(move['white_avg_elo']);
114 line.push(move['black_avg_elo']);
115 line.push(move['white_avg_elo'] - move['black_avg_elo']);
117 // Win% corrected for Elo.
118 var win_elo = -400.0 * Math.log(1.0 / white_win_ratio - 1.0) / Math.LN10;
119 win_elo -= (move['white_avg_elo'] - move['black_avg_elo']);
120 white_win_ratio = 1.0 / (1.0 + Math.pow(10, win_elo / -400.0));
121 win_ratio = (game.turn() == 'w') ? white_win_ratio : 1.0 - white_win_ratio;
122 line.push(win_ratio);
132 lines.sort(function(a, b) { return b[sort_by] - a[sort_by]; });
134 var tbl = $("#lines");
137 for (var i = 0; i < moves.length; ++i) {
139 var tr = document.createElement("tr");
141 for (var j = 0; j < line.length; ++j) {
142 if (line[j] === null) {
144 } else if (headings[j][1] == TYPE_MOVE) {
145 var td = document.createElement("td");
147 $(td).addClass("move");
148 var move_a = document.createElement("a");
149 move_a.href = "javascript:make_move('" + line[j] + "')";
150 td.appendChild(move_a);
151 $(move_a).text(line[j]);
152 } else if (headings[j][1] == TYPE_INTEGER) {
154 } else if (headings[j][1] == TYPE_FLOAT) {
155 add_td(tr, line[j].toFixed(1));
157 add_td(tr, (100.0 * line[j]).toFixed(1) + "%");
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);