]> git.sesse.net Git - remoteglot-book/commitdiff
Show a game summary instead of the entire PGN.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Sat, 13 Dec 2014 20:17:29 +0000 (21:17 +0100)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Sat, 13 Dec 2014 20:17:29 +0000 (21:17 +0100)
www/book.html
www/css/remoteglot.css
www/js/book.js
www/opening-stats.pl

index 4889b865fcfd91fb4e867042603e1a38c6ca984f..9a760b300e8f7b27817f9c937fbaa0ae18534584 100644 (file)
@@ -17,6 +17,7 @@
     <p id="whiteclock"><a href="javascript:prev_move()">&lt;&lt;&lt;</a></p>
     <p id="blackclock"><a href="javascript:next_move()">&gt;&gt;&gt;</a></p>
     <p id="numviewers"></p>
+    <p id="gamesummary"></p>
   </div>
 </div>
 <div id="analysis">
@@ -50,7 +51,6 @@
     </tbody>
   </table>
 </div>
-<p id="rootgame"></p>
 <!-- For faster development -->
 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
 <script type="text/javascript" src="js/chessboard-0.3.0.min.js"></script>
index bbd9b707880d44a2fc736663817a2da8527b6c26..f8d99ba9ed6e76469ad8d421afb139f2214e75f2 100644 (file)
@@ -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. */
index ab5e4112e04a28ebf740234609f807e732d59ec9..c54fb94e700d9a8e1338f8c9db38ebada169f20e 100644 (file)
@@ -4,6 +4,20 @@ var board = null;
 var moves = [];
 var move_override = 0;
 
+var entity_map = {
+       "&": "&amp;",
+       "<": "&lt;",
+       ">": "&gt;",
+       '"': '&quot;',
+       "'": '&#39;',
+};
+
+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 += " &ndash; " + escape_html(data['root_game']['black']);
+               if (data['root_game']['black_elo']) {
+                       text += " (" + escape_html(data['root_game']['black_elo']) + ")";
+               }
+               text += " &nbsp;" + escape_html(data['root_game']['result']).replace(/-/, "&ndash;") + "<br />";
+               if (data['root_game']['eco']) {
+                       text += "[" + escape_html(data['root_game']['eco']) + "] ";
+               }
+               text += "(" + data['root_game']['moves'] + ") ";
+               text += escape_html(data['root_game']['event']) + " &nbsp;" + 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];
index 076a6a3636ae0498b0af861c121de4ed5f3941de..adf138f0a80dbb63a2cab9f533156280e33aa019 100755 (executable)
@@ -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;