+ # Piece together historic score information, to the degree we have it.
+ if (!$historic_json_only && exists($pos_calculating->{'pretty_history'})) {
+ my %score_history = ();
+
+ my $pos = Position->start_pos('white', 'black');
+ my $halfmove_num = 0;
+ for my $move (@{$pos_calculating->{'pretty_history'}}) {
+ my $id = id_for_pos($pos, $halfmove_num);
+ if (exists($json_for_pos{$id}) && defined($json_for_pos{$id}->{'plot_score'})) {
+ $score_history{$halfmove_num} = [
+ $json_for_pos{$id}->{'plot_score'},
+ $json_for_pos{$id}->{'short_score'}
+ ];
+ }
+ ++$halfmove_num;
+ ($pos) = $pos->make_pretty_move($move);
+ }
+
+ # If at any point we are missing 10 consecutive moves,
+ # truncate the history there. This is so we don't get into
+ # a situation where we e.g. start analyzing at move 45,
+ # but we have analysis for 1. e4 from some completely different game
+ # and thus show a huge hole.
+ my $consecutive_missing = 0;
+ my $truncate_until = 0;
+ for (my $i = $halfmove_num; $i --> 0; ) {
+ if ($consecutive_missing >= 10) {
+ delete $score_history{$i};
+ next;
+ }
+ if (exists($score_history{$i})) {
+ $consecutive_missing = 0;
+ } else {
+ ++$consecutive_missing;
+ }
+ }
+
+ $json->{'score_history'} = \%score_history;
+ }
+
+ my $json_enc = JSON::XS->new;
+ $json_enc->canonical(1);
+ my $encoded = $json_enc->encode($json);