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) {
29 var update = function() {
30 var game = get_game();
31 board.position(game.fen());
35 var fetch_analysis = function() {
36 var game = get_game();
38 url: "/opening-stats.pl?fen=" + encodeURIComponent(game.fen())
39 }).done(function(data, textstatus, xhr) {
40 show_lines(data, game);
44 var add_td = function(tr, value) {
45 var td = document.createElement("td");
47 $(td).addClass("num");
57 [ "Move", TYPE_MOVE ],
58 [ "Games", TYPE_INTEGER ],
60 [ "Win%", TYPE_RATIO ],
61 [ "WWin", TYPE_INTEGER ],
62 [ "%WW", TYPE_RATIO ],
63 [ "Bwin", TYPE_INTEGER ],
64 [ "%BW", TYPE_RATIO ],
65 [ "Draw", TYPE_INTEGER ],
66 [ "Draw%", TYPE_RATIO ],
67 [ "AvWElo", TYPE_FLOAT ],
68 [ "AvBElo", TYPE_FLOAT ],
69 [ "EloVar", TYPE_FLOAT ],
70 [ "AWin%", TYPE_RATIO ],
75 var show_lines = function(data, game) {
76 var moves = data['moves'];
77 $('#numviewers').text(data['opening']);
79 if (data['root_game']) {
80 var text = escape_html(data['root_game']['white']);
81 if (data['root_game']['white_elo']) {
82 text += " (" + escape_html(data['root_game']['white_elo']) + ")";
84 text += " – " + escape_html(data['root_game']['black']);
85 if (data['root_game']['black_elo']) {
86 text += " (" + escape_html(data['root_game']['black_elo']) + ")";
88 text += " " + escape_html(data['root_game']['result']).replace(/-/, "–") + "<br />";
89 if (data['root_game']['eco']) {
90 text += "[" + escape_html(data['root_game']['eco']) + "] ";
92 text += "(" + data['root_game']['moves'] + ") ";
93 text += escape_html(data['root_game']['event']) + " " + escape_html(data['root_game']['date']);
94 $('#gamesummary').html(text);
98 for (var i = 0; i < moves.length; ++i) {
100 total_num += parseInt(move['white']);
101 total_num += parseInt(move['draw']);
102 total_num += parseInt(move['black']);
105 var headings_tr = $("#headings");
107 for (var i = 0; i < headings.length; ++i) {
108 var th = document.createElement("th");
109 headings_tr.append(th);
110 $(th).text(headings[i][0]);
111 (function(new_sort_by) {
112 $(th).click(function() {
113 if (sort_by == new_sort_by) {
114 direction = -direction;
116 sort_by = new_sort_by;
119 show_lines(data, game);
125 for (var i = 0; i < moves.length; ++i) {
129 var white = parseInt(move['white']);
130 var draw = parseInt(move['draw']);
131 var black = parseInt(move['black']);
133 line.push(move['move']); // Move.
134 var num = white + draw + black;
135 line.push(num); // N.
136 line.push(num / total_num); // %.
139 var white_win_ratio = (white + 0.5 * draw) / num;
140 var win_ratio = (game.turn() == 'w') ? white_win_ratio : 1.0 - white_win_ratio;
141 line.push(win_ratio);
143 line.push(white); // WWin.
144 line.push(white / num); // %WW.
145 line.push(black); // BWin.
146 line.push(black / num); // %BW.
147 line.push(draw); // Draw.
148 line.push(draw / num); // %Draw.
150 if (move['num_elo'] >= 10) {
152 line.push(move['white_avg_elo']);
153 line.push(move['black_avg_elo']);
154 line.push(move['white_avg_elo'] - move['black_avg_elo']);
156 // Win% corrected for Elo.
157 var win_elo = -400.0 * Math.log(1.0 / white_win_ratio - 1.0) / Math.LN10;
158 win_elo -= (move['white_avg_elo'] - move['black_avg_elo']);
159 white_win_ratio = 1.0 / (1.0 + Math.pow(10, win_elo / -400.0));
160 win_ratio = (game.turn() == 'w') ? white_win_ratio : 1.0 - white_win_ratio;
161 line.push(win_ratio);
171 lines.sort(function(a, b) { return direction * ( b[sort_by] - a[sort_by]); });
173 var tbl = $("#lines");
176 for (var i = 0; i < moves.length; ++i) {
178 var tr = document.createElement("tr");
180 for (var j = 0; j < line.length; ++j) {
181 if (line[j] === null) {
183 } else if (headings[j][1] == TYPE_MOVE) {
184 var td = document.createElement("td");
186 $(td).addClass("move");
187 var move_a = document.createElement("a");
188 move_a.href = "javascript:make_move('" + line[j] + "')";
189 td.appendChild(move_a);
190 $(move_a).text(line[j]);
191 } else if (headings[j][1] == TYPE_INTEGER) {
193 } else if (headings[j][1] == TYPE_FLOAT) {
194 add_td(tr, line[j].toFixed(1));
196 add_td(tr, (100.0 * line[j]).toFixed(1) + "%");
204 var make_move = function(move) {
205 moves.length = move_override;
207 move_override = moves.length;
210 window['make_move'] = make_move;
212 var prev_move = function() {
213 if (move_override > 0) {
218 window['prev_move'] = prev_move;
220 var next_move = function() {
221 if (move_override < moves.length) {
226 window['next_move'] = next_move;
228 // almost all of this stuff comes from the chessboard.js example page
229 var onDragStart = function(source, piece, position, orientation) {
230 var game = get_game();
231 if (game.game_over() === true ||
232 (game.turn() === 'w' && piece.search(/^b/) !== -1) ||
233 (game.turn() === 'b' && piece.search(/^w/) !== -1)) {
238 var onDrop = function(source, target) {
239 // see if the move is legal
240 var game = get_game();
241 var move = game.move({
244 promotion: 'q' // NOTE: always promote to a queen for example simplicity
248 if (move === null) return 'snapback';
250 moves = game.history({ verbose: true });
251 move_override = moves.length;
254 // update the board position after the piece snap
255 // for castling, en passant, pawn promotion
256 var onSnapEnd = function() {
257 var game = get_game();
258 board.position(game.fen());
262 var init = function() {
264 board = new window.ChessBoard('board', {
267 onDragStart: onDragStart,
273 $(window).keyup(function(event) {
274 if (event.which == 39) {
276 } else if (event.which == 37) {
283 $(document).ready(init);