6 var includetransp = true;
16 function escape_html(string) {
17 return String(string).replace(/[&<>"']/g, function (s) {
22 var get_game = function(skip_last_move) {
23 var moves_to_drop = 0;
24 if (skip_last_move === true) {
28 var game = new Chess();
29 for (var i = 0; i < move_override - moves_to_drop; ++i) {
30 game.move(history[i]);
35 var update = function() {
37 for (var i = 0; i < history.length; ++i) {
39 text += (i/2 + 1) + ". ";
41 if (i + 1 == move_override) {
42 text += '<strong>' + history[i] + '</strong>';
44 text += '<a href="javascript:set_move(' + (i + 1) + ')">' + history[i] + '</a>';
48 $('#gamehistory').html(text);
50 var game = get_game();
51 board.position(game.fen());
53 var all_moves = game.history({ verbose: true });
54 if (all_moves.length > 0) {
55 var last_move = all_moves.pop();
56 var highlight_from = last_move.from;
57 var highlight_to = last_move.to;
58 $("#board").find('.square-55d63').removeClass('nonuglyhighlight');
59 $("#board").find('.square-' + highlight_from).addClass('nonuglyhighlight');
60 $("#board").find('.square-' + highlight_to).addClass('nonuglyhighlight');
66 var fetch_analysis = function() {
67 var game = get_game();
70 url: "/opening-stats.pl?fen=" + encodeURIComponent(fen) +
71 ";includetransp=" + (includetransp ? 1 : 0)
72 }).done(function(data, textstatus, xhr) {
73 show_lines(data, game);
77 var add_td = function(tr, value) {
78 var td = document.createElement("td");
80 $(td).addClass("num");
90 [ "Move", TYPE_MOVE ],
91 [ "Games", TYPE_INTEGER ],
93 [ "Win%", TYPE_RATIO ],
94 [ "WWin", TYPE_INTEGER ],
95 [ "%WW", TYPE_RATIO ],
96 [ "Bwin", TYPE_INTEGER ],
97 [ "%BW", TYPE_RATIO ],
98 [ "Draw", TYPE_INTEGER ],
99 [ "Draw%", TYPE_RATIO ],
100 [ "AvWElo", TYPE_FLOAT ],
101 [ "AvBElo", TYPE_FLOAT ],
102 [ "EloVar", TYPE_FLOAT ],
103 [ "AWin%", TYPE_RATIO ],
108 var show_lines = function(data, game) {
109 var moves = data['moves'];
110 $('#numviewers').text(data['opening']);
112 if (data['root_game']) {
113 var text = escape_html(data['root_game']['white']);
114 if (data['root_game']['white_elo']) {
115 text += " (" + escape_html(data['root_game']['white_elo']) + ")";
117 text += " – " + escape_html(data['root_game']['black']);
118 if (data['root_game']['black_elo']) {
119 text += " (" + escape_html(data['root_game']['black_elo']) + ")";
121 text += " " + escape_html(data['root_game']['result']).replace(/-/, "–") + "<br />";
122 if (data['root_game']['eco']) {
123 text += "[" + escape_html(data['root_game']['eco']) + "] ";
125 text += "(" + data['root_game']['moves'] + ") ";
126 text += escape_html(data['root_game']['event']) + " " + escape_html(data['root_game']['date']);
127 $('#gamesummary').html(text);
131 for (var i = 0; i < moves.length; ++i) {
134 total_num += parseInt(move['white']);
135 total_num += parseInt(move['draw']);
136 total_num += parseInt(move['black']);
140 var headings_tr = $("#headings");
142 for (var i = 0; i < headings.length; ++i) {
143 var th = document.createElement("th");
144 headings_tr.append(th);
145 $(th).text(headings[i][0]);
146 (function(new_sort_by) {
147 $(th).click(function() {
148 if (sort_by == new_sort_by) {
149 direction = -direction;
151 sort_by = new_sort_by;
154 show_lines(data, game);
160 var transpose_only = [];
161 for (var i = 0; i < moves.length; ++i) {
165 var white = parseInt(move['white']);
166 var draw = parseInt(move['draw']);
167 var black = parseInt(move['black']);
169 line.push(move['move']); // Move.
170 transpose_only.push(move['transpose_only']);
171 var num = white + draw + black;
172 line.push(num); // N.
173 line.push(num / total_num); // %.
176 var white_win_ratio = (white + 0.5 * draw) / num;
177 var win_ratio = (game.turn() == 'w') ? white_win_ratio : 1.0 - white_win_ratio;
178 line.push(win_ratio);
180 line.push(white); // WWin.
181 line.push(white / num); // %WW.
182 line.push(black); // BWin.
183 line.push(black / num); // %BW.
184 line.push(draw); // Draw.
185 line.push(draw / num); // %Draw.
187 if (move['num_elo'] >= 10) {
189 line.push(move['white_avg_elo']);
190 line.push(move['black_avg_elo']);
191 line.push(move['white_avg_elo'] - move['black_avg_elo']);
193 // Win% corrected for Elo.
194 var win_elo = -400.0 * Math.log(1.0 / white_win_ratio - 1.0) / Math.LN10;
195 win_elo -= (move['white_avg_elo'] - move['black_avg_elo']);
196 white_win_ratio = 1.0 / (1.0 + Math.pow(10, win_elo / -400.0));
197 win_ratio = (game.turn() == 'w') ? white_win_ratio : 1.0 - white_win_ratio;
198 line.push(win_ratio);
208 lines.sort(function(a, b) { return direction * ( b[sort_by] - a[sort_by]); });
210 var tbl = $("#lines");
213 for (var i = 0; i < moves.length; ++i) {
215 var tr = document.createElement("tr");
217 if (line[0] === undefined) {
218 $(tr).addClass("totals");
219 } else if (transpose_only[i]) {
220 $(tr).addClass("transponly");
223 for (var j = 0; j < line.length; ++j) {
224 if (line[j] === null) {
226 } else if (headings[j][1] == TYPE_MOVE) {
227 var td = document.createElement("td");
229 $(td).addClass("move");
230 if (line[j] !== undefined) {
231 if (move_override % 2 == 0) {
232 $(td).text(((move_override / 2) + 1) + ". ");
234 $(td).text(((move_override / 2) + 0.5) + "…");
238 var move_a = document.createElement("a");
239 move_a.href = "javascript:make_move('" + line[j] + "')";
240 td.appendChild(move_a);
241 $(move_a).text(line[j]);
242 } else if (headings[j][1] == TYPE_INTEGER) {
244 } else if (headings[j][1] == TYPE_FLOAT) {
245 add_td(tr, line[j].toFixed(1));
247 add_td(tr, (100.0 * line[j]).toFixed(1) + "%");
255 var set_includetransp = function(value) {
256 includetransp = value;
259 window['set_includetransp'] = set_includetransp;
261 var make_move = function(move) {
262 if (move_override < history.length && history[move_override] == move) {
263 // User effectively only moved forward in history.
266 history.length = move_override;
268 move_override = history.length;
272 window['make_move'] = make_move;
274 var prev_move = function() {
275 if (move_override > 0) {
280 window['prev_move'] = prev_move;
282 var next_move = function() {
283 if (move_override < history.length) {
288 window['next_move'] = next_move;
290 var set_move = function(n) {
294 window['set_move'] = set_move;
296 // almost all of this stuff comes from the chessboard.js example page
297 var onDragStart = function(source, piece, position, orientation) {
298 var game = get_game();
299 if (game.game_over() === true ||
300 (game.turn() === 'w' && piece.search(/^b/) !== -1) ||
301 (game.turn() === 'b' && piece.search(/^w/) !== -1)) {
306 var onDrop = function(source, target) {
307 // see if the move is legal
308 var game = get_game();
309 var move = game.move({
312 promotion: 'q' // NOTE: always promote to a queen for example simplicity
316 if (move === null) return 'snapback';
318 var new_history = game.history({ verbose: true });
320 for (var i = 0; i < new_history.length; ++i) {
321 history.push(new_history[i].san);
323 move_override = history.length;
326 // update the board position after the piece snap
327 // for castling, en passant, pawn promotion
328 var onSnapEnd = function() {
329 var game = get_game();
330 board.position(game.fen());
334 var init = function() {
336 board = new window.ChessBoard('board', {
339 onDragStart: onDragStart,
345 $(window).keyup(function(event) {
346 if (event.which == 39) {
348 } else if (event.which == 37) {
355 $(document).ready(init);