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 for (var i = 0; i < moves.length; ++i) {
164 var white = parseInt(move['white']);
165 var draw = parseInt(move['draw']);
166 var black = parseInt(move['black']);
168 line.push(move['move']); // Move.
169 var num = white + draw + black;
170 line.push(num); // N.
171 line.push(num / total_num); // %.
174 var white_win_ratio = (white + 0.5 * draw) / num;
175 var win_ratio = (game.turn() == 'w') ? white_win_ratio : 1.0 - white_win_ratio;
176 line.push(win_ratio);
178 line.push(white); // WWin.
179 line.push(white / num); // %WW.
180 line.push(black); // BWin.
181 line.push(black / num); // %BW.
182 line.push(draw); // Draw.
183 line.push(draw / num); // %Draw.
185 if (move['num_elo'] >= 10) {
187 line.push(move['white_avg_elo']);
188 line.push(move['black_avg_elo']);
189 line.push(move['white_avg_elo'] - move['black_avg_elo']);
191 // Win% corrected for Elo.
192 var win_elo = -400.0 * Math.log(1.0 / white_win_ratio - 1.0) / Math.LN10;
193 win_elo -= (move['white_avg_elo'] - move['black_avg_elo']);
194 white_win_ratio = 1.0 / (1.0 + Math.pow(10, win_elo / -400.0));
195 win_ratio = (game.turn() == 'w') ? white_win_ratio : 1.0 - white_win_ratio;
196 line.push(win_ratio);
206 lines.sort(function(a, b) { return direction * ( b[sort_by] - a[sort_by]); });
208 var tbl = $("#lines");
211 for (var i = 0; i < moves.length; ++i) {
213 var tr = document.createElement("tr");
215 if (line[0] === undefined) {
216 $(tr).addClass("totals");
219 for (var j = 0; j < line.length; ++j) {
220 if (line[j] === null) {
222 } else if (headings[j][1] == TYPE_MOVE) {
223 var td = document.createElement("td");
225 $(td).addClass("move");
226 if (line[j] !== undefined) {
227 if (move_override % 2 == 0) {
228 $(td).text(((move_override / 2) + 1) + ". ");
230 $(td).text(((move_override / 2) + 0.5) + "…");
234 var move_a = document.createElement("a");
235 move_a.href = "javascript:make_move('" + line[j] + "')";
236 td.appendChild(move_a);
237 $(move_a).text(line[j]);
238 } else if (headings[j][1] == TYPE_INTEGER) {
240 } else if (headings[j][1] == TYPE_FLOAT) {
241 add_td(tr, line[j].toFixed(1));
243 add_td(tr, (100.0 * line[j]).toFixed(1) + "%");
251 var set_includetransp = function(value) {
252 includetransp = value;
255 window['set_includetransp'] = set_includetransp;
257 var make_move = function(move) {
258 if (move_override < history.length && history[move_override] == move) {
259 // User effectively only moved forward in history.
262 history.length = move_override;
264 move_override = history.length;
268 window['make_move'] = make_move;
270 var prev_move = function() {
271 if (move_override > 0) {
276 window['prev_move'] = prev_move;
278 var next_move = function() {
279 if (move_override < history.length) {
284 window['next_move'] = next_move;
286 var set_move = function(n) {
290 window['set_move'] = set_move;
292 // almost all of this stuff comes from the chessboard.js example page
293 var onDragStart = function(source, piece, position, orientation) {
294 var game = get_game();
295 if (game.game_over() === true ||
296 (game.turn() === 'w' && piece.search(/^b/) !== -1) ||
297 (game.turn() === 'b' && piece.search(/^w/) !== -1)) {
302 var onDrop = function(source, target) {
303 // see if the move is legal
304 var game = get_game();
305 var move = game.move({
308 promotion: 'q' // NOTE: always promote to a queen for example simplicity
312 if (move === null) return 'snapback';
314 var new_history = game.history({ verbose: true });
316 for (var i = 0; i < new_history.length; ++i) {
317 history.push(new_history[i].san);
319 move_override = history.length;
322 // update the board position after the piece snap
323 // for castling, en passant, pawn promotion
324 var onSnapEnd = function() {
325 var game = get_game();
326 board.position(game.fen());
330 var init = function() {
332 board = new window.ChessBoard('board', {
335 onDragStart: onDragStart,
341 $(window).keyup(function(event) {
342 if (event.which == 39) {
344 } else if (event.which == 37) {
351 $(document).ready(init);