X-Git-Url: https://git.sesse.net/?p=remoteglot;a=blobdiff_plain;f=remoteglot.pl;h=39a0f4ba805f4312f6a12afe69be7757a6ed0384;hp=9b1635c3ebc8955642fb68bff08abf46921dd3a1;hb=77b62690312c008e5dc2c76bac2f5ac85ecd393d;hpb=e3abe43333d1f536f34f746889ba46428989f50e diff --git a/remoteglot.pl b/remoteglot.pl index 9b1635c..39a0f4b 100755 --- a/remoteglot.pl +++ b/remoteglot.pl @@ -209,7 +209,6 @@ sub handle_fics { for my $pos ($pos_waiting, $pos_calculating) { next if (!defined($pos)); if ($pos->fen() eq $pos_for_movelist->fen()) { - $pos->{'history'} = \@uci_movelist; $pos->{'pretty_history'} = \@pretty_movelist; } } @@ -279,7 +278,6 @@ sub handle_pgn { ($pos, $uci_move) = $pos->make_pretty_move($move); push @uci_moves, $uci_move; } - $pos->{'history'} = \@uci_moves; $pos->{'pretty_history'} = $moves; # Sometimes, PGNs lose a move or two for a short while, @@ -711,8 +709,7 @@ sub output_json { $json->{'seldepth'} = $info->{'seldepth'}; $json->{'tablebase'} = $info->{'tablebase'}; - # single-PV only for now - $json->{'pv_uci'} = $info->{'pv'}; + $json->{'pv_uci'} = $info->{'pv'}; # Still needs to be there for the JS to calculate arrows; only for the primary PV, though! $json->{'pv_pretty'} = [ prettyprint_pv($pos_calculating, @{$info->{'pv'}}) ]; my %refutation_lines = (); @@ -736,7 +733,6 @@ sub output_json { pretty_move => $pretty_move, pv_pretty => \@pretty_pv, }; - $refutation_lines{$pv->[0]}->{'pv_uci'} = $pv; }; } } @@ -745,14 +741,25 @@ sub output_json { my $encoded = JSON::XS::encode_json($json); atomic_set_contents($remoteglotconf::json_output, $encoded); - if (exists($pos_calculating->{'history'}) && + if (exists($pos_calculating->{'pretty_history'}) && defined($remoteglotconf::json_history_dir)) { - my $halfmove_num = scalar @{$pos_calculating->{'history'}}; + my $halfmove_num = scalar @{$pos_calculating->{'pretty_history'}}; (my $fen = $pos_calculating->fen()) =~ tr,/ ,-_,; my $filename = $remoteglotconf::json_history_dir . "/move$halfmove_num-$fen.json"; - # TODO: Avoid overwriting earlier analysis if it's better. - atomic_set_contents($filename, $encoded); + # Overwrite old analysis (assuming it exists at all) if we're + # using a different engine, or if we've calculated deeper. + # nodes is used as a tiebreaker. Don't bother about Multi-PV + # data; it's not that important. + my ($old_engine, $old_depth, $old_nodes) = get_json_analysis_stats($filename); + my $new_depth = $json->{'depth'} // 0; + my $new_nodes = $json->{'nodes'} // 0; + if (!defined($old_engine) || + $old_engine ne $json->{'id'}{'name'} || + $new_depth > $old_depth || + ($new_depth == $old_depth && $new_nodes >= $old_nodes)) { + atomic_set_contents($filename, $encoded); + } } } @@ -766,6 +773,28 @@ sub atomic_set_contents { rename($filename . ".tmp", $filename); } +sub get_json_analysis_stats { + my $filename = shift; + + my ($engine, $depth, $nodes); + + open my $fh, "<", $filename + or return undef; + local $/ = undef; + eval { + my $json = JSON::XS::decode_json(<$fh>); + $engine = $json->{'id'}{'name'} // die; + $depth = $json->{'depth'} // 0; + $nodes = $json->{'nodes'} // 0; + }; + close $fh; + if ($@) { + warn "Error in decoding $filename: $@"; + return undef; + } + return ($engine, $depth, $nodes); +} + sub uciprint { my ($engine, $msg) = @_; $engine->print($msg);