]> git.sesse.net Git - remoteglot/blobdiff - remoteglot.pl
When a position comes in with the same position but new result, copy the result over...
[remoteglot] / remoteglot.pl
index b546589e5c03bfb56e9fd69caa3b6a13a996b075..86c447489e2f095f8a5d7eb8a439a258ca62e693 100755 (executable)
@@ -35,6 +35,7 @@ my $stop_pgn_fetch = 0;
 my $tb_retry_timer = undef;
 my %tb_cache = ();
 my $tb_lookup_running = 0;
+my $last_written_json = undef;
 
 # TODO: Persist (parts of) this so that we can restart.
 my %clock_target_for_pos = ();
@@ -268,10 +269,16 @@ sub handle_pgn {
        }
 
        my $pgn = Chess::PGN::Parse->new(undef, $body);
-       if (!defined($pgn) || !$pgn->read_game()) {
+       if (!defined($pgn) || !$pgn->read_game() || $body !~ /^\[/) {
                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;
@@ -308,7 +315,7 @@ sub handle_pgn {
                        }
                };
                if ($@) {
-                       warn "Error in parsing moves from $url\n";
+                       warn "Error in parsing moves from $url: $@\n";
                }
        }
        
@@ -321,13 +328,19 @@ sub handle_position {
        my ($pos) = @_;
        find_clock_start($pos);
                
-       # if this is already in the queue, ignore it
-       return if (defined($pos_waiting) && $pos->fen() eq $pos_waiting->fen());
+       # if this is already in the queue, ignore it (just update the result)
+       if (defined($pos_waiting) && $pos->fen() eq $pos_waiting->fen()) {
+               $pos_waiting->{'result'} = $pos->{'result'};
+               return;
+       }
 
        # if we're already chewing on this and there's nothing else in the queue,
        # also ignore it
-       return if (!defined($pos_waiting) && defined($pos_calculating) &&
-                $pos->fen() eq $pos_calculating->fen());
+       if (!defined($pos_waiting) && defined($pos_calculating) &&
+           $pos->fen() eq $pos_calculating->fen()) {
+               $pos_calculating->{'result'} = $pos->{'result'};
+               return;
+       }
 
        # if we're already thinking on something, stop and wait for the engine
        # to approve
@@ -760,8 +773,10 @@ sub output_json {
        $json->{'refutation_lines'} = \%refutation_lines;
 
        my $encoded = JSON::XS::encode_json($json);
-       unless ($historic_json_only) {
+       unless ($historic_json_only || !defined($remoteglotconf::json_output) ||
+               (defined($last_written_json) && $last_written_json eq $encoded)) {
                atomic_set_contents($remoteglotconf::json_output, $encoded);
+               $last_written_json = $encoded;
        }
 
        if (exists($pos_calculating->{'pretty_history'}) &&
@@ -983,6 +998,9 @@ sub extract_clock {
        if (exists($tags->{'WhiteClock'}) && exists($tags->{'BlackClock'})) {
                $pos->{'white_clock'} = $tags->{'WhiteClock'};
                $pos->{'black_clock'} = $tags->{'BlackClock'};
+
+               $pos->{'white_clock'} =~ s/\b(\d)\b/0$1/g;
+               $pos->{'black_clock'} =~ s/\b(\d)\b/0$1/g;
                return;
        }
 
@@ -997,23 +1015,30 @@ sub extract_clock {
 
        if (exists($comments->{$white_key}) &&
            exists($comments->{$black_key}) &&
-           $comments->{$white_key} =~ /tl=(\d+:\d+:\d+)/ &&
-           $comments->{$black_key} =~ /tl=(\d+:\d+:\d+)/) {
-               $comments->{$white_key} =~ /tl=(\d+:\d+:\d+)/;
+           $comments->{$white_key} =~ /(?:tl=|clk )(\d+:\d+:\d+)/ &&
+           $comments->{$black_key} =~ /(?:tl=|clk )(\d+:\d+:\d+)/) {
+               $comments->{$white_key} =~ /(?:tl=|clk )(\d+:\d+:\d+)/;
                $pos->{'white_clock'} = $1;
-               $comments->{$black_key} =~ /tl=(\d+:\d+:\d+)/;
+               $comments->{$black_key} =~ /(?:tl=|clk )(\d+:\d+:\d+)/;
                $pos->{'black_clock'} = $1;
+
+               $pos->{'white_clock'} =~ s/\b(\d)\b/0$1/g;
+               $pos->{'black_clock'} =~ s/\b(\d)\b/0$1/g;
                return;
        }
+
+       delete $pos->{'white_clock'};
+       delete $pos->{'black_clock'};
 }
 
 sub find_clock_start {
        my $pos = shift;
 
        # If the game is over, the clock is stopped.
-       if ($pos->{'result'} eq '1-0' ||
-           $pos->{'result'} eq '1/2-1/2' ||
-           $pos->{'result'} eq '0-1') {
+       if (exists($pos->{'result'}) &&
+           ($pos->{'result'} eq '1-0' ||
+            $pos->{'result'} eq '1/2-1/2' ||
+            $pos->{'result'} eq '0-1')) {
                return;
        }
 
@@ -1022,6 +1047,12 @@ sub find_clock_start {
                return;
        }
 
+       # TODO(sesse): Maybe we can get the number of moves somehow else for FICS games.
+       # The history is needed for id_for_pos.
+       if (!exists($pos->{'pretty_history'})) {
+               return;
+       }
+
        my $id = id_for_pos($pos);
        if (exists($clock_target_for_pos{$id})) {
                if ($pos->{'toplay'} eq 'W') {