]> git.sesse.net Git - remoteglot/blobdiff - remoteglot.pl
Fix castling display when looking down PVs.
[remoteglot] / remoteglot.pl
index 675573def081eabb2c1c6ee204456abfaf067d42..9301ac7939d25796dbf6407d36741e9b7a6a0029 100755 (executable)
@@ -21,7 +21,7 @@ use warnings;
 my $server = "freechess.org";
 my $target = "GMCarlsen";
 my $engine_cmdline = "'./Deep Rybka 4 SSE42 x64'";
-my $engine2_cmdline = "./stockfish_13111119_x64_modern_sse42";
+my $engine2_cmdline = "./stockfish_13111119_x64_modern_sse42";  # undef for none
 my $telltarget = undef;   # undef to be silent
 my @tell_intervals = (5, 20, 60, 120, 240, 480, 960);  # after each move
 my $uci_assume_full_compliance = 0;                    # dangerous :-)
@@ -68,13 +68,15 @@ uciprint($engine, "setoption name Hash value 1024");
 # uciprint($engine, "setoption name MultiPV value 2");
 uciprint($engine, "ucinewgame");
 
-uciprint($engine2, "setoption name UCI_AnalyseMode value true");
-# uciprint($engine2, "setoption name NalimovPath value /srv/tablebase");
-uciprint($engine2, "setoption name NalimovUsage value Rarely");
-uciprint($engine2, "setoption name Hash value 1024");
-uciprint($engine2, "setoption name Threads value 8");
-uciprint($engine2, "setoption name MultiPV value 500");
-uciprint($engine2, "ucinewgame");
+if (defined($engine2)) {
+       uciprint($engine2, "setoption name UCI_AnalyseMode value true");
+       # uciprint($engine2, "setoption name NalimovPath value /srv/tablebase");
+       uciprint($engine2, "setoption name NalimovUsage value Rarely");
+       uciprint($engine2, "setoption name Hash value 1024");
+       uciprint($engine2, "setoption name Threads value 8");
+       uciprint($engine2, "setoption name MultiPV value 500");
+       uciprint($engine2, "ucinewgame");
+}
 
 print "Chess engine ready.\n";
 
