X-Git-Url: https://git.sesse.net/?p=remoteglot;a=blobdiff_plain;f=remoteglot.pl;h=e3433aa12414037ff86ba81c5d92e0449514d253;hp=b128e29735b99e7a520d1d31ecec61db0d7cfe40;hb=944e74aaa3c5b3038d16d8be1461be6f6f33a4b4;hpb=96875773d3cd75a57b13cba9d1497c616cf76503 diff --git a/remoteglot.pl b/remoteglot.pl index b128e29..e3433aa 100755 --- a/remoteglot.pl +++ b/remoteglot.pl @@ -28,9 +28,10 @@ use warnings; no warnings qw(once); # Program starts here -$SIG{ALRM} = sub { output(); }; my $latest_update = undef; +my $output_timer = undef; my $http_timer = undef; +my $stop_pgn_fetch = 0; my $tb_retry_timer = undef; my %tb_cache = (); my $tb_lookup_running = 0; @@ -199,7 +200,6 @@ sub handle_fics { }; if ($@) { warn "Error when getting FICS move history: $@"; - exit; $getting_movelist = 0; } } @@ -232,6 +232,7 @@ sub handle_fics { fetch_pgn($url); } elsif ($msg =~ /^stoppgn$/) { $t->cmd("tell $who Stopping poll."); + $stop_pgn_fetch = 1; $http_timer = undef; } elsif ($msg =~ /^quit$/) { $t->cmd("tell $who Bye bye."); @@ -257,41 +258,53 @@ my $pgn_hysteresis_counter = 0; sub handle_pgn { my ($body, $header, $url) = @_; + + if ($stop_pgn_fetch) { + $stop_pgn_fetch = 0; + $http_timer = undef; + return; + } + my $pgn = Chess::PGN::Parse->new(undef, $body); if (!defined($pgn) || !$pgn->read_game()) { warn "Error in parsing PGN from $url\n"; } else { - $pgn->quick_parse_game; - my $pos = Position->start_pos($pgn->white, $pgn->black); - my $moves = $pgn->moves; - my @uci_moves = (); - for my $move (@$moves) { - my $uci_move; - ($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, - # or people push out new ones non-atomically. - # Thus, if we PGN doesn't change names but becomes - # shorter, we mistrust it for a few seconds. - my $trust_pgn = 1; - if (defined($last_pgn_white) && defined($last_pgn_black) && - $last_pgn_white eq $pgn->white && - $last_pgn_black eq $pgn->black && - scalar(@uci_moves) < scalar(@last_pgn_uci_moves)) { - if (++$pgn_hysteresis_counter < 3) { - $trust_pgn = 0; + eval { + $pgn->quick_parse_game; + my $pos = Position->start_pos($pgn->white, $pgn->black); + my $moves = $pgn->moves; + my @uci_moves = (); + for my $move (@$moves) { + my $uci_move; + ($pos, $uci_move) = $pos->make_pretty_move($move); + push @uci_moves, $uci_move; } - } - if ($trust_pgn) { - $last_pgn_white = $pgn->white; - $last_pgn_black = $pgn->black; - @last_pgn_uci_moves = @uci_moves; - $pgn_hysteresis_counter = 0; - handle_position($pos); + $pos->{'history'} = \@uci_moves; + $pos->{'pretty_history'} = $moves; + + # Sometimes, PGNs lose a move or two for a short while, + # or people push out new ones non-atomically. + # Thus, if we PGN doesn't change names but becomes + # shorter, we mistrust it for a few seconds. + my $trust_pgn = 1; + if (defined($last_pgn_white) && defined($last_pgn_black) && + $last_pgn_white eq $pgn->white && + $last_pgn_black eq $pgn->black && + scalar(@uci_moves) < scalar(@last_pgn_uci_moves)) { + if (++$pgn_hysteresis_counter < 3) { + $trust_pgn = 0; + } + } + if ($trust_pgn) { + $last_pgn_white = $pgn->white; + $last_pgn_black = $pgn->black; + @last_pgn_uci_moves = @uci_moves; + $pgn_hysteresis_counter = 0; + handle_position($pos); + } + }; + if ($@) { + warn "Error in parsing moves from $url\n"; } } @@ -478,7 +491,8 @@ sub output { # Don't update too often. my $age = Time::HiRes::tv_interval($latest_update); if ($age < $remoteglotconf::update_max_interval) { - Time::HiRes::alarm($remoteglotconf::update_max_interval + 0.01 - $age); + my $wait = $remoteglotconf::update_max_interval + 0.01 - $age; + $output_timer = AnyEvent->timer(after => $wait, cb => \&output); return; } @@ -502,7 +516,7 @@ sub output { my $t = $tb_cache{$fen}; my $pv = $t->{'pv'}; - my $matelen = int((1 + scalar @$pv) / 2); + my $matelen = int((1 + $t->{'score'}) / 2); if ($t->{'result'} eq '1/2-1/2') { $info->{'score_cp'} = 0; } elsif ($t->{'result'} eq '1-0') { @@ -728,11 +742,61 @@ sub output_json { } $json->{'refutation_lines'} = \%refutation_lines; - open my $fh, ">", $remoteglotconf::json_output . ".tmp" + my $encoded = JSON::XS::encode_json($json); + atomic_set_contents($remoteglotconf::json_output, $encoded); + + if (exists($pos_calculating->{'history'}) && + defined($remoteglotconf::json_history_dir)) { + my $halfmove_num = scalar @{$pos_calculating->{'history'}}; + (my $fen = $pos_calculating->fen()) =~ tr,/ ,-_,; + my $filename = $remoteglotconf::json_history_dir . "/move$halfmove_num-$fen.json"; + + # 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); + } + } +} + +sub atomic_set_contents { + my ($filename, $contents) = @_; + + open my $fh, ">", $filename . ".tmp" or return; - print $fh JSON::XS::encode_json($json); + print $fh $contents; close $fh; - rename($remoteglotconf::json_output . ".tmp", $remoteglotconf::json_output); + 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 { @@ -937,7 +1001,8 @@ sub handle_tb_lookup_return { } $tb_cache{$fen} = { result => $pgn->result, - pv => \@uci_moves + pv => \@uci_moves, + score => $response->{'Response'}{'Score'}, }; output(); }