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 fetch_analysis = function() {
64 var fen = current_display_fen();
66 url: "/opening-stats.pl?fen=" + encodeURIComponent(fen) +
67 ";includetransp=" + (includetransp ? 1 : 0)
68 }).done(function(data, textstatus, xhr) {
69 show_lines(data, game);
73 var add_td = function(tr, value) {
74 var td = document.createElement("td");
76 $(td).addClass("num");
86 [ "Move", TYPE_MOVE ],
87 [ "Games", TYPE_INTEGER ],
89 [ "Win%", TYPE_RATIO ],
90 [ "WWin", TYPE_INTEGER ],
91 [ "%WW", TYPE_RATIO ],
92 [ "Bwin", TYPE_INTEGER ],
93 [ "%BW", TYPE_RATIO ],
94 [ "Draw", TYPE_INTEGER ],
95 [ "Draw%", TYPE_RATIO ],
96 [ "AvWElo", TYPE_FLOAT ],
97 [ "AvBElo", TYPE_FLOAT ],
98 [ "EloVar", TYPE_FLOAT ],
99 [ "AWin%", TYPE_RATIO ],
104 var show_lines = function(data, game) {
105 var moves = data['moves'];
106 $('#numviewers').text(data['opening']);
108 if (data['root_game']) {
109 var text = escape_html(data['root_game']['white']);
110 if (data['root_game']['white_elo']) {
111 text += " (" + escape_html(data['root_game']['white_elo']) + ")";
113 text += " – " + escape_html(data['root_game']['black']);
114 if (data['root_game']['black_elo']) {
115 text += " (" + escape_html(data['root_game']['black_elo']) + ")";
117 text += " " + escape_html(data['root_game']['result']).replace(/-/, "–") + "<br />";
118 if (data['root_game']['eco']) {
119 text += "[" + escape_html(data['root_game']['eco']) + "] ";
121 text += "(" + data['root_game']['moves'] + ") ";
122 text += escape_html(data['root_game']['event']) + " " + escape_html(data['root_game']['date']);
123 $('#gamesummary').html(text);
127 for (var i = 0; i < moves.length; ++i) {
130 total_num += parseInt(move['white']);
131 total_num += parseInt(move['draw']);
132 total_num += parseInt(move['black']);
136 var headings_tr = $("#headings");
138 for (var i = 0; i < headings.length; ++i) {
139 var th = document.createElement("th");
140 headings_tr.append(th);
141 $(th).text(headings[i][0]);
142 (function(new_sort_by) {
143 $(th).click(function() {
144 if (sort_by == new_sort_by) {
145 direction = -direction;
147 sort_by = new_sort_by;
150 show_lines(data, game);
156 var transpose_only = [];
157 for (var i = 0; i < moves.length; ++i) {
161 var white = parseInt(move['white']);
162 var draw = parseInt(move['draw']);
163 var black = parseInt(move['black']);
165 line.push(move['move']); // Move.
166 transpose_only.push(move['transpose_only']);
167 var num = white + draw + black;
168 line.push(num); // N.
169 line.push(num / total_num); // %.
172 var white_win_ratio = (white + 0.5 * draw) / num;
173 var win_ratio = (i % 2 == 0) ? white_win_ratio : 1.0 - white_win_ratio;
174 line.push(win_ratio);
176 line.push(white); // WWin.
177 line.push(white / num); // %WW.
178 line.push(black); // BWin.
179 line.push(black / num); // %BW.
180 line.push(draw); // Draw.
181 line.push(draw / num); // %Draw.
183 if (move['num_elo'] >= 10) {
185 line.push(move['white_avg_elo']);
186 line.push(move['black_avg_elo']);
187 line.push(move['white_avg_elo'] - move['black_avg_elo']);
189 // Win% corrected for Elo.
190 var win_elo = -400.0 * Math.log(1.0 / white_win_ratio - 1.0) / Math.LN10;
191 win_elo -= (move['white_avg_elo'] - move['black_avg_elo']);
192 white_win_ratio = 1.0 / (1.0 + Math.pow(10, win_elo / -400.0));
193 win_ratio = (i % 2 == 0) ? white_win_ratio : 1.0 - white_win_ratio;
194 line.push(win_ratio);
204 lines.sort(function(a, b) { return direction * ( b[sort_by] - a[sort_by]); });
206 var tbl = $("#lines");
209 for (var i = 0; i < moves.length; ++i) {
211 var tr = document.createElement("tr");
213 if (line[0] === undefined) {
214 $(tr).addClass("totals");
215 } else if (transpose_only[i]) {
216 $(tr).addClass("transponly");
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 var history = game.history({ verbose: true });
259 if (move_override < history.length && history[move_override].san == move) {
260 // User effectively only moved forward in history.
263 var moves = game.history();
264 // Truncate the history if needed.
265 if (move_override < moves.length) {
267 for (var i = 0; i < move_override; ++i) {
270 fens.length = move_override;
273 fens.push(game.fen());
278 window['make_move'] = make_move;
280 var prev_move = function() {
281 if (move_override > 0) {
286 window['prev_move'] = prev_move;
288 var next_move = function() {
289 if (move_override < game.history().length) {
294 window['next_move'] = next_move;
296 var set_move = function(n) {
300 window['set_move'] = set_move;
302 // almost all of this stuff comes from the chessboard.js example page
303 var onDragStart = function(source, piece, position, orientation) {
304 var pseudogame = new Chess(current_display_fen());
305 if (pseudogame.game_over() === true ||
306 (pseudogame.turn() === 'w' && piece.search(/^b/) !== -1) ||
307 (pseudogame.turn() === 'b' && piece.search(/^w/) !== -1)) {
312 var onDrop = function(source, target) {
313 // see if the move is legal
314 var pseudogame = new Chess(current_display_fen());
315 var move = pseudogame.move({
318 promotion: 'q' // NOTE: always promote to a queen for example simplicity
322 if (move === null) return 'snapback';
325 var onSnapEnd = function(source, target) {
326 var pseudogame = new Chess(current_display_fen());
327 var move = pseudogame.move({
330 promotion: 'q' // NOTE: always promote to a queen for example simplicity
333 make_move(pseudogame.history({ verbose: true }).pop().san);
336 var init = function() {
338 board = new window.ChessBoard('board', {
341 onDragStart: onDragStart,
347 $(window).keyup(function(event) {
348 if (event.which == 39) {
350 } else if (event.which == 37) {
357 $(document).ready(init);