@@ -98,7 +100,9 @@ while (1) {
        my $rin = '';
        my $rout;
        vec($rin, fileno($engine->{'read'}), 1) = 1;
-       vec($rin, fileno($engine2->{'read'}), 1) = 1;
+       if (defined($engine2)) {
+               vec($rin, fileno($engine2->{'read'}), 1) = 1;
+       }
        vec($rin, fileno($t), 1) = 1;
 
        my ($nfound, $timeleft) = select($rout=$rin, undef, undef, 5.0);
@@ -143,16 +147,18 @@ while (1) {
                                $pos_calculating = $pos;
                        }
 
-                       if (defined($pos_calculating_second_engine)) {
-                               uciprint($engine2, "stop");
-                       } else {
-                               uciprint($engine2, "position fen " . $pos->{'fen'});
-                               uciprint($engine2, "go infinite");
-                               $pos_calculating_second_engine = $pos;
+                       if (defined($engine2)) {
+                               if (defined($pos_calculating_second_engine)) {
+                                       uciprint($engine2, "stop");
+                               } else {
+                                       uciprint($engine2, "position fen " . $pos->{'fen'});
+                                       uciprint($engine2, "go infinite");
+                                       $pos_calculating_second_engine = $pos;
+                               }
+                               $engine2->{'info'} = {};
                        }
 
                        $engine->{'info'} = {};
-                       $engine2->{'info'} = {};
                        $last_move = time;
 
                        # 
@@ -194,7 +200,7 @@ while (1) {
 
                output();
        }
-       if ($nfound > 0 && vec($rout, fileno($engine2->{'read'}), 1) == 1) {
+       if (defined($engine2) && $nfound > 0 && vec($rout, fileno($engine2->{'read'}), 1) == 1) {
                my @lines = read_lines($engine2);
                for my $line (@lines) {
                        next if $line =~ /(upper|lower)bound/;
@@ -343,6 +349,10 @@ sub style12_to_pos {
        $pos{'black_castle_k'} = $x[13];
        $pos{'black_castle_q'} = $x[14];
        $pos{'time_to_100move_rule'} = $x[15];
+       $pos{'player_w'} = $x[17];
+       $pos{'player_b'} = $x[18];
+       $pos{'player_w'} =~ s/^[IG]M//;
+       $pos{'player_b'} =~ s/^[IG]M//;
        $pos{'move_num'} = $x[26];
        if ($x[27] =~ /([a-h][1-8])-([a-h][1-8])/) {
                $pos{'last_move_uci'} = $1 . $2;
@@ -750,9 +760,9 @@ sub output_screen {
                }
                if (exists($info->{'tbhits'}) && $info->{'tbhits'} > 0) {
                        if ($info->{'tbhits'} == 1) {
-                               $text .= ", one Nalimov hit";
+                               $text .= ", one Syzygy hit";
                        } else {
-                               $text .= sprintf ", %u Nalimov hits", $info->{'tbhits'};
+                               $text .= sprintf ", %u Syzygy hits", $info->{'tbhits'};
                        }
                }
                $text .= "\n\n";
@@ -761,26 +771,28 @@ sub output_screen {
        #$text .= book_info($pos_calculating->{'fen'}, $pos_calculating->{'board'}, $pos_calculating->{'toplay'});
 
        my @refutation_lines = ();
-       for (my $mpv = 1; $mpv < 500; ++$mpv) {
-               my $info = $engine2->{'info'};
-               last if (!exists($info->{'pv' . $mpv}));
-               eval {
-                       my $pv = $info->{'pv' . $mpv};
-
-                       my $pretty_move = join('', prettyprint_pv($pos_calculating_second_engine->{'board'}, $pv->[0]));
-                       my @pretty_pv = prettyprint_pv($pos_calculating_second_engine->{'board'}, @$pv);
-                       if (scalar @pretty_pv > 5) {
-                               @pretty_pv = @pretty_pv[0..4];
-                               push @pretty_pv, "...";
-                       }
-                       my $key = $pretty_move;
-                       my $line = sprintf("  %-6s %6s %3s  %s",
-                               $pretty_move,
-                               short_score($info, $pos_calculating_second_engine, $mpv, 0),
-                               "d" . $info->{'depth' . $mpv},
-                               join(', ', @pretty_pv));
-                       push @refutation_lines, [ $key, $line ];
-               };
+       if (defined($engine2)) {
+               for (my $mpv = 1; $mpv < 500; ++$mpv) {
+                       my $info = $engine2->{'info'};
+                       last if (!exists($info->{'pv' . $mpv}));
+                       eval {
+                               my $pv = $info->{'pv' . $mpv};
+
+                               my $pretty_move = join('', prettyprint_pv($pos_calculating_second_engine->{'board'}, $pv->[0]));
+                               my @pretty_pv = prettyprint_pv($pos_calculating_second_engine->{'board'}, @$pv);
+                               if (scalar @pretty_pv > 5) {
+                                       @pretty_pv = @pretty_pv[0..4];
+                                       push @pretty_pv, "...";
+                               }
+                               my $key = $pretty_move;
+                               my $line = sprintf("  %-6s %6s %3s  %s",
+                                       $pretty_move,
+                                       short_score($info, $pos_calculating_second_engine, $mpv, 0),
+                                       "d" . $info->{'depth' . $mpv},
+                                       join(', ', @pretty_pv));
+                               push @refutation_lines, [ $key, $line ];
+                       };
+               }
        }
 
        if ($#refutation_lines >= 0) {
@@ -876,34 +888,36 @@ sub output_json {
 
        my %refutation_lines = ();
        my @refutation_lines = ();
-       for (my $mpv = 1; $mpv < 500; ++$mpv) {
-               my $info = $engine2->{'info'};
-               my $pretty_move = "";
-               my @pretty_pv = ();
-               last if (!exists($info->{'pv' . $mpv}));
-
-               eval {
-                       my $pv = $info->{'pv' . $mpv};
-                       my $pretty_move = join('', prettyprint_pv($pos_calculating->{'board'}, $pv->[0]));
-                       my @pretty_pv = prettyprint_pv($pos_calculating->{'board'}, @$pv);
-                       $refutation_lines{$pv->[0]} = {
-                               sort_key => $pretty_move,
-                               depth => $info->{'depth' . $mpv},
-                               score_sort_key => score_sort_key($info, $pos_calculating, $mpv, 0),
-                               pretty_score => short_score($info, $pos_calculating, $mpv, 0),
-                               pretty_move => $pretty_move,
-                               pv_pretty => \@pretty_pv,
+       if (defined($engine2)) {
+               for (my $mpv = 1; $mpv < 500; ++$mpv) {
+                       my $info = $engine2->{'info'};
+                       my $pretty_move = "";
+                       my @pretty_pv = ();
+                       last if (!exists($info->{'pv' . $mpv}));
+
+                       eval {
+                               my $pv = $info->{'pv' . $mpv};
+                               my $pretty_move = join('', prettyprint_pv($pos_calculating->{'board'}, $pv->[0]));
+                               my @pretty_pv = prettyprint_pv($pos_calculating->{'board'}, @$pv);
+                               $refutation_lines{$pv->[0]} = {
+                                       sort_key => $pretty_move,
+                                       depth => $info->{'depth' . $mpv},
+                                       score_sort_key => score_sort_key($info, $pos_calculating, $mpv, 0),
+                                       pretty_score => short_score($info, $pos_calculating, $mpv, 0),
+                                       pretty_move => $pretty_move,
+                                       pv_pretty => \@pretty_pv,
+                               };
+                               $refutation_lines{$pv->[0]}->{'pv_uci'} = $pv;
                        };
-                       $refutation_lines{$pv->[0]}->{'pv_uci'} = $pv;
-               };
+               }
        }
        $json->{'refutation_lines'} = \%refutation_lines;
 
-       open my $fh, ">analysis.json.tmp"
+       open my $fh, ">/srv/analysis.sesse.net/www/analysis.json.tmp"
                or return;
        print $fh JSON::XS::encode_json($json);
        close $fh;
-       rename("analysis.json.tmp", "analysis.json");   
+       rename("/srv/analysis.sesse.net/www/analysis.json.tmp", "/srv/analysis.sesse.net/www/analysis.json");
 }
 
 sub find_kings {
@@ -1178,7 +1192,7 @@ sub long_score {
                if (exists($info->{'score_cp' . $mpv})) {
                        my $score = $info->{'score_cp' . $mpv} * 0.01;
                        if ($score == 0) {
-                               return " 0.00";
+                               return "Score:  0.00";
                        }
                        if ($pos->{'toplay'} eq 'B') {
                                $score = -$score;
@@ -1244,6 +1258,9 @@ sub book_info {
 
 sub open_engine {
        my ($cmdline, $tag) = @_;
+
+       return undef if (!defined($cmdline));
+
        my ($uciread, $uciwrite);
        my $pid = IPC::Open2::open2($uciread, $uciwrite, $cmdline);