+sub output_json {
+ my $info = $engine->{'info'};
+
+ my $json = {};
+ $json->{'position'} = $pos_calculating;
+ $json->{'id'} = $engine->{'id'};
+ $json->{'score'} = long_score($info, $pos_calculating, '');
+
+ $json->{'nodes'} = $info->{'nodes'};
+ $json->{'nps'} = $info->{'nps'};
+ $json->{'depth'} = $info->{'depth'};
+ $json->{'tbhits'} = $info->{'tbhits'};
+ $json->{'seldepth'} = $info->{'seldepth'};
+
+ # single-PV only for now
+ $json->{'pv_uci'} = $info->{'pv'};
+ $json->{'pv_pretty'} = [ prettyprint_pv($pos_calculating->{'board'}, @{$info->{'pv'}}) ];
+
+ 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,
+ };
+ $refutation_lines{$pv->[0]}->{'pv_uci'} = $pv;
+ };
+ }
+ $json->{'refutation_lines'} = \%refutation_lines;
+
+ open my $fh, ">analysis.json.tmp"
+ or return;
+ print $fh JSON::XS::encode_json($json);
+ close $fh;
+ rename("analysis.json.tmp", "analysis.json");
+}
+