15 function escape_html(string) {
16 return String(string).replace(/[&<>"']/g, function (s) {
21 var get_game = function() {
22 var game = new Chess();
23 for (var i = 0; i < move_override; ++i) {
24 game.move(history[i]);
29 var update = function() {
31 for (var i = 0; i < history.length; ++i) {
33 text += (i/2 + 1) + ". ";
35 if (i + 1 == move_override) {
36 text += '<strong>' + history[i] + '</strong>';
38 text += '<a href="javascript:set_move(' + (i + 1) + ')">' + history[i] + '</a>';
42 $('#gamehistory').html(text);
44 var game = get_game();
45 board.position(game.fen());
47 var all_moves = game.history({ verbose: true });
48 if (all_moves.length > 0) {
49 var last_move = all_moves.pop();
50 var highlight_from = last_move.from;
51 var highlight_to = last_move.to;
52 $("#board").find('.square-55d63').removeClass('nonuglyhighlight');
53 $("#board").find('.square-' + highlight_from).addClass('nonuglyhighlight');
54 $("#board").find('.square-' + highlight_to).addClass('nonuglyhighlight');
60 var fetch_analysis = function() {
61 var game = get_game();
63 url: "/opening-stats.pl?fen=" + encodeURIComponent(game.fen())
64 }).done(function(data, textstatus, xhr) {
65 show_lines(data, game);
69 var add_td = function(tr, value) {
70 var td = document.createElement("td");
72 $(td).addClass("num");
82 [ "Move", TYPE_MOVE ],
83 [ "Games", TYPE_INTEGER ],
85 [ "Win%", TYPE_RATIO ],
86 [ "WWin", TYPE_INTEGER ],
87 [ "%WW", TYPE_RATIO ],
88 [ "Bwin", TYPE_INTEGER ],
89 [ "%BW", TYPE_RATIO ],
90 [ "Draw", TYPE_INTEGER ],
91 [ "Draw%", TYPE_RATIO ],
92 [ "AvWElo", TYPE_FLOAT ],
93 [ "AvBElo", TYPE_FLOAT ],
94 [ "EloVar", TYPE_FLOAT ],
95 [ "AWin%", TYPE_RATIO ],
100 var show_lines = function(data, game) {
101 var moves = data['moves'];
102 $('#numviewers').text(data['opening']);
104 if (data['root_game']) {
105 var text = escape_html(data['root_game']['white']);
106 if (data['root_game']['white_elo']) {
107 text += " (" + escape_html(data['root_game']['white_elo']) + ")";
109 text += " – " + escape_html(data['root_game']['black']);
110 if (data['root_game']['black_elo']) {
111 text += " (" + escape_html(data['root_game']['black_elo']) + ")";
113 text += " " + escape_html(data['root_game']['result']).replace(/-/, "–") + "<br />";
114 if (data['root_game']['eco']) {
115 text += "[" + escape_html(data['root_game']['eco']) + "] ";
117 text += "(" + data['root_game']['moves'] + ") ";
118 text += escape_html(data['root_game']['event']) + " " + escape_html(data['root_game']['date']);
119 $('#gamesummary').html(text);
123 for (var i = 0; i < moves.length; ++i) {
126 total_num += parseInt(move['white']);
127 total_num += parseInt(move['draw']);
128 total_num += parseInt(move['black']);
132 var headings_tr = $("#headings");
134 for (var i = 0; i < headings.length; ++i) {
135 var th = document.createElement("th");
136 headings_tr.append(th);
137 $(th).text(headings[i][0]);
138 (function(new_sort_by) {
139 $(th).click(function() {
140 if (sort_by == new_sort_by) {
141 direction = -direction;
143 sort_by = new_sort_by;
146 show_lines(data, game);
152 for (var i = 0; i < moves.length; ++i) {
156 var white = parseInt(move['white']);
157 var draw = parseInt(move['draw']);
158 var black = parseInt(move['black']);
160 line.push(move['move']); // Move.
161 var num = white + draw + black;
162 line.push(num); // N.
163 line.push(num / total_num); // %.
166 var white_win_ratio = (white + 0.5 * draw) / num;
167 var win_ratio = (game.turn() == 'w') ? white_win_ratio : 1.0 - white_win_ratio;
168 line.push(win_ratio);
170 line.push(white); // WWin.
171 line.push(white / num); // %WW.
172 line.push(black); // BWin.
173 line.push(black / num); // %BW.
174 line.push(draw); // Draw.
175 line.push(draw / num); // %Draw.
177 if (move['num_elo'] >= 10) {
179 line.push(move['white_avg_elo']);
180 line.push(move['black_avg_elo']);
181 line.push(move['white_avg_elo'] - move['black_avg_elo']);
183 // Win% corrected for Elo.
184 var win_elo = -400.0 * Math.log(1.0 / white_win_ratio - 1.0) / Math.LN10;
185 win_elo -= (move['white_avg_elo'] - move['black_avg_elo']);
186 white_win_ratio = 1.0 / (1.0 + Math.pow(10, win_elo / -400.0));
187 win_ratio = (game.turn() == 'w') ? white_win_ratio : 1.0 - white_win_ratio;
188 line.push(win_ratio);
198 lines.sort(function(a, b) { return direction * ( b[sort_by] - a[sort_by]); });
200 var tbl = $("#lines");
203 for (var i = 0; i < moves.length; ++i) {
205 var tr = document.createElement("tr");
207 if (line[0] === undefined) {
208 $(tr).addClass("totals");
211 for (var j = 0; j < line.length; ++j) {
212 if (line[j] === null) {
214 } else if (headings[j][1] == TYPE_MOVE) {
215 var td = document.createElement("td");
217 $(td).addClass("move");
218 if (line[j] !== undefined) {
219 if (move_override % 2 == 0) {
220 $(td).text(((move_override / 2) + 1) + ". ");
222 $(td).text(((move_override / 2) + 0.5) + "…");
226 var move_a = document.createElement("a");
227 move_a.href = "javascript:make_move('" + line[j] + "')";
228 td.appendChild(move_a);
229 $(move_a).text(line[j]);
230 } else if (headings[j][1] == TYPE_INTEGER) {
232 } else if (headings[j][1] == TYPE_FLOAT) {
233 add_td(tr, line[j].toFixed(1));
235 add_td(tr, (100.0 * line[j]).toFixed(1) + "%");
243 var make_move = function(move) {
244 if (move_override < history.length && history[move_override] == move) {
245 // User effectively only moved forward in history.
248 history.length = move_override;
250 move_override = history.length;
254 window['make_move'] = make_move;
256 var prev_move = function() {
257 if (move_override > 0) {
262 window['prev_move'] = prev_move;
264 var next_move = function() {
265 if (move_override < history.length) {
270 window['next_move'] = next_move;
272 var set_move = function(n) {
276 window['set_move'] = set_move;
278 // almost all of this stuff comes from the chessboard.js example page
279 var onDragStart = function(source, piece, position, orientation) {
280 var game = get_game();
281 if (game.game_over() === true ||
282 (game.turn() === 'w' && piece.search(/^b/) !== -1) ||
283 (game.turn() === 'b' && piece.search(/^w/) !== -1)) {
288 var onDrop = function(source, target) {
289 // see if the move is legal
290 var game = get_game();
291 var move = game.move({
294 promotion: 'q' // NOTE: always promote to a queen for example simplicity
298 if (move === null) return 'snapback';
300 var new_history = game.history({ verbose: true });
302 for (var i = 0; i < new_history.length; ++i) {
303 history.push(new_history[i].san);
305 move_override = history.length;
309 // update the board position after the piece snap
310 // for castling, en passant, pawn promotion
311 var onSnapEnd = function() {
312 var game = get_game();
313 board.position(game.fen());
317 var init = function() {
319 board = new window.ChessBoard('board', {
322 onDragStart: onDragStart,
328 $(window).keyup(function(event) {
329 if (event.which == 39) {
331 } else if (event.which == 37) {
338 $(document).ready(init);