From 01a8eaa3db2c015b25c0cf53df96b12493f3db58 Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Sat, 6 Jan 2018 13:00:26 +0100 Subject: [PATCH 1/1] Support starting from a nonstandard position. --- Position.pm | 3 +++ remoteglot.pl | 18 ++++++++++++++++-- www/js/remoteglot.js | 10 ++++++---- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/Position.pm b/Position.pm index 39b89ac..6976c96 100644 --- a/Position.pm +++ b/Position.pm @@ -218,6 +218,9 @@ sub make_move { } $np->{'player_w'} = $pos->{'player_w'}; $np->{'player_b'} = $pos->{'player_b'}; + if (exists($pos->{'start_fen'})) { + $np->{'start_fen'} = $pos->{'start_fen'}; + } if (defined($pretty_move)) { $np->{'last_move'} = $pretty_move; } else { diff --git a/remoteglot.pl b/remoteglot.pl index 19f0446..7f4fd3c 100755 --- a/remoteglot.pl +++ b/remoteglot.pl @@ -286,7 +286,16 @@ sub handle_pgn { my $black = $pgn->black; $white =~ s/,.*//; # Remove first name. $black =~ s/,.*//; # Remove first name. - my $pos = Position->start_pos($white, $black); + my $tags = $pgn->tags(); + my $pos; + if (exists($tags->{'FEN'})) { + $pos = Position->from_fen($tags->{'FEN'}); + $pos->{'player_w'} = $white; + $pos->{'player_b'} = $black; + $pos->{'start_fen'} = $tags->{'FEN'}; + } else { + $pos = Position->start_pos($white, $black); + } my $moves = $pgn->moves; my @uci_moves = (); my @repretty_moves = (); @@ -880,7 +889,12 @@ sub output_json { local $dbh->{AutoCommit} = 0; my $q = $dbh->prepare('SELECT * FROM scores WHERE id=?'); - my $pos = Position->start_pos('white', 'black'); + my $pos; + if (exists($pos_calculating->{'start_fen'})) { + $pos = Position->from_fen($pos_calculating->{'start_fen'}); + } else { + $pos = Position->start_pos('white', 'black'); + } my $halfmove_num = 0; for my $move (@{$pos_calculating->{'history'}}) { my $id = id_for_pos($pos, $halfmove_num); diff --git a/www/js/remoteglot.js b/www/js/remoteglot.js index d614c7e..ff566ab 100644 --- a/www/js/remoteglot.js +++ b/www/js/remoteglot.js @@ -864,7 +864,7 @@ var update_refutation_lines = function() { */ var chess_from = function(fen, moves, last_move) { var hiddenboard = new Chess(); - if (fen !== null) { + if (fen !== null && fen !== undefined) { hiddenboard.load(fen); } for (var i = 0; i <= last_move; ++i) { @@ -957,7 +957,8 @@ var update_board = function() { // unconditionally taken from current_data (we're not interested in // historic history). if (current_data['position']['history']) { - add_pv('start', current_data['position']['history'], 1, 'W', null, 0, 8, true); + var start = (current_data['position'] && current_data['position']['start_fen']) ? current_data['position']['start_fen'] : 'start'; + add_pv(start, current_data['position']['history'], 1, 'W', null, 0, 8, true); } else { display_lines.push(null); } @@ -1083,7 +1084,8 @@ var update_board = function() { // We don't have historic analysis for this position, but we // can reconstruct what the last move was by just replaying // from the start. - var hiddenboard = chess_from(null, current_display_line.pv, current_display_move); + var position = (data['position'] && data['position']['start_fen']) ? data['position']['start_fen'] : null; + var hiddenboard = chess_from(position, current_display_line.pv, current_display_move); var moves = hiddenboard.history({ verbose: true }); last_move = moves.pop(); highlight_from = last_move.from; @@ -1607,7 +1609,7 @@ var update_historic_analysis = function() { } // Fetch old analysis for this line if it exists. - var hiddenboard = chess_from(null, current_display_line.pv, current_display_move); + var hiddenboard = chess_from(current_display_line.start_fen, current_display_line.pv, current_display_move); var filename = "/history/move" + (current_display_move + 1) + "-" + hiddenboard.fen().replace(/ /g, '_').replace(/\//g, '-') + ".json"; -- 2.39.2