4 var game = new Chess();
7 var includetransp = true;
17 function escape_html(string) {
18 return String(string).replace(/[&<>"']/g, function (s) {
23 var current_display_fen = function() {
24 if (move_override == 0) {
25 return 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1';
27 return fens[move_override - 1];
31 var update = function() {
33 var history = game.history({ verbose: true });
34 for (var i = 0; i < history.length; ++i) {
36 text += (i/2 + 1) + ". ";
38 if (i + 1 == move_override) {
39 text += '<strong>' + history[i].san + '</strong>';
41 text += '<a href="javascript:set_move(' + (i + 1) + ')">' + history[i].san + '</a>';
45 $('#gamehistory').html(text);
47 if (board.fen() != current_display_fen()) {
48 board.position(current_display_fen());
51 if (move_override > 0) {
52 var last_move = history[move_override - 1];
53 var highlight_from = last_move.from;
54 var highlight_to = last_move.to;
55 $("#board").find('.square-55d63').removeClass('nonuglyhighlight');
56 $("#board").find('.square-' + highlight_from).addClass('nonuglyhighlight');
57 $("#board").find('.square-' + highlight_to).addClass('nonuglyhighlight');
63 var get_history_url = function() {
64 var history = game.history({ verbose: true }).map(function(x) { return x.san; });
65 history.length = move_override;
66 return '/?' + history.join(',');
69 var fetch_analysis = function() {
70 var fen = current_display_fen();
72 url: "/opening-stats.pl?fen=" + encodeURIComponent(fen) +
73 ";includetransp=" + (includetransp ? 1 : 0)
74 }).done(function(data, textstatus, xhr) {
75 show_lines(data, game);
79 var add_td = function(tr, value) {
80 var td = document.createElement("td");
82 $(td).addClass("num");
92 [ "Move", TYPE_MOVE ],
93 [ "Games", TYPE_INTEGER ],
95 [ "Win%", TYPE_RATIO ],
96 [ "WWin", TYPE_INTEGER ],
97 [ "%WW", TYPE_RATIO ],
98 [ "Bwin", TYPE_INTEGER ],
99 [ "%BW", TYPE_RATIO ],
100 [ "Draw", TYPE_INTEGER ],
101 [ "Draw%", TYPE_RATIO ],
102 [ "AvWElo", TYPE_FLOAT ],
103 [ "AvBElo", TYPE_FLOAT ],
104 [ "EloVar", TYPE_FLOAT ],
105 [ "AWin%", TYPE_RATIO ],
110 var show_lines = function(data, game) {
111 var moves = data['moves'];
112 $('#numviewers').text(data['opening']);
114 if (data['root_game']) {
115 var text = escape_html(data['root_game']['white']);
116 if (data['root_game']['white_elo']) {
117 text += " (" + escape_html(data['root_game']['white_elo']) + ")";
119 text += " – " + escape_html(data['root_game']['black']);
120 if (data['root_game']['black_elo']) {
121 text += " (" + escape_html(data['root_game']['black_elo']) + ")";
123 text += " " + escape_html(data['root_game']['result']).replace(/-/, "–") + "<br />";
124 if (data['root_game']['eco']) {
125 text += "[" + escape_html(data['root_game']['eco']) + "] ";
127 text += "(" + data['root_game']['moves'] + ") ";
128 text += escape_html(data['root_game']['event']) + " " + escape_html(data['root_game']['date']);
129 $('#gamesummary').html(text);
133 for (var i = 0; i < moves.length; ++i) {
136 total_num += parseInt(move['white']);
137 total_num += parseInt(move['draw']);
138 total_num += parseInt(move['black']);
142 var headings_tr = $("#headings");
144 for (var i = 0; i < headings.length; ++i) {
145 var th = document.createElement("th");
146 headings_tr.append(th);
147 $(th).text(headings[i][0]);
148 (function(new_sort_by) {
149 $(th).click(function() {
150 if (sort_by == new_sort_by) {
151 direction = -direction;
153 sort_by = new_sort_by;
156 show_lines(data, game);
162 var transpose_only = [];
163 for (var i = 0; i < moves.length; ++i) {
167 var white = parseInt(move['white']);
168 var draw = parseInt(move['draw']);
169 var black = parseInt(move['black']);
171 line.push(move['move']); // Move.
172 transpose_only.push(move['transpose_only']);
173 var num = white + draw + black;
174 line.push(num); // N.
175 line.push(num / total_num); // %.
178 var white_win_ratio = (white + 0.5 * draw) / num;
179 var win_ratio = (move_override % 2 == 0) ? white_win_ratio : 1.0 - white_win_ratio;
180 line.push(win_ratio);
182 line.push(white); // WWin.
183 line.push(white / num); // %WW.
184 line.push(black); // BWin.
185 line.push(black / num); // %BW.
186 line.push(draw); // Draw.
187 line.push(draw / num); // %Draw.
189 if (move['num_elo'] >= 10) {
191 line.push(move['white_avg_elo']);
192 line.push(move['black_avg_elo']);
193 line.push(move['white_avg_elo'] - move['black_avg_elo']);
195 // Win% corrected for Elo.
196 var win_elo = -400.0 * Math.log(1.0 / white_win_ratio - 1.0) / Math.LN10;
197 win_elo -= (move['white_avg_elo'] - move['black_avg_elo']);
198 white_win_ratio = 1.0 / (1.0 + Math.pow(10, win_elo / -400.0));
199 win_ratio = (move_override % 2 == 0) ? white_win_ratio : 1.0 - white_win_ratio;
200 line.push(win_ratio);
210 lines.sort(function(a, b) { return direction * ( b[sort_by] - a[sort_by]); });
212 var tbl = $("#lines");
215 for (var i = 0; i < moves.length; ++i) {
217 var tr = document.createElement("tr");
219 if (line[0] === undefined) {
220 $(tr).addClass("totals");
221 } else if (transpose_only[i]) {
222 $(tr).addClass("transponly");
225 for (var j = 0; j < line.length; ++j) {
226 if (line[j] === null) {
228 } else if (headings[j][1] == TYPE_MOVE) {
229 var td = document.createElement("td");
231 $(td).addClass("move");
232 if (line[j] !== undefined) {
233 if (move_override % 2 == 0) {
234 $(td).text(((move_override / 2) + 1) + ". ");
236 $(td).text(((move_override / 2) + 0.5) + "…");
240 var move_a = document.createElement("a");
241 move_a.href = "javascript:make_move('" + line[j] + "')";
242 td.appendChild(move_a);
243 $(move_a).text(line[j]);
244 } else if (headings[j][1] == TYPE_INTEGER) {
245 add_td(tr, line[j] || 0);
246 } else if (headings[j][1] == TYPE_FLOAT) {
247 if (isNaN(line[j]) || !isFinite(line[j])) {
250 add_td(tr, line[j].toFixed(1));
253 if (isNaN(line[j]) || !isFinite(line[j])) {
256 add_td(tr, (100.0 * line[j]).toFixed(1) + "%");
265 var set_includetransp = function(value) {
266 includetransp = value;
269 window['set_includetransp'] = set_includetransp;
271 var make_move = function(move, do_update) {
272 var history = game.history({ verbose: true });
273 if (move_override < history.length && history[move_override].san == move) {
274 // User effectively only moved forward in history.
277 var moves = game.history();
278 // Truncate the history if needed.
279 if (move_override < moves.length) {
281 for (var i = 0; i < move_override; ++i) {
284 fens.length = move_override;
287 fens.push(game.fen());
291 if (do_update !== false) {
293 window.history.pushState(null, null, get_history_url());
296 window['make_move'] = make_move;
298 var prev_move = function() {
299 if (move_override > 0) {
302 window.history.replaceState(null, null, get_history_url());
305 window['prev_move'] = prev_move;
307 var next_move = function() {
308 if (move_override < game.history().length) {
311 window.history.replaceState(null, null, get_history_url());
314 window['next_move'] = next_move;
316 var set_move = function(n, do_update) {
318 if (do_update !== false) {
320 window.history.replaceState(null, null, get_history_url());
323 window['set_move'] = set_move;
325 // almost all of this stuff comes from the chessboard.js example page
326 var onDragStart = function(source, piece, position, orientation) {
327 var pseudogame = new Chess(current_display_fen());
328 if (pseudogame.game_over() === true ||
329 (pseudogame.turn() === 'w' && piece.search(/^b/) !== -1) ||
330 (pseudogame.turn() === 'b' && piece.search(/^w/) !== -1)) {
335 var onDrop = function(source, target) {
336 // see if the move is legal
337 var pseudogame = new Chess(current_display_fen());
338 var move = pseudogame.move({
341 promotion: 'q' // NOTE: always promote to a queen for example simplicity
345 if (move === null) return 'snapback';
348 var onSnapEnd = function(source, target) {
349 var pseudogame = new Chess(current_display_fen());
350 var move = pseudogame.move({
353 promotion: 'q' // NOTE: always promote to a queen for example simplicity
356 make_move(pseudogame.history({ verbose: true }).pop().san);
359 var onpopstate = function() {
360 var old_moves = game.history({ verbose: true }).map(function(x) { return x.san; });
361 var new_moves = document.location.search.replace(/^\?/, "").split(",");
363 if (new_moves.length == 1 && new_moves[0] == "") {
367 var num_shared_moves;
368 for (num_shared_moves = 0; num_shared_moves < Math.min(old_moves.length, new_moves.length); ++num_shared_moves) {
369 if (old_moves[i] != new_moves[i]) {
374 set_move(num_shared_moves, false);
375 for (var i = num_shared_moves; i < new_moves.length; ++i) {
376 make_move(new_moves[i], false);
379 window.history.replaceState(null, null, get_history_url());
382 var init = function() {
384 board = new window.ChessBoard('board', {
387 onDragStart: onDragStart,
392 window.onpopstate = onpopstate;
395 $(window).keyup(function(event) {
396 if (event.which == 39) {
398 } else if (event.which == 37) {
405 $(document).ready(init);