]> git.sesse.net Git - remoteglot/blobdiff - remoteglot.pl
Add local time management, for when incoming time is bad or nonexistent.
[remoteglot] / remoteglot.pl
index 61e7237b1d2926b632f141dc50ef481858c41872..7b3d9670771b14d6d3e4608ea237cd94b02c10c3 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 = ();
@@ -272,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;
@@ -308,7 +315,7 @@ sub handle_pgn {
                        }
                };
                if ($@) {
-                       warn "Error in parsing moves from $url\n";
+                       warn "Error in parsing moves from $url: $@\n";
                }
        }
        
@@ -319,15 +326,21 @@ sub handle_pgn {
 
 sub handle_position {
        my ($pos) = @_;
-       find_clock_start($pos);
+       find_clock_start($pos, $pos_calculating);
                
-       # 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 || !defined($remoteglotconf::json_output)) {
+       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'}) &&
@@ -981,11 +996,8 @@ sub extract_clock {
        # Look for extended PGN clock tags.
        my $tags = $pgn->tags;
        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;
+               $pos->{'white_clock'} = hms_to_sec($tags->{'WhiteClock'});
+               $pos->{'black_clock'} = hms_to_sec($tags->{'BlackClock'});
                return;
        }
 
@@ -1000,18 +1012,28 @@ 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+)/;
-               $pos->{'white_clock'} = $1;
-               $comments->{$black_key} =~ /tl=(\d+:\d+:\d+)/;
-               $pos->{'black_clock'} = $1;
+           $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'} = hms_to_sec($1);
+               $comments->{$black_key} =~ /(?:tl=|clk )(\d+:\d+:\d+)/;
+               $pos->{'black_clock'} = hms_to_sec($1);
                return;
        }
+
+       delete $pos->{'white_clock'};
+       delete $pos->{'black_clock'};
+}
+
+sub hms_to_sec {
+       my $hms = shift;
+       return undef if (!defined($hms));
+       $hms =~ /(\d+):(\d+):(\d+)/;
+       return $1 * 3600 + $2 * 60 + $3;
 }
 
 sub find_clock_start {
-       my $pos = shift;
+       my ($pos, $prev_pos) = @_;
 
        # If the game is over, the clock is stopped.
        if (exists($pos->{'result'}) &&
@@ -1023,10 +1045,14 @@ sub find_clock_start {
 
        # When we don't have any moves, we assume the clock hasn't started yet.
        if ($pos->{'move_num'} == 1 && $pos->{'toplay'} eq 'W') {
+               if (defined($remoteglotconf::adjust_clocks_before_move)) {
+                       &$remoteglotconf::adjust_clocks_before_move(\$pos->{'white_clock'}, \$pos->{'black_clock'}, 1, 'W');
+               }
                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;
        }
@@ -1043,18 +1069,36 @@ sub find_clock_start {
 
        # OK, we haven't seen this position before, so we assume the move
        # happened right now.
+
+       # See if we should do our own clock management (ie., clock information
+       # is spurious or non-existent).
+       if (defined($remoteglotconf::adjust_clocks_before_move)) {
+               my $wc = $pos->{'white_clock'} // $prev_pos->{'white_clock'};
+               my $bc = $pos->{'black_clock'} // $prev_pos->{'black_clock'};
+               if (defined($prev_pos->{'white_clock_target'})) {
+                       $wc = $prev_pos->{'white_clock_target'} - time;
+               }
+               if (defined($prev_pos->{'black_clock_target'})) {
+                       $bc = $prev_pos->{'black_clock_target'} - time;
+               }
+               &$remoteglotconf::adjust_clocks_before_move(\$wc, \$bc, $pos->{'move_num'}, $pos->{'toplay'});
+               $pos->{'white_clock'} = $wc;
+               $pos->{'black_clock'} = $bc;
+       }
+
        my $key = ($pos->{'toplay'} eq 'W') ? 'white_clock' : 'black_clock';
        if (!exists($pos->{$key})) {
                # No clock information.
                return;
        }
-       $pos->{$key} =~ /(\d+):(\d+):(\d+)/;
-       my $time_left = $1 * 3600 + $2 * 60 + $3;
+       my $time_left = $pos->{$key};
        $clock_target_for_pos{$id} = time + $time_left;
        if ($pos->{'toplay'} eq 'W') {
                $pos->{'white_clock_target'} = $clock_target_for_pos{$id};
+               delete $pos->{'black_clock_target'};
        } else {
                $pos->{'black_clock_target'} = $clock_target_for_pos{$id};
+               delete $pos->{'white_clock_target'};
        }
 }