]> git.sesse.net Git - remoteglot/blobdiff - remoteglot.pl
Fix display of castling (Chess.js does not like 0-0 and 0-0-0, but wants O-O and...
[remoteglot] / remoteglot.pl
index 61e7237b1d2926b632f141dc50ef481858c41872..9c2130cc55fca075cb853c0904614d322a7fd3e3 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";
                }
        }
        
@@ -760,8 +767,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'}) &&
@@ -1000,14 +1009,20 @@ 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 {
@@ -1027,6 +1042,7 @@ sub find_clock_start {
        }
 
        # 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;
        }