Add support for filtering individual games from a PGN.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Sat, 17 Jan 2015 12:37:57 +0000 (13:37 +0100)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Sat, 17 Jan 2015 12:37:57 +0000 (13:37 +0100)
config.pm
remoteglot.pl

index 182d6eb..e036a4d 100644 (file)
--- a/config.pm
+++ b/config.pm
@@ -7,6 +7,15 @@ package remoteglotconf;
 our $server = "freechess.org";
 our $nick = "SesseBOT";
 our $target = "GMCarlsen";  # FICS username or HTTP to a PGN file.
+
+# Set to non-undef to pick out one specific game from a PGN file with many games.
+# See example.
+our $pgn_filter = undef;
+#our $pgn_filter = sub {
+#      my $pgn = shift;
+#      return $pgn->round() eq '7' && $pgn->white eq 'Carlsen,M';
+#};
+
 our $json_output = "/srv/analysis.sesse.net/www/analysis.json";
 our $json_history_dir = "/srv/analysis.sesse.net/www/history/";  # undef for none.
 
index 6a5778a..2a6e2a6 100755 (executable)
@@ -273,6 +273,12 @@ sub handle_pgn {
                warn "Error in parsing PGN from $url\n";
        } else {
                eval {
+                       # Skip to the right game.
+                       while (defined($remoteglotconf::pgn_filter) &&
+                              !&$remoteglotconf::pgn_filter($pgn)) {
+                               $pgn->read_game() or die "Out of games during filtering";
+                       }
+
                        $pgn->parse_game({ save_comments => 'yes' });
                        my $pos = Position->start_pos($pgn->white, $pgn->black);
                        my $moves = $pgn->moves;
@@ -309,7 +315,7 @@ sub handle_pgn {
                        }
                };
                if ($@) {
-                       warn "Error in parsing moves from $url\n";
+                       warn "Error in parsing moves from $url: $@\n";
                }
        }