]> git.sesse.net Git - remoteglot-book/commitdiff
Load and display the root PGN.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Sat, 13 Dec 2014 14:53:16 +0000 (15:53 +0100)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Sat, 13 Dec 2014 14:58:49 +0000 (15:58 +0100)
www/book.html
www/css/remoteglot.css
www/js/book.js
www/opening-stats.pl

index 38b0335563e9476f67c8bf22f99a72868ab453f8..4889b865fcfd91fb4e867042603e1a38c6ca984f 100644 (file)
@@ -50,6 +50,7 @@
     </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 32402b2ecde5352c5a1f8c9a9fb617ea99f84b89..bbd9b707880d44a2fc736663817a2da8527b6c26 100644 (file)
@@ -113,6 +113,10 @@ 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 796b04ed8b8d7e3caa5fed7433b0cf1430e7d135..ab5e4112e04a28ebf740234609f807e732d59ec9 100644 (file)
@@ -61,6 +61,7 @@ var direction = 1;
 var show_lines = function(data, game) {
        var moves = data['moves'];
        $('#numviewers').text(data['opening']);
+       $('#rootgame').text(data['root_pgn']);
        var total_num = 0;
        for (var i = 0; i < moves.length; ++i) {
                var move = moves[i];
index d3034359040ddd40296746a0e2a34e061948cd16..9a2ccbc4ad32380598abc2d9a17d6a46e2a4fa59 100755 (executable)
@@ -24,6 +24,14 @@ 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;
+eval {
+       $root_pgn = read_root_pgn($pgn_file_number, $pgn_start_position);
+};
+if ($@) {
+       $root_pgn = "Could not find root PGN. ($@)";
+}
+
 # Explore one move out.
 my @json_moves = ();
 for my $move (@moves) {
@@ -44,7 +52,7 @@ for my $move (@moves) {
 }
 
 print $cgi->header(-type=>'application/json');
-print JSON::XS::encode_json({ moves => \@json_moves, opening => $opening });
+print JSON::XS::encode_json({ moves => \@json_moves, opening => $opening, root_pgn => $root_pgn });
 
 sub num {
        my $x = shift;
@@ -65,3 +73,32 @@ sub read_openings {
        }
        close $fh;
 }
+
+sub read_root_pgn {
+       my @pgnnames;
+       open my $pgnnamesfh, "<", "../pgnnames.txt"
+               or die "../pgnnames.txt: $!";
+       while (<$pgnnamesfh>) {
+               chomp;
+               push @pgnnames, $_;
+               warn "PGN name: $_";
+       }
+       close $pgnnamesfh;
+
+       if ($pgn_file_number > $#pgnnames) {
+               die "Unknown PGN file number $pgn_file_number";
+       }
+
+       my $root_pgn;
+       open my $pgnfh, "<", "../" . $pgnnames[$pgn_file_number]
+               or die $pgnnames[$pgn_file_number] . ": $!";
+       sysseek($pgnfh, $pgn_start_position, 0)
+               or die "Could not seek to $pgn_start_position: $!";
+       sysread($pgnfh, $root_pgn, 32768)
+               or die "Could not read PGN from $pgn_start_position at $pgnnames[$pgn_file_number]: $!";
+       close $pgnfh;
+       $root_pgn =~ s/^.*?(\[Event )/$1/s;
+       $root_pgn =~ s/^(.+?)\[Event .*/$1/s;
+
+       return $root_pgn;
+}