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 if (move_override > 0) {
71 prevfen = get_game(true).fen();
74 url: "/opening-stats.pl?fen=" + encodeURIComponent(fen) +
75 ";prevfen=" + encodeURIComponent(prevfen) +
76 ";includetransp=" + (includetransp ? 1 : 0)
77 }).done(function(data, textstatus, xhr) {
78 show_lines(data, game);
82 var add_td = function(tr, value) {
83 var td = document.createElement("td");
85 $(td).addClass("num");
95 [ "Move", TYPE_MOVE ],
96 [ "Games", TYPE_INTEGER ],
98 [ "Win%", TYPE_RATIO ],
99 [ "WWin", TYPE_INTEGER ],
100 [ "%WW", TYPE_RATIO ],
101 [ "Bwin", TYPE_INTEGER ],
102 [ "%BW", TYPE_RATIO ],
103 [ "Draw", TYPE_INTEGER ],
104 [ "Draw%", TYPE_RATIO ],
105 [ "AvWElo", TYPE_FLOAT ],
106 [ "AvBElo", TYPE_FLOAT ],
107 [ "EloVar", TYPE_FLOAT ],
108 [ "AWin%", TYPE_RATIO ],
113 var show_lines = function(data, game) {
114 var moves = data['moves'];
115 $('#numviewers').text(data['opening']);
117 if (data['root_game']) {
118 var text = escape_html(data['root_game']['white']);
119 if (data['root_game']['white_elo']) {
120 text += " (" + escape_html(data['root_game']['white_elo']) + ")";
122 text += " – " + escape_html(data['root_game']['black']);
123 if (data['root_game']['black_elo']) {
124 text += " (" + escape_html(data['root_game']['black_elo']) + ")";
126 text += " " + escape_html(data['root_game']['result']).replace(/-/, "–") + "<br />";
127 if (data['root_game']['eco']) {
128 text += "[" + escape_html(data['root_game']['eco']) + "] ";
130 text += "(" + data['root_game']['moves'] + ") ";
131 text += escape_html(data['root_game']['event']) + " " + escape_html(data['root_game']['date']);
132 $('#gamesummary').html(text);
136 for (var i = 0; i < moves.length; ++i) {
139 total_num += parseInt(move['white']);
140 total_num += parseInt(move['draw']);
141 total_num += parseInt(move['black']);
145 var headings_tr = $("#headings");
147 for (var i = 0; i < headings.length; ++i) {
148 var th = document.createElement("th");
149 headings_tr.append(th);
150 $(th).text(headings[i][0]);
151 (function(new_sort_by) {
152 $(th).click(function() {
153 if (sort_by == new_sort_by) {
154 direction = -direction;
156 sort_by = new_sort_by;
159 show_lines(data, game);
165 for (var i = 0; i < moves.length; ++i) {
169 var white = parseInt(move['white']);
170 var draw = parseInt(move['draw']);
171 var black = parseInt(move['black']);
173 line.push(move['move']); // Move.
174 var num = white + draw + black;
175 line.push(num); // N.
176 line.push(num / total_num); // %.
179 var white_win_ratio = (white + 0.5 * draw) / num;
180 var win_ratio = (game.turn() == 'w') ? white_win_ratio : 1.0 - white_win_ratio;
181 line.push(win_ratio);
183 line.push(white); // WWin.
184 line.push(white / num); // %WW.
185 line.push(black); // BWin.
186 line.push(black / num); // %BW.
187 line.push(draw); // Draw.
188 line.push(draw / num); // %Draw.
190 if (move['num_elo'] >= 10) {
192 line.push(move['white_avg_elo']);
193 line.push(move['black_avg_elo']);
194 line.push(move['white_avg_elo'] - move['black_avg_elo']);
196 // Win% corrected for Elo.
197 var win_elo = -400.0 * Math.log(1.0 / white_win_ratio - 1.0) / Math.LN10;
198 win_elo -= (move['white_avg_elo'] - move['black_avg_elo']);
199 white_win_ratio = 1.0 / (1.0 + Math.pow(10, win_elo / -400.0));
200 win_ratio = (game.turn() == 'w') ? white_win_ratio : 1.0 - white_win_ratio;
201 line.push(win_ratio);
211 lines.sort(function(a, b) { return direction * ( b[sort_by] - a[sort_by]); });
213 var tbl = $("#lines");
216 for (var i = 0; i < moves.length; ++i) {
218 var tr = document.createElement("tr");
220 if (line[0] === undefined) {
221 $(tr).addClass("totals");
224 for (var j = 0; j < line.length; ++j) {
225 if (line[j] === null) {
227 } else if (headings[j][1] == TYPE_MOVE) {
228 var td = document.createElement("td");
230 $(td).addClass("move");
231 if (line[j] !== undefined) {
232 if (move_override % 2 == 0) {
233 $(td).text(((move_override / 2) + 1) + ". ");
235 $(td).text(((move_override / 2) + 0.5) + "…");
239 var move_a = document.createElement("a");
240 move_a.href = "javascript:make_move('" + line[j] + "')";
241 td.appendChild(move_a);
242 $(move_a).text(line[j]);
243 } else if (headings[j][1] == TYPE_INTEGER) {
245 } else if (headings[j][1] == TYPE_FLOAT) {
246 add_td(tr, line[j].toFixed(1));
248 add_td(tr, (100.0 * line[j]).toFixed(1) + "%");
256 var set_includetransp = function(value) {
257 includetransp = value;
260 window['set_includetransp'] = set_includetransp;
262 var make_move = function(move) {
263 if (move_override < history.length && history[move_override] == move) {
264 // User effectively only moved forward in history.
267 history.length = move_override;
269 move_override = history.length;
273 window['make_move'] = make_move;
275 var prev_move = function() {
276 if (move_override > 0) {
281 window['prev_move'] = prev_move;
283 var next_move = function() {
284 if (move_override < history.length) {
289 window['next_move'] = next_move;
291 var set_move = function(n) {
295 window['set_move'] = set_move;
297 // almost all of this stuff comes from the chessboard.js example page
298 var onDragStart = function(source, piece, position, orientation) {
299 var game = get_game();
300 if (game.game_over() === true ||
301 (game.turn() === 'w' && piece.search(/^b/) !== -1) ||
302 (game.turn() === 'b' && piece.search(/^w/) !== -1)) {
307 var onDrop = function(source, target) {
308 // see if the move is legal
309 var game = get_game();
310 var move = game.move({
313 promotion: 'q' // NOTE: always promote to a queen for example simplicity
317 if (move === null) return 'snapback';
319 var new_history = game.history({ verbose: true });
321 for (var i = 0; i < new_history.length; ++i) {
322 history.push(new_history[i].san);
324 move_override = history.length;
327 // update the board position after the piece snap
328 // for castling, en passant, pawn promotion
329 var onSnapEnd = function() {
330 var game = get_game();
331 board.position(game.fen());
335 var init = function() {
337 board = new window.ChessBoard('board', {
340 onDragStart: onDragStart,
346 $(window).keyup(function(event) {
347 if (event.which == 39) {
349 } else if (event.which == 37) {
356 $(document).ready(init);