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']);
65 for (var i = 0; i < moves.length; ++i) {
67 total_num += parseInt(move['white']);
68 total_num += parseInt(move['draw']);
69 total_num += parseInt(move['black']);
72 var headings_tr = $("#headings");
74 for (var i = 0; i < headings.length; ++i) {
75 var th = document.createElement("th");
76 headings_tr.append(th);
77 $(th).text(headings[i][0]);
78 (function(new_sort_by) {
79 $(th).click(function() {
80 if (sort_by == new_sort_by) {
81 direction = -direction;
83 sort_by = new_sort_by;
86 show_lines(data, game);
92 for (var i = 0; i < moves.length; ++i) {
96 var white = parseInt(move['white']);
97 var draw = parseInt(move['draw']);
98 var black = parseInt(move['black']);
100 line.push(move['move']); // Move.
101 var num = white + draw + black;
102 line.push(num); // N.
103 line.push(num / total_num); // %.
106 var white_win_ratio = (white + 0.5 * draw) / num;
107 var win_ratio = (game.turn() == 'w') ? white_win_ratio : 1.0 - white_win_ratio;
108 line.push(win_ratio);
110 line.push(white); // WWin.
111 line.push(white / num); // %WW.
112 line.push(black); // BWin.
113 line.push(black / num); // %BW.
114 line.push(draw); // Draw.
115 line.push(draw / num); // %Draw.
117 if (move['num_elo'] >= 10) {
119 line.push(move['white_avg_elo']);
120 line.push(move['black_avg_elo']);
121 line.push(move['white_avg_elo'] - move['black_avg_elo']);
123 // Win% corrected for Elo.
124 var win_elo = -400.0 * Math.log(1.0 / white_win_ratio - 1.0) / Math.LN10;
125 win_elo -= (move['white_avg_elo'] - move['black_avg_elo']);
126 white_win_ratio = 1.0 / (1.0 + Math.pow(10, win_elo / -400.0));
127 win_ratio = (game.turn() == 'w') ? white_win_ratio : 1.0 - white_win_ratio;
128 line.push(win_ratio);
138 lines.sort(function(a, b) { return direction * ( b[sort_by] - a[sort_by]); });
140 var tbl = $("#lines");
143 for (var i = 0; i < moves.length; ++i) {
145 var tr = document.createElement("tr");
147 for (var j = 0; j < line.length; ++j) {
148 if (line[j] === null) {
150 } else if (headings[j][1] == TYPE_MOVE) {
151 var td = document.createElement("td");
153 $(td).addClass("move");
154 var move_a = document.createElement("a");
155 move_a.href = "javascript:make_move('" + line[j] + "')";
156 td.appendChild(move_a);
157 $(move_a).text(line[j]);
158 } else if (headings[j][1] == TYPE_INTEGER) {
160 } else if (headings[j][1] == TYPE_FLOAT) {
161 add_td(tr, line[j].toFixed(1));
163 add_td(tr, (100.0 * line[j]).toFixed(1) + "%");
171 var make_move = function(move) {
172 moves.length = move_override;
174 move_override = moves.length;
177 window['make_move'] = make_move;
179 var prev_move = function() {
180 if (move_override > 0) {
185 window['prev_move'] = prev_move;
187 var next_move = function() {
188 if (move_override < moves.length) {
193 window['next_move'] = next_move;
195 // almost all of this stuff comes from the chessboard.js example page
196 var onDragStart = function(source, piece, position, orientation) {
197 var game = get_game();
198 if (game.game_over() === true ||
199 (game.turn() === 'w' && piece.search(/^b/) !== -1) ||
200 (game.turn() === 'b' && piece.search(/^w/) !== -1)) {
205 var onDrop = function(source, target) {
206 // see if the move is legal
207 var game = get_game();
208 var move = game.move({
211 promotion: 'q' // NOTE: always promote to a queen for example simplicity
215 if (move === null) return 'snapback';
217 moves = game.history({ verbose: true });
218 move_override = moves.length;
221 // update the board position after the piece snap
222 // for castling, en passant, pawn promotion
223 var onSnapEnd = function() {
224 var game = get_game();
225 board.position(game.fen());
229 var init = function() {
231 board = new window.ChessBoard('board', {
234 onDragStart: onDragStart,
240 $(window).keyup(function(event) {
241 if (event.which == 39) {
243 } else if (event.which == 37) {
250 $(document).ready(init);