From: Steinar H. Gunderson Date: Sat, 13 Dec 2014 20:17:29 +0000 (+0100) Subject: Show a game summary instead of the entire PGN. X-Git-Url: https://git.sesse.net/?a=commitdiff_plain;h=7948ff31ce0c53b85cccb9c851dbbf36d46f9c0c;p=remoteglot-book Show a game summary instead of the entire PGN. --- diff --git a/www/book.html b/www/book.html index 4889b86..9a760b3 100644 --- a/www/book.html +++ b/www/book.html @@ -17,6 +17,7 @@

<<<

>>>

+

@@ -50,7 +51,6 @@
-

diff --git a/www/css/remoteglot.css b/www/css/remoteglot.css index bbd9b70..f8d99ba 100644 --- a/www/css/remoteglot.css +++ b/www/css/remoteglot.css @@ -94,6 +94,10 @@ p { width: auto; text-align: center; } +#gamesummary { + width: 100%; + text-align: center; +} #whiteclock { float: left; width: 20%; @@ -113,10 +117,6 @@ p { min-width: 400px; overflow: hidden; } -#rootgame { - white-space: pre; - font-family: monospace; -} /* If the board is too wide for the screen, shrink it to fit, * and then put the analysis below. */ diff --git a/www/js/book.js b/www/js/book.js index ab5e411..c54fb94 100644 --- a/www/js/book.js +++ b/www/js/book.js @@ -4,6 +4,20 @@ var board = null; var moves = []; var move_override = 0; +var entity_map = { + "&": "&", + "<": "<", + ">": ">", + '"': '"', + "'": ''', +}; + +function escape_html(string) { + return String(string).replace(/[&<>"']/g, function (s) { + return entity_map[s]; + }); +} + var get_game = function() { var game = new Chess(); for (var i = 0; i < move_override; ++i) { @@ -61,7 +75,25 @@ var direction = 1; var show_lines = function(data, game) { var moves = data['moves']; $('#numviewers').text(data['opening']); - $('#rootgame').text(data['root_pgn']); + + if (data['root_game']) { + var text = escape_html(data['root_game']['white']); + if (data['root_game']['white_elo']) { + text += " (" + escape_html(data['root_game']['white_elo']) + ")"; + } + text += " – " + escape_html(data['root_game']['black']); + if (data['root_game']['black_elo']) { + text += " (" + escape_html(data['root_game']['black_elo']) + ")"; + } + text += "  " + escape_html(data['root_game']['result']).replace(/-/, "–") + "
"; + if (data['root_game']['eco']) { + text += "[" + escape_html(data['root_game']['eco']) + "] "; + } + text += "(" + data['root_game']['moves'] + ") "; + text += escape_html(data['root_game']['event']) + "  " + escape_html(data['root_game']['date']); + $('#gamesummary').html(text); + } + var total_num = 0; for (var i = 0; i < moves.length; ++i) { var move = moves[i]; diff --git a/www/opening-stats.pl b/www/opening-stats.pl index 076a6a3..adf138f 100755 --- a/www/opening-stats.pl +++ b/www/opening-stats.pl @@ -6,6 +6,7 @@ use JSON::XS; use lib '..'; use Position; use IPC::Open2; +use Chess::PGN::Parse; our %openings = (); read_openings(); @@ -24,14 +25,26 @@ chomp (my $line = <$chld_out>); my ($white, $draw, $black, $opening_num, $white_avg_elo, $black_avg_elo, $num_elo, $timestamp, $pgn_file_number, $pgn_start_position, @moves) = split / /, $line; my $opening = $openings{$opening_num} // 'A00: Start position'; -my $root_pgn; +my $root_game; eval { die "Missing PGN position data." if (!defined($pgn_file_number) || !defined($pgn_start_position)); - $root_pgn = read_root_pgn($pgn_file_number, $pgn_start_position); + my $pgntext = read_root_pgn($pgn_file_number, $pgn_start_position); + my $pgn = Chess::PGN::Parse->new(undef, $pgntext); + $pgn->read_game() or die; + $pgn->parse_game() or die; + + my $tags = $pgn->tags; + $root_game = {}; + $root_game->{'white'} = $pgn->white; + $root_game->{'white_elo'} = $tags->{'WhiteElo'}; + $root_game->{'black'} = $pgn->black; + $root_game->{'black_elo'} = $tags->{'BlackElo'}; + $root_game->{'event'} = $pgn->event; + $root_game->{'date'} = $pgn->date; + $root_game->{'result'} = $pgn->result; + $root_game->{'eco'} = $pgn->eco; + $root_game->{'moves'} = scalar @{$pgn->moves}; }; -if ($@) { - $root_pgn = "Could not find root PGN. ($@)"; -} # Explore one move out. my @json_moves = (); @@ -53,7 +66,7 @@ for my $move (@moves) { } print $cgi->header(-type=>'application/json'); -print JSON::XS::encode_json({ moves => \@json_moves, opening => $opening, root_pgn => $root_pgn }); +print JSON::XS::encode_json({ moves => \@json_moves, opening => $opening, root_game => $root_game }); sub num { my $x = shift;