4 # remoteglot - Connects an abitrary UCI-speaking engine to a (live) PGN,
5 # for live analysis of relayed games.
7 # Copyright 2007 Steinar H. Gunderson <steinar+remoteglot@gunderson.no>
8 # Licensed under the GNU General Public License, version 2.
14 use Chess::PGN::Parse;
23 require 'Position.pm';
31 my $latest_update = undef;
32 my $output_timer = undef;
33 my $http_timer = undef;
34 my $stop_pgn_fetch = 0;
35 my $tb_retry_timer = undef;
36 my $tb_lookup_running = 0;
37 my $last_written_json = undef;
39 # Persisted so we can restart.
40 # TODO: Figure out an appropriate way to deal with database restarts
41 # and/or Postgres going away entirely.
42 my $dbh = DBI->connect($remoteglotconf::dbistr, $remoteglotconf::dbiuser, $remoteglotconf::dbipass)
44 $dbh->{RaiseError} = 1;
48 open(UCILOG, ">ucilog.txt")
49 or die "ucilog.txt: $!";
50 print UCILOG "Log starting.\n";
54 open(TBLOG, ">tblog.txt")
55 or die "tblog.txt: $!";
56 print TBLOG "Log starting.\n";
61 umask 0022; # analysis.json should not be served to users.
63 # open the chess engine
64 my $engine = open_engine($remoteglotconf::engine_cmdline, 'E1', sub { handle_uci(@_, 1); });
65 my $engine2 = open_engine($remoteglotconf::engine2_cmdline, 'E2', sub { handle_uci(@_, 0); });
68 my ($pos_calculating, $pos_calculating_second_engine);
70 # If not undef, we've started calculating this position but haven't ever given out
71 # any analysis for it, so we're on a forced timer to do so.
72 my $pos_calculating_started = undef;
74 # If not undef, we've output this position, but without a main PV, so we're on
75 # _another_ forced timer to do so.
76 my $pos_pv_started = undef;
77 my $last_output_had_pv = 0;
79 setoptions($engine, \%remoteglotconf::engine_config);
80 uciprint($engine, "ucinewgame");
82 if (defined($engine2)) {
83 setoptions($engine2, \%remoteglotconf::engine2_config);
84 uciprint($engine2, "setoption name MultiPV value 500");
85 uciprint($engine2, "ucinewgame");
88 print "Chess engine ready.\n";
90 if (defined($remoteglotconf::target)) {
91 if ($remoteglotconf::target =~ /^(?:\/|https?:)/) {
92 fetch_pgn($remoteglotconf::target);
96 # Engine events have already been set up by Engine.pm.
100 my ($engine, $line, $primary) = @_;
102 return if $line =~ /(upper|lower)bound/;
104 $line =~ s/ / /g; # Sometimes needed for Zappa Mexico
105 print UCILOG localtime() . " $engine->{'tag'} <= $line\n";
107 # If we've sent a stop command, gobble up lines until we see bestmove.
108 return if ($engine->{'stopping'} && $line !~ /^bestmove/);
109 $engine->{'stopping'} = 0;
111 if ($line =~ /^info/ && $line !~ / cluster /) {
112 my (@infos) = split / /, $line;
115 parse_infos($engine, @infos);
117 if ($line =~ /^id/) {
118 my (@ids) = split / /, $line;
121 parse_ids($engine, @ids);
126 my $getting_movelist = 0;
127 my $pos_for_movelist = undef;
128 my @uci_movelist = ();
129 my @pretty_movelist = ();
131 # Starts periodic fetching of PGNs from the given URL.
134 if ($url =~ m#^/#) { # Local file.
137 open my $fh, "<", $url
141 handle_pgn($pgn, '', $url);
145 $http_timer = AnyEvent->timer(after => 1.0, cb => sub {
150 AnyEvent::HTTP::http_get($url, sub {
151 handle_pgn(@_, $url);
156 my ($last_pgn_white, $last_pgn_black);
157 my @last_pgn_uci_moves = ();
158 my $pgn_hysteresis_counter = 0;
161 my ($body, $header, $url) = @_;
163 if ($stop_pgn_fetch) {
169 my $pgn = Chess::PGN::Parse->new(undef, $body);
170 if (!defined($pgn)) {
171 warn "Error in parsing PGN from $url [body='$body']\n";
172 } elsif (!$pgn->read_game()) {
173 warn "Error in reading PGN game from $url [body='$body']\n";
174 } elsif ($body !~ /^\[/) {
175 warn "Malformed PGN from $url [body='$body']\n";
178 # Skip to the right game.
179 while (defined($remoteglotconf::pgn_filter) &&
180 !&$remoteglotconf::pgn_filter($pgn)) {
181 $pgn->read_game() or die "Out of games during filtering";
184 $pgn->parse_game({ save_comments => 'yes' });
185 my $white = $pgn->white;
186 my $black = $pgn->black;
187 $white =~ s/,.*//; # Remove first name.
188 $black =~ s/,.*//; # Remove first name.
189 my $tags = $pgn->tags();
191 if (exists($tags->{'FEN'})) {
192 $pos = Position->from_fen($tags->{'FEN'});
193 $pos->{'last_move'} = 'none';
194 $pos->{'player_w'} = $white;
195 $pos->{'player_b'} = $black;
196 $pos->{'start_fen'} = $tags->{'FEN'};
198 $pos = Position->start_pos($white, $black);
200 if (exists($tags->{'Variant'}) &&
201 $tags->{'Variant'} =~ /960|fischer/i) {
202 $pos->{'chess960'} = 1;
204 $pos->{'chess960'} = 0;
206 my $moves = $pgn->moves;
208 my @repretty_moves = ();
209 for my $move (@$moves) {
210 my ($npos, $uci_move) = $pos->make_pretty_move($move);
211 push @uci_moves, $uci_move;
213 # Re-prettyprint the move.
214 my ($from_row, $from_col, $to_row, $to_col, $promo) = parse_uci_move($uci_move);
215 my ($pretty, undef) = $pos->{'board'}->prettyprint_move($from_row, $from_col, $to_row, $to_col, $promo);
216 push @repretty_moves, $pretty;
219 if ($pgn->result eq '1-0' || $pgn->result eq '1/2-1/2' || $pgn->result eq '0-1') {
220 $pos->{'result'} = $pgn->result;
223 my @extra_moves = ();
224 $pos = extend_from_manual_override($pos, \@repretty_moves, \@extra_moves);
225 extract_clock($pgn, $pos);
226 $pos->{'history'} = \@repretty_moves;
227 $pos->{'extra_moves'} = \@extra_moves;
229 # Sometimes, PGNs lose a move or two for a short while,
230 # or people push out new ones non-atomically.
231 # Thus, if we PGN doesn't change names but becomes
232 # shorter, we mistrust it for a few seconds.
234 if (defined($last_pgn_white) && defined($last_pgn_black) &&
235 $last_pgn_white eq $pgn->white &&
236 $last_pgn_black eq $pgn->black &&
237 scalar(@uci_moves) < scalar(@last_pgn_uci_moves)) {
238 if (++$pgn_hysteresis_counter < 3) {
243 $last_pgn_white = $pgn->white;
244 $last_pgn_black = $pgn->black;
245 @last_pgn_uci_moves = @uci_moves;
246 $pgn_hysteresis_counter = 0;
247 handle_position($pos);
251 warn "Error in parsing moves from $url: $@\n";
255 $http_timer = AnyEvent->timer(after => 1.0, cb => sub {
260 sub handle_position {
262 find_clock_start($pos, $pos_calculating);
264 # If we're already chewing on this and there's nothing else in the queue,
266 if (defined($pos_calculating) && $pos->fen() eq $pos_calculating->fen()) {
267 $pos_calculating->{'result'} = $pos->{'result'};
268 for my $key ('white_clock', 'black_clock', 'white_clock_target', 'black_clock_target') {
269 $pos_calculating->{$key} //= $pos->{$key};
274 # If we're already thinking on something, stop and wait for the engine
276 if (defined($pos_calculating)) {
277 # Store the final data we have for this position in the history,
278 # with the precise clock information we just got from the new
279 # position. (Historic positions store the clock at the end of
282 # Do not output anything new to the main analysis; that's
283 # going to be obsolete really soon. (Exception: If we've never
284 # output anything for this move, ie., it didn't hit the 200ms
285 # limit, spit it out to the user anyway. It's probably a really
286 # fast blitz game or something, and it's good to show the moves
287 # as they come in even without great analysis.)
288 $pos_calculating->{'white_clock'} = $pos->{'white_clock'};
289 $pos_calculating->{'black_clock'} = $pos->{'black_clock'};
290 delete $pos_calculating->{'white_clock_target'};
291 delete $pos_calculating->{'black_clock_target'};
293 if (defined($pos_calculating_started)) {
298 $pos_calculating_started = [Time::HiRes::gettimeofday];
299 $pos_pv_started = undef;
301 # Ask the engine to stop; we will throw away its data until it
302 # sends us "bestmove", signaling the end of it.
303 $engine->{'stopping'} = 1;
304 uciprint($engine, "stop");
307 # It's wrong to just give the FEN (the move history is useful,
308 # and per the UCI spec, we should really have sent "ucinewgame"),
309 # but it's easier, and it works around a Stockfish repetition issue.
310 if ($engine->{'chess960'} != $pos->{'chess960'}) {
311 uciprint($engine, "setoption name UCI_Chess960 value " . ($pos->{'chess960'} ? 'true' : 'false'));
312 $engine->{'chess960'} = $pos->{'chess960'};
314 uciprint($engine, "position fen " . $pos->fen());
315 uciprint($engine, "go infinite");
316 $pos_calculating = $pos;
317 $pos_calculating_started = [Time::HiRes::gettimeofday];
318 $pos_pv_started = undef;
320 if (defined($engine2)) {
321 if (defined($pos_calculating_second_engine)) {
322 $engine2->{'stopping'} = 1;
323 uciprint($engine2, "stop");
325 if ($engine2->{'chess960'} != $pos->{'chess960'}) {
326 uciprint($engine2, "setoption name UCI_Chess960 value " . ($pos->{'chess960'} ? 'true' : 'false'));
327 $engine2->{'chess960'} = $pos->{'chess960'};
329 uciprint($engine2, "position fen " . $pos->fen());
330 uciprint($engine2, "go infinite");
331 $pos_calculating_second_engine = $pos;
332 $engine2->{'info'} = {};
335 $engine->{'info'} = {};
340 my ($engine, @x) = @_;
343 my $info = $engine->{'info'};
345 # Search for "multipv" first of all, since e.g. Stockfish doesn't put it first.
346 for my $i (0..$#x - 1) {
347 if ($x[$i] eq 'multipv') {
353 while (scalar @x > 0) {
354 if ($x[0] eq 'multipv') {
360 if ($x[0] eq 'currmove' || $x[0] eq 'currmovenumber' || $x[0] eq 'cpuload') {
362 my $value = shift @x;
363 $info->{$key} = $value;
366 if ($x[0] eq 'depth' || $x[0] eq 'seldepth' || $x[0] eq 'hashfull' ||
367 $x[0] eq 'time' || $x[0] eq 'nodes' || $x[0] eq 'nps' ||
370 my $value = shift @x;
371 $info->{$key . $mpv} = $value;
374 if ($x[0] eq 'score') {
377 delete $info->{'score_cp' . $mpv};
378 delete $info->{'score_mate' . $mpv};
379 delete $info->{'splicepos' . $mpv};
381 while ($x[0] eq 'cp' || $x[0] eq 'mate') {
384 $info->{'score_cp' . $mpv} = shift @x;
385 } elsif ($x[0] eq 'mate') {
387 $info->{'score_mate' . $mpv} = shift @x;
395 $info->{'pv' . $mpv} = [ @x[1..$#x] ];
398 if ($x[0] eq 'string' || $x[0] eq 'UCI_AnalyseMode' || $x[0] eq 'setting' || $x[0] eq 'contempt') {
402 #print "unknown info '$x[0]', trying to recover...\n";
404 die "Unknown info '" . join(',', @x) . "'";
410 my ($engine, @x) = @_;
412 while (scalar @x > 0) {
413 if ($x[0] eq 'name') {
414 my $value = join(' ', @x);
415 $engine->{'id'}{'author'} = $value;
424 sub prettyprint_pv_no_cache {
425 my ($board, @pvs) = @_;
427 if (scalar @pvs == 0 || !defined($pvs[0])) {
433 my ($from_row, $from_col, $to_row, $to_col, $promo) = parse_uci_move($pv);
434 my ($pretty, $nb) = $board->prettyprint_move($from_row, $from_col, $to_row, $to_col, $promo);
442 my ($pos, @pvs) = @_;
444 my $cachekey = join('', @pvs);
445 if (exists($pos->{'prettyprint_cache'}{$cachekey})) {
446 return @{$pos->{'prettyprint_cache'}{$cachekey}};
448 my @res = prettyprint_pv_no_cache($pos->{'board'}, @pvs);
449 $pos->{'prettyprint_cache'}{$cachekey} = \@res;
454 my %tbprobe_cache = ();
456 sub complete_using_tbprobe {
457 my ($pos, $info, $mpv) = @_;
459 # We need Fathom installed to do standalone TB probes.
460 return if (!defined($remoteglotconf::fathom_cmdline));
462 # If we already have a mate, don't bother; in some cases, it would even be
463 # better than a tablebase score.
464 return if defined($info->{'score_mate' . $mpv});
466 # If we have a draw or near-draw score, there's also not much interesting
467 # we could add from a tablebase. We only really want mates.
468 return if ($info->{'score_cp' . $mpv} >= -12250 && $info->{'score_cp' . $mpv} <= 12250);
470 # Run through the PV until we are at a 6-man position.
471 # TODO: We could in theory only have 5-man data.
472 my @pv = @{$info->{'pv' . $mpv}};
473 my $key = $pos->fen() . " " . join('', @pv);
476 if (exists($tbprobe_cache{$key})) {
477 my $c = $tbprobe_cache{$key};
478 @moves = @{$c->{'moves'}};
479 $splicepos = $c->{'splicepos'};
482 # Force doing at least one move of the PV.
483 my $move = shift @pv;
485 $pos = $pos->make_move(parse_uci_move($move));
488 while ($pos->num_pieces() > 7 && $#pv > -1) {
489 my $move = shift @pv;
491 $pos = $pos->make_move(parse_uci_move($move));
494 return if ($pos->num_pieces() > 7);
496 my $fen = $pos->fen();
497 my $pgn_text = `$remoteglotconf::fathom_cmdline "$fen"`;
498 my $pgn = Chess::PGN::Parse->new(undef, $pgn_text);
499 return if (!defined($pgn) || !$pgn->read_game() || ($pgn->result ne '0-1' && $pgn->result ne '1-0'));
500 $pgn->quick_parse_game;
502 # Splice the PV from the tablebase onto what we have so far.
503 $splicepos = scalar @moves;
504 for my $move (@{$pgn->moves}) {
505 last if $move eq '#';
506 last if $move eq '1-0';
507 last if $move eq '0-1';
508 last if $move eq '1/2-1/2';
510 ($pos, $uci_move) = $pos->make_pretty_move($move);
511 push @moves, $uci_move;
514 $tbprobe_cache{$key} = {
516 splicepos => $splicepos
520 $info->{'pv' . $mpv} = \@moves;
522 my $matelen = int((1 + scalar @moves) / 2);
523 if ((scalar @moves) % 2 == 0) {
524 $info->{'score_mate' . $mpv} = -$matelen;
526 $info->{'score_mate' . $mpv} = $matelen;
528 $info->{'splicepos' . $mpv} = $splicepos;
534 return if (!defined($pos_calculating));
536 my $info = $engine->{'info'};
538 # Don't update too often.
539 my $wait = $remoteglotconf::update_max_interval - Time::HiRes::tv_interval($latest_update);
540 if (defined($pos_calculating_started)) {
541 my $new_pos_wait = $remoteglotconf::update_force_after_move - Time::HiRes::tv_interval($pos_calculating_started);
542 $wait = $new_pos_wait if ($new_pos_wait < $wait);
544 if (!$last_output_had_pv && has_pv($info)) {
545 if (!defined($pos_pv_started)) {
546 $pos_pv_started = [Time::HiRes::gettimeofday];
548 # We just got initial PV, and we're in a hurry since we gave out a blank one earlier,
549 # so give us just 200ms more to increase the quality and then force a display.
550 my $new_pos_wait = $remoteglotconf::update_force_after_move - Time::HiRes::tv_interval($pos_pv_started);
551 $wait = $new_pos_wait if ($new_pos_wait < $wait);
554 $output_timer = AnyEvent->timer(after => $wait + 0.01, cb => \&output);
557 $pos_pv_started = undef;
559 # We're outputting something for this position now, so the special handling
560 # for new positions is off.
561 undef $pos_calculating_started;
564 # Some programs _always_ report MultiPV, even with only one PV.
565 # In this case, we simply use that data as if MultiPV was never
568 if (exists($info->{'pv1'}) && !exists($info->{'pv2'})) {
569 for my $key (qw(pv score_cp score_mate nodes nps depth seldepth tbhits splicepos)) {
570 if (exists($info->{$key . '1'})) {
571 $info->{$key} = $info->{$key . '1'};
573 delete $info->{$key};
579 # Check the PVs first. if they're invalid, just wait, as our data
580 # is most likely out of sync. This isn't a very good solution, as
581 # it can frequently miss stuff, but it's good enough for most users.
585 if (exists($info->{'pv'})) {
586 $dummy = prettyprint_pv($pos_calculating, @{$info->{'pv'}});
590 while (exists($info->{'pv' . $mpv})) {
591 $dummy = prettyprint_pv($pos_calculating, @{$info->{'pv' . $mpv}});
596 $engine->{'info'} = {};
600 # Now do our own Syzygy tablebase probes to convert scores like +123.45 to mate.
601 if (exists($info->{'pv'})) {
602 complete_using_tbprobe($pos_calculating, $info, '');
606 while (exists($info->{'pv' . $mpv})) {
607 complete_using_tbprobe($pos_calculating, $info, $mpv);
613 $latest_update = [Time::HiRes::gettimeofday];
614 $last_output_had_pv = has_pv($info);
619 return 1 if (exists($info->{'pv'}) && (scalar(@{$info->{'pv'}}) > 0));
620 return 1 if (exists($info->{'pv1'}) && (scalar(@{$info->{'pv1'}}) > 0));
625 my $info = $engine->{'info'};
626 my $id = $engine->{'id'};
628 my $text = 'Analysis';
629 if ($pos_calculating->{'last_move'} ne 'none') {
630 if ($pos_calculating->{'toplay'} eq 'W') {
631 $text .= sprintf ' after %u. ... %s', ($pos_calculating->{'move_num'}-1), $pos_calculating->{'last_move'};
633 $text .= sprintf ' after %u. %s', $pos_calculating->{'move_num'}, $pos_calculating->{'last_move'};
635 if (exists($id->{'name'})) {
640 if (exists($id->{'name'})) {
641 $text .= " by $id->{'name'}:\n\n";
646 return unless (exists($pos_calculating->{'board'}));
648 my $extra_moves = $pos_calculating->{'extra_moves'};
649 if (defined($extra_moves) && scalar @$extra_moves > 0) {
650 $text .= " Manual move extensions: " . join(' ', @$extra_moves) . "\n";
653 if (exists($info->{'pv1'}) && exists($info->{'pv2'})) {
656 while (exists($info->{'pv' . $mpv})) {
657 $text .= sprintf " PV%2u", $mpv;
658 my $score = short_score($info, $pos_calculating, $mpv);
659 $text .= " ($score)" if (defined($score));
662 if (exists($info->{'tbhits' . $mpv}) && $info->{'tbhits' . $mpv} > 0) {
663 if ($info->{'tbhits' . $mpv} == 1) {
664 $tbhits = ", 1 tbhit";
666 $tbhits = sprintf ", %u tbhits", $info->{'tbhits' . $mpv};
670 if (exists($info->{'nodes' . $mpv}) && exists($info->{'nps' . $mpv}) && exists($info->{'depth' . $mpv})) {
671 $text .= sprintf " (%5u kn, %3u kn/s, %2u ply$tbhits)",
672 $info->{'nodes' . $mpv} / 1000, $info->{'nps' . $mpv} / 1000, $info->{'depth' . $mpv};
676 $text .= " " . join(', ', prettyprint_pv($pos_calculating, @{$info->{'pv' . $mpv}})) . "\n";
682 my $score = long_score($info, $pos_calculating, '');
683 $text .= " $score\n" if defined($score);
684 $text .= " PV: " . join(', ', prettyprint_pv($pos_calculating, @{$info->{'pv'}}));
687 if (exists($info->{'nodes'}) && exists($info->{'nps'}) && exists($info->{'depth'})) {
688 $text .= sprintf " %u nodes, %7u nodes/sec, depth %u ply",
689 $info->{'nodes'}, $info->{'nps'}, $info->{'depth'};
691 if (exists($info->{'seldepth'})) {
692 $text .= sprintf " (%u selective)", $info->{'seldepth'};
694 if (exists($info->{'tbhits'}) && $info->{'tbhits'} > 0) {
695 if ($info->{'tbhits'} == 1) {
696 $text .= ", one Syzygy hit";
698 $text .= sprintf ", %u Syzygy hits", $info->{'tbhits'};
704 my @refutation_lines = ();
705 if (defined($engine2)) {
706 for (my $mpv = 1; $mpv < 500; ++$mpv) {
707 my $info = $engine2->{'info'};
708 last if (!exists($info->{'pv' . $mpv}));
710 complete_using_tbprobe($pos_calculating_second_engine, $info, $mpv);
711 my $pv = $info->{'pv' . $mpv};
712 my $pretty_move = join('', prettyprint_pv($pos_calculating_second_engine, $pv->[0]));
713 my @pretty_pv = prettyprint_pv($pos_calculating_second_engine, @$pv);
714 if (scalar @pretty_pv > 5) {
715 @pretty_pv = @pretty_pv[0..4];
716 push @pretty_pv, "...";
718 my $key = $pretty_move;
719 my $line = sprintf(" %-6s %6s %3s %s",
721 short_score($info, $pos_calculating_second_engine, $mpv),
722 "d" . $info->{'depth' . $mpv},
723 join(', ', @pretty_pv));
724 push @refutation_lines, [ $key, $line ];
729 if ($#refutation_lines >= 0) {
730 $text .= "Shallow search of all legal moves:\n\n";
731 for my $line (sort { $a->[0] cmp $b->[0] } @refutation_lines) {
732 $text .= $line->[1] . "\n";
737 if ($last_text ne $text) {
738 print "
\e[H
\e[2J"; # clear the screen
745 my $historic_json_only = shift;
746 my $info = $engine->{'info'};
749 $json->{'position'} = $pos_calculating->to_json_hash();
750 $json->{'engine'} = $engine->{'id'};
751 if (defined($remoteglotconf::engine_url)) {
752 $json->{'engine'}{'url'} = $remoteglotconf::engine_url;
754 if (defined($remoteglotconf::engine_details)) {
755 $json->{'engine'}{'details'} = $remoteglotconf::engine_details;
757 my @grpc_backends = ();
758 if (defined($remoteglotconf::engine_grpc_backend)) {
759 push @grpc_backends, $remoteglotconf::engine_grpc_backend;
761 if (defined($remoteglotconf::engine2_grpc_backend)) {
762 push @grpc_backends, $remoteglotconf::engine2_grpc_backend;
764 $json->{'internal'}{'grpc_backends'} = \@grpc_backends;
765 if (defined($remoteglotconf::move_source)) {
766 $json->{'move_source'} = $remoteglotconf::move_source;
768 if (defined($remoteglotconf::move_source_url)) {
769 $json->{'move_source_url'} = $remoteglotconf::move_source_url;
771 $json->{'score'} = score_digest($info, $pos_calculating, '');
773 $json->{'nodes'} = $info->{'nodes'};
774 $json->{'nps'} = $info->{'nps'};
775 $json->{'depth'} = $info->{'depth'};
776 $json->{'tbhits'} = $info->{'tbhits'};
777 $json->{'seldepth'} = $info->{'seldepth'};
778 $json->{'tablebase'} = $info->{'tablebase'};
779 $json->{'pv'} = [ prettyprint_pv($pos_calculating, @{$info->{'pv'}}) ];
781 my %refutation_lines = ();
782 my @refutation_lines = ();
783 if (defined($engine2)) {
784 for (my $mpv = 1; $mpv < 500; ++$mpv) {
785 my $info = $engine2->{'info'};
786 my $pretty_move = "";
788 last if (!exists($info->{'pv' . $mpv}));
791 complete_using_tbprobe($pos_calculating, $info, $mpv);
792 my $pv = $info->{'pv' . $mpv};
793 my $pretty_move = join('', prettyprint_pv($pos_calculating, $pv->[0]));
794 my @pretty_pv = prettyprint_pv($pos_calculating, @$pv);
795 $refutation_lines{$pretty_move} = {
796 depth => $info->{'depth' . $mpv},
797 score => score_digest($info, $pos_calculating, $mpv),
798 move => $pretty_move,
801 if (exists($info->{'splicepos' . $mpv})) {
802 $refutation_lines{$pretty_move}->{'splicepos'} = $info->{'splicepos' . $mpv};
807 $json->{'refutation_lines'} = \%refutation_lines;
809 # Piece together historic score information, to the degree we have it.
810 if (!$historic_json_only && exists($pos_calculating->{'history'})) {
811 my %score_history = ();
813 local $dbh->{AutoCommit} = 0;
814 my $q = $dbh->prepare('SELECT * FROM scores WHERE id=?');
816 if (exists($pos_calculating->{'start_fen'})) {
817 $pos = Position->from_fen($pos_calculating->{'start_fen'});
819 $pos = Position->start_pos('white', 'black');
821 $pos->{'chess960'} = $pos_calculating->{'chess960'};
822 my $halfmove_num = 0;
823 for my $move (@{$pos_calculating->{'history'}}) {
824 my $id = id_for_pos($pos, $halfmove_num);
825 my $ref = $dbh->selectrow_hashref($q, undef, $id);
827 $score_history{$halfmove_num} = [
828 $ref->{'score_type'},
829 $ref->{'score_value'}
833 ($pos) = $pos->make_pretty_move($move);
838 # If at any point we are missing 10 consecutive moves,
839 # truncate the history there. This is so we don't get into
840 # a situation where we e.g. start analyzing at move 45,
841 # but we have analysis for 1. e4 from some completely different game
842 # and thus show a huge hole.
843 my $consecutive_missing = 0;
844 my $truncate_until = 0;
845 for (my $i = $halfmove_num; $i --> 0; ) {
846 if ($consecutive_missing >= 10) {
847 delete $score_history{$i};
850 if (exists($score_history{$i})) {
851 $consecutive_missing = 0;
853 ++$consecutive_missing;
857 $json->{'score_history'} = \%score_history;
860 # Give out a list of other games going on. (Empty is fine.)
861 # TODO: Don't bother reading our own file, the data will be stale anyway.
862 if (!$historic_json_only) {
865 my $q = $dbh->prepare('SELECT * FROM current_games ORDER BY priority DESC, id');
867 while (my $ref = $q->fetchrow_hashref) {
869 my $other_game_contents = File::Slurp::read_file($ref->{'json_path'});
870 my $other_game_json = JSON::XS::decode_json($other_game_contents);
872 die "Missing position" if (!exists($other_game_json->{'position'}));
873 my $white = $other_game_json->{'position'}{'player_w'} // die 'Missing white';
874 my $black = $other_game_json->{'position'}{'player_b'} // die 'Missing black';
878 name => "$white–$black",
879 url => $ref->{'url'},
880 hashurl => $ref->{'hash_url'},
882 if (defined($other_game_json->{'position'}{'result'})) {
883 $game->{'result'} = $other_game_json->{'position'}{'result'};
885 $game->{'score'} = $other_game_json->{'score'};
890 warn "Could not add external game " . $ref->{'json_path'} . ": $@";
894 if (scalar @games > 0) {
895 $json->{'games'} = \@games;
899 my $json_enc = JSON::XS->new;
900 $json_enc->canonical(1);
901 my $encoded = $json_enc->encode($json);
902 unless ($historic_json_only || !defined($remoteglotconf::json_output) ||
903 (defined($last_written_json) && $last_written_json eq $encoded)) {
904 atomic_set_contents($remoteglotconf::json_output, $encoded);
905 $last_written_json = $encoded;
908 if (exists($pos_calculating->{'history'}) &&
909 defined($remoteglotconf::json_history_dir) && defined($json->{'engine'}{name})) {
910 my $id = id_for_pos($pos_calculating);
911 my $filename = $remoteglotconf::json_history_dir . "/" . $id . ".json";
913 # Overwrite old analysis (assuming it exists at all) if we're
914 # using a different engine, or if we've calculated deeper.
915 # nodes is used as a tiebreaker. Don't bother about Multi-PV
916 # data; it's not that important.
917 my ($old_engine, $old_depth, $old_nodes) = get_json_analysis_stats($id);
918 my $new_depth = $json->{'depth'} // 0;
919 my $new_nodes = $json->{'nodes'} // 0;
920 if (!defined($old_engine) ||
921 $old_engine ne $json->{'engine'}{'name'} ||
922 $new_depth > $old_depth ||
923 ($new_depth == $old_depth && $new_nodes >= $old_nodes)) {
924 atomic_set_contents($filename, $encoded);
925 if (defined($json->{'score'})) {
926 $dbh->do('INSERT INTO scores (id, score_type, score_value, engine, depth, nodes) VALUES (?,?,?,?,?,?) ' .
927 ' ON CONFLICT (id) DO UPDATE SET ' .
928 ' score_type=EXCLUDED.score_type, ' .
929 ' score_value=EXCLUDED.score_value, ' .
930 ' engine=EXCLUDED.engine, ' .
931 ' depth=EXCLUDED.depth, ' .
932 ' nodes=EXCLUDED.nodes',
934 $id, $json->{'score'}[0], $json->{'score'}[1],
935 $json->{'engine'}{'name'}, $new_depth, $new_nodes);
941 sub atomic_set_contents {
942 my ($filename, $contents) = @_;
944 open my $fh, ">", $filename . ".tmp"
948 rename($filename . ".tmp", $filename);
952 my ($pos, $halfmove_num) = @_;
954 $halfmove_num //= scalar @{$pos->{'history'}};
955 (my $fen = $pos->fen()) =~ tr,/ ,-_,;
956 return "move$halfmove_num-$fen";
959 sub get_json_analysis_stats {
961 my $ref = $dbh->selectrow_hashref('SELECT * FROM scores WHERE id=?', undef, $id);
963 return ($ref->{'engine'}, $ref->{'depth'}, $ref->{'nodes'});
970 my ($engine, $msg) = @_;
971 $engine->print($msg);
972 print UCILOG localtime() . " $engine->{'tag'} => $msg\n";
976 my ($info, $pos, $mpv) = @_;
978 my $invert = ($pos->{'toplay'} eq 'B');
979 if (defined($info->{'score_mate' . $mpv})) {
981 return sprintf "M%3d", -$info->{'score_mate' . $mpv};
983 return sprintf "M%3d", $info->{'score_mate' . $mpv};
986 if (exists($info->{'score_cp' . $mpv})) {
987 my $score = $info->{'score_cp' . $mpv} * 0.01;
989 if ($info->{'tablebase'}) {
998 return sprintf "%+5.2f", $score;
1005 # Sufficient for computing long_score, short_score, plot_score and
1006 # (with side-to-play information) score_sort_key.
1008 my ($info, $pos, $mpv) = @_;
1010 if (defined($info->{'score_mate' . $mpv})) {
1011 my $mate = $info->{'score_mate' . $mpv};
1012 if ($pos->{'toplay'} eq 'B') {
1015 if (exists($info->{'splicepos' . $mpv})) {
1016 my $sp = $info->{'splicepos' . $mpv};
1024 return ['M', $mate];
1025 } elsif ($mate < 0) {
1026 return ['m', -$mate];
1027 } elsif ($pos->{'toplay'} eq 'B') {
1034 if (exists($info->{'score_cp' . $mpv})) {
1035 my $score = $info->{'score_cp' . $mpv};
1036 if ($pos->{'toplay'} eq 'B') {
1039 if ($score == 0 && $info->{'tablebase'}) {
1040 return ['d', undef];
1042 return ['cp', int($score)];
1051 my ($info, $pos, $mpv) = @_;
1053 if (defined($info->{'score_mate' . $mpv})) {
1054 my $mate = $info->{'score_mate' . $mpv};
1055 if ($pos->{'toplay'} eq 'B') {
1058 if (exists($info->{'splicepos' . $mpv})) {
1059 my $sp = $info->{'splicepos' . $mpv};
1061 return sprintf "White wins in %u", int(($sp + 1) * 0.5);
1063 return sprintf "Black wins in %u", int(($sp + 1) * 0.5);
1067 return sprintf "White mates in %u", $mate;
1069 return sprintf "Black mates in %u", -$mate;
1073 if (exists($info->{'score_cp' . $mpv})) {
1074 my $score = $info->{'score_cp' . $mpv} * 0.01;
1076 if ($info->{'tablebase'}) {
1077 return "Theoretical draw";
1079 return "Score: 0.00";
1082 if ($pos->{'toplay'} eq 'B') {
1085 return sprintf "Score: %+5.2f", $score;
1092 # For graphs; a single number in centipawns, capped at +/- 500.
1094 my ($info, $pos, $mpv) = @_;
1096 my $invert = ($pos->{'toplay'} eq 'B');
1097 if (defined($info->{'score_mate' . $mpv})) {
1098 my $mate = $info->{'score_mate' . $mpv};
1108 if (exists($info->{'score_cp' . $mpv})) {
1109 my $score = $info->{'score_cp' . $mpv};
1113 $score = 500 if ($score > 500);
1114 $score = -500 if ($score < -500);
1122 sub extend_from_manual_override {
1123 my ($pos, $moves, $extra_moves) = @_;
1125 my $q = $dbh->prepare('SELECT next_move FROM game_extensions WHERE fen=? AND history=? AND player_w=? AND player_b=? AND (CURRENT_TIMESTAMP - ts) < INTERVAL \'1 hour\'');
1127 my $player_w = $pos->{'player_w'};
1128 my $player_b = $pos->{'player_b'};
1129 if ($player_w =~ /^base64:(.*)$/) {
1130 $player_w = MIME::Base64::decode_base64($1);
1132 if ($player_b =~ /^base64:(.*)$/) {
1133 $player_b = MIME::Base64::decode_base64($1);
1135 #use Data::Dumper; print Dumper([$pos->fen(), JSON::XS::encode_json($moves), $player_w, $player_b]);
1136 $q->execute($pos->fen(), JSON::XS::encode_json($moves), $player_w, $player_b);
1137 my $ref = $q->fetchrow_hashref;
1138 if (defined($ref)) {
1139 my $move = $ref->{'next_move'};
1140 ($pos) = $pos->make_pretty_move($move);
1141 push @$moves, $move;
1142 push @$extra_moves, $move;
1151 my ($pgn, $pos) = @_;
1153 # Look for extended PGN clock tags.
1154 my $tags = $pgn->tags;
1155 if (exists($tags->{'WhiteClock'}) && exists($tags->{'BlackClock'})) {
1156 $pos->{'white_clock'} = hms_to_sec($tags->{'WhiteClock'});
1157 $pos->{'black_clock'} = hms_to_sec($tags->{'BlackClock'});
1161 # Look for TCEC-style time comments.
1162 my $moves = $pgn->moves;
1163 my $comments = $pgn->comments;
1164 my $last_black_move = int((scalar @$moves) / 2);
1165 my $last_white_move = int((1 + scalar @$moves) / 2);
1167 my $black_key = $last_black_move . "b";
1168 my $white_key = $last_white_move . "w";
1170 if (exists($comments->{$white_key}) &&
1171 exists($comments->{$black_key}) &&
1172 $comments->{$white_key} =~ /(?:tl=|clk )(\d+:\d+:\d+)/ &&
1173 $comments->{$black_key} =~ /(?:tl=|clk )(\d+:\d+:\d+)/) {
1174 $comments->{$white_key} =~ /(?:tl=|clk )(\d+:\d+:\d+)/;
1175 $pos->{'white_clock'} = hms_to_sec($1);
1176 $comments->{$black_key} =~ /(?:tl=|clk )(\d+:\d+:\d+)/;
1177 $pos->{'black_clock'} = hms_to_sec($1);
1181 delete $pos->{'white_clock'};
1182 delete $pos->{'black_clock'};
1187 return undef if (!defined($hms));
1188 $hms =~ /(\d+):(\d+):(\d+)/;
1189 return $1 * 3600 + $2 * 60 + $3;
1192 sub find_clock_start {
1193 my ($pos, $prev_pos) = @_;
1195 # If the game is over, the clock is stopped.
1196 if (exists($pos->{'result'}) &&
1197 ($pos->{'result'} eq '1-0' ||
1198 $pos->{'result'} eq '1/2-1/2' ||
1199 $pos->{'result'} eq '0-1')) {
1203 # When we don't have any moves, we assume the clock hasn't started yet.
1204 if ($pos->{'move_num'} == 1 && $pos->{'toplay'} eq 'W') {
1205 if (defined($remoteglotconf::adjust_clocks_before_move)) {
1206 &$remoteglotconf::adjust_clocks_before_move(\$pos->{'white_clock'}, \$pos->{'black_clock'}, 1, 'W');
1211 # The history is needed for id_for_pos.
1212 if (!exists($pos->{'history'})) {
1216 my $id = id_for_pos($pos);
1217 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);
1218 if (defined($clock_info)) {
1219 $pos->{'white_clock'} //= $clock_info->{'white_clock'};
1220 $pos->{'black_clock'} //= $clock_info->{'black_clock'};
1221 if ($pos->{'toplay'} eq 'W') {
1222 $pos->{'white_clock_target'} = $clock_info->{'white_clock_target'};
1224 $pos->{'black_clock_target'} = $clock_info->{'black_clock_target'};
1229 # OK, we haven't seen this position before, so we assume the move
1230 # happened right now.
1232 # See if we should do our own clock management (ie., clock information
1233 # is spurious or non-existent).
1234 if (defined($remoteglotconf::adjust_clocks_before_move)) {
1235 my $wc = $pos->{'white_clock'} // $prev_pos->{'white_clock'};
1236 my $bc = $pos->{'black_clock'} // $prev_pos->{'black_clock'};
1237 if (defined($prev_pos->{'white_clock_target'})) {
1238 $wc = $prev_pos->{'white_clock_target'} - time;
1240 if (defined($prev_pos->{'black_clock_target'})) {
1241 $bc = $prev_pos->{'black_clock_target'} - time;
1243 &$remoteglotconf::adjust_clocks_before_move(\$wc, \$bc, $pos->{'move_num'}, $pos->{'toplay'});
1244 $pos->{'white_clock'} = $wc;
1245 $pos->{'black_clock'} = $bc;
1248 my $key = ($pos->{'toplay'} eq 'W') ? 'white_clock' : 'black_clock';
1249 if (!exists($pos->{$key})) {
1250 # No clock information.
1253 my $time_left = $pos->{$key};
1254 my ($white_clock_target, $black_clock_target);
1255 if ($pos->{'toplay'} eq 'W') {
1256 $white_clock_target = $pos->{'white_clock_target'} = time + $time_left;
1258 $black_clock_target = $pos->{'black_clock_target'} = time + $time_left;
1260 local $dbh->{AutoCommit} = 0;
1261 $dbh->do('DELETE FROM clock_info WHERE id=?', undef, $id);
1262 $dbh->do('INSERT INTO clock_info (id, white_clock, black_clock, white_clock_target, black_clock_target) VALUES (?, ?, ?, ?, ?)', undef,
1263 $id, $pos->{'white_clock'}, $pos->{'black_clock'}, $white_clock_target, $black_clock_target);
1268 my ($cmdline, $tag, $cb) = @_;
1269 return undef if (!defined($cmdline));
1270 return Engine->open($cmdline, $tag, $cb);
1273 sub col_letter_to_num {
1274 return ord(shift) - ord('a');
1277 sub row_letter_to_num {
1278 return 7 - (ord(shift) - ord('1'));
1281 sub parse_uci_move {
1283 my $from_col = col_letter_to_num(substr($move, 0, 1));
1284 my $from_row = row_letter_to_num(substr($move, 1, 1));
1285 my $to_col = col_letter_to_num(substr($move, 2, 1));
1286 my $to_row = row_letter_to_num(substr($move, 3, 1));
1287 my $promo = substr($move, 4, 1);
1288 return ($from_row, $from_col, $to_row, $to_col, $promo);
1292 my ($engine, $config) = @_;
1293 uciprint($engine, "setoption name UCI_AnalyseMode value true");
1294 uciprint($engine, "setoption name Analysis Contempt value Off");
1295 if (exists($config->{'Threads'})) { # Threads first, because clearing hash can be multithreaded then.
1296 uciprint($engine, "setoption name Threads value " . $config->{'Threads'});
1298 while (my ($key, $value) = each %$config) {
1299 next if $key eq 'Threads';
1300 uciprint($engine, "setoption name $key value $value");