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 == move_override) {
36 text += '<strong>' + history[i] + '</strong>';
42 $('#gamehistory').html(text);
44 var game = get_game();
45 board.position(game.fen());
49 var fetch_analysis = function() {
50 var game = get_game();
52 url: "/opening-stats.pl?fen=" + encodeURIComponent(game.fen())
53 }).done(function(data, textstatus, xhr) {
54 show_lines(data, game);
58 var add_td = function(tr, value) {
59 var td = document.createElement("td");
61 $(td).addClass("num");
71 [ "Move", TYPE_MOVE ],
72 [ "Games", TYPE_INTEGER ],
74 [ "Win%", TYPE_RATIO ],
75 [ "WWin", TYPE_INTEGER ],
76 [ "%WW", TYPE_RATIO ],
77 [ "Bwin", TYPE_INTEGER ],
78 [ "%BW", TYPE_RATIO ],
79 [ "Draw", TYPE_INTEGER ],
80 [ "Draw%", TYPE_RATIO ],
81 [ "AvWElo", TYPE_FLOAT ],
82 [ "AvBElo", TYPE_FLOAT ],
83 [ "EloVar", TYPE_FLOAT ],
84 [ "AWin%", TYPE_RATIO ],
89 var show_lines = function(data, game) {
90 var moves = data['moves'];
91 $('#numviewers').text(data['opening']);
93 if (data['root_game']) {
94 var text = escape_html(data['root_game']['white']);
95 if (data['root_game']['white_elo']) {
96 text += " (" + escape_html(data['root_game']['white_elo']) + ")";
98 text += " – " + escape_html(data['root_game']['black']);
99 if (data['root_game']['black_elo']) {
100 text += " (" + escape_html(data['root_game']['black_elo']) + ")";
102 text += " " + escape_html(data['root_game']['result']).replace(/-/, "–") + "<br />";
103 if (data['root_game']['eco']) {
104 text += "[" + escape_html(data['root_game']['eco']) + "] ";
106 text += "(" + data['root_game']['moves'] + ") ";
107 text += escape_html(data['root_game']['event']) + " " + escape_html(data['root_game']['date']);
108 $('#gamesummary').html(text);
112 for (var i = 0; i < moves.length; ++i) {
115 total_num += parseInt(move['white']);
116 total_num += parseInt(move['draw']);
117 total_num += parseInt(move['black']);
121 var headings_tr = $("#headings");
123 for (var i = 0; i < headings.length; ++i) {
124 var th = document.createElement("th");
125 headings_tr.append(th);
126 $(th).text(headings[i][0]);
127 (function(new_sort_by) {
128 $(th).click(function() {
129 if (sort_by == new_sort_by) {
130 direction = -direction;
132 sort_by = new_sort_by;
135 show_lines(data, game);
141 for (var i = 0; i < moves.length; ++i) {
145 var white = parseInt(move['white']);
146 var draw = parseInt(move['draw']);
147 var black = parseInt(move['black']);
149 line.push(move['move']); // Move.
150 var num = white + draw + black;
151 line.push(num); // N.
152 line.push(num / total_num); // %.
155 var white_win_ratio = (white + 0.5 * draw) / num;
156 var win_ratio = (game.turn() == 'w') ? white_win_ratio : 1.0 - white_win_ratio;
157 line.push(win_ratio);
159 line.push(white); // WWin.
160 line.push(white / num); // %WW.
161 line.push(black); // BWin.
162 line.push(black / num); // %BW.
163 line.push(draw); // Draw.
164 line.push(draw / num); // %Draw.
166 if (move['num_elo'] >= 10) {
168 line.push(move['white_avg_elo']);
169 line.push(move['black_avg_elo']);
170 line.push(move['white_avg_elo'] - move['black_avg_elo']);
172 // Win% corrected for Elo.
173 var win_elo = -400.0 * Math.log(1.0 / white_win_ratio - 1.0) / Math.LN10;
174 win_elo -= (move['white_avg_elo'] - move['black_avg_elo']);
175 white_win_ratio = 1.0 / (1.0 + Math.pow(10, win_elo / -400.0));
176 win_ratio = (game.turn() == 'w') ? white_win_ratio : 1.0 - white_win_ratio;
177 line.push(win_ratio);
187 lines.sort(function(a, b) { return direction * ( b[sort_by] - a[sort_by]); });
189 var tbl = $("#lines");
192 for (var i = 0; i < moves.length; ++i) {
194 var tr = document.createElement("tr");
196 if (line[0] === undefined) {
197 $(tr).addClass("totals");
200 for (var j = 0; j < line.length; ++j) {
201 if (line[j] === null) {
203 } else if (headings[j][1] == TYPE_MOVE) {
204 var td = document.createElement("td");
206 $(td).addClass("move");
207 if (line[j] !== undefined) {
208 if (move_override % 2 == 0) {
209 $(td).text(((move_override / 2) + 1) + ". ");
211 $(td).text(((move_override / 2) + 0.5) + "…");
215 var move_a = document.createElement("a");
216 move_a.href = "javascript:make_move('" + line[j] + "')";
217 td.appendChild(move_a);
218 $(move_a).text(line[j]);
219 } else if (headings[j][1] == TYPE_INTEGER) {
221 } else if (headings[j][1] == TYPE_FLOAT) {
222 add_td(tr, line[j].toFixed(1));
224 add_td(tr, (100.0 * line[j]).toFixed(1) + "%");
232 var make_move = function(move) {
233 if (move_override < history.length && history[move_override] == move) {
234 // User effectively only moved forward in history.
237 history.length = move_override;
239 move_override = history.length;
243 window['make_move'] = make_move;
245 var prev_move = function() {
246 if (move_override > 0) {
251 window['prev_move'] = prev_move;
253 var next_move = function() {
254 if (move_override < history.length) {
259 window['next_move'] = next_move;
261 // almost all of this stuff comes from the chessboard.js example page
262 var onDragStart = function(source, piece, position, orientation) {
263 var game = get_game();
264 if (game.game_over() === true ||
265 (game.turn() === 'w' && piece.search(/^b/) !== -1) ||
266 (game.turn() === 'b' && piece.search(/^w/) !== -1)) {
271 var onDrop = function(source, target) {
272 // see if the move is legal
273 var game = get_game();
274 var move = game.move({
277 promotion: 'q' // NOTE: always promote to a queen for example simplicity
281 if (move === null) return 'snapback';
283 var new_history = game.history({ verbose: true });
285 for (var i = 0; i < new_history.length; ++i) {
286 history.push(new_history[i].san);
288 move_override = history.length;
292 // update the board position after the piece snap
293 // for castling, en passant, pawn promotion
294 var onSnapEnd = function() {
295 var game = get_game();
296 board.position(game.fen());
300 var init = function() {
302 board = new window.ChessBoard('board', {
305 onDragStart: onDragStart,
311 $(window).keyup(function(event) {
312 if (event.which == 39) {
314 } else if (event.which == 37) {
321 $(document).ready(init);