Support starting from a nonstandard position.
[remoteglot] / remoteglot.pl
index 2b81e5c..7f4fd3c 100755 (executable)
@@ -286,7 +286,16 @@ sub handle_pgn {
                        my $black = $pgn->black;
                        $white =~ s/,.*//;  # Remove first name.
                        $black =~ s/,.*//;  # Remove first name.
-                       my $pos = Position->start_pos($white, $black);
+                       my $tags = $pgn->tags();
+                       my $pos;
+                       if (exists($tags->{'FEN'})) {
+                               $pos = Position->from_fen($tags->{'FEN'});
+                               $pos->{'player_w'} = $white;
+                               $pos->{'player_b'} = $black;
+                               $pos->{'start_fen'} = $tags->{'FEN'};
+                       } else {
+                               $pos = Position->start_pos($white, $black);
+                       }
                        my $moves = $pgn->moves;
                        my @uci_moves = ();
                        my @repretty_moves = ();
@@ -346,6 +355,9 @@ sub handle_position {
        # ignore it.
        if (defined($pos_calculating) && $pos->fen() eq $pos_calculating->fen()) {
                $pos_calculating->{'result'} = $pos->{'result'};
+               for my $key ('white_clock', 'black_clock', 'white_clock_target', 'black_clock_target') {
+                       $pos_calculating->{$key} //= $pos->{$key};
+               }
                return;
        }
 
@@ -564,6 +576,7 @@ sub complete_using_tbprobe {
 
                # Splice the PV from the tablebase onto what we have so far.
                for my $move (@{$pgn->moves}) {
+                       last if $move eq '#';
                        my $uci_move;
                        ($pos, $uci_move) = $pos->make_pretty_move($move);
                        push @moves, $uci_move;
@@ -876,7 +889,12 @@ sub output_json {
 
                local $dbh->{AutoCommit} = 0;
                my $q = $dbh->prepare('SELECT * FROM scores WHERE id=?');
-               my $pos = Position->start_pos('white', 'black');
+               my $pos;
+               if (exists($pos_calculating->{'start_fen'})) {
+                       $pos = Position->from_fen($pos_calculating->{'start_fen'});
+               } else {
+                       $pos = Position->start_pos('white', 'black');
+               }
                my $halfmove_num = 0;
                for my $move (@{$pos_calculating->{'history'}}) {
                        my $id = id_for_pos($pos, $halfmove_num);
@@ -1271,7 +1289,7 @@ sub find_clock_start {
        }
 
        my $id = id_for_pos($pos);
-       my $clock_info = $dbh->selectrow_hashref('SELECT * FROM clock_info WHERE id=?', undef, $id);
+       my $clock_info = $dbh->selectrow_hashref('SELECT * FROM clock_info WHERE id=? AND COALESCE(white_clock_target, black_clock_target) >= EXTRACT(EPOCH FROM (CURRENT_TIMESTAMP - INTERVAL \'1 day\'));', undef, $id);
        if (defined($clock_info)) {
                $pos->{'white_clock'} //= $clock_info->{'white_clock'};
                $pos->{'black_clock'} //= $clock_info->{'black_clock'};
@@ -1335,7 +1353,7 @@ sub schedule_tb_lookup {
        return if ($tb_lookup_running);
 
        $tb_lookup_running = 1;
-       my $url = 'http://158.250.18.203:6904/tasks/addtask?auth.login=' .
+       my $url = 'http://tb7-api.chessok.com:6904/tasks/addtask?auth.login=' .
                $remoteglotconf::tb_serial_key .
                '&auth.password=aquarium&type=0&fen=' . 
                URI::Escape::uri_escape($pos->fen());