]> git.sesse.net Git - remoteglot/commitdiff
Support starting from a nonstandard position.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Sat, 6 Jan 2018 12:00:26 +0000 (13:00 +0100)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Sat, 6 Jan 2018 12:00:28 +0000 (13:00 +0100)
Position.pm
remoteglot.pl
www/js/remoteglot.js

index 39b89acbb47a7278dffac3c60e58240b96a83b92..6976c96b68958880b0c58932a58cae0bedd92a14 100644 (file)
@@ -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 {
index 19f0446cb496128cc4fa4b04cf80f8d7ad345cdb..7f4fd3c2ddc58d03fff0b0a276236572cfffe993 100755 (executable)
@@ -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);
index d614c7ea1016a44f8b92afa841eb42c09b5a81b0..ff566ab85c81c4ba4b33ff8a9853bec23d8f89ca 100644 (file)
@@ -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";